From aba98113577d5f7eae0b788f733fafacb1ab79db Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Mon, 16 Nov 2026 15:00:05 -0500 Subject: [PATCH] fix(ci): wait out the API rate limit in build validation (#2672) * fix(ci): wait out the API rate limit in build validation * fix(ci): skip rate-limit sleep retries on status POSTs * fix(ci): detect status POSTs via --method without changing api() call signature --- .github/workflows/authorship.yml | 2 +- tools/release/validate_apk.py | 24 ++++++++++++++++++-- tools/tests/test_validate_apk.py | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/.github/workflows/authorship.yml b/.github/workflows/authorship.yml index ebbcae644f..615267b6c0 100644 --- a/.github/workflows/authorship.yml +++ b/.github/workflows/authorship.yml @@ -25,7 +25,7 @@ jobs: validate: name: Validate source and build result runs-on: ubuntu-24.04 - timeout-minutes: 5 + timeout-minutes: 70 steps: - uses: actions/checkout@v4 with: diff --git a/tools/release/validate_apk.py b/tools/release/validate_apk.py index 1e7e31f960..7a3fb67530 100644 --- a/tools/release/validate_apk.py +++ b/tools/release/validate_apk.py @@ -2,13 +2,33 @@ import json import os import subprocess +import sys +import time from urllib.parse import urlencode from check_authorship import check_revision, event_violations -def api(path, *args): - return json.loads(subprocess.check_output(["gh", "api", path, *args], text=True)) +RATE_LIMIT_WAIT = 1800 + + +def rate_limit_wait(): + try: + reset = json.loads(subprocess.check_output(["gh", "api", "rate_limit"], text=True))["resources"]["core"]["reset"] + except (subprocess.CalledProcessError, KeyError, ValueError): + return 60 + return max(5, min(RATE_LIMIT_WAIT, reset - time.time() + 5)) + + +def api(path, *args, attempts=3): + for attempt in range(attempts): + done = subprocess.run(["gh", "api", path, *args], text=True, capture_output=True) + if done.returncode == 0: + return json.loads(done.stdout) + if "--method" in args or "rate limit" not in done.stderr.lower() or attempt == attempts - 1: + sys.stderr.write(done.stderr) + raise subprocess.CalledProcessError(done.returncode, done.args, done.stdout, done.stderr) + time.sleep(rate_limit_wait()) def compilation_state(run, jobs): diff --git a/tools/tests/test_validate_apk.py b/tools/tests/test_validate_apk.py index 7bacefe74d..707779a9f6 100644 --- a/tools/tests/test_validate_apk.py +++ b/tools/tests/test_validate_apk.py @@ -130,3 +130,41 @@ class ValidateApkTest(unittest.TestCase): self.git("commit", "-qm", "Rename source") with contextlib.chdir(self.root): self.assertFalse(validate_apk.documentation_only(self.git("rev-parse", "HEAD"))) + + +class RateLimitTest(unittest.TestCase): + def call(self, results): + replies = list(results) + runs = [] + + def run(args, **kwargs): + runs.append(args) + code, out, err = replies.pop(0) + return subprocess.CompletedProcess(args, code, out, err) + + with patch.object(validate_apk.subprocess, "run", side_effect=run), \ + patch.object(validate_apk, "rate_limit_wait", return_value=7), \ + patch.object(validate_apk.time, "sleep") as sleep, \ + contextlib.redirect_stderr(open(os.devnull, "w")): + try: + return validate_apk.api("repos/x/pulls/1"), runs, sleep + except subprocess.CalledProcessError as error: + return error, runs, sleep + + def test_waits_for_the_rate_limit_then_retries(self): + result, runs, sleep = self.call([(1, "", "gh: API rate limit exceeded for installation."), (0, '{"number": 1}', "")]) + self.assertEqual(result, {"number": 1}) + self.assertEqual(len(runs), 2) + sleep.assert_called_once_with(7) + + def test_other_errors_fail_without_waiting(self): + result, runs, sleep = self.call([(1, "", "gh: Not Found (HTTP 404)")]) + self.assertIsInstance(result, subprocess.CalledProcessError) + self.assertEqual(len(runs), 1) + sleep.assert_not_called() + + def test_gives_up_after_the_last_attempt(self): + result, runs, sleep = self.call([(1, "", "API rate limit exceeded")] * 3) + self.assertIsInstance(result, subprocess.CalledProcessError) + self.assertEqual(len(runs), 3) + self.assertEqual(sleep.call_count, 2)