diff --git a/README.md b/README.md index e141779..f8be7c7 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,28 @@ Overall score (0 to 100), letter grade, and your top role tags. ![grade](https://gh-stats.com/api/YOUR_USERNAME/grade.svg) ``` +#### How the score works + +The score is a weighted sum of seven public signals. Each signal earns +0 to 100 points, reaching 100 at the cap shown, and the weights add up to +100%. + +| Signal | Weight | Cap | +| --- | --- | --- | +| Contributions, all time (commits, PRs, issues, reviews) | 33% | 3,000 | +| Active weeks in the last year | 22% | 40 | +| Repositories (owned and contributed to, at most 50 counted) | 15% | 40 | +| Stars across those repositories | 11% | 200 | +| Forks across those repositories | 8% | 20 | +| Recent public events | 8% | 80 | +| Followers | 3% | 100 | + +Letters follow the total: S++ from 97, S+ 93, S 89, A++ 86, A+ 82, A 78, +A- 72, B++ 68, B+ 64, B 58, B- 50, C+ 42, and C below that. The score +measures the volume and regularity of public activity, not code quality. +Private work is invisible to it. To leave it off your card, turn off the +Grade widget in the editor. + ### Impact Your contribution volume over the last 6 months, drawn as a smooth area diff --git a/fetcher/src/api.py b/fetcher/src/api.py index 8987fe5..d832884 100644 --- a/fetcher/src/api.py +++ b/fetcher/src/api.py @@ -87,6 +87,10 @@ def get_data(username: str): t0 = time.monotonic() try: data = github.fetch_github_data(username, token=config.GITHUB_PAT) + except github.GitHubTransientError as e: + analytics.record_fetch(username, 503, int((time.monotonic() - t0) * 1000)) + log.warning("github fetch rate-limited for %s: %s", username, e) + return jsonify({"error": "github_rate_limited"}), 503 except Exception: analytics.record_fetch(username, 502, int((time.monotonic() - t0) * 1000)) log.exception("github fetch failed for %s", username) diff --git a/fetcher/tests/test_api.py b/fetcher/tests/test_api.py index 7c6ca5a..bec8a3e 100644 --- a/fetcher/tests/test_api.py +++ b/fetcher/tests/test_api.py @@ -203,3 +203,15 @@ def test_rate_limit_body_is_not_classified_as_valid_user_data(): assert apimod._is_github_not_found({"user": {"login": "alice"}}) is False assert apimod._is_github_not_found({"user": {"message": "Not Found"}}) is True assert apimod._is_github_not_found({"user": None}) is True + + +def test_data_miss_rate_limited_returns_503(client, monkeypatch): + # A spent GitHub quota is transient: the fetcher says so (503), so the + # generator can tell the user the card will build after the reset. + def fake_fetch(*_a, **_kw): + raise apimod.github.GitHubTransientError("REST rate limit exhausted") + monkeypatch.setattr(apimod.github, "fetch_github_data", fake_fetch) + r = client.get("/data/alice", headers={"X-Internal-Token": "secret"}) + assert r.status_code == 503 + assert r.get_json() == {"error": "github_rate_limited"} + assert dbmod.get_user("alice") is None diff --git a/generator/frontend/src/App.tsx b/generator/frontend/src/App.tsx index 0e3e8d2..a90d37b 100644 --- a/generator/frontend/src/App.tsx +++ b/generator/frontend/src/App.tsx @@ -350,6 +350,13 @@ export default function App() { setGenerateError(body.status === 'not_found' ? 'User not found' : 'Not enrolled') return } + if (r.status === 503) { + const body = await r.json().catch(() => ({} as { error?: string })) + if (body.error === 'github_rate_limited') { + setGenerateError("GitHub's API limit is reached for now. Your card builds automatically once it resets (within the hour), and you can paste the embed now.") + return + } + } if (!r.ok) { setGenerateError(`Generate failed (HTTP ${r.status})`) return diff --git a/generator/frontend/src/screens/WorkshopScreen.tsx b/generator/frontend/src/screens/WorkshopScreen.tsx index 3d99b4d..5171d86 100644 --- a/generator/frontend/src/screens/WorkshopScreen.tsx +++ b/generator/frontend/src/screens/WorkshopScreen.tsx @@ -540,6 +540,16 @@ export function WorkshopScreen({ /> )} + {w.id === 'grade' && ( + + How the score works + + )} {w.id === 'impact' && (
diff --git a/generator/src/api.py b/generator/src/api.py index fa1ed81..fc237c6 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -1135,7 +1135,14 @@ def generate(username: str): widgets = worker.render_widgets_now(username) except LookupError: return jsonify({"error": "not_enrolled"}), 404 - except Exception: + except Exception as e: + # The fetcher answers 503 when GitHub's quota is spent; the card + # still builds on its own once it resets, so say that, not 502. + if getattr(getattr(e, "response", None), "status_code", None) == 503: + resp = jsonify({"error": "github_rate_limited"}) + resp.status_code = 503 + resp.headers["Retry-After"] = "600" + return resp log.exception("generate failed for %s", username) return jsonify({"error": "render_failed"}), 502 finally: diff --git a/generator/tests/test_api.py b/generator/tests/test_api.py index 935a453..02799de 100644 --- a/generator/tests/test_api.py +++ b/generator/tests/test_api.py @@ -316,6 +316,21 @@ def test_generate_endpoint_renders_and_stores_svg(client): assert b"r" in r2.data +def test_generate_rate_limited_returns_503(client): + """A spent GitHub quota surfaces as 503 github_rate_limited, not 502.""" + import requests + dbmod.enroll("alice", {"theme": "dark"}) + with client.session_transaction() as s: + s["gh_login"] = "alice" + resp = requests.Response() + resp.status_code = 503 + err = requests.HTTPError("503 Server Error", response=resp) + with patch("src.worker.render_widgets_now", side_effect=err): + r = client.post("/api/alice/generate", headers={"Origin": "https://gh-stats.com"}) + assert r.status_code == 503 + assert r.get_json() == {"error": "github_rate_limited"} + + def test_generate_endpoint_rejects_unenrolled(client): """With OAuth, unenrolled means having a session but no profile.""" with client.session_transaction() as s: