krz/hutch-stats

Server-side utility for calculating contributions for sourcehut users. contributions server sourcehut stats

Commit 1878dff520

1878dff520ee2e424b088a963c8f6417f5106ce9

parent: acbff854f2

Verified · cmc

cmc <hello@cleberg.net> · 2026-04-11 17:34 UTC

feat: harden contribution api for production use

Layout: unified · split

README.md +7 −9
@@ -9,7 +9,7 @@ The current V1 is intentionally narrow and production-oriented:
99- polling-based ingestion
1010- complete `todo.sr.ht` ingestion path
1111- practical `git.sr.ht` commit ingestion for tracked repositories
12- API-key protection for `/api/*`
12- public read-only contribution endpoints plus API-key protection for mutating/admin routes
1313- Alembic-managed schema migrations
1414
1515## What It Does
@@ -75,7 +75,7 @@ Tracked git repositories are persisted in the `tracked_repositories` table and s
7575
7676Environment variables:
7777
78- `API_KEY`: required header token for all `/api/*` routes via `X-API-Key`
78- `API_KEY`: required header token for mutating/admin routes via `X-API-Key`
7979- `ENABLE_SCHEDULER`: defaults to `false`; enables in-process polling when set to `true`
8080- `SRHT_TOKEN`: bearer token for SourceHut GraphQL
8181- `TODO_SRHT_ENDPOINT`: defaults to `https://todo.sr.ht/query`
@@ -189,15 +189,13 @@ Response:
189189### Contribution Calendar by Year
190190
191191```bash
192curl "http://127.0.0.1:8000/api/contributions/~ccleberg?year=2026" \
193 -H "X-API-Key: replace-me"
192curl "http://127.0.0.1:8000/api/contributions/~ccleberg?year=2026"
194193```
195194
196195### Contribution Calendar by Date Range
197196
198197```bash
199curl "http://127.0.0.1:8000/api/contributions/~ccleberg?from=2026-01-01&to=2026-03-30" \
200 -H "X-API-Key: replace-me"
198curl "http://127.0.0.1:8000/api/contributions/~ccleberg?from=2026-01-01&to=2026-03-30"
201199```
202200
203201Example response:
@@ -218,8 +216,7 @@ Example response:
218216### Contribution Stats
219217
220218```bash
221curl "http://127.0.0.1:8000/api/contributions/~ccleberg/stats?year=2026" \
222 -H "X-API-Key: replace-me"
219curl "http://127.0.0.1:8000/api/contributions/~ccleberg/stats?year=2026"
223220```
224221
225222Example response:
@@ -292,7 +289,8 @@ pytest
292289Covered areas:
293290
294291- health endpoint
295- API key enforcement
292- public read-only contribution endpoints
293- API key enforcement for mutating/admin routes
296294- calendar aggregation
297295- zero-filled ranges
298296- stats calculations
src/srht_contrib/api/routes_contributions.py +2 −2
@@ -13,7 +13,7 @@ from srht_contrib.services.srht_client import SourceHutClientError
1313from srht_contrib.utils.dates import parse_date, year_bounds
1414from srht_contrib.utils.identity import ActorIdentityResolver
1515
16router = APIRouter(prefix="/api/contributions", tags=["contributions"], dependencies=[Depends(require_api_key)])
16router = APIRouter(prefix="/api/contributions", tags=["contributions"])
1717
1818
1919def _resolve_range(year: int | None, from_date: str | None, to_date: str | None) -> tuple[date, date]:
@@ -63,7 +63,7 @@ def get_contribution_stats(
6363 return ContributionAggregator().build_stats(db, canonical_actor, start, end)
6464
6565
66@router.post("/poll", response_model=PollResponse)
66@router.post("/poll", response_model=PollResponse, dependencies=[Depends(require_api_key)])
6767def trigger_manual_poll(
6868 actor: str,
6969 poller: PollerService = Depends(get_poller),
tests/test_contributions_api.py +6 −2
@@ -6,14 +6,18 @@ from srht_contrib.main import create_app
66from srht_contrib.models import ContributionEvent
77
88
9def test_api_routes_require_api_key(settings, db_engine, session_factory) -> None:
9def test_read_only_contribution_routes_are_public_and_write_routes_require_api_key(settings, db_engine, session_factory) -> None:
1010 app = create_app(settings, engine=db_engine, session_factory=session_factory)
1111 with TestClient(app) as open_client:
1212 response = open_client.get("/health")
13 public_contributions = open_client.get("/api/contributions/~ccleberg?from=2026-03-28&to=2026-03-30")
14 public_stats = open_client.get("/api/contributions/~ccleberg/stats?from=2026-03-28&to=2026-03-30")
1315 assert response.status_code == 200
16 assert public_contributions.status_code == 200
17 assert public_stats.status_code == 200
1418
1519 with TestClient(app) as unauthorized:
16 unauthorized_response = unauthorized.get("/api/contributions/~ccleberg?from=2026-03-28&to=2026-03-30")
20 unauthorized_response = unauthorized.post("/api/contributions/poll?actor=~ccleberg")
1721 assert unauthorized_response.status_code == 401
1822
1923 with TestClient(app) as invalid: