diff --git a/scripts/python/remove-buildkit-cache.py b/scripts/python/remove-buildkit-cache.py index b6926c525225b8411ead20b7670160f80f8fd741..6213f77bcfc038fead1abe93ce28c613a4c679d9 100644 --- a/scripts/python/remove-buildkit-cache.py +++ b/scripts/python/remove-buildkit-cache.py @@ -11,8 +11,12 @@ import requests def append_names(names, data): """Appends names from JSON to name list""" + if not isinstance(data, list): + print(f"Warning: Expected list but got {type(data).__name__}: {data}") + return for record in data: - names.append(record['name']) + if isinstance(record, dict) and 'name' in record: + names.append(record['name']) def can_tag_be_deleted(branches, branch_name): """Checks if a tag should be deleted.""" @@ -26,6 +30,7 @@ def process_cache_registry_repository(gitlab_api_url, project_id, """Processes cache registry repositories""" repository_url = f"{gitlab_api_url}/projects/{project_id}/registry/repositories/{repository_id}" tags_url = f"{repository_url}/tags" + auth_headers = {'JOB-TOKEN': job_token} page_num = 1 params = { @@ -36,10 +41,13 @@ def process_cache_registry_repository(gitlab_api_url, project_id, image_tags = [] print(f"Fetching first page of tag list for repository ID {repository_id}.") - response = requests.get(url=tags_url, params=params, timeout=60) + response = requests.get(url=tags_url, params=params, headers=auth_headers, timeout=60) + if response.status_code != 200: + print(f"Error fetching tags: {response.status_code} - {response.text}") + return data = response.json() - headers = response.headers - total_pages = int(headers.get('x-total-pages', 1) if data else 0) + resp_headers = response.headers + total_pages = int(resp_headers.get('x-total-pages', 1) if data else 0) print(f"Total pages available: {total_pages}.") append_names(image_tags, data) @@ -48,7 +56,10 @@ def process_cache_registry_repository(gitlab_api_url, project_id, page_num += 1 params['page'] = page_num print(f"Fetching page {page_num} of tag list.") - response = requests.get(url=tags_url, params=params, timeout=60) + response = requests.get(url=tags_url, params=params, headers=auth_headers, timeout=60) + if response.status_code != 200: + print(f"Error fetching tags page {page_num}: {response.status_code}") + continue data = response.json() append_names(image_tags, data) @@ -75,15 +86,26 @@ def main(): job_token = os.environ['CI_JOB_TOKEN'] cache_repositories = os.environ['CACHE_REPOSITORIES'].split(',') cache_repository_ids = [] + auth_headers = {'JOB-TOKEN': job_token} print("Fetching cache registry repositories list.") repositories_url = f"{gitlab_api_url}/projects/{project_id}/registry/repositories" - response = requests.get(url=repositories_url, timeout=60) + response = requests.get(url=repositories_url, headers=auth_headers, timeout=60) + if response.status_code != 200: + print(f"Error fetching repositories: {response.status_code} - {response.text}") + return data = response.json() + if not isinstance(data, list): + print(f"Error: Expected list of repositories but got: {data}") + return for repo in data: - if repo['name'] in cache_repositories: + if isinstance(repo, dict) and repo.get('name') in cache_repositories: cache_repository_ids.append(repo['id']) + if not cache_repository_ids: + print("No cache repositories found to process.") + return + print("Fetching first page of branch list.") page_num = 1 params = { @@ -91,10 +113,16 @@ def main(): 'page': page_num } branches_url = f"{gitlab_api_url}/projects/{project_id}/repository/branches" - response = requests.get(url=branches_url, params=params, timeout=60) + response = requests.get(url=branches_url, params=params, headers=auth_headers, timeout=60) + if response.status_code != 200: + print(f"Error fetching branches: {response.status_code} - {response.text}") + return data = response.json() - headers = response.headers - total_pages = int(headers.get('x-total-pages', 1)) + if not isinstance(data, list): + print(f"Error: Expected list of branches but got: {data}") + return + resp_headers = response.headers + total_pages = int(resp_headers.get('x-total-pages', 1)) print(f"Total pages available: {total_pages}.") branches = data.copy() @@ -103,11 +131,15 @@ def main(): page_num += 1 params['page'] = page_num print(f"Fetching page {page_num} of branch list.") - response = requests.get(url=branches_url, params=params, timeout=60) + response = requests.get(url=branches_url, params=params, headers=auth_headers, timeout=60) + if response.status_code != 200: + print(f"Error fetching branches page {page_num}: {response.status_code}") + continue data = response.json() - branches.extend(data) + if isinstance(data, list): + branches.extend(data) - length=len(branches) + length = len(branches) print(f"Found total {length} branches.") for repository_id in cache_repository_ids: