Skip to content

Commit 732146d

Browse files
committed
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 <nathan@kernel.org>
1 parent a75b275 commit 732146d

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

utils.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -184,40 +184,42 @@ def prepare_initrd(architecture, rootfs_format='cpio', gh_json_file=None):
184184
f"rootfs.{rootfs_format}.zst")
185185
src.parent.mkdir(exist_ok=True, parents=True)
186186

187-
# First, make sure that the current user is not rate limited by GitHub,
188-
# otherwise the next API call will not return valid information.
189-
gh_json_rl = get_gh_json('https://api.github.com/rate_limit')
190-
remaining = gh_json_rl['resources']['core']['remaining']
191-
192-
# If we have API calls remaining or have already queried the API previously
193-
# and cached the result, we can query for the latest release to make sure
194-
# that we are up to date.
195-
if remaining > 0 or gh_json_file:
196-
if gh_json_file:
197-
if not gh_json_file.exists():
198-
raise FileNotFoundError(
199-
f"Provided GitHub JSON file ('{gh_json_file}') does not exist!"
200-
)
201-
gh_json_rel = json.loads(gh_json_file.read_text(encoding='utf-8'))
202-
else:
187+
# If the user supplied a GitHub release JSON file, we do not need to bother
188+
# querying the GitHub API at all.
189+
if gh_json_file:
190+
if not gh_json_file.exists():
191+
raise FileNotFoundError(
192+
f"Provided GitHub JSON file ('{gh_json_file}') does not exist!"
193+
)
194+
gh_json_rel = json.loads(gh_json_file.read_text(encoding='utf-8'))
195+
else:
196+
# Make sure that the current user is not rate limited by GitHub,
197+
# otherwise the next API call will not return valid information.
198+
gh_json_rl = get_gh_json('https://api.github.com/rate_limit')
199+
200+
# If we have API calls remaining or have already queried the API previously
201+
# and cached the result, we can query for the latest release to make sure
202+
# that we are up to date.
203+
if (remaining := gh_json_rl['resources']['core']['remaining']) > 0:
203204
gh_json_rel = get_gh_json(
204205
f"https://api.github.com/repos/{REPO}/releases/latest")
205-
# Download the ramdisk if it is not already downloaded
206-
if not src.exists():
206+
elif not src.exists():
207+
limit = gh_json_rl['resources']['core']['limit']
208+
raise RuntimeError(
209+
f"Cannot query GitHub API for latest images release due to rate limit (remaining: {remaining}, limit: {limit}) and {src} does not exist already! "
210+
'Download it manually or supply a GitHub personal access token via the GITHUB_TOKEN environment variable to make an authenticated GitHub API request.'
211+
)
212+
213+
# Download the ramdisk if it is not already downloaded
214+
if not src.exists():
215+
download_initrd(gh_json_rel, src)
216+
# If it is already downloaded, check that it is up to date and download
217+
# an update only if necessary.
218+
elif (rel_file := src.with_name('.release')).exists():
219+
cur_rel = rel_file.read_text(encoding='utf-8')
220+
supplied_rel = gh_json_rel['tag_name']
221+
if cur_rel != supplied_rel:
207222
download_initrd(gh_json_rel, src)
208-
# If it is already downloaded, check that it is up to date and download
209-
# an update only if necessary.
210-
elif (rel_file := src.with_name('.release')).exists():
211-
cur_rel = rel_file.read_text(encoding='utf-8')
212-
latest_rel = gh_json_rel['tag_name']
213-
if cur_rel != latest_rel:
214-
download_initrd(gh_json_rel, src)
215-
elif not src.exists():
216-
limit = gh_json_rl['resources']['core']['limit']
217-
raise RuntimeError(
218-
f"Cannot query GitHub API for latest images release due to rate limit (remaining: {remaining}, limit: {limit}) and {src} does not exist already! "
219-
'Download it manually or supply a GitHub personal access token via the GITHUB_TOKEN environment variable to make an authenticated GitHub API request.'
220-
)
221223

222224
check_cmd('zstd')
223225
(dst := src.with_suffix('')).unlink(missing_ok=True)

0 commit comments

Comments
 (0)