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 <noreply@anthropic.com>
This commit is contained in:
Mark Eaton
2026-01-25 00:27:23 -05:00
co-authored by Claude Opus 4.5
parent d8f72b7837
commit 2b4ef71458
+17 -3
View File
@@ -142,12 +142,26 @@ def aggregate_language_bytes(session: requests.Session, repos: list[dict]) -> di
totals: dict[str, int] = defaultdict(int) totals: dict[str, int] = defaultdict(int)
for repo in repos: for repo in repos:
langs = session.get( resp = session.get(
f"{BASE_URL}/api/v1/repos/{USERNAME}/{repo['name']}/languages", f"{BASE_URL}/api/v1/repos/{USERNAME}/{repo['name']}/languages",
timeout=30, 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(): 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) return dict(totals)