From bbbab219998078a91c9b283dac9389b825894603 Mon Sep 17 00:00:00 2001 From: Chizkiyahu Raful <37312901+Chizkiyahu@users.noreply.github.com> Date: Mon, 6 Nov 2023 21:47:26 +0200 Subject: [PATCH] check pr 33 (#34) * Allow deleting multiple explicit packages at once This also fixes the repo filtering when specifying packages explicitly * Quote bash variables correctly * yapf --------- Co-authored-by: maleo --- .github/workflows/test.yml | 23 +++++++++++++--- README.md | 8 +++--- action.yml | 27 ++++++++---------- clean_ghcr.py | 56 +++++++++++++++++++++----------------- 4 files changed, 66 insertions(+), 48 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 78b09cb..226d85e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,8 +16,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - i: [1,2] - type: ['p1', 'p2'] + i: [1, 2] + type: ['p1', 'p2', 'p3'] steps: - uses: actions/checkout@v4 - name: Set up Docker Buildx @@ -48,8 +48,8 @@ jobs: needs: add_temp_pkgs1 strategy: matrix: - i: [ 1,2 ] - type: [ 'p1', 'p2' ] + i: [1, 2] + type: ['p1', 'p2', 'p3'] steps: - uses: actions/checkout@v4 - name: Set up Docker Buildx @@ -130,6 +130,21 @@ jobs: untagged_only: false owner_type: user + delete_multiple_packages: + name: delete package + runs-on: ubuntu-latest + needs: clean_untagged_pkgs2 + steps: + - uses: actions/checkout@v4 + - uses: ./ + with: + token: ${{ secrets.GITHUB_TOKEN }} + repository_owner: ${{ github.repository_owner }} + repository: ${{ github.repository }} + package_name: p2, p3 + untagged_only: false + owner_type: user + clean_repo: name: delete all package in repo runs-on: ubuntu-latest diff --git a/README.md b/README.md index 3227dde..50ace26 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ delete all / untagged ghcr containers in a repository # 'The repository owner name' # Default: ${{ github.repository_owner }} repository_owner: '' - # 'The package name' - # Delete only from package name + # 'The package names' + # Delete only from comma separated package names # required: false package_name: '' # Delete only package versions without tag @@ -159,7 +159,7 @@ delete all / untagged ghcr containers in a repository except_untagged_multiplatform: true ``` -## Delete all containers from package +## Delete all containers from packages ```yaml - name: Delete all containers from package uses: Chizkiyahu/delete-untagged-ghcr-action@v3 @@ -167,7 +167,7 @@ delete all / untagged ghcr containers in a repository token: ${{ github.token }} repository_owner: ${{ github.repository_owner }} repository: ${{ github.repository }} - package_name: the-package-name + package_name: the-package-name, other-package-name untagged_only: false owner_type: org # or user ``` diff --git a/action.yml b/action.yml index 20fa9f1..beae8e6 100644 --- a/action.yml +++ b/action.yml @@ -23,7 +23,7 @@ inputs: required: false package_name: - description: 'Delete only from package name' + description: 'Delete only from comma separated package names' required: false untagged_only: @@ -49,20 +49,17 @@ runs: - run: pip install -r ${{ github.action_path }}/requirements.txt shell: bash - run: | - args="--token ${{ inputs.token }} " - args+=" --repository_owner ${{ inputs.repository_owner }} " - if [[ $repo ]]; then - args+=" --repository $repo " + args=( "--token" "${{ inputs.token }}" ) + args+=( "--repository_owner" "${{ inputs.repository_owner }}" ) + if [[ -n "${{ inputs.repository }}" ]]; then + args+=( "--repository" "${{ inputs.repository }}" ) fi - if [[ $pkg ]]; then - args+=" --package_name $pkg " + if [[ -n "${{ inputs.package_name }}" ]]; then + args+=( "--package_names" "${{ inputs.package_name }}" ) fi - args+=" --untagged_only ${{ inputs.untagged_only }} " - args+=" --except_untagged_multiplatform ${{ inputs.except_untagged_multiplatform }} " - args+=" --owner_type ${{ inputs.owner_type }} " - echo "args: $args" - python ${{ github.action_path }}/clean_ghcr.py $args + args+=( "--untagged_only" "${{ inputs.untagged_only }}" ) + args+=( "--except_untagged_multiplatform" "${{ inputs.except_untagged_multiplatform }}" ) + args+=( "--owner_type" "${{ inputs.owner_type }}" ) + echo "args: ${args[@]}" + python ${{ github.action_path }}/clean_ghcr.py "${args[@]}" shell: bash - env: - repo: ${{ inputs.repository }} - pkg: ${{ inputs.package_name }} diff --git a/clean_ghcr.py b/clean_ghcr.py index 14343dc..1c34753 100644 --- a/clean_ghcr.py +++ b/clean_ghcr.py @@ -54,33 +54,37 @@ def get_req(path, params=None): return result -def get_list_packages(owner, repo_name, owner_type, package_name): - if package_name: - clean_package_name = urllib.parse.quote(package_name, safe='') - url = get_url( - f"/{owner_type}s/{owner}/packages/container/{clean_package_name}") - response = requests.get(url, headers=get_base_headers()) - if not response.ok: - if response.status_code == 404: - return [] - raise Exception(response.text) - return [response.json()] +def get_list_packages(owner, repo_name, owner_type, package_names): + pkgs = [] + if package_names: + for package_name in package_names: + clean_package_name = urllib.parse.quote(package_name, safe='') + url = get_url( + f"/{owner_type}s/{owner}/packages/container/{clean_package_name}" + ) + response = requests.get(url, headers=get_base_headers()) + if not response.ok: + if response.status_code == 404: + return [] + raise Exception(response.text) + pkgs.append(response.json()) + else: + pkgs = get_req( + f"/{owner_type}s/{owner}/packages?package_type=container") - all_org_pkg = get_req( - f"/{owner_type}s/{owner}/packages?package_type=container") if repo_name: - all_org_pkg = [ - pkg for pkg in all_org_pkg if pkg.get("repository") + pkgs = [ + pkg for pkg in pkgs if pkg.get("repository") and pkg["repository"]["name"].lower() == repo_name ] - return all_org_pkg + return pkgs -def get_all_package_versions(owner, repo_name, package_name, owner_type): +def get_all_package_versions(owner, repo_name, package_names, owner_type): packages = get_list_packages( owner=owner, repo_name=repo_name, - package_name=package_name, + package_names=package_names, owner_type=owner_type, ) return { @@ -118,13 +122,13 @@ def get_manifest(image): return res.stdout.decode("utf-8") -def delete_pkgs(owner, repo_name, owner_type, package_name, untagged_only, +def delete_pkgs(owner, repo_name, owner_type, package_names, untagged_only, except_untagged_multiplatform): if untagged_only: all_packages = get_all_package_versions( owner=owner, repo_name=repo_name, - package_name=package_name, + package_names=package_names, owner_type=owner_type, ) tagged_pkgs = { @@ -150,7 +154,7 @@ def delete_pkgs(owner, repo_name, owner_type, package_name, untagged_only, packages = get_list_packages( owner=owner, repo_name=repo_name, - package_name=package_name, + package_names=package_names, owner_type=owner_type, ) status = [del_req(pkg["url"]).ok for pkg in packages] @@ -192,11 +196,11 @@ def get_args(): help="Delete only repository name", ) parser.add_argument( - "--package_name", + "--package_names", type=str, required=False, default="", - help="Delete only package name", + help="Delete only comma separated package names", ) parser.add_argument( "--untagged_only", @@ -225,7 +229,9 @@ def get_args(): args.repository = repository args.repository = args.repository.lower() args.repository_owner = args.repository_owner.lower() - args.package_name = args.package_name.lower() + args.package_names = args.package_names.lower() + args.package_names = [p.strip() for p in args.package_names.split(",") + ] if args.package_names else [] return args @@ -234,7 +240,7 @@ def get_args(): delete_pkgs( owner=args.repository_owner, repo_name=args.repository, - package_name=args.package_name, + package_names=args.package_names, untagged_only=args.untagged_only, owner_type=args.owner_type, except_untagged_multiplatform=args.except_untagged_multiplatform)