From 2b4ef71458eea705e23ba8710189182ba8d9257e Mon Sep 17 00:00:00 2001 From: Mark Eaton Date: Sun, 25 Jan 2026 00:27:23 -0500 Subject: [PATCH] fix ValueError when API returns error response for repo languages Handle cases where the Gitea API returns an error message (e.g., "token scope is limited to public repos") instead of language data. Now skips repos with non-OK responses or error messages gracefully. Co-Authored-By: Claude Opus 4.5 --- scripts/language_stats.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/scripts/language_stats.py b/scripts/language_stats.py index 203a8b7..133a44d 100644 --- a/scripts/language_stats.py +++ b/scripts/language_stats.py @@ -142,12 +142,26 @@ def aggregate_language_bytes(session: requests.Session, repos: list[dict]) -> di totals: dict[str, int] = defaultdict(int) for repo in repos: - langs = session.get( + resp = session.get( f"{BASE_URL}/api/v1/repos/{USERNAME}/{repo['name']}/languages", timeout=30, - ).json() + ) + if not resp.ok: + # Skip repos we can't access (e.g., token scope limitations) + print(f"Warning: Could not fetch languages for {repo['name']}: {resp.status_code}", file=sys.stderr) + continue + langs = resp.json() + # Skip error responses (e.g., {"message": "..."}) + if not isinstance(langs, dict) or "message" in langs: + print(f"Warning: Unexpected response for {repo['name']}: {langs}", file=sys.stderr) + continue for lang, bytes_ in langs.items(): - totals[lang] += int(bytes_) + # Handle bytes as either int or string + if isinstance(bytes_, int): + totals[lang] += bytes_ + elif isinstance(bytes_, str) and bytes_.isdigit(): + totals[lang] += int(bytes_) + # Skip non-numeric values return dict(totals)