refactor: improve error handling, encoding safety, and async perf#23
Closed
AndrewDongminYoo wants to merge 2 commits into
Closed
refactor: improve error handling, encoding safety, and async perf#23AndrewDongminYoo wants to merge 2 commits into
AndrewDongminYoo wants to merge 2 commits into
Conversation
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
generate_images.py: encoding safety, GITHUB_TOKEN fallback now enabled,ValueErrorinstead ofException,bool()env var check, PEP 8 formattinggithub_stats.py: specific exception handling,timeout=30on sync requests, exponential backoff in retry loop,asyncio.gatherparallelism, occurrence-weighted language proportions,_ignored_reposinit fixTest plan
languages.svgoverview.svgcontent