Skip to content

refactor: improve error handling, encoding safety, and async perf#23

Closed
AndrewDongminYoo wants to merge 2 commits into
rahul-jha98:mainfrom
AndrewDongminYoo:main
Closed

refactor: improve error handling, encoding safety, and async perf#23
AndrewDongminYoo wants to merge 2 commits into
rahul-jha98:mainfrom
AndrewDongminYoo:main

Conversation

@AndrewDongminYoo

Copy link
Copy Markdown

Changes

  • generate_images.py: encoding safety, GITHUB_TOKEN fallback now enabled, ValueError instead of Exception, bool() env var check, PEP 8 formatting
  • github_stats.py: specific exception handling, timeout=30 on sync requests, exponential backoff in retry loop, asyncio.gather parallelism, occurrence-weighted language proportions, _ignored_repos init fix

Test plan

  • Run the GitHub Action workflow and verify SVG outputs are generated correctly
  • Confirm GITHUB_TOKEN fallback works when ACCESS_TOKEN is unset
  • Check that language proportions update correctly in languages.svg
  • Verify no regressions in overview.svg content

- Add explicit encoding='utf-8' to all file opens
- Replace bare except with specific exception types and error details
- Enable GITHUB_TOKEN fallback (was inadvertently commented out)
- Add timeout=30 to all synchronous requests calls
- Use exponential backoff in REST retry loop (2s -> up to 60s)
- Parallelize lines_changed and views fetches with asyncio.gather
- Weight language proportions by occurrence count, not raw byte size
- Initialize _ignored_repos in __init__ to avoid AttributeError
- Fix assert style: assert x instead of assert(x)
- General PEP 8 formatting improvements
The GitHub API for contributor statistics (`/repos/{repo}/stats/contributors`) often returns `202` (Accepted but not ready), `403` (Forbidden), or `429` (Too Many Requests) for large repositories or under heavy load. This frequently results in incomplete or missing "lines changed" statistics.

This change implements a robust fallback mechanism. If the API returns one of these statuses after multiple retries, the system now performs a local `git clone` of the repository and uses `git log --numstat` to accurately calculate the user's contributions.

To identify user commits, the system retrieves the user's associated email addresses via the `/user/emails` API, falling back to the noreply address if necessary. Cloning is optimized using `--bare`, `--filter=blob:limit=1m`, and `--single-branch` to minimize data transfer. Concurrency for cloning operations is also limited.

Furthermore, the core `query_rest_response` method is enhanced with more flexible retry statuses, randomized backoff delays, and improved error handling for both `aiohttp` and `requests` calls. The `username` parameter is now optional, as the user's login is reliably fetched from the API.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant