From e3b8a297c8e871c133e8e9ef9949cd9f430b689b Mon Sep 17 00:00:00 2001 From: maleo Date: Tue, 22 Aug 2023 17:18:58 +0000 Subject: [PATCH] Avoid listing org packages where repo token doesn't have access --- clean_ghcr.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/clean_ghcr.py b/clean_ghcr.py index 0fe8f22..5ffd6eb 100644 --- a/clean_ghcr.py +++ b/clean_ghcr.py @@ -54,6 +54,17 @@ def get_req(path, params=None): def get_list_packages(owner, repo_name, owner_type, package_name): + if package_name: + url = get_url( + f"/{owner_type}s/{owner}/packages/container/{package_name}") + response = requests.get(url, headers=get_base_headers(), params=params) + result = [] + if not response.ok: + if response.status_code != 404: + raise Exception(response.text) + result.extend(response.json()) + return result + all_org_pkg = get_req( f"/{owner_type}s/{owner}/packages?package_type=container") if repo_name: @@ -61,10 +72,6 @@ def get_list_packages(owner, repo_name, owner_type, package_name): 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