From 1126fc6ede9a74a6776f1816636ee8457c50ecb2 Mon Sep 17 00:00:00 2001 From: mgoerens Date: Tue, 1 Oct 2024 10:02:41 +0200 Subject: [PATCH] Generate a valid empty index when none is found (#389) Signed-off-by: Matthias Goerens --- scripts/src/submission/submission.py | 38 ++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/scripts/src/submission/submission.py b/scripts/src/submission/submission.py index 0867878c..d556dcee 100644 --- a/scripts/src/submission/submission.py +++ b/scripts/src/submission/submission.py @@ -575,15 +575,37 @@ def get_file_type(file_path): return "unknwown", None -def download_index_data(repository, branch="gh_pages"): - """Download the helm repository index""" - r = requests.get( - f"https://raw.githubusercontent.com/{repository}/{branch}/index.yaml" - ) +def download_index_data( + repository: str, branch: str = "gh-pages", ignore_missing: bool = False +) -> dict: + """Download the helm repository index + + Args: + repository (str): Name of the GitHub repository to download the index file from. + (e.g. "openshift-helm-charts/charts") + branch (str): GitHub branch to download the index file from. Defaults to "gh-pages". + ignore_missing (bool): Set to True to return a valid empty index, in the case the + download of index.yaml fails. + + Returns: + dict: The helm repository index + + Raise: + HelmIndexError if the download fails. If not on the production repository, doesn't raise + and returns an empty index file instead. + HelmIndexError if the index file is not valid YAML. + + """ + index_url = f"https://raw.githubusercontent.com/{repository}/{branch}/index.yaml" + r = requests.get(index_url) - if r.status_code == 200: - data = yaml.load(r.text, Loader=Loader) + data = {"apiVersion": "v1", "entries": {}} + if r.status_code != 200 and not ignore_missing: + raise HelmIndexError(f"Error retrieving index file at {index_url}") else: - data = {} + try: + data = yaml.load(r.text, Loader=Loader) + except yaml.YAMLError as e: + raise HelmIndexError(f"Error parsing index file at {index_url}") from e return data