From 732146d9faf61fd3972afe7339b1c4e8f4bb885a Mon Sep 17 00:00:00 2001 From: Nathan Chancellor Date: Fri, 18 Aug 2023 13:56:05 -0700 Subject: [PATCH] utils.py: Do not query GitHub's API at all with '--gh-json-file' Occasionally, I have noticed the occasional failure to query GitHub's rate limit API for various reasons, even when providing an up to date release JSON file via '--gh-json-file'. A rate limit query is unneeded in this instance because we are not going to query GitHub's API for the latest release information, as it has already been provided to us by the user. Restructure the code so that the rate limit API is only queried when we will actually need to query the API for the latest release. Signed-off-by: Nathan Chancellor --- utils.py | 64 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/utils.py b/utils.py index 577d812..51d7419 100755 --- a/utils.py +++ b/utils.py @@ -184,40 +184,42 @@ def prepare_initrd(architecture, rootfs_format='cpio', gh_json_file=None): f"rootfs.{rootfs_format}.zst") src.parent.mkdir(exist_ok=True, parents=True) - # First, make sure that the current user is not rate limited by GitHub, - # otherwise the next API call will not return valid information. - gh_json_rl = get_gh_json('https://api.github.com/rate_limit') - remaining = gh_json_rl['resources']['core']['remaining'] - - # If we have API calls remaining or have already queried the API previously - # and cached the result, we can query for the latest release to make sure - # that we are up to date. - if remaining > 0 or gh_json_file: - if gh_json_file: - if not gh_json_file.exists(): - raise FileNotFoundError( - f"Provided GitHub JSON file ('{gh_json_file}') does not exist!" - ) - gh_json_rel = json.loads(gh_json_file.read_text(encoding='utf-8')) - else: + # If the user supplied a GitHub release JSON file, we do not need to bother + # querying the GitHub API at all. + if gh_json_file: + if not gh_json_file.exists(): + raise FileNotFoundError( + f"Provided GitHub JSON file ('{gh_json_file}') does not exist!" + ) + gh_json_rel = json.loads(gh_json_file.read_text(encoding='utf-8')) + else: + # Make sure that the current user is not rate limited by GitHub, + # otherwise the next API call will not return valid information. + gh_json_rl = get_gh_json('https://api.github.com/rate_limit') + + # If we have API calls remaining or have already queried the API previously + # and cached the result, we can query for the latest release to make sure + # that we are up to date. + if (remaining := gh_json_rl['resources']['core']['remaining']) > 0: gh_json_rel = get_gh_json( f"https://api.github.com/repos/{REPO}/releases/latest") - # Download the ramdisk if it is not already downloaded - if not src.exists(): + elif not src.exists(): + limit = gh_json_rl['resources']['core']['limit'] + raise RuntimeError( + f"Cannot query GitHub API for latest images release due to rate limit (remaining: {remaining}, limit: {limit}) and {src} does not exist already! " + 'Download it manually or supply a GitHub personal access token via the GITHUB_TOKEN environment variable to make an authenticated GitHub API request.' + ) + + # Download the ramdisk if it is not already downloaded + if not src.exists(): + download_initrd(gh_json_rel, src) + # If it is already downloaded, check that it is up to date and download + # an update only if necessary. + elif (rel_file := src.with_name('.release')).exists(): + cur_rel = rel_file.read_text(encoding='utf-8') + supplied_rel = gh_json_rel['tag_name'] + if cur_rel != supplied_rel: download_initrd(gh_json_rel, src) - # If it is already downloaded, check that it is up to date and download - # an update only if necessary. - elif (rel_file := src.with_name('.release')).exists(): - cur_rel = rel_file.read_text(encoding='utf-8') - latest_rel = gh_json_rel['tag_name'] - if cur_rel != latest_rel: - download_initrd(gh_json_rel, src) - elif not src.exists(): - limit = gh_json_rl['resources']['core']['limit'] - raise RuntimeError( - f"Cannot query GitHub API for latest images release due to rate limit (remaining: {remaining}, limit: {limit}) and {src} does not exist already! " - 'Download it manually or supply a GitHub personal access token via the GITHUB_TOKEN environment variable to make an authenticated GitHub API request.' - ) check_cmd('zstd') (dst := src.with_suffix('')).unlink(missing_ok=True)