This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch delete-retired-image-packages in repository https://gitbox.apache.org/repos/asf/airflow.git
commit bc31a2ebf70fd8766037b05db50d7427963bf304 Author: Jarek Potiuk <[email protected]> AuthorDate: Mon Nov 16 01:19:34 2026 +0900 Add a workflow that deletes the image packages of retired branches Nothing removes the CI and PROD image packages of a release branch or Python version once CI stops building them: ghcr.io still holds all versions of every branch since v2-2-test and of Python 3.6 to 3.10 on main, about 425k of the 532k image versions. Committers cannot delete them, because the packages are linked to the repository and deleting them needs admin rights on it, so a manually run workflow deletes them with its own token. A prek hook keeps its defaults on the maintained branches and Python versions, so a default run never deletes images that CI still uses. Generated-by: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01NyQq9STgK3cUrmMZrbJtqJ --- .../workflows/delete-retired-image-packages.yml | 78 +++++++++++++ .pre-commit-config.yaml | 14 +++ dev/breeze/doc/ci/02_images.md | 50 +++++++- scripts/ci/delete_retired_image_packages.py | 125 ++++++++++++++++++++ .../prek/update_retired_image_packages_defaults.py | 78 +++++++++++++ .../test_update_retired_image_packages_defaults.py | 84 ++++++++++++++ .../tests/ci/test_delete_retired_image_packages.py | 129 +++++++++++++++++++++ 7 files changed, 557 insertions(+), 1 deletion(-) diff --git a/.github/workflows/delete-retired-image-packages.yml b/.github/workflows/delete-retired-image-packages.yml new file mode 100644 index 00000000000..ee29d31e2c9 --- /dev/null +++ b/.github/workflows/delete-retired-image-packages.yml @@ -0,0 +1,78 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +--- +name: Delete retired image packages +# Run by hand after a branch or Python version is retired: deletes the CI and PROD image packages +# of every branch and Python version not listed to keep. Only the workflow token can delete the +# packages, which are linked to this repository. See "Deleting images of retired branches and +# Python versions" in dev/breeze/doc/ci/02_images.md. +on: # yamllint disable-line rule:truthy + workflow_dispatch: + inputs: + keep-branches: + description: "Space-separated branches whose images are kept" + type: string + default: "main v3-4-test" + keep-python: + description: "Space-separated Python versions whose images are kept" + type: string + default: "3.11 3.12 3.13 3.14" + only: + description: "Space-separated globs to delete only some packages, e.g. 'airflow/v2-*' (empty: all)" + type: string + default: "" + parallel: + description: "Deletions to run at once" + type: string + default: "4" + dry-run: + description: "Only list what would be deleted" + type: boolean + default: true +permissions: + contents: read +concurrency: + group: delete-retired-image-packages + cancel-in-progress: false +jobs: + delete: + name: "Delete retired image packages" + runs-on: ["ubuntu-slim"] + timeout-minutes: 30 + permissions: + contents: read + packages: write + if: github.repository == 'apache/airflow' + steps: + - name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )" + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: "Delete retired image packages" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_REPOSITORY: ${{ github.repository }} + KEEP_BRANCHES: ${{ inputs.keep-branches }} + KEEP_PYTHON: ${{ inputs.keep-python }} + ONLY: ${{ inputs.only }} + PARALLEL: ${{ inputs.parallel }} + DRY_RUN: ${{ inputs.dry-run && '--dry-run' || '' }} + run: > + python3 scripts/ci/delete_retired_image_packages.py + --keep-branches "${KEEP_BRANCHES}" --keep-python "${KEEP_PYTHON}" + --only "${ONLY}" --parallel "${PARALLEL}" ${DRY_RUN} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5aa9c046bd0..651941f7177 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1107,6 +1107,20 @@ repos: language: python files: ^airflow-core/docs/installation/prerequisites\.rst$|^README\.md$|^dev/breeze/src/airflow_breeze/global_constants\.py$|^scripts/ci/prek/update_tested_versions\.py$|^scripts/ci/prek/common_prek_utils\.py$ pass_filenames: false + - id: update-retired-image-packages-defaults + name: Update the kept branches and Python versions of the retired image packages workflow + env: + PYTHONPYCACHEPREFIX: .build/pycache + entry: ./scripts/ci/prek/update_retired_image_packages_defaults.py + language: python + files: > + (?x) + ^\.github/workflows/delete-retired-image-packages\.yml$| + ^\.github/workflows/scheduled-upgrade-check-.*\.yml$| + ^dev/breeze/src/airflow_breeze/global_constants\.py$| + ^scripts/ci/prek/update_retired_image_packages_defaults\.py$| + ^scripts/ci/prek/common_prek_utils\.py$ + pass_filenames: false - id: check-revision-heads-map name: Check that the REVISION_HEADS_MAP is up-to-date env: diff --git a/dev/breeze/doc/ci/02_images.md b/dev/breeze/doc/ci/02_images.md index b5d1796d4ff..fb8f0c9fe2b 100644 --- a/dev/breeze/doc/ci/02_images.md +++ b/dev/breeze/doc/ci/02_images.md @@ -33,6 +33,7 @@ - [CI image build arguments](#ci-image-build-arguments) - [Running the CI image](#running-the-ci-image) - [Naming conventions for stored images](#naming-conventions-for-stored-images) +- [Deleting images of retired branches and Python versions](#deleting-images-of-retired-branches-and-python-versions) <!-- END doctoc generated TOC please keep comment here to allow auto update --> @@ -619,9 +620,56 @@ percent-encoded when you access them via UI (/ = %2F) | CI image | airflow/\<BRANCH\>/ci/python\<X.Y\> | CI image - this is the image used for most of the tests. | | PROD image | airflow/\<BRANCH\>/prod/python\<X.Y\> | faster to build or pull. Production image optimized for size. | -- \<BRANCH\> might be either "main" or "v2-\*-test" +- \<BRANCH\> might be either "main" or a "vX-Y-test" release branch - \<X.Y\> - Python version (Major + Minor).Should be one of \["3.11", "3.12", "3.13", "3.14" \]. +# Deleting images of retired branches and Python versions + +Nothing removes the image packages of a release branch, or of a Python version, once CI stops +building them: every `airflow/<BRANCH>/(ci|prod)/python<X.Y>` package stays in the registry with +all of its versions. When a release branch is no longer maintained, or a Python version is dropped +from a branch, delete its packages with the **Delete retired image packages** workflow +(`.github/workflows/delete-retired-image-packages.yml`). It only runs when dispatched by hand, +from the Actions tab or with `gh workflow run`. + +The packages are linked to the `apache/airflow` repository, so deleting them needs admin rights on +it. Committers do not have them, and their own tokens get `404 Package not found`; the workflow +deletes with its own token instead. + +The workflow deletes every image package whose branch is not in `keep-branches` or whose Python +version is not in `keep-python`: + +| Input | Default | Meaning | +|-----------------|-----------------------|-------------------------------------------------------------------------------------------| +| `keep-branches` | `main v3-4-test` | Branches whose images are kept: every branch that is still maintained. | +| `keep-python` | `3.11 3.12 3.13 3.14` | Python versions whose images are kept, on the kept branches. | +| `only` | *(empty)* | Glob patterns to delete just some of the retired packages, e.g. `airflow/v2-*`; empty deletes them all. | +| `parallel` | `4` | How many packages are deleted at once. | +| `dry-run` | `true` | Only list the packages that would be deleted. | + +A run with the defaults must never delete images that CI still uses, so the +`update-retired-image-packages-defaults` prek hook keeps both defaults in sync: `keep-branches` +lists `main` and every branch with a `scheduled-upgrade-check-<branch>.yml` workflow, and +`keep-python` lists `ALLOWED_PYTHON_MAJOR_MINOR_VERSIONS` from +`dev/breeze/src/airflow_breeze/global_constants.py` on `main`. A kept branch keeps only the +Python versions in `keep-python`, so when a maintained branch still supports a Python version that +`main` dropped, add that version to `keep-python` for the run. + +Always start with a dry run and check the list, then delete one package before the rest: + +```bash +gh workflow run delete-retired-image-packages.yml -R apache/airflow \ + -f keep-branches="main v3-4-test" -f keep-python="3.11 3.12 3.13 3.14" +gh workflow run delete-retired-image-packages.yml -R apache/airflow \ + -f only="airflow/v2-2-test/prod/python3.6" -f dry-run=false +gh workflow run delete-retired-image-packages.yml -R apache/airflow -f dry-run=false +``` + +Deleting a package removes all of its versions. GitHub can restore a deleted package within 30 +days, from the organization's package settings, as long as nothing has been pushed under the same +name since. Building the Breeze image on a branch whose packages were deleted still works, but +without the registry cache. + ---- diff --git a/scripts/ci/delete_retired_image_packages.py b/scripts/ci/delete_retired_image_packages.py new file mode 100644 index 00000000000..0e8aae9b9f7 --- /dev/null +++ b/scripts/ci/delete_retired_image_packages.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Delete the CI and PROD image packages of branches and Python versions that are no longer built. + +Every branch and Python version keeps its ``<repo>/<branch>/(ci|prod)/python<version>`` package in +ghcr.io after it is retired, with all its versions. A package is deleted unless its branch is in +``--keep-branches`` and its Python version in ``--keep-python``; ``--only`` narrows the run to the +packages matching one of its glob patterns, for example to delete a single package first. Deleting a +package removes all of its versions in one call; GitHub can restore it within 30 days unless the +name is pushed to again. + +Usage: delete_retired_image_packages.py --keep-branches "main v3-4-test" + --keep-python "3.11 3.12 3.13 3.14" [--only "airflow/v2-*"] [--dry-run] +Environment: GITHUB_REPOSITORY (``owner/repo``) and a token for ``gh`` that may delete the packages. +""" + +from __future__ import annotations + +import argparse +import fnmatch +import os +import re +import subprocess +import sys +from concurrent.futures import ThreadPoolExecutor +from urllib.parse import quote + + +def get_image_package_pattern(repo: str) -> re.Pattern[str]: + return re.compile(rf"^{re.escape(repo)}/(?P<branch>[^/]+)/(ci|prod)/python(?P<python>[0-9.]+)$") + + +def find_retired_packages( + names: list[str], + repo: str, + keep_branches: set[str], + keep_python: set[str], + only: list[str] | None = None, +) -> list[str]: + pattern = get_image_package_pattern(repo) + retired = [] + for name in names: + match = pattern.match(name) + if not match: + continue + if match["branch"] in keep_branches and match["python"] in keep_python: + continue + if only and not any(fnmatch.fnmatchcase(name, glob) for glob in only): + continue + retired.append(name) + return sorted(retired) + + +def _gh_api(args: list[str]) -> subprocess.CompletedProcess[str]: + return subprocess.run(["gh", "api", *args], check=False, capture_output=True, text=True) + + +def _delete_package(owner: str, name: str) -> tuple[str, str | None]: + result = _gh_api(["-X", "DELETE", f"/orgs/{owner}/packages/container/{quote(name, safe='')}"]) + return name, None if result.returncode == 0 else (result.stderr.strip() or result.stdout.strip()) + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument("--keep-branches", required=True, help="Space-separated branches to keep") + parser.add_argument("--keep-python", required=True, help="Space-separated Python versions to keep") + parser.add_argument( + "--only", default="", help="Space-separated glob patterns; delete only the packages matching one" + ) + parser.add_argument("--parallel", type=int, default=4, help="Deletions to run at once") + parser.add_argument("--dry-run", action="store_true", help="Only print what would be deleted") + args = parser.parse_args() + + keep_branches = set(args.keep_branches.split()) + keep_python = set(args.keep_python.split()) + if not keep_branches or not keep_python: + print("Refusing to run without branches and Python versions to keep.") + return 1 + + owner, repo = os.environ["GITHUB_REPOSITORY"].split("/", 1) + listing = _gh_api( + ["--paginate", f"/orgs/{owner}/packages?package_type=container&per_page=100", "--jq", ".[].name"] + ) + if listing.returncode != 0: + print(f"Cannot list the packages: {listing.stderr.strip()}") + return 1 + retired = find_retired_packages( + listing.stdout.split(), repo, keep_branches, keep_python, args.only.split() + ) + print(f"{len(retired)} retired image package(s), keeping {sorted(keep_branches)} x {sorted(keep_python)}") + if args.dry_run: + for name in retired: + print(f"Would delete {name}") + return 0 + + failed = 0 + with ThreadPoolExecutor(max_workers=args.parallel) as executor: + for name, error in executor.map(lambda name: _delete_package(owner, name), retired): + if error: + failed += 1 + print(f"Could not delete {name}: {error}") + else: + print(f"Deleted {name}") + print(f"{len(retired) - failed} deleted, {failed} failed.") + return 1 if failed else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/ci/prek/update_retired_image_packages_defaults.py b/scripts/ci/prek/update_retired_image_packages_defaults.py new file mode 100644 index 00000000000..ddc12fc2577 --- /dev/null +++ b/scripts/ci/prek/update_retired_image_packages_defaults.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# /// script +# requires-python = ">=3.11,<3.12" +# dependencies = [] +# /// +""" +Keep the defaults of the "Delete retired image packages" workflow in sync. + +A run with the defaults must never delete images that CI still builds, so its ``keep-branches`` +default lists every maintained branch, which is every branch with a +``scheduled-upgrade-check-<branch>.yml`` workflow, and its ``keep-python`` default lists +ALLOWED_PYTHON_MAJOR_MINOR_VERSIONS from global_constants.py. +""" + +from __future__ import annotations + +import re +import sys +from pathlib import Path + +from common_prek_utils import AIRFLOW_ROOT_PATH, read_allowed_python_major_minor_versions + +WORKFLOWS_PATH = AIRFLOW_ROOT_PATH / ".github" / "workflows" +WORKFLOW_PATH = WORKFLOWS_PATH / "delete-retired-image-packages.yml" +SCHEDULED_UPGRADE_CHECK = re.compile(r"^scheduled-upgrade-check-(?P<branch>.+)\.yml$") + + +def find_maintained_branches(workflows_path: Path) -> list[str]: + branches = sorted( + match["branch"] + for path in workflows_path.iterdir() + if (match := SCHEDULED_UPGRADE_CHECK.match(path.name)) + ) + if "main" not in branches: + raise RuntimeError(f"No scheduled-upgrade-check-main.yml in {workflows_path}") + return ["main", *(branch for branch in branches if branch != "main")] + + +def set_input_default(text: str, input_name: str, value: str) -> str: + pattern = re.compile( + rf"(^ {{6}}{re.escape(input_name)}:\n(?: {{8}}(?!default:).*\n)*? {{8}}default: )\"[^\"\n]*\"$", + re.MULTILINE, + ) + updated, count = pattern.subn(lambda match: f'{match[1]}"{value}"', text) + if count != 1: + raise RuntimeError(f"Could not find the default of the {input_name!r} input") + return updated + + +def main() -> int: + original = WORKFLOW_PATH.read_text() + updated = set_input_default(original, "keep-branches", " ".join(find_maintained_branches(WORKFLOWS_PATH))) + updated = set_input_default(updated, "keep-python", " ".join(read_allowed_python_major_minor_versions())) + if updated == original: + return 0 + WORKFLOW_PATH.write_text(updated) + print(f"Updated the keep defaults in {WORKFLOW_PATH.relative_to(AIRFLOW_ROOT_PATH)}.") + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/tests/ci/prek/test_update_retired_image_packages_defaults.py b/scripts/tests/ci/prek/test_update_retired_image_packages_defaults.py new file mode 100644 index 00000000000..7de76696504 --- /dev/null +++ b/scripts/tests/ci/prek/test_update_retired_image_packages_defaults.py @@ -0,0 +1,84 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import textwrap + +import pytest +from ci.prek import update_retired_image_packages_defaults as hook + +WORKFLOW = textwrap.dedent( + """\ + on: + workflow_dispatch: + inputs: + keep-branches: + description: "Branches" + type: string + default: "main v3-3-test" + keep-python: + description: "Python versions" + type: string + default: "3.10 3.11" + only: + description: "Globs" + type: string + default: "" + """ +) + + +def test_find_maintained_branches_puts_main_first(tmp_path): + for name in [ + "scheduled-upgrade-check-v3-4-test.yml", + "scheduled-upgrade-check-main.yml", + "scheduled-upgrade-check-v3-5-test.yml", + "ci-amd.yml", + ]: + (tmp_path / name).touch() + + assert hook.find_maintained_branches(tmp_path) == ["main", "v3-4-test", "v3-5-test"] + + +def test_find_maintained_branches_requires_main(tmp_path): + (tmp_path / "scheduled-upgrade-check-v3-4-test.yml").touch() + + with pytest.raises(RuntimeError, match="scheduled-upgrade-check-main.yml"): + hook.find_maintained_branches(tmp_path) + + +def test_set_input_default_changes_only_that_input(): + updated = hook.set_input_default(WORKFLOW, "keep-python", "3.11 3.12") + + assert updated == WORKFLOW.replace('default: "3.10 3.11"', 'default: "3.11 3.12"') + + +def test_set_input_default_fails_for_a_missing_input(): + with pytest.raises(RuntimeError, match="'parallel'"): + hook.set_input_default(WORKFLOW, "parallel", "8") + + +def test_workflow_defaults_are_in_sync(): + text = hook.WORKFLOW_PATH.read_text() + expected = hook.set_input_default( + text, "keep-branches", " ".join(hook.find_maintained_branches(hook.WORKFLOWS_PATH)) + ) + expected = hook.set_input_default( + expected, "keep-python", " ".join(hook.read_allowed_python_major_minor_versions()) + ) + + assert text == expected, "run 'prek run update-retired-image-packages-defaults --all-files'" diff --git a/scripts/tests/ci/test_delete_retired_image_packages.py b/scripts/tests/ci/test_delete_retired_image_packages.py new file mode 100644 index 00000000000..2f0ac50baa3 --- /dev/null +++ b/scripts/tests/ci/test_delete_retired_image_packages.py @@ -0,0 +1,129 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import importlib.util +import subprocess +import sys +from pathlib import Path +from unittest import mock + +import pytest + +MODULE_PATH = Path(__file__).resolve().parents[3] / "scripts" / "ci" / "delete_retired_image_packages.py" +KEEP = ["--keep-branches", "main v3-4-test", "--keep-python", "3.11 3.12"] + + [email protected] +def script(): + module_name = "test_delete_retired_image_packages_module" + sys.modules.pop(module_name, None) + spec = importlib.util.spec_from_file_location(module_name, MODULE_PATH) + assert spec is not None + assert spec.loader is not None + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + spec.loader.exec_module(module) + return module + + +def _completed(returncode: int = 0, stdout: str = "", stderr: str = "") -> subprocess.CompletedProcess[str]: + return subprocess.CompletedProcess([], returncode, stdout=stdout, stderr=stderr) + + [email protected]( + ("name", "retired"), + [ + pytest.param("airflow/main/ci/python3.11", False, id="kept-branch-and-python"), + pytest.param("airflow/v3-4-test/prod/python3.12", False, id="kept-prod"), + pytest.param("airflow/main/ci/python3.10", True, id="retired-python-on-kept-branch"), + pytest.param("airflow/v3-3-test/ci/python3.11", True, id="retired-branch"), + pytest.param("airflow/v2-2-test/prod/python3.6", True, id="retired-both"), + pytest.param("airflow/main/ci/python3.1", True, id="python-prefix-is-not-kept"), + pytest.param("airflow-site/main/ci/python3.10", False, id="other-repo"), + pytest.param("airflow/main/docs/python3.10", False, id="not-an-image-package"), + pytest.param("airflow/main/ci/python3.10/extra", False, id="deeper-name"), + ], +) +def test_find_retired_packages(script, name, retired): + found = script.find_retired_packages([name], "airflow", {"main", "v3-4-test"}, {"3.11", "3.12"}) + + assert found == ([name] if retired else []) + + [email protected]( + ("only", "expected"), + [ + pytest.param(None, ["airflow/main/ci/python3.10", "airflow/v2-2-test/ci/python3.7"], id="no-filter"), + pytest.param(["airflow/v2-*"], ["airflow/v2-2-test/ci/python3.7"], id="branch-glob"), + pytest.param(["airflow/main/ci/python3.10"], ["airflow/main/ci/python3.10"], id="exact-name"), + pytest.param(["airflow/main/ci/python3.11"], [], id="never-a-kept-package"), + ], +) +def test_only_narrows_the_retired_packages(script, only, expected): + names = ["airflow/main/ci/python3.11", "airflow/main/ci/python3.10", "airflow/v2-2-test/ci/python3.7"] + + assert script.find_retired_packages(names, "airflow", {"main"}, {"3.11"}, only) == expected + + +def test_dry_run_deletes_nothing(script, monkeypatch, capsys): + monkeypatch.setenv("GITHUB_REPOSITORY", "apache/airflow") + monkeypatch.setattr(sys, "argv", ["script", *KEEP, "--dry-run"]) + listing = "airflow/main/ci/python3.11\nairflow/main/ci/python3.10\n" + with mock.patch.object(script, "_gh_api", autospec=True, return_value=_completed(stdout=listing)) as gh: + assert script.main() == 0 + + assert gh.call_count == 1 + assert "Would delete airflow/main/ci/python3.10" in capsys.readouterr().out + + +def test_deletes_retired_packages_and_reports_failures(script, monkeypatch, capsys): + monkeypatch.setenv("GITHUB_REPOSITORY", "apache/airflow") + monkeypatch.setattr(sys, "argv", ["script", *KEEP]) + listing = "airflow/main/ci/python3.11\nairflow/main/ci/python3.10\nairflow/v2-2-test/ci/python3.7\n" + + def gh_api(args): + if args[0] == "--paginate": + return _completed(stdout=listing) + if args[-1].endswith("v2-2-test%2Fci%2Fpython3.7"): + return _completed(returncode=1, stderr="Package not found. (HTTP 404)") + return _completed() + + with mock.patch.object(script, "_gh_api", autospec=True, side_effect=gh_api) as gh: + assert script.main() == 1 + + deleted = [call.args[0][-1] for call in gh.call_args_list if call.args[0][0] == "-X"] + assert sorted(deleted) == [ + "/orgs/apache/packages/container/airflow%2Fmain%2Fci%2Fpython3.10", + "/orgs/apache/packages/container/airflow%2Fv2-2-test%2Fci%2Fpython3.7", + ] + out = capsys.readouterr().out + assert "Deleted airflow/main/ci/python3.10" in out + assert "Could not delete airflow/v2-2-test/ci/python3.7: Package not found. (HTTP 404)" in out + + [email protected]( + "keep", + [["--keep-branches", " ", "--keep-python", "3.11"], ["--keep-branches", "main", "--keep-python", ""]], +) +def test_refuses_to_run_without_anything_to_keep(script, monkeypatch, keep): + monkeypatch.setenv("GITHUB_REPOSITORY", "apache/airflow") + monkeypatch.setattr(sys, "argv", ["script", *keep]) + with mock.patch.object(script, "_gh_api", autospec=True) as gh: + assert script.main() == 1 + + gh.assert_not_called()
