fix(fetcher): guard REST calls so a rate limit can't poison user records

The five REST calls parsed `.json()` with no status check, so a 403
{"message": "API rate limit exceeded..."} body was returned as user data.
`_is_github_not_found` only matched "Not Found", so it was recorded as a
successful fetch and upsert_user overwrote the user's last-good row. Every
user touched during the exhaustion window then served a blank card for up
to 24h while /metrics reported ok.

Reuse the existing `_raise_if_transient` on all five so exhaustion raises
GitHubTransientError before the upsert, preserving last-good and letting
the caller retry. `_raise_if_transient` was GraphQL-shaped and crashed on
the JSON arrays the list endpoints return, so it now ignores non-dict
bodies. A rate limit inside the per-repo fan-out aborts the fetch; a 404
on a single repo stays non-fatal.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BwMoSbmK77Ph7XaV5YnAdS
This commit is contained in:
shay
2026-08-22 16:10:26 -07:00
co-authored by Claude Opus 5
parent eea1734a04
commit 06ab1c878e
4 changed files with 181 additions and 6 deletions
+136
View File
@@ -114,3 +114,139 @@ def test_commit_count_zero_on_not_found_is_not_transient():
)
src = github.DirectAPISource(token="t")
assert src.fetch_commit_count("ghost", []) == 0
# --- REST rate-limit guards -------------------------------------------------
# The REST calls used to hand `.json()` straight back, so a 403 rate-limit body
# ({"message": "API rate limit exceeded for user ID ..."}) was persisted as if
# it were real data, wiping the user's last-good record for up to 24h.
RATE_LIMIT_BODY = {
"message": "API rate limit exceeded for user ID 123.",
"documentation_url": "https://docs.github.com/rest/overview/rate-limits",
}
RATE_LIMIT_HEADERS = {"X-RateLimit-Remaining": "0"}
@responses.activate
def test_fetch_user_data_raises_on_rest_rate_limit():
responses.add(
responses.GET, "https://api.github.com/users/alice",
json=RATE_LIMIT_BODY, status=403, headers=RATE_LIMIT_HEADERS,
)
src = github.DirectAPISource(token="t")
with pytest.raises(github.GitHubTransientError):
src.fetch_user_data("alice")
@responses.activate
def test_fetch_user_data_raises_on_rate_limit_without_header():
# Secondary rate limits come back as 403 without the remaining-budget
# header — the message is the only signal.
responses.add(
responses.GET, "https://api.github.com/users/alice",
json={"message": "You have exceeded a secondary rate limit."}, status=403,
)
src = github.DirectAPISource(token="t")
with pytest.raises(github.GitHubTransientError):
src.fetch_user_data("alice")
@responses.activate
def test_fetch_repos_raises_on_rest_rate_limit():
responses.add(
responses.GET, "https://api.github.com/users/alice/repos",
json=RATE_LIMIT_BODY, status=403, headers=RATE_LIMIT_HEADERS,
)
src = github.DirectAPISource(token="t")
with pytest.raises(github.GitHubTransientError):
src.fetch_repos("alice")
@responses.activate
def test_fetch_events_raises_on_rest_rate_limit():
responses.add(
responses.GET, "https://api.github.com/users/alice/events",
json=RATE_LIMIT_BODY, status=403, headers=RATE_LIMIT_HEADERS,
)
src = github.DirectAPISource(token="t")
with pytest.raises(github.GitHubTransientError):
src.fetch_events("alice")
@responses.activate
def test_fetch_repo_contributors_raises_on_rest_rate_limit():
responses.add(
responses.GET, "https://api.github.com/repos/alice/proj/contributors",
json=RATE_LIMIT_BODY, status=403, headers=RATE_LIMIT_HEADERS,
)
src = github.DirectAPISource(token="t")
with pytest.raises(github.GitHubTransientError):
src.fetch_repo_contributors("alice/proj")
@responses.activate
def test_fetch_repo_languages_raises_on_rest_rate_limit():
responses.add(
responses.GET, "https://api.github.com/repos/alice/proj/languages",
json=RATE_LIMIT_BODY, status=403, headers=RATE_LIMIT_HEADERS,
)
src = github.DirectAPISource(token="t")
with pytest.raises(github.GitHubTransientError):
src.fetch_repo_languages("alice/proj")
@responses.activate
def test_single_repo_404_stays_non_fatal():
# A deleted/renamed repo is a real per-repo answer, not a budget problem:
# it must keep degrading gracefully instead of aborting the whole fetch.
responses.add(
responses.GET, "https://api.github.com/repos/alice/gone/languages",
json={"message": "Not Found"}, status=404,
)
responses.add(
responses.GET, "https://api.github.com/repos/alice/gone/contributors",
json={"message": "Not Found"}, status=404,
)
src = github.DirectAPISource(token="t")
assert src.fetch_repo_languages("alice/gone") == {}
assert src.fetch_repo_contributors("alice/gone") == []
@responses.activate
def test_rest_success_path_unchanged():
responses.add(
responses.GET, "https://api.github.com/users/alice",
json={"login": "alice", "followers": 5}, status=200,
)
responses.add(
responses.GET, "https://api.github.com/users/alice/events",
json=[{"type": "PushEvent"}], status=200,
)
responses.add(
responses.GET, "https://api.github.com/repos/alice/proj/languages",
json={"Python": 100}, status=200,
)
responses.add(
responses.GET, "https://api.github.com/repos/alice/proj/contributors",
json=[{"login": "bob", "contributions": 4}], status=200,
)
src = github.DirectAPISource(token="t")
assert src.fetch_user_data("alice")["login"] == "alice"
assert src.fetch_events("alice") == [{"type": "PushEvent"}]
assert src.fetch_repo_languages("alice/proj") == {"Python": 100}
assert src.fetch_repo_contributors("alice/proj") == [{"login": "bob", "contributions": 4}]
@responses.activate
def test_rate_limited_repo_languages_aborts_whole_fetch():
# A rate limit inside the per-repo fan-out means the budget is gone, so it
# must propagate rather than being swallowed into an empty breakdown.
responses.add(
responses.GET, "https://api.github.com/repos/alice/proj/languages",
json=RATE_LIMIT_BODY, status=403, headers=RATE_LIMIT_HEADERS,
)
src = github.DirectAPISource(token="t")
repos = [{"full_name": "alice/proj"}]
with pytest.raises(github.GitHubTransientError):
github._enrich_repo_languages(repos, src)