Skip to content

Commit

Permalink
Merge pull request #4 from eugene-davis/add-org-support
Browse files Browse the repository at this point in the history
Add organization support
  • Loading branch information
chipkent authored Aug 16, 2022
2 parents 137af1c + 6aadec8 commit 1316a66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
github-token:
description: 'GitHub Personal Access Token with package deletion permissions.'
required: true
github-org:
description: 'If hosted in an organization, provide it here'
default: ''
required: true
runs:
using: "composite"
steps:
Expand All @@ -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.github-org }}
shell: bash
57 changes: 36 additions & 21 deletions docker-cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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"

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)
Expand All @@ -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/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 <package_name> <tag> <github_token>\nArgs: {sys.argv}")
if len(sys.argv) < 4 or len(sys.argv) > 5:
raise SystemExit(f"Usage: docker-cleanup.py <package_name> <tag> <github_token> <github-org (optional)>\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)

0 comments on commit 1316a66

Please sign in to comment.