From 1830909ae3b4ebefe5e69b0f6fb1157b63bcab88 Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Sat, 9 Jul 2022 00:06:54 +0200 Subject: [PATCH 1/7] Add org deletion support --- docker-cleanup.py | 57 ++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/docker-cleanup.py b/docker-cleanup.py index a5a71e0..24e58d1 100644 --- a/docker-cleanup.py +++ b/docker-cleanup.py @@ -6,15 +6,21 @@ import requests -def get_package_versions(package_name, package_type): - print("get_package_versions", package_name, package_type) - # https://docs.github.com/en/rest/reference/packages#get-all-package-versions-for-a-package-owned-by-the-authenticated-user +def get_package_versions(package_name, package_type, org): + print("get_package_versions", package_name, package_type, org) result = [] done = False page = 1 + if org: + # https://docs.github.com/en/rest/packages#list-packages-for-an-organization= + f"https://api.github.com/user/packages/orgs/{org}/packages/{package_type}/{package_name}/versions" + else: + # https://docs.github.com/en/rest/reference/packages#get-all-package-versions-for-a-package-owned-by-the-authenticated-user + package_url = f"https://api.github.com/user/packages/{package_type}/{package_name}/versions" + while not done: - response = session.get(f"https://api.github.com/user/packages/{package_type}/{package_name}/versions", params={"per_page" : 100, "page": page}) + response = session.get(package_url, params={"per_page" : 100, "page": page}) response.raise_for_status() json = response.json() result.extend(json) @@ -24,46 +30,55 @@ def get_package_versions(package_name, package_type): return result -def delete_package_version(package_name, package_type, id): - print("delete_package_version", package_name, package_type, id) - - # https://docs.github.com/en/rest/reference/packages#delete-a-package-version-for-the-authenticated-user - response = session.delete(f"https://api.github.com/user/packages/{package_type}/{package_name}/versions/{id}") +def delete_package_version(package_name, package_type, id, org): + print("delete_package_version", package_name, package_type, id, org) + + if org: + # https://docs.github.com/en/rest/packages#delete-package-version-for-an-organization= + delete_url = f"https://api.github.com/user/packages/orgs/{org}/packages/{package_type}/{package_name}/versions/{id}" + else: + # https://docs.github.com/en/rest/reference/packages#delete-a-package-version-for-the-authenticated-user + delete_url = f"https://api.github.com/user/packages/{package_type}/{package_name}/versions/{id}" + response = session.delete(delete_url) response.raise_for_status() -def get_tagged_container(package_name, tag): - print("get_tagged_container", package_name, tag) - packages = get_package_versions(package_name, "container") +def get_tagged_container(package_name, tag, org): + print("get_tagged_container", package_name, tag, org) + packages = get_package_versions(package_name, "container", org) return [version for version in packages if tag in version["metadata"]["container"]["tags"]] -def delete_tagged_container(package_name, tag): - print("delete_tagged_container", package_name, tag) - tags = get_tagged_container(package_name, tag) +def delete_tagged_container(package_name, tag, org): + print("delete_tagged_container", package_name, tag, org) + tags = get_tagged_container(package_name, tag, org) if tags: for tag in tags: - delete_package_version(package_name, "container", tag["id"]) + delete_package_version(package_name, "container", tag["id"], org) else: - raise Exception(f"No containers to delete: {package_name} {tag}") + raise Exception(f"No containers to delete: {package_name} {tag}") ################################ import sys -if len(sys.argv) != 4: - raise SystemExit(f"Usage: docker-cleanup.py \nArgs: {sys.argv}") +if len(sys.argv) < 4 or len(sys.argv) > 5: + raise SystemExit(f"Usage: docker-cleanup.py \nArgs: {sys.argv}") package_name = sys.argv[1] tag = sys.argv[2] token = sys.argv[3] +org = None +if len(sys.argv) == 5: + org = sys.argv[4] + print("package_name = ", package_name) print("tag = ", tag) +print("org = ", org) session = requests.Session() session.headers.update({"Authorization": f"token {token}", "Accept" : "application/vnd.github.v3+json"}) -delete_tagged_container(package_name, tag) - +delete_tagged_container(package_name, tag, org) From 378a446491523035ed2d606efcfec0573281f0ab Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Sat, 9 Jul 2022 16:40:06 +0200 Subject: [PATCH 2/7] Update action configuration --- action.yml | 6 +++++- docker-cleanup.py | 7 ++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/action.yml b/action.yml index 89a3376..f379068 100644 --- a/action.yml +++ b/action.yml @@ -11,6 +11,10 @@ inputs: github-token: description: 'GitHub Personal Access Token with package deletion permissions.' required: true + org: + description: 'If hosted in an organization, provide it here' + default: "" + required: true runs: using: "composite" steps: @@ -21,5 +25,5 @@ runs: run: pip install requests shell: bash - name: Delete image from ghcr.io - run: python ${{ github.action_path }}/docker-cleanup.py ${{ inputs.package-name }} ${{ inputs.tag }} ${{ inputs.github-token }} + run: python ${{ github.action_path }}/docker-cleanup.py ${{ inputs.package-name }} ${{ inputs.tag }} ${{ inputs.github-token }} ${{ inputs.org }} shell: bash \ No newline at end of file diff --git a/docker-cleanup.py b/docker-cleanup.py index 24e58d1..80873e4 100644 --- a/docker-cleanup.py +++ b/docker-cleanup.py @@ -64,16 +64,13 @@ def delete_tagged_container(package_name, tag, org): import sys -if len(sys.argv) < 4 or len(sys.argv) > 5: +if len(sys.argv) != 5: raise SystemExit(f"Usage: docker-cleanup.py \nArgs: {sys.argv}") package_name = sys.argv[1] tag = sys.argv[2] token = sys.argv[3] - -org = None -if len(sys.argv) == 5: - org = sys.argv[4] +org = sys.argv[4] print("package_name = ", package_name) print("tag = ", tag) From cc30ae43592301397c3baeb6e8c996eb41f6c7ab Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Sat, 9 Jul 2022 16:48:33 +0200 Subject: [PATCH 3/7] Cleanup error --- docker-cleanup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-cleanup.py b/docker-cleanup.py index 80873e4..2025261 100644 --- a/docker-cleanup.py +++ b/docker-cleanup.py @@ -65,7 +65,7 @@ def delete_tagged_container(package_name, tag, org): import sys if len(sys.argv) != 5: - raise SystemExit(f"Usage: docker-cleanup.py \nArgs: {sys.argv}") + raise SystemExit(f"Usage: docker-cleanup.py \nArgs: {sys.argv}") package_name = sys.argv[1] tag = sys.argv[2] From 3a7d988e533b029e937d5c8d466b27af2ef2bb76 Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Sat, 9 Jul 2022 16:56:52 +0200 Subject: [PATCH 4/7] Change org input variable --- action.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index f379068..7b4b619 100644 --- a/action.yml +++ b/action.yml @@ -11,9 +11,9 @@ inputs: github-token: description: 'GitHub Personal Access Token with package deletion permissions.' required: true - org: + github-org: description: 'If hosted in an organization, provide it here' - default: "" + default: '' required: true runs: using: "composite" @@ -25,5 +25,5 @@ runs: run: pip install requests shell: bash - name: Delete image from ghcr.io - run: python ${{ github.action_path }}/docker-cleanup.py ${{ inputs.package-name }} ${{ inputs.tag }} ${{ inputs.github-token }} ${{ inputs.org }} + run: python ${{ github.action_path }}/docker-cleanup.py ${{ inputs.package-name }} ${{ inputs.tag }} ${{ inputs.github-token }} ${{ inputs.github-org }} shell: bash \ No newline at end of file From 321119bb86b563e936135b7f8911a27b164c8356 Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Sat, 9 Jul 2022 17:01:27 +0200 Subject: [PATCH 5/7] Reset to accomadate args properly --- docker-cleanup.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docker-cleanup.py b/docker-cleanup.py index 2025261..2e3164c 100644 --- a/docker-cleanup.py +++ b/docker-cleanup.py @@ -64,13 +64,16 @@ def delete_tagged_container(package_name, tag, org): import sys -if len(sys.argv) != 5: - raise SystemExit(f"Usage: docker-cleanup.py \nArgs: {sys.argv}") +if len(sys.argv) < 4 or len(sys.argv) > 5: + raise SystemExit(f"Usage: docker-cleanup.py \nArgs: {sys.argv}") package_name = sys.argv[1] tag = sys.argv[2] token = sys.argv[3] -org = sys.argv[4] + +org = None +if len(sys.argv) == 5: + org = sys.argv[4] print("package_name = ", package_name) print("tag = ", tag) From 38463c0ad3c46ebb028bc4e70ee78942b81dfaaf Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Sun, 10 Jul 2022 12:36:31 +0200 Subject: [PATCH 6/7] Fix unset variable --- docker-cleanup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-cleanup.py b/docker-cleanup.py index 2e3164c..6e5d02d 100644 --- a/docker-cleanup.py +++ b/docker-cleanup.py @@ -14,7 +14,7 @@ def get_package_versions(package_name, package_type, org): if org: # https://docs.github.com/en/rest/packages#list-packages-for-an-organization= - f"https://api.github.com/user/packages/orgs/{org}/packages/{package_type}/{package_name}/versions" + package_url = f"https://api.github.com/user/packages/orgs/{org}/packages/{package_type}/{package_name}/versions" else: # https://docs.github.com/en/rest/reference/packages#get-all-package-versions-for-a-package-owned-by-the-authenticated-user package_url = f"https://api.github.com/user/packages/{package_type}/{package_name}/versions" From 6aadec84945c307836d4c420c14fef637ab4e349 Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Sun, 10 Jul 2022 12:38:43 +0200 Subject: [PATCH 7/7] Correct copy paste error --- docker-cleanup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-cleanup.py b/docker-cleanup.py index 6e5d02d..1117586 100644 --- a/docker-cleanup.py +++ b/docker-cleanup.py @@ -14,7 +14,7 @@ def get_package_versions(package_name, package_type, org): if org: # https://docs.github.com/en/rest/packages#list-packages-for-an-organization= - package_url = f"https://api.github.com/user/packages/orgs/{org}/packages/{package_type}/{package_name}/versions" + package_url = f"https://api.github.com/orgs/{org}/packages/{package_type}/{package_name}/versions" else: # https://docs.github.com/en/rest/reference/packages#get-all-package-versions-for-a-package-owned-by-the-authenticated-user package_url = f"https://api.github.com/user/packages/{package_type}/{package_name}/versions" @@ -35,7 +35,7 @@ def delete_package_version(package_name, package_type, id, org): if org: # https://docs.github.com/en/rest/packages#delete-package-version-for-an-organization= - delete_url = f"https://api.github.com/user/packages/orgs/{org}/packages/{package_type}/{package_name}/versions/{id}" + delete_url = f"https://api.github.com/orgs/{org}/packages/{package_type}/{package_name}/versions/{id}" else: # https://docs.github.com/en/rest/reference/packages#delete-a-package-version-for-the-authenticated-user delete_url = f"https://api.github.com/user/packages/{package_type}/{package_name}/versions/{id}"