diff --git a/cekit/tools.py b/cekit/tools.py
index e5f8e52..624434a 100644
--- a/cekit/tools.py
+++ b/cekit/tools.py
@@ -83,6 +83,20 @@ def get_brew_url(md5):
         get_build_cmd = ['brew', 'call', '--json-output', 'getBuild', 'buildInfo=%s' % build_id]
         logger.debug("Executing '%s'" % " ".join(get_build_cmd))
         build = yaml.safe_load(subprocess.check_output(get_build_cmd))
+
+        build_states = ['BUILDING', 'COMPLETE', 'DELETED', 'FAILED', 'CANCELED']
+
+        # State 1 means: COMPLETE which is the only success state. Other states are:
+        #
+        # 'BUILDING': 0
+        # 'COMPLETE': 1
+        # 'DELETED': 2
+        # 'FAILED': 3
+        # 'CANCELED': 4
+        if build['state'] != 1:
+            raise CekitError(
+                "Artifact with checksum {} was found in Koji metadata but the build is in incorrect state ({}) making the artifact not available for downloading anymore".format(md5, build_states[build['state']]))
+
         package = build['package_name']
         release = build['release']
 

Handle deleted artifacts in koji
When an artifact is deleted we should not try to fetch it. Instead the build should be terminated.
diff --git a/tests/test_unit_tools.py b/tests/test_unit_tools.py
index 4ea9fdb..421b7e9 100644
--- a/tests/test_unit_tools.py
+++ b/tests/test_unit_tools.py
@@ -139,7 +139,7 @@ def test_merge_run_cmd():
     assert override['user'] == 'foo'
 
 
-def brew_call(*args, **kwargs):
+def brew_call_ok(*args, **kwargs):
     if 'listArchives' in args[0]:
         return """
         [
@@ -155,19 +155,53 @@ def brew_call(*args, **kwargs):
         return """
         {
           "package_name": "package_name",
-          "release": "release"
+          "release": "release",
+          "state": 1
+        }
+        """
+    return ""
+
+
+def brew_call_removed(*args, **kwargs):
+    if 'listArchives' in args[0]:
+        return """
+        [
+          {
+            "build_id": "build_id",
+            "filename": "filename",
+            "group_id": "group_id",
+            "artifact_id": "artifact_id",
+            "version": "version",
+          }
+        ]"""
+    if 'getBuild' in args[0]:
+        return """
+        {
+          "package_name": "package_name",
+          "release": "release",
+          "state": 2
         }
         """
     return ""
 
 
 def test_get_brew_url(mocker):
-    mocker.patch('subprocess.check_output', side_effect=brew_call)
+    mocker.patch('subprocess.check_output', side_effect=brew_call_ok)
     url = tools.get_brew_url('aa')
     assert url == "http://download.devel.redhat.com/brewroot/packages/package_name/" + \
         "version/release/maven/group_id/artifact_id/version/filename"
 
 
+def test_get_brew_url_when_build_was_removed(mocker):
+    mocker.patch('subprocess.check_output', side_effect=brew_call_removed)
+
+    with pytest.raises(CekitError) as excinfo:
+        tools.get_brew_url('aa')
+
+    assert 'Artifact with checksum aa was found in Koji metadata but the build is in incorrect state (DELETED) making the artifact not available for downloading anymore' in str(
+        excinfo.value)
+
+
 @contextmanager
 def mocked_dependency_handler(mocker, data="ID=fedora\nNAME=somefedora\nVERSION=123"):
     dh = None

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-mock
apt-get update apt-get install -y gcc
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
-e git+https://github.com/cekit/cekit.git@93fc5628cbbf65bc31c40e0a6a42f0f2ff1ee2bf#egg=cekit
colorlog==6.9.0
coverage==7.8.0
docopt==0.6.2
exceptiongroup==1.2.2
iniconfig==2.1.0
Jinja2==3.1.6
MarkupSafe==3.0.2
packaging==24.2
pluggy==1.5.0
pykwalify==1.8.0
pytest==8.3.5
pytest-cov==6.0.0
pytest-mock==3.14.0
python-dateutil==2.9.0.post0
PyYAML==6.0.2
ruamel.yaml==0.18.10
ruamel.yaml.clib==0.2.12
six==1.17.0
tomli==2.2.1

name: cekit
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - colorlog==6.9.0
      - coverage==7.8.0
      - docopt==0.6.2
      - exceptiongroup==1.2.2
      - iniconfig==2.1.0
      - jinja2==3.1.6
      - markupsafe==3.0.2
      - packaging==24.2
      - pluggy==1.5.0
      - pykwalify==1.8.0
      - pytest==8.3.5
      - pytest-cov==6.0.0
      - pytest-mock==3.14.0
      - python-dateutil==2.9.0.post0
      - pyyaml==6.0.2
      - ruamel-yaml==0.18.10
      - ruamel-yaml-clib==0.2.12
      - six==1.17.0
      - tomli==2.2.1
prefix: /opt/conda/envs/cekit

tests/test_unit_tools.py::test_get_brew_url_when_build_was_removed
tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_library_only
tests/test_unit_tools.py::test_merging_description_image tests/test_unit_tools.py::test_merging_description_modules tests/test_unit_tools.py::test_merging_description_override tests/test_unit_tools.py::test_merging_plain_descriptors tests/test_unit_tools.py::test_merging_emdedded_descriptors tests/test_unit_tools.py::test_merging_plain_lists tests/test_unit_tools.py::test_merging_plain_list_of_list tests/test_unit_tools.py::test_merging_list_of_descriptors tests/test_unit_tools.py::test_merge_run_cmd tests/test_unit_tools.py::test_get_brew_url tests/test_unit_tools.py::test_dependency_handler_init_on_unknown_env_with_os_release_file tests/test_unit_tools.py::test_dependency_handler_init_on_known_env tests/test_unit_tools.py::test_dependency_handler_init_on_unknown_env_without_os_release_file tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_doesnt_fail_without_deps tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_executable_only tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_executable_only_failed tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_executable_and_package_on_known_platform tests/test_unit_tools.py::test_dependency_handler_handle_dependencies_with_platform_specific_package tests/test_unit_tools.py::test_dependency_handler_check_for_executable_with_executable_only tests/test_unit_tools.py::test_dependency_handler_check_for_executable_with_executable_fail tests/test_unit_tools.py::test_dependency_handler_check_for_executable_with_executable_fail_with_package
cekit/tools.py
cekit/tools.py