Skip to content

Commit

Permalink
check pr 33 (#34)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Chizkiyahu and mering authored Nov 6, 2023
1 parent f2dddfa commit bbbab21
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 48 deletions.
23 changes: 19 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -159,15 +159,15 @@ 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
with:
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
```
27 changes: 12 additions & 15 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}
56 changes: 31 additions & 25 deletions clean_ghcr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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]
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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


Expand All @@ -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)

0 comments on commit bbbab21

Please sign in to comment.