Skip to content

Commit

Permalink
rebase PR 27 (#30)
Browse files Browse the repository at this point in the history
don't fails if pkg name not exist 

Co-authored-by: maleo <maleo@google.com>
  • Loading branch information
Chizkiyahu and mering committed Oct 31, 2023
1 parent 4f4ecbe commit 48d865f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ delete all / untagged ghcr containers in a repository
- name: Delete all containers from package without tags
uses: Chizkiyahu/delete-untagged-ghcr-action@v2
with:
token: ${{ secrets.PAT_TOKEN }}
token: ${{ github.token }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
package_name: the-package-name
Expand All @@ -145,12 +145,12 @@ delete all / untagged ghcr containers in a repository
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.PAT_TOKEN }}
username: ${{ github.repository_owner }}
password: ${{ github.token }}
- name: Delete all containers from package without tags
uses: Chizkiyahu/delete-untagged-ghcr-action@v2
with:
token: ${{ secrets.PAT_TOKEN }}
token: ${{ github.token }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
package_name: the-package-name
Expand All @@ -164,7 +164,7 @@ delete all / untagged ghcr containers in a repository
- name: Delete all containers from package
uses: Chizkiyahu/delete-untagged-ghcr-action@v2
with:
token: ${{ secrets.PAT_TOKEN }}
token: ${{ github.token }}
repository_owner: ${{ github.repository_owner }}
repository: ${{ github.repository }}
package_name: the-package-name
Expand Down
31 changes: 19 additions & 12 deletions clean_ghcr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import subprocess
import urllib.parse

import requests
import argparse
Expand Down Expand Up @@ -38,34 +39,40 @@ def get_req(path, params=None):
if "per_page" not in params:
params["per_page"] = PER_PAGE
url = get_url(path)
another_page = True
result = []
while another_page:
while True:
response = requests.get(url, headers=get_base_headers(), params=params)
if not response.ok:
raise Exception(response.text)
result.extend(response.json())
if "next" in response.links:
url = response.links["next"]["url"]
if "page" in params:
del params["page"]
else:
another_page = False

if "next" not in response.links:
break
url = response.links["next"]["url"]
if "page" in params:
del params["page"]
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()]

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")
and pkg["repository"]["name"].lower() == repo_name
]
if package_name:
all_org_pkg = [
pkg for pkg in all_org_pkg if pkg["name"] == package_name
]
return all_org_pkg


Expand Down

0 comments on commit 48d865f

Please sign in to comment.