diff --git a/package_alerts/package_alerts b/package_alerts/package_alerts index 60e5901c67e06d50ca330b44251fd23938c308cc..a1af9f5e7f1fbe42965603c81b768a4b09bdbcc3 100755 --- a/package_alerts/package_alerts +++ b/package_alerts/package_alerts @@ -82,17 +82,35 @@ def do_execute(cmd, tdir="/tmp", return_output=False, return_error=False): def get_changelog(token, checksum): """get the changelog for a rpm, which requires downloading it""" - url = f"https://api.access.redhat.com/management/v1/packages/{checksum}/download" - result = call_api(token, url, stream=True) + # We don't usually retry stuff from api calls as the script runs + # frequently, however - in this case a failure results in a blank + # changelog which is not very useful + attempts = 0 + max_attempts = 3 + rpm_downloaded = False temp_file = generate_temp_file("checksum") - # TODO: try and do this with dnf 'add_remote_rpms', although we would need to auth - with open(temp_file, "wb") as f: - try: - for chunk in result.iter_content(chunk_size=1024): - if chunk: - f.write(chunk) - except: - f.close() + url = f"https://api.access.redhat.com/management/v1/packages/{checksum}/download" + while attempts < max_attempts: + attempts += 1 + result = call_api(token, url, stream=True) + if result.status_code == 200: + # TODO: try and do this with dnf 'add_remote_rpms', although we would need to auth + with open(temp_file, "wb") as f: + try: + for chunk in result.iter_content(chunk_size=1024): + if chunk: + f.write(chunk) + rpm_downloaded = True + except: + print(f"Saving changelog appeared to have failed. Retrying {attempts}/{max_attempts}", flush=True) + f.close() + if rpm_downloaded: + break + else: + print(f"API call failed, Retrying {attempts}/{max_attempts}", flush=True) + if not rpm_downloaded: + print(f"Changelog failed to download, exiting", flush=True) + sys.exit(1) # TODO: maybe have some better logic instead of the head command changelog = do_execute(f"rpm -qp {temp_file} --changelog | head -20", return_output=True) # Even though we are running through a container, let's clean up after ourselves