diff --git a/.github/scripts/cleanup-tags.py b/.github/scripts/cleanup-tags.py index b89bd8ae0..9b299d048 100644 --- a/.github/scripts/cleanup-tags.py +++ b/.github/scripts/cleanup-tags.py @@ -249,10 +249,12 @@ class MainImageTagsCleaner(RegistryTagsCleaner): will be removed, if the corresponding branch no longer exists. """ + # Default to everything gets kept still + super().decide_what_tags_to_keep() + # Locate the feature branches feature_branches = {} for branch in self.branch_api.get_branches( - owner=self.repo_owner, repo=self.repo_name, ): if branch.name.startswith("feature-"): @@ -261,6 +263,10 @@ class MainImageTagsCleaner(RegistryTagsCleaner): logger.info(f"Located {len(feature_branches)} feature branches") + if not len(feature_branches): + # Our work here is done, delete nothing + return + # Filter to packages which are tagged with feature-* packages_tagged_feature: List[ContainerPackage] = [] for package in self.all_package_versions: diff --git a/.github/scripts/github.py b/.github/scripts/github.py index 63f34a1e9..b24e168f5 100644 --- a/.github/scripts/github.py +++ b/.github/scripts/github.py @@ -15,7 +15,7 @@ from typing import Dict from typing import List from typing import Optional -import requests +import httpx logger = logging.getLogger("github-api") @@ -28,15 +28,15 @@ class _GithubApiBase: def __init__(self, token: str) -> None: self._token = token - self._session: Optional[requests.Session] = None + self._client: Optional[httpx.Client] = None def __enter__(self) -> "_GithubApiBase": """ Sets up the required headers for auth and response type from the API """ - self._session = requests.Session() - self._session.headers.update( + self._client = httpx.Client() + self._client.headers.update( { "Accept": "application/vnd.github.v3+json", "Authorization": f"token {self._token}", @@ -49,14 +49,14 @@ class _GithubApiBase: Ensures the authorization token is cleaned up no matter the reason for the exit """ - if "Accept" in self._session.headers: - del self._session.headers["Accept"] - if "Authorization" in self._session.headers: - del self._session.headers["Authorization"] + if "Accept" in self._client.headers: + del self._client.headers["Accept"] + if "Authorization" in self._client.headers: + del self._client.headers["Authorization"] # Close the session as well - self._session.close() - self._session = None + self._client.close() + self._client = None def _read_all_pages(self, endpoint): """ @@ -66,7 +66,7 @@ class _GithubApiBase: internal_data = [] while True: - resp = self._session.get(endpoint) + resp = self._client.get(endpoint) if resp.status_code == 200: internal_data += resp.json() if "next" in resp.links: @@ -76,7 +76,7 @@ class _GithubApiBase: break else: logger.warning(f"Request to {endpoint} return HTTP {resp.status_code}") - break + resp.raise_for_status() return internal_data @@ -113,14 +113,15 @@ class GithubBranchApi(_GithubApiBase): def __init__(self, token: str) -> None: super().__init__(token) - self._ENDPOINT = "https://api.github.com/repos/{OWNER}/{REPO}/branches" + self._ENDPOINT = "https://api.github.com/repos/{REPO}/branches" - def get_branches(self, owner: str, repo: str) -> List[GithubBranch]: + def get_branches(self, repo: str) -> List[GithubBranch]: """ Returns all current branches of the given repository owned by the given owner or organization. """ - endpoint = self._ENDPOINT.format(OWNER=owner, REPO=repo) + # The environment GITHUB_REPOSITORY already contains the owner in the correct location + endpoint = self._ENDPOINT.format(REPO=repo) internal_data = self._read_all_pages(endpoint) return [GithubBranch(branch) for branch in internal_data] @@ -247,7 +248,7 @@ class GithubContainerRegistryApi(_GithubApiBase): """ Deletes the given package version from the GHCR """ - resp = self._session.delete(package_data.url) + resp = self._client.delete(package_data.url) if resp.status_code != 204: logger.warning( f"Request to delete {package_data.url} returned HTTP {resp.status_code}", @@ -266,7 +267,7 @@ class GithubContainerRegistryApi(_GithubApiBase): PACKAGE_VERSION_ID=package_data.id, ) - resp = self._session.post(endpoint) + resp = self._client.post(endpoint) if resp.status_code != 204: logger.warning( f"Request to delete {endpoint} returned HTTP {resp.status_code}", diff --git a/.github/workflows/cleanup-tags.yml b/.github/workflows/cleanup-tags.yml index 308b7f2ed..2b63b3000 100644 --- a/.github/workflows/cleanup-tags.yml +++ b/.github/workflows/cleanup-tags.yml @@ -64,9 +64,9 @@ jobs: with: python-version: "3.10" - - name: Install requests + name: Install httpx run: | - python -m pip install requests + python -m pip install httpx # # Clean up primary package #