Compare commits
31
Commits
cd326a34ba
...
0.2.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb8f25d5ff | ||
|
|
34e558d31c | ||
|
|
c4dffb1dc6 | ||
|
|
4825c45a7c | ||
|
|
ac515400e0 | ||
|
|
be6146d5ad | ||
|
|
20e943a2b5 | ||
|
|
cf7bd8d70c | ||
|
|
de831beaaa | ||
|
|
9c028aaf5e | ||
|
|
8e349c961b | ||
|
|
14ea788522 | ||
|
|
4ff45b7c46 | ||
|
|
41e88144a4 | ||
|
|
91bda21f89 | ||
|
|
0d7cf68771 | ||
|
|
e7f14dfd35 | ||
|
|
095f3e0253 | ||
|
|
e2a324b74a | ||
|
|
a76ed6efc8 | ||
|
|
bce564d68b | ||
|
|
b3dc7a2298 | ||
|
|
9087f91855 | ||
|
|
885dec357e | ||
|
|
78e7a72ee9 | ||
|
|
c929fdfda6 | ||
|
|
3d2c0487b7 | ||
|
|
cb89a1c6b9 | ||
|
|
5e27a5bc93 | ||
|
|
f52a3449c6 | ||
|
|
ca94b80beb |
@@ -19,3 +19,7 @@ GITLAB_URL=
|
||||
GITEA_USER=
|
||||
GITEA_TOKEN=
|
||||
GITEA_URL=
|
||||
|
||||
# Number of days of contribution history to keep in var/data/contributions.db.
|
||||
# 0 (or unset) keeps history forever.
|
||||
CONTRIBUTIONS_RETENTION_DAYS=0
|
||||
|
||||
+39
-1
@@ -7,6 +7,43 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.2.0] - 2026-07-12
|
||||
|
||||
### Added
|
||||
|
||||
- **Contribution history store (SQLite)** — `ContributionStore` persists each provider's daily counts in `var/data/contributions.db`; `ContributionAggregator` now fetches only the window since each provider's last stored date (plus a 3-day trailing overlap for late corrections), merges the result into the store, and prunes rows older than `CONTRIBUTIONS_RETENTION_DAYS`
|
||||
- **`graph:contributions:refetch` console command** — forces a full or explicit-range re-fetch of contribution history, chunked into ≤365-day windows, for one, several, or all configured providers
|
||||
- **Concurrent, non-blocking provider fetches** — `ProviderInterface::fetch()` split into `startFetch()`/`resolveFetch()` so GitHub, GitLab, and Gitea requests are in flight simultaneously instead of one after another
|
||||
- **Bounded fetch windows** — `startFetch()`/`resolveFetch()` accept optional `$since`/`$until` bounds, used by both the incremental aggregator and the refetch command
|
||||
- **PHPStan static analysis** — level 8 over `src/` and `tests/`, run via `composer phpstan`
|
||||
- `Contribution` and `ContributionCollection` typed entities
|
||||
- `CONTRIBUTIONS_RETENTION_DAYS` environment variable
|
||||
|
||||
### Changed
|
||||
|
||||
- **Namespace renamed from `App` to `GitContributionGraph`** across the codebase, Composer autoloading, and routing config
|
||||
- Removed the Guzzle and GraphQL client bundles — GitHub requests now go through Symfony's `HttpClientInterface` directly, the same as GitLab and Gitea
|
||||
- HTTP client requests are capped (10s connect/first-byte timeout, 15s max duration) so a slow provider can no longer stall the render path
|
||||
- `docker-compose.yml` and `docker-compose.prod.yml` mount a `data` volume for the SQLite store and pass through `CONTRIBUTIONS_RETENTION_DAYS`
|
||||
- Dev Docker Compose override targets the `dev` build stage explicitly and mounts `vendor` read-write instead of as an anonymous volume
|
||||
|
||||
### Fixed
|
||||
|
||||
- `ContributionCollection`'s variadic constructor now re-indexes its arguments with `array_values()`, avoiding non-sequential keys that didn't match its declared property type
|
||||
- `SvgRenderer` coerces `strtotime()` results to `int` before formatting dates
|
||||
- `docker-compose.prod.yml` was missing the `data` volume and `CONTRIBUTIONS_RETENTION_DAYS` env var present in `docker-compose.yml`, so contribution history would not survive a container recreate on that deployment path
|
||||
- `composer phpstan` no longer crashes with an out-of-memory error under the default 128M limit
|
||||
|
||||
### Documentation
|
||||
|
||||
- README and `CLAUDE.md` updated to describe the SQLite store, incremental fetch, refetch command, and PHPStan setup; corrected several stale examples (test path, `/health` sample response, Docker volume mounts)
|
||||
- Added PHPDoc to the remaining public methods across `src/`
|
||||
|
||||
### Tests
|
||||
|
||||
- Added tests for `ContributionStore`, `Contribution`, `ContributionCollection`, and `RefetchContributionsCommand`
|
||||
- Extended provider and aggregator tests to cover `$since`/`$until` windowing, concurrent-fetch failure modes, and store interactions
|
||||
|
||||
## [0.1.0] - 2026-05-30
|
||||
|
||||
### Added
|
||||
@@ -42,6 +79,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Docker multi-stage build (`base → deps → build → final`)
|
||||
- Basic `/health` endpoint
|
||||
|
||||
[Unreleased]: https://github.com/ArthurErlich/git-contribution-graph/compare/0.1.0...HEAD
|
||||
[Unreleased]: https://github.com/ArthurErlich/git-contribution-graph/compare/0.2.0...HEAD
|
||||
[0.2.0]: https://github.com/ArthurErlich/git-contribution-graph/compare/0.1.0...0.2.0
|
||||
[0.1.0]: https://github.com/ArthurErlich/git-contribution-graph/compare/0.0.1...0.1.0
|
||||
[0.0.1]: https://github.com/ArthurErlich/git-contribution-graph/releases/tag/0.0.1
|
||||
|
||||
@@ -99,6 +99,9 @@ vendor/bin/phpunit tests/Unit/Service/SvgRendererTest.php
|
||||
|
||||
# Run tests matching a filter
|
||||
vendor/bin/phpunit --filter it_renders
|
||||
|
||||
# Static analysis (PHPStan level 8)
|
||||
composer phpstan
|
||||
```
|
||||
|
||||
On Windows, run via WSL (If Docker Desctop is not):
|
||||
@@ -190,20 +193,24 @@ There is no `composer.lock` in the repo. If you add or change dependencies, run
|
||||
|
||||
## Architecture
|
||||
|
||||
Single-controller Symfony app with no database. The request flow:
|
||||
Single-controller Symfony app. Two-tier cache: a 1h filesystem SVG cache in front of a SQLite raw-data store, in front of the provider APIs. The request flow:
|
||||
|
||||
```
|
||||
GET /graph.svg?theme=dark|light
|
||||
└─ GraphController
|
||||
├─ host check (ALLOWED_HOSTS env, optional)
|
||||
├─ cache lookup (filesystem, 1h TTL, key = "graph_{theme}")
|
||||
│ └─ on miss:
|
||||
│ ├─ GitHubProvider → GitHub GraphQL API (contributionCalendar query)
|
||||
│ ├─ GitLabProvider → GitLab REST /users/:id/events (paginated, 100/page)
|
||||
│ └─ GiteaProvider → Gitea REST /api/v1/users/:user/heatmap
|
||||
│ each returns array<string, int> (Y-m-d => count)
|
||||
│ failures are caught and logged; remaining providers still render
|
||||
│ └─ merge by date (sum counts across providers)
|
||||
│ └─ on miss: ContributionAggregator::aggregate()
|
||||
│ ├─ per configured provider: ContributionStore::latestDate($name)
|
||||
│ │ → $since = latest - 3 days (trailing overlap), or null on first run
|
||||
│ ├─ GitHubProvider → GitHub GraphQL API (contributionCalendar query, bounded by $since/$until)
|
||||
│ ├─ GitLabProvider → GitLab REST /users/:id/events (paginated, 100/page, `after`/`before` bounded)
|
||||
│ ├─ GiteaProvider → Gitea REST /api/v1/users/:user/heatmap (filtered client-side by $since/$until)
|
||||
│ │ each returns array<string, int> (Y-m-d => count)
|
||||
│ │ failures are caught and logged; remaining providers still render
|
||||
│ ├─ ContributionStore::merge() persists fresh data per provider (SQLite upsert)
|
||||
│ ├─ ContributionStore::all(sinceDays: 371) reads back the render window (source of truth)
|
||||
│ ├─ ContributionStore::prune() drops rows older than CONTRIBUTIONS_RETENTION_DAYS
|
||||
│ └─ SvgRenderer::render()
|
||||
└─ Response: image/svg+xml, Cache-Control: public max-age=3600
|
||||
```
|
||||
@@ -212,7 +219,11 @@ GET /graph.svg?theme=dark|light
|
||||
|
||||
**SvgRenderer:** builds a 53-column × 7-row grid aligned so the last column always ends on the Saturday of the current week. Five intensity levels (0 contributions → level 0, 1–3 → 1, 4–6 → 2, 7–9 → 3, 10+ → 4) mapped to GitHub's exact colour tokens. No external assets — the SVG is fully self-contained.
|
||||
|
||||
**Cache:** filesystem adapter (`var/cache/`), mounted as a Docker volume to survive container restarts. Theme is part of the cache key so dark and light are cached independently.
|
||||
**SVG cache:** filesystem adapter (`var/cache/`), mounted as a Docker volume to survive container restarts. Theme is part of the cache key so dark and light are cached independently.
|
||||
|
||||
**ContributionStore:** PDO SQLite at `var/data/contributions.db` (also a Docker volume), table `contributions (provider, date unixtime, count)`. Keyed by `(provider, date)`, upserted via `INSERT ... ON CONFLICT`. Bounds every re-fetch to a 3-day trailing window off the last stored date per provider — the root fix for the full-365-day-refetch `MaxExecutionTimeError` that used to hit on every cache miss. `CONTRIBUTIONS_RETENTION_DAYS` controls how far back rows are kept (empty = forever).
|
||||
|
||||
**`graph:contributions:refetch` command:** manual escape hatch (`src/Command/RefetchContributionsCommand.php`) that bypasses the incremental trailing-window fetch and re-pulls a full or explicit date range. Options: `--provider=github,gitlab` (comma-separated, default all configured), `--from=YYYY-MM-DD` (default 365 days ago), `--to=YYYY-MM-DD` (default today), `--all` (shorthand for `--from=2005-01-01`). Splits the requested range into ≤365-day chunks (GitHub's GraphQL window limit; also caps GitLab pagination per call) and merges each chunk into the store as it completes, so a large `--all` run can't reintroduce the original runaway-pagination timeout.
|
||||
|
||||
## Environment variables
|
||||
|
||||
@@ -224,5 +235,6 @@ GET /graph.svg?theme=dark|light
|
||||
| `GITLAB_URL` | No | Defaults to `https://gitlab.com` |
|
||||
| `GITEA_USER` / `GITEA_TOKEN` / `GITEA_URL` | For Gitea | Token scope: `read:user` |
|
||||
| `ALLOWED_HOSTS` | No | Comma-separated hostnames; empty = allow all |
|
||||
| `CONTRIBUTIONS_RETENTION_DAYS` | No | Days of history to keep in the SQLite store; empty = keep forever |
|
||||
|
||||
Copy `.env` to `.env.local` for local development — `.env.local` is gitignored.
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ COPY --link docker/frankenphp/Caddyfile /etc/caddy/Caddyfile
|
||||
COPY --link docker/php/conf.d/20-app.prod.ini $PHP_INI_DIR/app.conf.d/
|
||||
|
||||
RUN chmod +x bin/console && \
|
||||
mkdir -p var/cache/prod/pools var/log /config/caddy /data/caddy && \
|
||||
mkdir -p var/cache/prod/pools var/log var/data /config/caddy /data/caddy && \
|
||||
chown -R app:app /app /config /data
|
||||
|
||||
USER app
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
FROM dunglas/frankenphp:1-php8.4-alpine
|
||||
FROM dunglas/frankenphp:1-php8.4-alpine AS dev
|
||||
|
||||
RUN apk add --no-cache icu-dev libzip-dev \
|
||||
&& docker-php-ext-install -j$(nproc) intl opcache zip \
|
||||
|
||||
@@ -40,10 +40,14 @@ services:
|
||||
env_file:
|
||||
- .env.local
|
||||
volumes:
|
||||
- cache:/var/www/html/var/cache
|
||||
- cache:/app/var/cache/prod/pools
|
||||
- logs:/app/var/log
|
||||
- data:/app/var/data
|
||||
|
||||
volumes:
|
||||
cache:
|
||||
logs:
|
||||
data:
|
||||
```
|
||||
|
||||
The image is published to the Gitea container registry. Pull it manually with:
|
||||
@@ -77,6 +81,9 @@ GITEA_URL=https://git.example.com
|
||||
|
||||
# Optional: restrict to specific hostnames (comma-separated), leave empty to allow all
|
||||
ALLOWED_HOSTS=
|
||||
|
||||
# Optional: days of contribution history to keep in the SQLite store, empty/0 = keep forever
|
||||
CONTRIBUTIONS_RETENTION_DAYS=
|
||||
```
|
||||
|
||||
Only configure the platforms you use — unused ones are silently skipped.
|
||||
@@ -87,15 +94,26 @@ Only configure the platforms you use — unused ones are silently skipped.
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
The service listens on **port 8080** by default. Put Traefik or nginx in front of it for HTTPS.
|
||||
The service listens on **port 8080** by default (served by FrankenPHP/Caddy, baked into the image — no separate PHP-FPM/nginx needed). Put Traefik or nginx in front of it only if you need HTTPS termination.
|
||||
|
||||
### 4. Verify
|
||||
|
||||
```bash
|
||||
curl http://localhost:8080/health
|
||||
# {"status":"ok"}
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"status": "ok",
|
||||
"providers": {
|
||||
"github": { "status": "ok" },
|
||||
"gitlab": { "status": "ok" }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Returns HTTP 503 with `"status": "degraded"` if any configured provider's probe fails (its entry then includes `error` and `message`).
|
||||
|
||||
---
|
||||
|
||||
## API
|
||||
@@ -175,7 +193,7 @@ docker compose up -d --build
|
||||
docker compose exec graph sh
|
||||
|
||||
# Run tests inside the container
|
||||
docker compose exec graph php bin/phpunit
|
||||
docker compose exec graph vendor/bin/phpunit
|
||||
|
||||
# Disable Xdebug for faster test runs
|
||||
XDEBUG_MODE=off docker compose up -d
|
||||
@@ -195,34 +213,49 @@ docker compose -f docker-compose.yml up -d --build
|
||||
|
||||
```bash
|
||||
# Run full suite
|
||||
php bin/phpunit
|
||||
vendor/bin/phpunit
|
||||
|
||||
# Human-readable output
|
||||
php bin/phpunit --testdox
|
||||
vendor/bin/phpunit --testdox
|
||||
|
||||
# Single file
|
||||
php bin/phpunit tests/Unit/Service/SvgRendererTest.php
|
||||
vendor/bin/phpunit tests/Unit/Service/Renderer/SvgRendererTest.php
|
||||
|
||||
# Filter by name
|
||||
php bin/phpunit --filter it_renders
|
||||
vendor/bin/phpunit --filter it_renders
|
||||
```
|
||||
|
||||
### Static analysis
|
||||
|
||||
```bash
|
||||
composer phpstan
|
||||
```
|
||||
|
||||
Runs PHPStan at level 8 over `src/` and `tests/` (see `phpstan.neon`). First
|
||||
run's findings are catalogued in [PHP-Stan-Errors.md](PHP-Stan-Errors.md).
|
||||
|
||||
---
|
||||
|
||||
## Architecture
|
||||
|
||||
Two-tier cache: a 1h filesystem SVG cache in front of a SQLite raw-data store, in front of the provider APIs.
|
||||
|
||||
```
|
||||
GET /graph.svg?theme=dark|light
|
||||
└─ GraphController
|
||||
├─ host check (ALLOWED_HOSTS env, optional)
|
||||
├─ cache lookup (filesystem, 1h TTL, key = "graph_{theme}")
|
||||
│ └─ on miss:
|
||||
│ ├─ GitHubProvider → GitHub GraphQL API (contributionCalendar)
|
||||
│ ├─ GitLabProvider → GitLab REST API (/users/:id/events, paginated)
|
||||
│ └─ GiteaProvider → Gitea REST API (/users/:user/heatmap)
|
||||
│ each returns array<string, int> (Y-m-d => count)
|
||||
│ failures are caught and logged; remaining providers still render
|
||||
│ └─ merge by date (sum counts across providers)
|
||||
│ └─ on miss: ContributionAggregator::aggregate()
|
||||
│ ├─ per configured provider: ContributionStore::latestDate($name)
|
||||
│ │ → $since = latest - 3 days (trailing overlap), or null on first run
|
||||
│ ├─ GitHubProvider → GitHub GraphQL API (contributionCalendar query, bounded by $since/$until)
|
||||
│ ├─ GitLabProvider → GitLab REST /users/:id/events (paginated, 100/page, `after`/`before` bounded)
|
||||
│ ├─ GiteaProvider → Gitea REST /api/v1/users/:user/heatmap (filtered client-side by $since/$until)
|
||||
│ │ each returns array<string, int> (Y-m-d => count); providers fetch concurrently
|
||||
│ │ (start/resolve split); failures are caught and logged, remaining providers still render
|
||||
│ ├─ ContributionStore::merge() persists fresh data per provider (SQLite upsert)
|
||||
│ ├─ ContributionStore::all(sinceDays: 371) reads back the render window (source of truth)
|
||||
│ ├─ ContributionStore::prune() drops rows older than CONTRIBUTIONS_RETENTION_DAYS
|
||||
│ └─ SvgRenderer::render()
|
||||
└─ Response: image/svg+xml, Cache-Control: public max-age=3600
|
||||
```
|
||||
@@ -231,7 +264,30 @@ GET /graph.svg?theme=dark|light
|
||||
|
||||
**SvgRenderer:** builds a 53-column × 7-row grid aligned so the last column always ends on the Saturday of the current week. Five intensity levels (0 → level 0, 1–3 → 1, 4–6 → 2, 7–9 → 3, 10+ → 4) mapped to GitHub's colour tokens. No external assets — the SVG is fully self-contained.
|
||||
|
||||
**Cache:** filesystem adapter (`var/cache/`), mounted as a Docker volume to survive container restarts. Theme is part of the cache key so dark and light are cached independently.
|
||||
**SVG cache:** filesystem adapter (`var/cache/`), mounted as a Docker volume to survive container restarts. Theme is part of the cache key so dark and light are cached independently.
|
||||
|
||||
**ContributionStore:** PDO SQLite at `var/data/contributions.db` (also a Docker volume), keyed by `(provider, date)`. Bounds every re-fetch to a trailing window off the last stored date per provider, instead of re-pulling all 365 days on every cache miss. `CONTRIBUTIONS_RETENTION_DAYS` controls how far back rows are kept (empty/0 = forever).
|
||||
|
||||
**Health check:** `GET /health` probes each configured provider's credentials/reachability (without fetching contribution data) and reports `ok`/`degraded`.
|
||||
|
||||
---
|
||||
|
||||
## CLI
|
||||
|
||||
`graph:contributions:refetch` is a manual escape hatch that bypasses the incremental trailing-window fetch and re-pulls a full or explicit date range — useful for backfilling history or recovering from a gap.
|
||||
|
||||
```bash
|
||||
docker compose exec graph bin/console graph:contributions:refetch --all
|
||||
```
|
||||
|
||||
| Option | Default | Description |
|
||||
| ------------ | -------------------- | --------------------------------------------------------- |
|
||||
| `--provider` | all configured | Comma-separated provider names to refetch, e.g. `github,gitlab` |
|
||||
| `--from` | 365 days ago | Start date (`YYYY-MM-DD`) |
|
||||
| `--to` | today | End date (`YYYY-MM-DD`) |
|
||||
| `--all` | off | Shorthand for `--from=2005-01-01` |
|
||||
|
||||
Large ranges are split into ≤365-day chunks and merged into the store as each completes.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,133 +0,0 @@
|
||||
# TODO: persist contribution history in SQLite + audit cleanup
|
||||
|
||||
Backend-only work. Follow Red → Green → Refactor per `CLAUDE.md` for every
|
||||
step that adds behavior (store, retention, provider `$since` handling).
|
||||
Order matters — later steps assume earlier ones are done.
|
||||
|
||||
## 1. Audit cleanup
|
||||
|
||||
- [ ] **Remove `eightpoints/guzzle-bundle` + `idci/graphql-client-bundle`.**
|
||||
They exist only to build one near-static GitHub GraphQL query string
|
||||
(`GitHubProvider.php:59-74`); the bundle's own HTTP transport is already
|
||||
bypassed (comment at `GitHubProvider.php:76-77`). Replace
|
||||
`$graphqlClient->buildQuery(...)->getGraphQLQuery()` with a plain
|
||||
`sprintf`/heredoc query string. Delete the two packages from
|
||||
`composer.json`, `config/bundles.php`, and delete
|
||||
`config/packages/eight_points_guzzle.yaml` +
|
||||
`config/packages/idci_graphql_client.yaml`. Run `composer update` and
|
||||
commit the regenerated `composer.lock`.
|
||||
- [ ] **Dedupe `baseUrl` normalization.** `GitLabProvider.php` (lines 41,
|
||||
53) and `GiteaProvider.php` (lines 42, 54) each call
|
||||
`rtrim($this->baseUrl..., '/')` twice — once in `ping()`, once in
|
||||
`fetch()`. Compute it once as a `private readonly string $baseUrl` in
|
||||
the constructor instead.
|
||||
|
||||
## 2. Fix stale `config/services.yaml` (found during exploration, blocks step 1 & 4)
|
||||
|
||||
- [ ] `services.yaml` still binds `$username`/`$token`/`$baseUrl` to the
|
||||
pre-refactor FQCNs (`App\Service\GitHubProvider` etc.) and
|
||||
`_instanceof: App\Service\ProviderInterface`, left over from the
|
||||
`Service/` → `Provider/`+`Renderer/` namespace reorg. Update all of
|
||||
these to `App\Service\Provider\...`. Currently these bindings silently
|
||||
no-op, and scalar constructor args can't autowire without them — any
|
||||
edit to these constructors (steps 1, 4) needs this fixed first, or the
|
||||
container fails to compile.
|
||||
|
||||
## 3. `ContributionStore` (SQLite via native PDO)
|
||||
|
||||
- [ ] New `src/Service/ContributionStore.php`. PDO SQLite, DB file at
|
||||
`%kernel.project_dir%/var/data/contributions.db` (configurable
|
||||
constructor arg), table created lazily:
|
||||
```sql
|
||||
CREATE TABLE IF NOT EXISTS contributions (
|
||||
provider TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
count INTEGER NOT NULL,
|
||||
PRIMARY KEY (provider, date)
|
||||
);
|
||||
```
|
||||
- [ ] `latestDate(string $provider): ?string` — `SELECT MAX(date) WHERE provider = ?`.
|
||||
- [ ] `merge(string $provider, array $dateCounts): void` — upsert via
|
||||
`INSERT ... ON CONFLICT(provider, date) DO UPDATE SET count = excluded.count`.
|
||||
- [ ] `all(string $provider, ?int $sinceDays = null): array` — `date => count`;
|
||||
`null` returns full history (no arbitrary cutoff), a value filters to the
|
||||
last N days.
|
||||
- [ ] `prune(): void` — `DELETE FROM contributions WHERE date < ?` using
|
||||
the configured retention window; no-ops if retention is unset/0.
|
||||
- [ ] Constructor takes `?int $retentionDays` bound from new env var
|
||||
`CONTRIBUTIONS_RETENTION_DAYS` (empty/default = keep forever).
|
||||
- [ ] Tests: `tests/Unit/Service/ContributionStoreTest.php` — store &
|
||||
retrieve, upsert overwrites existing date, `sinceDays` filtering,
|
||||
`prune()` no-ops when retention unset, `prune()` deletes rows older
|
||||
than the window when set.
|
||||
|
||||
## 4. Wire `$since` through providers
|
||||
|
||||
- [ ] `ProviderInterface::fetch()` → `fetch(?\DateTimeImmutable $since = null): array`.
|
||||
- [ ] `GitHubProvider::fetch()` — use `$since ?? new \DateTimeImmutable('-365 days')`
|
||||
for the GraphQL `from` param.
|
||||
- [ ] `GitLabProvider::fetch()` — same, feeds the `after` query param
|
||||
(this is what actually shrinks the pagination loop).
|
||||
- [ ] `GiteaProvider::fetch()` — same, feeds the `$cutoff` timestamp filter.
|
||||
- [ ] Update `GitHubProviderTest.php` (drop `GraphQLApiClientRegistryInterface`
|
||||
stub setup entirely per step 1), `GitLabProviderTest.php`,
|
||||
`GiteaProviderTest.php` — add a case asserting a passed `$since` narrows
|
||||
the request window.
|
||||
|
||||
## 5. Wire `ContributionStore` into `ContributionAggregator`
|
||||
|
||||
- [ ] Inject `ContributionStore` into `ContributionAggregator`.
|
||||
- [ ] Per configured provider: `$since = $store->latestDate($name)` → if
|
||||
set, `(new \DateTimeImmutable($latest))->modify('-3 days')` (3-day
|
||||
overlap for late corrections), else `null` (first run).
|
||||
- [ ] `$fresh = $provider->fetch($since)`, `$store->merge($name, $fresh)`,
|
||||
then read back `$store->all($name, sinceDays: 371)` for the render
|
||||
window (53 weeks × 7 days) and merge into the returned array.
|
||||
- [ ] Call `$store->prune()` once per `aggregate()` call, after all
|
||||
providers have merged.
|
||||
- [ ] Keep the existing try/catch-and-log-per-provider behavior — a
|
||||
provider failure leaves its DB history stale, doesn't break the render.
|
||||
- [ ] Update `ContributionAggregatorTest.php` with store-interaction and
|
||||
prune-call cases.
|
||||
|
||||
## 6. Docker / env
|
||||
|
||||
- [ ] `docker-compose.yml` — add a `data` named volume mounted at
|
||||
`/app/var/data` (same pattern as `cache`/`logs`), and pass through
|
||||
`CONTRIBUTIONS_RETENTION_DAYS: "${CONTRIBUTIONS_RETENTION_DAYS:-}"`.
|
||||
- [ ] `Dockerfile` — add `var/data` to the `mkdir -p` in the `final` stage
|
||||
alongside `var/cache/prod/pools var/log`, owned by `app`.
|
||||
- [ ] `.env` — document `CONTRIBUTIONS_RETENTION_DAYS` (empty by default),
|
||||
same style as the existing `ALLOWED_HOSTS` comment.
|
||||
|
||||
## 7. PHPStan
|
||||
|
||||
- [ ] `composer require --dev phpstan/phpstan` (plain PHPStan — no
|
||||
Symfony extension needed for this app's size).
|
||||
- [ ] Add `phpstan.neon`: `paths: [src, tests]`, `level: 8`.
|
||||
- [ ] Add composer script `"phpstan": "phpstan analyse"`.
|
||||
- [ ] Fix whatever level-8 flags on first run.
|
||||
|
||||
## 8. `CLAUDE.md` update
|
||||
|
||||
- [ ] Architecture section: add the `ContributionStore` (SQLite) tier
|
||||
between providers and the renderer; note the two-tier cache (1h SVG
|
||||
cache → SQLite raw-data store → provider APIs).
|
||||
- [ ] Environment variables table: add `CONTRIBUTIONS_RETENTION_DAYS`.
|
||||
- [ ] Development section: add `composer phpstan` next to the existing
|
||||
`vendor/bin/phpunit` commands.
|
||||
- [ ] Remove any remaining mentions of the two deleted bundles.
|
||||
|
||||
## Verification
|
||||
|
||||
- `vendor/bin/phpunit --testdox` green after every step.
|
||||
- `composer phpstan` clean at level 8.
|
||||
- `composer install` succeeds with no dangling references to the removed bundles.
|
||||
- `docker compose up -d --build`; clear the FS cache
|
||||
(`docker compose exec graph rm -rf var/cache/*`); hit
|
||||
`/graph.svg?theme=dark` twice; check `docker compose logs -f graph` shows
|
||||
a narrower `since` window on the second fetch.
|
||||
- Set `CONTRIBUTIONS_RETENTION_DAYS=30`; confirm rows older than 30 days
|
||||
are gone after the next refresh
|
||||
(`sqlite3 var/data/contributions.db "SELECT date FROM contributions ORDER BY date LIMIT 1"`).
|
||||
- `curl localhost:8080/health` still reports all configured providers healthy.
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
use App\Kernel;
|
||||
use GitContributionGraph\Kernel;
|
||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||
|
||||
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
|
||||
|
||||
+6
-4
@@ -5,8 +5,6 @@
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=8.2",
|
||||
"eightpoints/guzzle-bundle": "^8.6",
|
||||
"idci/graphql-client-bundle": "^2.0",
|
||||
"monolog/monolog": "^3.10",
|
||||
"symfony/cache": "7.4.*",
|
||||
"symfony/console": "7.4.*",
|
||||
@@ -18,15 +16,16 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/"
|
||||
"GitContributionGraph\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"App\\Tests\\": "tests/"
|
||||
"GitContributionGraph\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"phpstan/phpstan": "^2.2",
|
||||
"phpunit/phpunit": "^11.5",
|
||||
"symfony/phpunit-bridge": "^7.4"
|
||||
},
|
||||
@@ -42,5 +41,8 @@
|
||||
"allow-contrib": false,
|
||||
"require": "7.4.*"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"phpstan": "phpstan analyse --memory-limit=512M"
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+372
-1501
File diff suppressed because it is too large
Load Diff
@@ -3,6 +3,4 @@
|
||||
return [
|
||||
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
|
||||
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
|
||||
EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle::class => ['all' => true],
|
||||
IDCI\Bundle\GraphQLClientBundle\IDCIGraphQLClientBundle::class => ['all' => true],
|
||||
];
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
eight_points_guzzle:
|
||||
clients:
|
||||
github_graphql:
|
||||
base_url: 'https://api.github.com/graphql'
|
||||
options:
|
||||
headers:
|
||||
Authorization: 'Bearer %env(GITHUB_TOKEN)%'
|
||||
@@ -1,4 +0,0 @@
|
||||
idci_graphql_client:
|
||||
clients:
|
||||
github:
|
||||
http_client: 'eight_points_guzzle.client.github_graphql'
|
||||
+3
-59
@@ -37,7 +37,7 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
|
||||
* @psalm-type ArgumentsType = list<mixed>|array<string, mixed>
|
||||
* @psalm-type CallType = array<string, ArgumentsType>|array{0:string, 1?:ArgumentsType, 2?:bool}|array{method:string, arguments?:ArgumentsType, returns_clone?:bool}
|
||||
* @psalm-type TagsType = list<string|array<string, array<string, mixed>>> // arrays inside the list must have only one element, with the tag name as the key
|
||||
* @psalm-type CallbackType = string|array{0:string|ReferenceConfigurator,1:string}|\Closure|ReferenceConfigurator|ExpressionConfigurator
|
||||
* @psalm-type CallbackType = string|array{0:string|ReferenceConfigurator,1:string}|\Closure|ReferenceConfigurator
|
||||
* @psalm-type DeprecationType = array{package: string, version: string, message?: string}
|
||||
* @psalm-type DefaultsType = array{
|
||||
* public?: bool,
|
||||
@@ -121,7 +121,7 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
|
||||
* }
|
||||
* @psalm-type ServicesConfig = array{
|
||||
* _defaults?: DefaultsType,
|
||||
* _instanceof?: InstanceofType,
|
||||
* _instanceof?: array<class-string, InstanceofType>,
|
||||
* ...<string, DefinitionType|AliasType|PrototypeType|StackType|ArgumentsType|null>
|
||||
* }
|
||||
* @psalm-type ExtensionType = array<string, mixed>
|
||||
@@ -302,7 +302,7 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
|
||||
* },
|
||||
* },
|
||||
* translator?: bool|array{ // Translator configuration
|
||||
* enabled?: bool|Param, // Default: true
|
||||
* enabled?: bool|Param, // Default: false
|
||||
* fallbacks?: string|list<scalar|Param|null>,
|
||||
* logging?: bool|Param, // Default: false
|
||||
* formatter?: scalar|Param|null, // Default: "translator.formatter.default"
|
||||
@@ -833,72 +833,18 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
|
||||
* },
|
||||
* }>,
|
||||
* }
|
||||
* @psalm-type EightPointsGuzzleConfig = array{
|
||||
* clients?: array<string, array{ // Default: []
|
||||
* class?: scalar|Param|null, // Default: "%eight_points_guzzle.http_client.class%"
|
||||
* base_url?: scalar|Param|null, // Default: null
|
||||
* lazy?: bool|Param, // Default: false
|
||||
* logging?: int|Param, // Default: null
|
||||
* handler?: scalar|Param|null, // Default: null
|
||||
* options?: array{
|
||||
* headers?: array<string, scalar|Param|null>,
|
||||
* allow_redirects?: mixed,
|
||||
* auth?: mixed,
|
||||
* query?: mixed,
|
||||
* curl?: list<scalar|Param|null>,
|
||||
* cert?: mixed,
|
||||
* connect_timeout?: scalar|Param|null,
|
||||
* debug?: bool|Param,
|
||||
* decode_content?: mixed,
|
||||
* delay?: float|Param,
|
||||
* form_params?: array<string, mixed>,
|
||||
* multipart?: list<mixed>,
|
||||
* sink?: scalar|Param|null,
|
||||
* http_errors?: bool|Param,
|
||||
* expect?: mixed,
|
||||
* ssl_key?: mixed,
|
||||
* stream?: bool|Param,
|
||||
* synchronous?: bool|Param,
|
||||
* read_timeout?: scalar|Param|null,
|
||||
* timeout?: scalar|Param|null,
|
||||
* verify?: mixed,
|
||||
* cookies?: bool|Param,
|
||||
* proxy?: string|array{
|
||||
* http?: scalar|Param|null,
|
||||
* https?: scalar|Param|null,
|
||||
* no?: list<scalar|Param|null>,
|
||||
* },
|
||||
* version?: scalar|Param|null,
|
||||
* },
|
||||
* plugin?: array<mixed>,
|
||||
* }>,
|
||||
* logging?: bool|Param, // Default: true
|
||||
* profiling?: bool|Param, // Default: true
|
||||
* slow_response_time?: int|Param, // Default: 0
|
||||
* }
|
||||
* @psalm-type IdciGraphqlClientConfig = array{
|
||||
* cache_enabled?: bool|Param, // Default: false
|
||||
* clients?: list<array{ // Default: []
|
||||
* http_client?: scalar|Param|null,
|
||||
* cache?: scalar|Param|null,
|
||||
* }>,
|
||||
* }
|
||||
* @psalm-type ConfigType = array{
|
||||
* imports?: ImportsConfig,
|
||||
* parameters?: ParametersConfig,
|
||||
* services?: ServicesConfig,
|
||||
* framework?: FrameworkConfig,
|
||||
* monolog?: MonologConfig,
|
||||
* eight_points_guzzle?: EightPointsGuzzleConfig,
|
||||
* idci_graphql_client?: IdciGraphqlClientConfig,
|
||||
* "when@dev"?: array{
|
||||
* imports?: ImportsConfig,
|
||||
* parameters?: ParametersConfig,
|
||||
* services?: ServicesConfig,
|
||||
* framework?: FrameworkConfig,
|
||||
* monolog?: MonologConfig,
|
||||
* eight_points_guzzle?: EightPointsGuzzleConfig,
|
||||
* idci_graphql_client?: IdciGraphqlClientConfig,
|
||||
* },
|
||||
* "when@prod"?: array{
|
||||
* imports?: ImportsConfig,
|
||||
@@ -906,8 +852,6 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
|
||||
* services?: ServicesConfig,
|
||||
* framework?: FrameworkConfig,
|
||||
* monolog?: MonologConfig,
|
||||
* eight_points_guzzle?: EightPointsGuzzleConfig,
|
||||
* idci_graphql_client?: IdciGraphqlClientConfig,
|
||||
* },
|
||||
* ...<string, ExtensionType|array{ // extra keys must follow the when@%env% pattern or match an extension alias
|
||||
* imports?: ImportsConfig,
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
controllers:
|
||||
resource:
|
||||
path: ../src/Controller/
|
||||
namespace: App\Controller
|
||||
namespace: GitContributionGraph\Controller
|
||||
type: attribute
|
||||
|
||||
+31
-26
@@ -1,14 +1,15 @@
|
||||
parameters:
|
||||
env(APP_SECRET): ''
|
||||
env(ALLOWED_HOSTS): ''
|
||||
env(GITHUB_USER): ''
|
||||
env(GITHUB_TOKEN): ''
|
||||
env(GITLAB_USER): ''
|
||||
env(GITLAB_TOKEN): ''
|
||||
env(GITLAB_URL): ''
|
||||
env(GITEA_USER): ''
|
||||
env(GITEA_TOKEN): ''
|
||||
env(GITEA_URL): ''
|
||||
env(APP_SECRET): ""
|
||||
env(ALLOWED_HOSTS): ""
|
||||
env(GITHUB_USER): ""
|
||||
env(GITHUB_TOKEN): ""
|
||||
env(GITLAB_USER): ""
|
||||
env(GITLAB_TOKEN): ""
|
||||
env(GITLAB_URL): ""
|
||||
env(GITEA_USER): ""
|
||||
env(GITEA_TOKEN): ""
|
||||
env(GITEA_URL): ""
|
||||
env(CONTRIBUTIONS_RETENTION_DAYS): "0"
|
||||
|
||||
services:
|
||||
_defaults:
|
||||
@@ -16,28 +17,32 @@ services:
|
||||
autoconfigure: true
|
||||
public: false
|
||||
|
||||
App\:
|
||||
resource: '../src/'
|
||||
GitContributionGraph\:
|
||||
resource: "../src/"
|
||||
exclude:
|
||||
- '../src/Kernel.php'
|
||||
- "../src/Kernel.php"
|
||||
|
||||
_instanceof:
|
||||
App\Service\Provider\ProviderInterface:
|
||||
tags: ['app.provider']
|
||||
GitContributionGraph\Service\Provider\ProviderInterface:
|
||||
tags: ["app.provider"]
|
||||
|
||||
App\Service\Provider\GitHubProvider:
|
||||
GitContributionGraph\Service\Provider\GitHubProvider:
|
||||
arguments:
|
||||
$username: '%env(GITHUB_USER)%'
|
||||
$token: '%env(GITHUB_TOKEN)%'
|
||||
$username: "%env(GITHUB_USER)%"
|
||||
$token: "%env(GITHUB_TOKEN)%"
|
||||
|
||||
App\Service\Provider\GitLabProvider:
|
||||
GitContributionGraph\Service\Provider\GitLabProvider:
|
||||
arguments:
|
||||
$username: '%env(GITLAB_USER)%'
|
||||
$token: '%env(GITLAB_TOKEN)%'
|
||||
$baseUrl: '%env(GITLAB_URL)%'
|
||||
$username: "%env(GITLAB_USER)%"
|
||||
$token: "%env(GITLAB_TOKEN)%"
|
||||
$baseUrl: "%env(GITLAB_URL)%"
|
||||
|
||||
App\Service\Provider\GiteaProvider:
|
||||
GitContributionGraph\Service\Provider\GiteaProvider:
|
||||
arguments:
|
||||
$username: '%env(GITEA_USER)%'
|
||||
$token: '%env(GITEA_TOKEN)%'
|
||||
$baseUrl: '%env(GITEA_URL)%'
|
||||
$username: "%env(GITEA_USER)%"
|
||||
$token: "%env(GITEA_TOKEN)%"
|
||||
$baseUrl: "%env(GITEA_URL)%"
|
||||
GitContributionGraph\Service\ContributionStore:
|
||||
arguments:
|
||||
$dbPath: "%kernel.project_dir%/var/data/contributions.db"
|
||||
$retentionDays: "%env(int:CONTRIBUTIONS_RETENTION_DAYS)%"
|
||||
|
||||
@@ -4,9 +4,11 @@ services:
|
||||
graph:
|
||||
build:
|
||||
dockerfile: Dockerfile.dev
|
||||
target: dev
|
||||
volumes:
|
||||
- .:/app
|
||||
- /app/vendor # keeps vendor from the dev image, not your local dir
|
||||
- ./vendor:/app/vendor
|
||||
- /app/var/data
|
||||
environment:
|
||||
APP_ENV: dev
|
||||
APP_DEBUG: "1"
|
||||
|
||||
@@ -18,9 +18,11 @@ services:
|
||||
GITEA_USER: "${GITEA_USER:-}"
|
||||
GITEA_TOKEN: "${GITEA_TOKEN:-}"
|
||||
GITEA_URL: "${GITEA_URL:-}"
|
||||
CONTRIBUTIONS_RETENTION_DAYS: "${CONTRIBUTIONS_RETENTION_DAYS:-0}"
|
||||
volumes:
|
||||
- cache:/app/var/cache/prod/pools
|
||||
- logs:/app/var/log
|
||||
- data:/app/var/data
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
|
||||
interval: 30s
|
||||
@@ -30,3 +32,4 @@ services:
|
||||
volumes:
|
||||
cache:
|
||||
logs:
|
||||
data:
|
||||
|
||||
@@ -20,9 +20,11 @@ services:
|
||||
GITEA_USER: "${GITEA_USER:-}"
|
||||
GITEA_TOKEN: "${GITEA_TOKEN:-}"
|
||||
GITEA_URL: "${GITEA_URL:-}"
|
||||
CONTRIBUTIONS_RETENTION_DAYS: "${CONTRIBUTIONS_RETENTION_DAYS:-0}"
|
||||
volumes:
|
||||
- cache:/app/var/cache/prod/pools
|
||||
- logs:/app/var/log
|
||||
- data:/app/var/data
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
|
||||
interval: 30s
|
||||
@@ -33,3 +35,4 @@ services:
|
||||
volumes:
|
||||
cache:
|
||||
logs:
|
||||
data:
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
parameters:
|
||||
level: 8
|
||||
paths:
|
||||
- src
|
||||
- tests
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Kernel;
|
||||
use GitContributionGraph\Kernel;
|
||||
|
||||
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Kernel;
|
||||
use GitContributionGraph\Kernel;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
require_once dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Command;
|
||||
|
||||
use GitContributionGraph\Service\ContributionStore;
|
||||
use GitContributionGraph\Service\Provider\ProviderInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Attribute\Option;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'graph:contributions:refetch',
|
||||
description: 'Force a full or ranged re-fetch of contribution history into the store.',
|
||||
)]
|
||||
final class RefetchContributionsCommand extends Command
|
||||
{
|
||||
private const MAX_CHUNK_DAYS = 365;
|
||||
private const ALL_SINCE = '2005-01-01';
|
||||
|
||||
private SymfonyStyle $io;
|
||||
|
||||
/** @param iterable<ProviderInterface> $providers */
|
||||
public function __construct(
|
||||
#[AutowireIterator('app.provider')]
|
||||
private readonly iterable $providers,
|
||||
private readonly ContributionStore $store,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/** Symfony console lifecycle hook: sets up the styled I/O helper used throughout the command. */
|
||||
public function initialize(InputInterface $input, OutputInterface $output): void
|
||||
{
|
||||
$this->io = new SymfonyStyle($input, $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bypasses the aggregator's incremental trailing-window fetch to re-pull a full or explicit
|
||||
* date range for one, several, or all configured providers, chunked into ≤365-day windows.
|
||||
*
|
||||
* @param string $provider comma-separated provider names to refetch; empty = all configured
|
||||
* @param string $from start date (YYYY-MM-DD); empty defaults to 365 days ago
|
||||
* @param string $to end date (YYYY-MM-DD); empty defaults to today
|
||||
* @param bool $all shorthand for --from=2005-01-01
|
||||
* @return int Command::SUCCESS if at least one provider refetched cleanly, else Command::FAILURE
|
||||
*/
|
||||
public function __invoke(
|
||||
#[Option('Comma-separated provider names to refetch (default: all configured)', 'provider', 'p')]
|
||||
string $provider = '',
|
||||
#[Option('Start date (YYYY-MM-DD), default: 365 days ago', 'from')]
|
||||
string $from = '',
|
||||
#[Option('End date (YYYY-MM-DD), default: today', 'to')]
|
||||
string $to = '',
|
||||
#[Option('Refetch full history (from ' . self::ALL_SINCE . ')', 'all')]
|
||||
bool $all = false,
|
||||
): int {
|
||||
$byName = $this->resolveProviders($provider);
|
||||
if ($byName === null) {
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$fromSpec = match (true) {
|
||||
$all => self::ALL_SINCE,
|
||||
$from !== '' => $from,
|
||||
default => '-365 days',
|
||||
};
|
||||
|
||||
$fromDate = new \DateTimeImmutable($fromSpec);
|
||||
$toDate = new \DateTimeImmutable($to !== '' ? $to : 'today');
|
||||
$chunks = $this->chunk($fromDate, $toDate);
|
||||
|
||||
$summary = [];
|
||||
$succeeded = 0;
|
||||
|
||||
foreach ($byName as $name => $providerInstance) {
|
||||
[$daysWritten, $errors] = $this->refetchProvider($name, $providerInstance, $chunks);
|
||||
|
||||
if ($errors === 0) {
|
||||
$succeeded++;
|
||||
}
|
||||
|
||||
$summary[] = [$name, $daysWritten, count($chunks), $errors];
|
||||
}
|
||||
|
||||
$this->io->table(['Provider', 'Days written', 'Chunks fetched', 'Errors'], $summary);
|
||||
|
||||
if ($succeeded === 0) {
|
||||
$this->io->error('No provider completed successfully.');
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->io->success(sprintf('%d of %d provider(s) refetched successfully.', $succeeded, count($byName)));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{0: \DateTimeImmutable, 1: \DateTimeImmutable}> $chunks
|
||||
*
|
||||
* @return array{0: int, 1: int} days written, errors
|
||||
*/
|
||||
private function refetchProvider(string $name, ProviderInterface $provider, array $chunks): array
|
||||
{
|
||||
$this->io->section("Refetching {$name}");
|
||||
|
||||
$progress = new ProgressBar($this->io, count($chunks));
|
||||
$progress->start();
|
||||
|
||||
$daysWritten = 0;
|
||||
$errors = 0;
|
||||
|
||||
foreach ($chunks as [$chunkFrom, $chunkTo]) {
|
||||
try {
|
||||
$fresh = $provider->resolveFetch($provider->startFetch($chunkFrom, $chunkTo));
|
||||
|
||||
$dateCounts = [];
|
||||
foreach ($fresh as $date => $count) {
|
||||
$dateCounts[(new \DateTimeImmutable($date))->getTimestamp()] = $count;
|
||||
}
|
||||
|
||||
$this->store->merge($name, $dateCounts);
|
||||
$daysWritten += count($dateCounts);
|
||||
} catch (\Throwable $e) {
|
||||
$errors++;
|
||||
$this->io->warning("{$name}: {$e->getMessage()}");
|
||||
}
|
||||
|
||||
$progress->advance();
|
||||
}
|
||||
|
||||
$progress->finish();
|
||||
$this->io->newLine(2);
|
||||
|
||||
return [$daysWritten, $errors];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ProviderInterface>|null null when an unknown provider name was requested
|
||||
*/
|
||||
private function resolveProviders(string $requested): ?array
|
||||
{
|
||||
$byName = [];
|
||||
foreach ($this->providers as $providerInstance) {
|
||||
$byName[$providerInstance->getName()] = $providerInstance;
|
||||
}
|
||||
|
||||
if ($requested === '') {
|
||||
return array_filter($byName, static fn (ProviderInterface $p): bool => $p->isConfigured());
|
||||
}
|
||||
|
||||
$names = explode(',', $requested);
|
||||
foreach ($names as $name) {
|
||||
if (!isset($byName[$name])) {
|
||||
$this->io->error("Unknown provider: {$name}");
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return array_intersect_key($byName, array_flip($names));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{0: \DateTimeImmutable, 1: \DateTimeImmutable}>
|
||||
*/
|
||||
private function chunk(\DateTimeImmutable $from, \DateTimeImmutable $to): array
|
||||
{
|
||||
$chunks = [];
|
||||
$cursor = $from;
|
||||
|
||||
while ($cursor < $to) {
|
||||
$chunkEnd = min($cursor->modify('+' . (self::MAX_CHUNK_DAYS - 1) . ' days'), $to);
|
||||
$chunks[] = [$cursor, $chunkEnd];
|
||||
$cursor = $chunkEnd->modify('+1 day');
|
||||
}
|
||||
|
||||
if ($chunks === []) {
|
||||
$chunks[] = [$from, $to];
|
||||
}
|
||||
|
||||
return $chunks;
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
namespace GitContributionGraph\Controller;
|
||||
|
||||
use App\Service\ContributionAggregator;
|
||||
use App\Service\Provider\ProviderHealthChecker;
|
||||
use App\Service\Renderer\SvgRenderer;
|
||||
use GitContributionGraph\Service\ContributionAggregator;
|
||||
use GitContributionGraph\Service\Provider\ProviderHealthChecker;
|
||||
use GitContributionGraph\Service\Renderer\SvgRenderer;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
@@ -34,8 +34,12 @@ final class GraphController
|
||||
}
|
||||
|
||||
/**
|
||||
* Query parameters:
|
||||
* theme string dark|light (default: dark)
|
||||
* Serves the contribution heatmap SVG, from a 1-hour cache on repeat requests.
|
||||
*
|
||||
* Rejects requests to disallowed hosts (ALLOWED_HOSTS env) with a 403.
|
||||
*
|
||||
* @param Request $request query parameter `theme` selects "dark" (default) or "light"
|
||||
* @return Response image/svg+xml body, cacheable for 3600s
|
||||
*/
|
||||
#[Route('/graph.svg', name: 'contribution_graph', methods: ['GET'])]
|
||||
public function graph(Request $request): Response
|
||||
@@ -66,6 +70,12 @@ final class GraphController
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects "/" to "/graph.svg", forwarding any query string (e.g. ?theme=light).
|
||||
*
|
||||
* @param Request $request incoming request whose query string is preserved
|
||||
* @return RedirectResponse 302 redirect to /graph.svg
|
||||
*/
|
||||
#[Route('/', name: 'index', methods: ['GET'])]
|
||||
public function index(Request $request): RedirectResponse
|
||||
{
|
||||
@@ -75,6 +85,12 @@ final class GraphController
|
||||
return new RedirectResponse($url, 302);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports per-provider connectivity status as JSON.
|
||||
*
|
||||
* @return Response 200 with {"status":"ok",...} or 503 with {"status":"degraded",...}
|
||||
* when any configured provider's probe reports an error
|
||||
*/
|
||||
#[Route('/health', name: 'health', methods: ['GET'])]
|
||||
public function health(): Response
|
||||
{
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Entity;
|
||||
|
||||
/** A single provider's contribution count for one day, as stored in {@see \GitContributionGraph\Service\ContributionStore}. */
|
||||
final class Contribution
|
||||
{
|
||||
/**
|
||||
* @param string $provider provider identifier, e.g. "github"
|
||||
* @param int $date day of the contribution, as a unix timestamp
|
||||
* @param int $count contribution count for that day
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly string $provider,
|
||||
public readonly int $date,
|
||||
public readonly int $count,
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Entity;
|
||||
|
||||
/**
|
||||
* @implements \IteratorAggregate<int, Contribution>
|
||||
*/
|
||||
final class ContributionCollection implements \IteratorAggregate, \Countable
|
||||
{
|
||||
/** @var array<int, Contribution> */
|
||||
private readonly array $contributions;
|
||||
|
||||
public function __construct(Contribution ...$contributions)
|
||||
{
|
||||
$this->contributions = array_values($contributions);
|
||||
}
|
||||
|
||||
/** @return \ArrayIterator<int, Contribution> */
|
||||
public function getIterator(): \ArrayIterator
|
||||
{
|
||||
return new \ArrayIterator($this->contributions);
|
||||
}
|
||||
|
||||
/** Number of contributions held in this collection. */
|
||||
public function count(): int
|
||||
{
|
||||
return count($this->contributions);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
namespace GitContributionGraph;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
|
||||
|
||||
@@ -2,33 +2,72 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
namespace GitContributionGraph\Service;
|
||||
|
||||
use App\Service\Provider\ProviderInterface;
|
||||
use GitContributionGraph\Service\Provider\ProviderInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
|
||||
|
||||
final class ContributionAggregator
|
||||
{
|
||||
private const RENDER_WINDOW_DAYS = 371;
|
||||
private const OVERLAP_DAYS = 3;
|
||||
|
||||
/** @param iterable<ProviderInterface> $providers */
|
||||
public function __construct(
|
||||
#[AutowireIterator('app.provider')]
|
||||
private readonly iterable $providers,
|
||||
private readonly ContributionStore $store,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {}
|
||||
|
||||
/** @return array<string, int> */
|
||||
/**
|
||||
* Incrementally fetches fresh contributions from each configured provider, merges them into
|
||||
* the store, and returns the rolling render window summed across providers.
|
||||
*
|
||||
* For each provider, only data since its last stored date (minus a trailing overlap) is
|
||||
* fetched; a provider with no stored history yet fetches its full default range. Fetches are
|
||||
* split into start/resolve phases so all providers' requests are in flight concurrently.
|
||||
* Failures are logged and the affected provider is skipped, not fatal to the others.
|
||||
*
|
||||
* @return array<string, int> date (Y-m-d) => contribution count, summed across all providers
|
||||
*/
|
||||
public function aggregate(): array
|
||||
{
|
||||
$pending = [];
|
||||
$configured = [];
|
||||
|
||||
/** @var ProviderInterface $provider */
|
||||
foreach ($this->providers as $provider) {
|
||||
if (!$provider->isConfigured()) {
|
||||
continue;
|
||||
if ($provider->isConfigured()) {
|
||||
$configured[] = $provider;
|
||||
}
|
||||
}
|
||||
|
||||
$pending = [];
|
||||
|
||||
foreach ($configured as $provider) {
|
||||
$latest = $this->store->latestDate($provider->getName());
|
||||
$since = $latest !== null
|
||||
? (new \DateTimeImmutable('@' . $latest))->modify('-' . self::OVERLAP_DAYS . ' days')
|
||||
: null;
|
||||
|
||||
try {
|
||||
$pending[] = [$provider, $provider->startFetch()];
|
||||
$pending[] = [$provider, $provider->startFetch($since)];
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()), ['exception' => $e]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($pending as [$provider, $handle]) {
|
||||
try {
|
||||
$fresh = $provider->resolveFetch($handle);
|
||||
|
||||
$dateCounts = [];
|
||||
foreach ($fresh as $date => $count) {
|
||||
$dateCounts[(new \DateTimeImmutable($date))->getTimestamp()] = $count;
|
||||
}
|
||||
|
||||
$this->store->merge($provider->getName(), $dateCounts);
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()), ['exception' => $e]);
|
||||
}
|
||||
@@ -36,16 +75,15 @@ final class ContributionAggregator
|
||||
|
||||
$contributions = [];
|
||||
|
||||
foreach ($pending as [$provider, $handle]) {
|
||||
try {
|
||||
foreach ($provider->resolveFetch($handle) as $date => $count) {
|
||||
$contributions[$date] = ($contributions[$date] ?? 0) + $count;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()), ['exception' => $e]);
|
||||
foreach ($configured as $provider) {
|
||||
foreach ($this->store->all($provider->getName(), sinceDays: self::RENDER_WINDOW_DAYS) as $contribution) {
|
||||
$date = (new \DateTimeImmutable('@' . $contribution->date))->format('Y-m-d');
|
||||
$contributions[$date] = ($contributions[$date] ?? 0) + $contribution->count;
|
||||
}
|
||||
}
|
||||
|
||||
$this->store->prune();
|
||||
|
||||
return $contributions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Service;
|
||||
|
||||
use GitContributionGraph\Entity\Contribution;
|
||||
use GitContributionGraph\Entity\ContributionCollection;
|
||||
use PDO;
|
||||
|
||||
class ContributionStore
|
||||
{
|
||||
private PDO $pdo;
|
||||
private const int DAY_IN_SECONDS = 86400;
|
||||
|
||||
/**
|
||||
* Opens (creating if needed) the SQLite store at $dbPath and ensures the schema exists.
|
||||
*
|
||||
* @param string $dbPath filesystem path to the SQLite database file
|
||||
* @param ?int $retentionDays days of history to keep; null or 0 keeps rows forever
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $dbPath = "%kernel.project_dir%/var/data/contributions.db",
|
||||
private readonly ?int $retentionDays = null,
|
||||
) {
|
||||
$dir = \dirname($this->dbPath);
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0755, recursive: true);
|
||||
}
|
||||
$this->pdo = new PDO('sqlite:' . $this->dbPath);
|
||||
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->prepareSchema();
|
||||
}
|
||||
|
||||
private function prepareSchema(): void
|
||||
{
|
||||
$this->pdo->exec('
|
||||
CREATE TABLE IF NOT EXISTS contributions (
|
||||
provider TEXT NOT NULL,
|
||||
date INTEGER NOT NULL,
|
||||
count INTEGER NOT NULL CHECK (count >= 0),
|
||||
PRIMARY KEY (provider, date)
|
||||
) WITHOUT ROWID, STRICT
|
||||
');
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a contribution count, overwriting any existing count for the same provider/date.
|
||||
*
|
||||
* @param string $provider provider identifier, e.g. "github"
|
||||
* @param int $unixtime day of the contribution, as a unix timestamp
|
||||
* @param int $count contribution count for that day
|
||||
*/
|
||||
public function add(string $provider, int $unixtime, int $count): void
|
||||
{
|
||||
$stmt = $this->pdo->prepare('
|
||||
INSERT INTO contributions (provider, date, count)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(provider, date) DO UPDATE SET count = excluded.count
|
||||
');
|
||||
$stmt->execute([$provider, $unixtime, $count]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the contribution row for the given provider/date, if any.
|
||||
*
|
||||
* @param string $provider provider identifier, e.g. "github"
|
||||
* @param int $unixtime day to delete, as a unix timestamp
|
||||
*/
|
||||
public function remove(string $provider, int $unixtime): void
|
||||
{
|
||||
$stmt = $this->pdo->prepare('
|
||||
DELETE FROM contributions
|
||||
WHERE provider = ? AND date = ?
|
||||
');
|
||||
$stmt->execute([$provider, $unixtime]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upserts a batch of date => count pairs for a provider via repeated add() calls.
|
||||
*
|
||||
* @param string $provider provider identifier, e.g. "github"
|
||||
* @param array<int, int> $dateCounts unix timestamp => count
|
||||
*/
|
||||
public function merge(string $provider, array $dateCounts): void
|
||||
{
|
||||
foreach ($dateCounts as $unixtime => $count) {
|
||||
$this->add($provider, $unixtime, $count);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most recent stored unix timestamp for a provider, or null if it has no rows.
|
||||
*
|
||||
* @param string $provider provider identifier, e.g. "github"
|
||||
* @return ?int unix timestamp of the latest stored day, or null if none stored yet
|
||||
*/
|
||||
public function latestDate(string $provider): ?int
|
||||
{
|
||||
$stmt = $this->pdo->prepare('SELECT MAX(date) FROM contributions WHERE provider = ?');
|
||||
$stmt->execute([$provider]);
|
||||
$result = $stmt->fetchColumn();
|
||||
|
||||
return $result !== null ? (int) $result : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all stored contributions for a provider, optionally limited to the last $sinceDays days.
|
||||
*
|
||||
* @param string $provider provider identifier, e.g. "github"
|
||||
* @param ?int $sinceDays if set, only rows from the last N days are returned
|
||||
* @return ContributionCollection
|
||||
*/
|
||||
public function all(string $provider, ?int $sinceDays = null): ContributionCollection
|
||||
{
|
||||
$sql = 'SELECT provider, date, count FROM contributions WHERE provider = ?';
|
||||
$params = [$provider];
|
||||
|
||||
if ($sinceDays !== null) {
|
||||
$sql .= ' AND date >= ?';
|
||||
$params[] = time() - $sinceDays * self::DAY_IN_SECONDS;
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
|
||||
/** @var array<int, array{provider: string, date: int|string, count: int|string}> $rows */
|
||||
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
$contributions = array_map(
|
||||
static fn(array $row): Contribution => new Contribution($row['provider'], (int) $row['date'], (int) $row['count']),
|
||||
$rows,
|
||||
);
|
||||
|
||||
return new ContributionCollection(...$contributions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes rows older than the configured retention window; a no-op if retention is unset or 0.
|
||||
*/
|
||||
public function prune(): void
|
||||
{
|
||||
if ($this->retentionDays === null || $this->retentionDays === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cutoff = time() - $this->retentionDays * self::DAY_IN_SECONDS;
|
||||
|
||||
$stmt = $this->pdo->prepare('DELETE FROM contributions WHERE date < ?');
|
||||
$stmt->execute([$cutoff]);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClient;
|
||||
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
@@ -24,7 +22,6 @@ final class GitHubProvider implements ProviderInterface
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly GraphQLApiClientRegistryInterface $registry,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly LoggerInterface $logger,
|
||||
@@ -47,32 +44,22 @@ final class GitHubProvider implements ProviderInterface
|
||||
])->getContent();
|
||||
}
|
||||
|
||||
public function startFetch(): ResponseInterface
|
||||
/** Fires the GraphQL contributionsCollection query bounded by $since/$until. */
|
||||
public function startFetch(?\DateTimeImmutable $since = null, ?\DateTimeImmutable $until = null): ResponseInterface
|
||||
{
|
||||
$this->logger->debug('GitHubProvider: fetching contributions', ['user' => $this->username]);
|
||||
|
||||
$from = (new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z');
|
||||
$to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z');
|
||||
$from = ($since ?? new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z');
|
||||
$to = ($until ?? new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z');
|
||||
|
||||
/** @var GraphQLApiClient $graphqlClient */
|
||||
$graphqlClient = $this->registry->get('github');
|
||||
$query = sprintf(
|
||||
'query { user(login: %s) { contributionsCollection(from: %s, to: %s) { contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }',
|
||||
json_encode($this->username),
|
||||
json_encode($from),
|
||||
json_encode($to),
|
||||
);
|
||||
|
||||
$query = $graphqlClient->buildQuery(
|
||||
['user' => ['login' => $this->username]],
|
||||
[
|
||||
'contributionsCollection' => [
|
||||
'_parameters' => ['from' => $from, 'to' => $to],
|
||||
'contributionCalendar' => [
|
||||
'weeks' => [
|
||||
'contributionDays' => ['date', 'contributionCount'],
|
||||
],
|
||||
],
|
||||
],
|
||||
]
|
||||
)->getGraphQLQuery();
|
||||
|
||||
// GitHub's GraphQL API requires application/json — the bundle's built-in
|
||||
// transport sends form_params, so we use Symfony HttpClient here instead.
|
||||
// GitHub's GraphQL API requires application/json.
|
||||
// request() doesn't block; the response is read in resolveFetch() so
|
||||
// multiple providers' requests can be in flight at once.
|
||||
return $this->client->request('POST', self::GRAPHQL_URL, [
|
||||
@@ -85,6 +72,9 @@ final class GitHubProvider implements ProviderInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the GraphQL response into a per-day count map, skipping zero-count days.
|
||||
*
|
||||
* @param ResponseInterface $handle the response returned by startFetch()
|
||||
* @return array<string, int> date (Y-m-d) => contribution count
|
||||
*/
|
||||
public function resolveFetch(mixed $handle): array
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
@@ -19,13 +19,17 @@ final class GitLabProvider implements ProviderInterface
|
||||
{
|
||||
use ProbeTrait;
|
||||
|
||||
private readonly string $baseUrl;
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly string $baseUrl = '',
|
||||
) {}
|
||||
string $baseUrl = '',
|
||||
) {
|
||||
$this->baseUrl = rtrim($baseUrl !== '' ? $baseUrl : 'https://gitlab.com', '/');
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
@@ -39,57 +43,62 @@ final class GitLabProvider implements ProviderInterface
|
||||
|
||||
public function ping(): void
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/');
|
||||
|
||||
$this->client->request('GET', "$baseUrl/api/v4/user", [
|
||||
$this->client->request('GET', "$this->baseUrl/api/v4/user", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
])->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{baseUrl: string, userId: int, after: string, page: int, response: ResponseInterface}
|
||||
* Resolves the username to a numeric user ID, then fires the first page of the events request.
|
||||
*
|
||||
* @return array{baseUrl: string, userId: int, after: string, before: ?string, page: int, response: ResponseInterface}
|
||||
*
|
||||
* ponytail: the user-id lookup and event pagination stay sequential within
|
||||
* this one provider (each page depends on the previous). Parallelism here
|
||||
* only spans across providers; revisit only if GitLab pagination itself
|
||||
* becomes the bottleneck.
|
||||
*/
|
||||
public function startFetch(): array
|
||||
public function startFetch(?\DateTimeImmutable $since = null, ?\DateTimeImmutable $until = null): array
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/');
|
||||
|
||||
$this->logger->debug('GitLabProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);
|
||||
$this->logger->debug('GitLabProvider: fetching contributions', ['user' => $this->username, 'url' => $this->baseUrl]);
|
||||
|
||||
$userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [
|
||||
$userResponse = $this->client->request('GET', "$this->baseUrl/api/v4/users", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => ['username' => $this->username],
|
||||
]);
|
||||
|
||||
$users = $userResponse->toArray();
|
||||
if (empty($users)) {
|
||||
throw new NotFoundHttpException("GitLab: user '{$this->username}' not found on $baseUrl");
|
||||
throw new NotFoundHttpException("GitLab: user '{$this->username}' not found on $this->baseUrl");
|
||||
}
|
||||
$userId = $users[0]['id'];
|
||||
$after = (new \DateTimeImmutable('-365 days'))->format('Y-m-d');
|
||||
$after = ($since ?? new \DateTimeImmutable('-365 days'))->format('Y-m-d');
|
||||
$before = $until?->format('Y-m-d');
|
||||
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
||||
$query = ['after' => $after, 'per_page' => 100, 'page' => 1];
|
||||
if ($before !== null) {
|
||||
$query['before'] = $before;
|
||||
}
|
||||
|
||||
$response = $this->client->request('GET', "$this->baseUrl/api/v4/users/$userId/events", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => [
|
||||
'after' => $after,
|
||||
'per_page' => 100,
|
||||
'page' => 1,
|
||||
],
|
||||
'query' => $query,
|
||||
]);
|
||||
|
||||
return ['baseUrl' => $baseUrl, 'userId' => $userId, 'after' => $after, 'page' => 1, 'response' => $response];
|
||||
return ['baseUrl' => $this->baseUrl, 'userId' => $userId, 'after' => $after, 'before' => $before, 'page' => 1, 'response' => $response];
|
||||
}
|
||||
|
||||
/**
|
||||
* Follows pagination (100 events/page) until a short page is returned, counting events per day.
|
||||
*
|
||||
* @param array{baseUrl: string, userId: int, after: string, before: ?string, page: int, response: ResponseInterface} $handle
|
||||
* @return array<string, int> date (Y-m-d) => event count
|
||||
*/
|
||||
public function resolveFetch(mixed $handle): array
|
||||
{
|
||||
['baseUrl' => $baseUrl, 'userId' => $userId, 'after' => $after, 'page' => $page, 'response' => $response] = $handle;
|
||||
['baseUrl' => $baseUrl, 'userId' => $userId, 'after' => $after, 'before' => $before, 'page' => $page, 'response' => $response] = $handle;
|
||||
|
||||
$result = [];
|
||||
|
||||
@@ -104,13 +113,14 @@ final class GitLabProvider implements ProviderInterface
|
||||
$page++;
|
||||
|
||||
if (count($events) === 100) {
|
||||
$query = ['after' => $after, 'per_page' => 100, 'page' => $page];
|
||||
if ($before !== null) {
|
||||
$query['before'] = $before;
|
||||
}
|
||||
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => [
|
||||
'after' => $after,
|
||||
'per_page' => 100,
|
||||
'page' => $page,
|
||||
],
|
||||
'query' => $query,
|
||||
]);
|
||||
}
|
||||
} while (count($events) === 100);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
@@ -20,13 +20,17 @@ final class GiteaProvider implements ProviderInterface
|
||||
{
|
||||
use ProbeTrait;
|
||||
|
||||
private readonly string $baseUrl;
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly string $baseUrl,
|
||||
string $baseUrl,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {}
|
||||
) {
|
||||
$this->baseUrl = rtrim($baseUrl, '/');
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
@@ -40,38 +44,52 @@ final class GiteaProvider implements ProviderInterface
|
||||
|
||||
public function ping(): void
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl, '/');
|
||||
|
||||
$this->client->request('GET', "$baseUrl/api/v1/user", [
|
||||
|
||||
$this->client->request('GET', "$this->baseUrl/api/v1/user", [
|
||||
'headers' => ['Authorization' => "token {$this->token}"],
|
||||
])->getContent();
|
||||
}
|
||||
|
||||
public function startFetch(): ResponseInterface
|
||||
/**
|
||||
* Fires a single heatmap request; $since/$until are carried through unused for client-side filtering in resolveFetch().
|
||||
*
|
||||
* @return array{response: ResponseInterface, since: ?\DateTimeImmutable, until: ?\DateTimeImmutable}
|
||||
*/
|
||||
public function startFetch(?\DateTimeImmutable $since = null, ?\DateTimeImmutable $until = null): array
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl, '/');
|
||||
$this->logger->debug('GiteaProvider: fetching contributions', ['user' => $this->username, 'url' => $this->baseUrl]);
|
||||
|
||||
$this->logger->debug('GiteaProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);
|
||||
|
||||
return $this->client->request('GET', "$baseUrl/api/v1/users/{$this->username}/heatmap", [
|
||||
$response = $this->client->request('GET', "$this->baseUrl/api/v1/users/{$this->username}/heatmap", [
|
||||
'headers' => ['Authorization' => "token {$this->token}"],
|
||||
]);
|
||||
|
||||
return ['response' => $response, 'since' => $since, 'until' => $until];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the heatmap entries to the [$since, $until] window and sums contributions per day.
|
||||
*
|
||||
* @param array{response: ResponseInterface, since: ?\DateTimeImmutable, until: ?\DateTimeImmutable} $handle
|
||||
* @return array<string, int> date (Y-m-d) => contribution count
|
||||
*/
|
||||
public function resolveFetch(mixed $handle): array
|
||||
{
|
||||
/** @var ResponseInterface $handle */
|
||||
$data = $handle->toArray();
|
||||
$cutoff = (new \DateTimeImmutable('-365 days'))->getTimestamp();
|
||||
['response' => $response, 'since' => $since, 'until' => $until] = $handle;
|
||||
|
||||
/** @var ResponseInterface $response */
|
||||
$data = $response->toArray();
|
||||
$cutoff = ($since ?? new \DateTimeImmutable('-365 days'))->getTimestamp();
|
||||
$ceiling = $until?->getTimestamp();
|
||||
$result = [];
|
||||
|
||||
foreach ($data as $entry) {
|
||||
if ($entry['timestamp'] < $cutoff) {
|
||||
continue;
|
||||
}
|
||||
if ($ceiling !== null && $entry['timestamp'] > $ceiling) {
|
||||
continue;
|
||||
}
|
||||
$date = date('Y-m-d', $entry['timestamp']);
|
||||
$result[$date] = ($result[$date] ?? 0) + (int) $entry['contributions'];
|
||||
}
|
||||
|
||||
@@ -2,13 +2,17 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
trait ProbeTrait
|
||||
{
|
||||
/**
|
||||
* Reports NotConfigured if credentials are missing, otherwise pings the provider
|
||||
* and reports Ok or Error (with a classified error code and message).
|
||||
*/
|
||||
public function probe(): ProviderStatus
|
||||
{
|
||||
if (!$this->isConfigured()) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
enum ProviderErrorCode: string
|
||||
{
|
||||
|
||||
@@ -2,19 +2,23 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
|
||||
|
||||
final class ProviderHealthChecker
|
||||
{
|
||||
/** @param iterable<ProviderInterface> $providers */
|
||||
public function __construct(
|
||||
#[AutowireIterator('app.provider')]
|
||||
private readonly iterable $providers,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{status: string, providers: array<string, array<string, string>>}
|
||||
* Probes every configured provider and aggregates the results for the /health endpoint.
|
||||
*
|
||||
* @return array{status: string, providers: array<string, array<string, string>>} 'status' is
|
||||
* "degraded" if any provider's probe() reported an error, otherwise "ok"
|
||||
*/
|
||||
public function check(): array
|
||||
{
|
||||
|
||||
@@ -2,21 +2,37 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
interface ProviderInterface
|
||||
{
|
||||
/** Fire the HTTP request(s) without blocking; returns an opaque handle for resolveFetch(). */
|
||||
public function startFetch(): mixed;
|
||||
/**
|
||||
* Fire the HTTP request(s) without blocking; the result is read later by
|
||||
* resolveFetch() so multiple providers' requests can be in flight at once.
|
||||
*
|
||||
* @param ?\DateTimeImmutable $since lower bound (inclusive); defaults to 365 days ago
|
||||
* @param ?\DateTimeImmutable $until upper bound (inclusive); defaults to now
|
||||
* @return mixed opaque handle to pass into resolveFetch()
|
||||
*/
|
||||
public function startFetch(?\DateTimeImmutable $since = null, ?\DateTimeImmutable $until = null): mixed;
|
||||
|
||||
/** @return array<string, int> date (Y-m-d) => contribution count */
|
||||
/**
|
||||
* Block on the handle from startFetch() and parse it into a per-day count map.
|
||||
*
|
||||
* @param mixed $handle the value returned by startFetch()
|
||||
* @return array<string, int> date (Y-m-d) => contribution count
|
||||
*/
|
||||
public function resolveFetch(mixed $handle): array;
|
||||
|
||||
/** Whether the required credentials/URL for this provider are all set. */
|
||||
public function isConfigured(): bool;
|
||||
|
||||
/** Short lowercase identifier for this provider, e.g. "github". */
|
||||
public function getName(): string;
|
||||
|
||||
/** Check reachability/credentials without fetching contribution data; used by the /health endpoint. */
|
||||
public function probe(): ProviderStatus;
|
||||
|
||||
/** Make a lightweight authenticated request to verify the provider is reachable; throws on failure. */
|
||||
public function ping(): void;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,17 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
/** Result of probing a single provider, as reported by the /health endpoint. */
|
||||
final class ProviderStatus
|
||||
{
|
||||
/**
|
||||
* @param string $name provider identifier, e.g. "github"
|
||||
* @param ProviderStatusType $status overall outcome of the probe
|
||||
* @param ?ProviderErrorCode $error classified error code, set only when $status is Error
|
||||
* @param ?string $message human-readable error detail, set only when $status is Error
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly string $name,
|
||||
public readonly ProviderStatusType $status,
|
||||
@@ -13,7 +20,11 @@ final class ProviderStatus
|
||||
public readonly ?string $message = null,
|
||||
) {}
|
||||
|
||||
/** @return array<string, string> */
|
||||
/**
|
||||
* Converts to the array shape used in the /health JSON response, omitting error/message when unset.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = ['status' => $this->status->value];
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
namespace GitContributionGraph\Service\Provider;
|
||||
|
||||
enum ProviderStatusType: string
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Renderer;
|
||||
namespace GitContributionGraph\Service\Renderer;
|
||||
|
||||
/**
|
||||
* Renders a GitHub-style contribution heatmap as an inline SVG.
|
||||
@@ -36,6 +36,12 @@ final class SvgRenderer
|
||||
private const MARGIN_Y = 20; // top margin for month labels
|
||||
private const PADDING = 10; // outer padding
|
||||
|
||||
/**
|
||||
* Builds the full self-contained SVG heatmap for a contribution map.
|
||||
*
|
||||
* @param array<string, int> $contributions date (Y-m-d) => contribution count
|
||||
* @param string $theme "dark" or "light"; unknown values fall back to "dark"
|
||||
*/
|
||||
public function render(array $contributions, string $theme = 'dark'): string
|
||||
{
|
||||
$colors = self::THEMES[$theme] ?? self::THEMES['dark'];
|
||||
@@ -91,6 +97,9 @@ final class SvgRenderer
|
||||
/**
|
||||
* Builds a [week][day] grid where each cell is ['date' => 'Y-m-d', 'count' => int]
|
||||
* or null if the date is in the future.
|
||||
*
|
||||
* @param array<string, int> $contributions date (Y-m-d) => contribution count
|
||||
* @return array<int, array<int, array{date: string, count: int}|null>>
|
||||
*/
|
||||
private function buildGrid(\DateTimeImmutable $start, \DateTimeImmutable $today, array $contributions): array
|
||||
{
|
||||
@@ -125,6 +134,10 @@ final class SvgRenderer
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, int> $contributions date (Y-m-d) => contribution count
|
||||
* @return array{total: int}
|
||||
*/
|
||||
private function computeStats(array $contributions): array
|
||||
{
|
||||
return ['total' => array_sum($contributions)];
|
||||
@@ -132,6 +145,7 @@ final class SvgRenderer
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** @param array<int, array<int, array{date: string, count: int}|null>> $grid */
|
||||
private function renderMonthLabels(array $grid, string $textColor): string
|
||||
{
|
||||
$out = '';
|
||||
@@ -142,7 +156,7 @@ final class SvgRenderer
|
||||
if ($cell === null) {
|
||||
continue;
|
||||
}
|
||||
$ts = strtotime($cell['date']);
|
||||
$ts = (int) strtotime($cell['date']);
|
||||
$month = (int) date('n', $ts);
|
||||
$dom = (int) date('j', $ts);
|
||||
|
||||
@@ -183,6 +197,10 @@ final class SvgRenderer
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<int, array{date: string, count: int}|null>> $grid
|
||||
* @param array{bg: string, text: string, levels: array<int, string>} $colors
|
||||
*/
|
||||
private function renderCells(array $grid, array $colors): string
|
||||
{
|
||||
$out = '';
|
||||
@@ -197,7 +215,7 @@ final class SvgRenderer
|
||||
$y = self::MARGIN_Y + $d * self::STEP;
|
||||
$color = $colors['levels'][$this->level($cell['count'])];
|
||||
|
||||
$ts = strtotime($cell['date']);
|
||||
$ts = (int) strtotime($cell['date']);
|
||||
$suffix = $cell['count'] !== 1 ? 's' : '';
|
||||
$label = $cell['count'] > 0
|
||||
? $cell['count'] . ' contribution' . $suffix . ' on ' . date('F j, Y', $ts)
|
||||
@@ -218,6 +236,7 @@ final class SvgRenderer
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** @param array{bg: string, text: string, levels: array<int, string>} $colors */
|
||||
private function renderLegend(int $totalH, array $colors): string
|
||||
{
|
||||
$y = $totalH - 14;
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Tests\Unit\Command;
|
||||
|
||||
use GitContributionGraph\Command\RefetchContributionsCommand;
|
||||
use GitContributionGraph\Service\ContributionStore;
|
||||
use GitContributionGraph\Service\Provider\ProviderInterface;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
#[CoversClass(RefetchContributionsCommand::class)]
|
||||
final class RefetchContributionsCommandTest extends TestCase
|
||||
{
|
||||
/** @param ?array<string, int> $fresh */
|
||||
private function makeProvider(
|
||||
string $name,
|
||||
bool $configured = true,
|
||||
?array $fresh = null,
|
||||
?\Throwable $throws = null,
|
||||
): ProviderInterface {
|
||||
$provider = $this->createStub(ProviderInterface::class);
|
||||
$provider->method('getName')->willReturn($name);
|
||||
$provider->method('isConfigured')->willReturn($configured);
|
||||
|
||||
if ($throws !== null) {
|
||||
$provider->method('startFetch')->willThrowException($throws);
|
||||
} elseif ($fresh !== null) {
|
||||
$provider->method('resolveFetch')->willReturn($fresh);
|
||||
}
|
||||
|
||||
return $provider;
|
||||
}
|
||||
|
||||
/** @param iterable<ProviderInterface> $providers */
|
||||
private function makeTester(iterable $providers, ContributionStore $store): CommandTester
|
||||
{
|
||||
$command = new RefetchContributionsCommand($providers, $store);
|
||||
|
||||
$application = new Application();
|
||||
$application->add($command);
|
||||
|
||||
return new CommandTester($application->find('graph:contributions:refetch'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_refetches_the_default_365_day_window_for_all_configured_providers(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$provider = $this->makeProvider('github', fresh: ['2024-06-10' => 3]);
|
||||
|
||||
$tester = $this->makeTester([$provider], $store);
|
||||
$exitCode = $tester->execute([]);
|
||||
|
||||
$this->assertSame(0, $exitCode);
|
||||
$this->assertCount(1, $store->all('github'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_skips_unconfigured_providers_by_default(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$provider = $this->makeProvider('github', configured: false);
|
||||
|
||||
$tester = $this->makeTester([$provider], $store);
|
||||
$tester->execute([]);
|
||||
|
||||
$this->assertCount(0, $store->all('github'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_fails_cleanly_on_an_unknown_provider_name(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$provider = $this->makeProvider('github');
|
||||
|
||||
$tester = $this->makeTester([$provider], $store);
|
||||
$exitCode = $tester->execute(['--provider' => 'bogus']);
|
||||
|
||||
$this->assertSame(1, $exitCode);
|
||||
$this->assertStringContainsString('Unknown provider: bogus', $tester->getDisplay());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_restricts_to_the_named_provider(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$github = $this->makeProvider('github', fresh: ['2024-06-10' => 1]);
|
||||
$gitlab = $this->makeProvider('gitlab', fresh: ['2024-06-10' => 1]);
|
||||
|
||||
$tester = $this->makeTester([$github, $gitlab], $store);
|
||||
$tester->execute(['--provider' => 'github']);
|
||||
|
||||
$this->assertCount(1, $store->all('github'));
|
||||
$this->assertCount(0, $store->all('gitlab'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_continues_with_remaining_providers_when_one_throws(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$failing = $this->makeProvider('gitlab', throws: new \RuntimeException('boom'));
|
||||
$healthy = $this->makeProvider('github', fresh: ['2024-06-10' => 2]);
|
||||
|
||||
$tester = $this->makeTester([$failing, $healthy], $store);
|
||||
$exitCode = $tester->execute([]);
|
||||
|
||||
$this->assertSame(0, $exitCode);
|
||||
$this->assertCount(1, $store->all('github'));
|
||||
$this->assertCount(0, $store->all('gitlab'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_fails_when_every_provider_errors(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$failing = $this->makeProvider('github', throws: new \RuntimeException('boom'));
|
||||
|
||||
$tester = $this->makeTester([$failing], $store);
|
||||
$exitCode = $tester->execute([]);
|
||||
|
||||
$this->assertSame(1, $exitCode);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_splits_a_multi_year_range_into_365_day_chunks(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$calls = 0;
|
||||
|
||||
$provider = $this->createStub(ProviderInterface::class);
|
||||
$provider->method('getName')->willReturn('github');
|
||||
$provider->method('isConfigured')->willReturn(true);
|
||||
$provider->method('resolveFetch')->willReturnCallback(function () use (&$calls): array {
|
||||
$calls++;
|
||||
|
||||
return [];
|
||||
});
|
||||
|
||||
$tester = $this->makeTester([$provider], $store);
|
||||
$tester->execute(['--from' => '2020-01-01', '--to' => '2023-01-01']);
|
||||
|
||||
$this->assertGreaterThan(1, $calls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Tests\Unit\Entity;
|
||||
|
||||
use GitContributionGraph\Entity\Contribution;
|
||||
use GitContributionGraph\Entity\ContributionCollection;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ContributionCollectionTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function it_counts_zero_when_empty(): void
|
||||
{
|
||||
$collection = new ContributionCollection();
|
||||
|
||||
$this->assertCount(0, $collection);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_counts_the_contributions_it_wraps(): void
|
||||
{
|
||||
$collection = new ContributionCollection(
|
||||
new Contribution('github', 1_700_000_000, 1),
|
||||
new Contribution('github', 1_700_086_400, 2),
|
||||
);
|
||||
|
||||
$this->assertCount(2, $collection);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_is_iterable_over_its_contributions(): void
|
||||
{
|
||||
$first = new Contribution('github', 1_700_000_000, 1);
|
||||
$second = new Contribution('gitlab', 1_700_086_400, 2);
|
||||
|
||||
$collection = new ContributionCollection($first, $second);
|
||||
|
||||
$this->assertSame([$first, $second], iterator_to_array($collection));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Tests\Unit\Entity;
|
||||
|
||||
use GitContributionGraph\Entity\Contribution;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ContributionTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function it_exposes_provider_date_and_count(): void
|
||||
{
|
||||
$contribution = new Contribution('github', 1_700_000_000, 4);
|
||||
|
||||
$this->assertSame('github', $contribution->provider);
|
||||
$this->assertSame(1_700_000_000, $contribution->date);
|
||||
$this->assertSame(4, $contribution->count);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service;
|
||||
namespace GitContributionGraph\Tests\Unit\Service;
|
||||
|
||||
use App\Service\ContributionAggregator;
|
||||
use App\Service\Provider\ProviderInterface;
|
||||
use GitContributionGraph\Service\ContributionAggregator;
|
||||
use GitContributionGraph\Service\ContributionStore;
|
||||
use GitContributionGraph\Service\Provider\ProviderInterface;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -15,16 +16,31 @@ use Psr\Log\LoggerInterface;
|
||||
final class ContributionAggregatorTest extends TestCase
|
||||
{
|
||||
private LoggerInterface $logger;
|
||||
private ContributionStore $store;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->logger = $this->createStub(LoggerInterface::class);
|
||||
$this->store = new ContributionStore(':memory:');
|
||||
}
|
||||
|
||||
/** @param ?array<string, int> $fresh */
|
||||
private function makeProvider(string $name, bool $configured = true, ?array $fresh = null): ProviderInterface
|
||||
{
|
||||
$provider = $this->createStub(ProviderInterface::class);
|
||||
$provider->method('getName')->willReturn($name);
|
||||
$provider->method('isConfigured')->willReturn($configured);
|
||||
if ($fresh !== null) {
|
||||
$provider->method('resolveFetch')->willReturn($fresh);
|
||||
}
|
||||
|
||||
return $provider;
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_empty_array_when_no_providers_are_given(): void
|
||||
{
|
||||
$aggregator = new ContributionAggregator([], $this->logger);
|
||||
$aggregator = new ContributionAggregator([], $this->store, $this->logger);
|
||||
|
||||
$result = $aggregator->aggregate();
|
||||
|
||||
@@ -34,10 +50,9 @@ final class ContributionAggregatorTest extends TestCase
|
||||
#[Test]
|
||||
public function it_skips_unconfigured_providers(): void
|
||||
{
|
||||
$provider = $this->createStub(ProviderInterface::class);
|
||||
$provider->method('isConfigured')->willReturn(false);
|
||||
$provider = $this->makeProvider('github', configured: false);
|
||||
|
||||
$aggregator = new ContributionAggregator([$provider], $this->logger);
|
||||
$aggregator = new ContributionAggregator([$provider], $this->store, $this->logger);
|
||||
|
||||
$result = $aggregator->aggregate();
|
||||
|
||||
@@ -47,69 +62,68 @@ final class ContributionAggregatorTest extends TestCase
|
||||
#[Test]
|
||||
public function it_returns_contributions_from_a_configured_provider(): void
|
||||
{
|
||||
$provider = $this->createStub(ProviderInterface::class);
|
||||
$provider->method('isConfigured')->willReturn(true);
|
||||
$provider->method('resolveFetch')->willReturn(['2024-01-01' => 3]);
|
||||
$date = (new \DateTimeImmutable('-1 day'))->format('Y-m-d');
|
||||
$provider = $this->makeProvider('github', fresh: [$date => 3]);
|
||||
|
||||
$aggregator = new ContributionAggregator([$provider], $this->logger);
|
||||
$aggregator = new ContributionAggregator([$provider], $this->store, $this->logger);
|
||||
|
||||
$result = $aggregator->aggregate();
|
||||
|
||||
$this->assertSame(['2024-01-01' => 3], $result);
|
||||
$this->assertSame([$date => 3], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_sums_contributions_from_multiple_providers_on_the_same_date(): void
|
||||
{
|
||||
$providerA = $this->createStub(ProviderInterface::class);
|
||||
$providerA->method('isConfigured')->willReturn(true);
|
||||
$providerA->method('resolveFetch')->willReturn(['2024-01-01' => 3, '2024-01-02' => 1]);
|
||||
$dateA = (new \DateTimeImmutable('-1 day'))->format('Y-m-d');
|
||||
$dateB = (new \DateTimeImmutable('-2 days'))->format('Y-m-d');
|
||||
|
||||
$providerB = $this->createStub(ProviderInterface::class);
|
||||
$providerB->method('isConfigured')->willReturn(true);
|
||||
$providerB->method('resolveFetch')->willReturn(['2024-01-01' => 2, '2024-01-03' => 5]);
|
||||
$providerA = $this->makeProvider('github', fresh: [$dateA => 3, $dateB => 1]);
|
||||
$providerB = $this->makeProvider('gitlab', fresh: [$dateA => 2]);
|
||||
|
||||
$aggregator = new ContributionAggregator([$providerA, $providerB], $this->logger);
|
||||
$aggregator = new ContributionAggregator([$providerA, $providerB], $this->store, $this->logger);
|
||||
|
||||
$result = $aggregator->aggregate();
|
||||
|
||||
$this->assertSame(['2024-01-01' => 5, '2024-01-02' => 1, '2024-01-03' => 5], $result);
|
||||
$this->assertSame([$dateB => 1, $dateA => 5], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_continues_fetching_remaining_providers_when_one_throws_on_start(): void
|
||||
{
|
||||
$date = (new \DateTimeImmutable('-1 day'))->format('Y-m-d');
|
||||
|
||||
$failing = $this->createStub(ProviderInterface::class);
|
||||
$failing->method('getName')->willReturn('gitlab');
|
||||
$failing->method('isConfigured')->willReturn(true);
|
||||
$failing->method('startFetch')->willThrowException(new \RuntimeException('Network error'));
|
||||
|
||||
$healthy = $this->createStub(ProviderInterface::class);
|
||||
$healthy->method('isConfigured')->willReturn(true);
|
||||
$healthy->method('resolveFetch')->willReturn(['2024-01-01' => 7]);
|
||||
$healthy = $this->makeProvider('github', fresh: [$date => 7]);
|
||||
|
||||
$aggregator = new ContributionAggregator([$failing, $healthy], $this->logger);
|
||||
$aggregator = new ContributionAggregator([$failing, $healthy], $this->store, $this->logger);
|
||||
|
||||
$result = $aggregator->aggregate();
|
||||
|
||||
$this->assertSame(['2024-01-01' => 7], $result);
|
||||
$this->assertSame([$date => 7], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_continues_fetching_remaining_providers_when_one_throws_on_resolve(): void
|
||||
{
|
||||
$date = (new \DateTimeImmutable('-1 day'))->format('Y-m-d');
|
||||
|
||||
$failing = $this->createStub(ProviderInterface::class);
|
||||
$failing->method('getName')->willReturn('gitlab');
|
||||
$failing->method('isConfigured')->willReturn(true);
|
||||
$failing->method('resolveFetch')->willThrowException(new \RuntimeException('Network error'));
|
||||
|
||||
$healthy = $this->createStub(ProviderInterface::class);
|
||||
$healthy->method('isConfigured')->willReturn(true);
|
||||
$healthy->method('resolveFetch')->willReturn(['2024-01-01' => 7]);
|
||||
$healthy = $this->makeProvider('github', fresh: [$date => 7]);
|
||||
|
||||
$aggregator = new ContributionAggregator([$failing, $healthy], $this->logger);
|
||||
$aggregator = new ContributionAggregator([$failing, $healthy], $this->store, $this->logger);
|
||||
|
||||
$result = $aggregator->aggregate();
|
||||
|
||||
$this->assertSame(['2024-01-01' => 7], $result);
|
||||
$this->assertSame([$date => 7], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
@@ -119,10 +133,11 @@ final class ContributionAggregatorTest extends TestCase
|
||||
$logger->expects($this->once())->method('warning');
|
||||
|
||||
$provider = $this->createStub(ProviderInterface::class);
|
||||
$provider->method('getName')->willReturn('github');
|
||||
$provider->method('isConfigured')->willReturn(true);
|
||||
$provider->method('startFetch')->willThrowException(new \RuntimeException('fail'));
|
||||
|
||||
(new ContributionAggregator([$provider], $logger))->aggregate();
|
||||
(new ContributionAggregator([$provider], $this->store, $logger))->aggregate();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
@@ -132,9 +147,77 @@ final class ContributionAggregatorTest extends TestCase
|
||||
$logger->expects($this->once())->method('warning');
|
||||
|
||||
$provider = $this->createStub(ProviderInterface::class);
|
||||
$provider->method('getName')->willReturn('github');
|
||||
$provider->method('isConfigured')->willReturn(true);
|
||||
$provider->method('resolveFetch')->willThrowException(new \RuntimeException('fail'));
|
||||
|
||||
(new ContributionAggregator([$provider], $logger))->aggregate();
|
||||
(new ContributionAggregator([$provider], $this->store, $logger))->aggregate();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_consults_latest_date_and_narrows_the_since_window_by_three_days(): void
|
||||
{
|
||||
$latest = (new \DateTimeImmutable('-30 days'))->getTimestamp();
|
||||
$this->store->add('github', $latest, 1);
|
||||
|
||||
$provider = $this->createMock(ProviderInterface::class);
|
||||
$provider->method('getName')->willReturn('github');
|
||||
$provider->method('isConfigured')->willReturn(true);
|
||||
$provider->expects($this->once())
|
||||
->method('startFetch')
|
||||
->with($this->callback(
|
||||
fn (?\DateTimeImmutable $since): bool => $since !== null
|
||||
&& $since->getTimestamp() === $latest - 3 * 86400
|
||||
))
|
||||
->willReturn(null);
|
||||
$provider->method('resolveFetch')->willReturn([]);
|
||||
|
||||
(new ContributionAggregator([$provider], $this->store, $this->logger))->aggregate();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_merges_freshly_fetched_contributions_into_the_store(): void
|
||||
{
|
||||
$date = (new \DateTimeImmutable('-1 day'))->format('Y-m-d');
|
||||
$provider = $this->makeProvider('github', fresh: [$date => 4]);
|
||||
|
||||
(new ContributionAggregator([$provider], $this->store, $this->logger))->aggregate();
|
||||
|
||||
$stored = iterator_to_array($this->store->all('github'));
|
||||
$this->assertCount(1, $stored);
|
||||
$this->assertSame(4, $stored[0]->count);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_prunes_the_store_once_per_aggregate_call(): void
|
||||
{
|
||||
$store = $this->createMock(ContributionStore::class);
|
||||
$store->method('latestDate')->willReturn(null);
|
||||
$store->method('all')->willReturn(new \GitContributionGraph\Entity\ContributionCollection());
|
||||
$store->expects($this->once())->method('prune');
|
||||
|
||||
$provider = $this->makeProvider('github', fresh: []);
|
||||
|
||||
(new ContributionAggregator([$provider], $store, $this->logger))->aggregate();
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_leaves_other_providers_stored_data_intact_when_one_provider_fails(): void
|
||||
{
|
||||
$date = (new \DateTimeImmutable('-1 day'))->getTimestamp();
|
||||
$this->store->add('gitlab', $date, 9);
|
||||
|
||||
$failing = $this->createStub(ProviderInterface::class);
|
||||
$failing->method('getName')->willReturn('gitlab');
|
||||
$failing->method('isConfigured')->willReturn(true);
|
||||
$failing->method('startFetch')->willThrowException(new \RuntimeException('Network error'));
|
||||
|
||||
$healthy = $this->makeProvider('github', fresh: []);
|
||||
|
||||
(new ContributionAggregator([$failing, $healthy], $this->store, $this->logger))->aggregate();
|
||||
|
||||
$stored = iterator_to_array($this->store->all('gitlab'));
|
||||
$this->assertCount(1, $stored);
|
||||
$this->assertSame(9, $stored[0]->count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GitContributionGraph\Tests\Unit\Service;
|
||||
|
||||
use GitContributionGraph\Service\ContributionStore;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ContributionStoreTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function it_stores_and_retrieves_a_contribution(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
|
||||
$store->add('github', 1_700_000_000, 4);
|
||||
$all = $store->all('github');
|
||||
|
||||
$this->assertCount(1, $all);
|
||||
$this->assertSame(4, iterator_to_array($all)[0]->count);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_upserts_on_a_repeated_date(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
|
||||
$store->add('github', 1_700_000_000, 4);
|
||||
$store->add('github', 1_700_000_000, 9);
|
||||
$all = $store->all('github');
|
||||
|
||||
$this->assertCount(1, $all);
|
||||
$this->assertSame(9, iterator_to_array($all)[0]->count);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_removes_a_contribution(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
|
||||
$store->add('github', 1_700_000_000, 4);
|
||||
$store->remove('github', 1_700_000_000);
|
||||
|
||||
$this->assertCount(0, $store->all('github'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_merges_a_batch_of_date_counts(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
|
||||
$store->merge('github', [1_700_000_000 => 1, 1_700_086_400 => 2]);
|
||||
|
||||
$this->assertCount(2, $store->all('github'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_reports_the_latest_date_for_a_provider(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
|
||||
$store->add('github', 1_700_000_000, 1);
|
||||
$store->add('github', 1_700_086_400, 2);
|
||||
|
||||
$this->assertSame(1_700_086_400, $store->latestDate('github'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_returns_null_latest_date_when_provider_has_no_rows(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
|
||||
$this->assertNull($store->latestDate('github'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_filters_all_by_since_days(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:');
|
||||
$now = time();
|
||||
|
||||
$store->add('github', $now - 10 * 86400, 1);
|
||||
$store->add('github', $now - 400 * 86400, 2);
|
||||
|
||||
$this->assertCount(1, $store->all('github', sinceDays: 30));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_does_not_prune_when_retention_is_unset(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:', retentionDays: null);
|
||||
|
||||
$store->add('github', time() - 1_000 * 86400, 1);
|
||||
$store->prune();
|
||||
|
||||
$this->assertCount(1, $store->all('github'));
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_prunes_rows_older_than_the_retention_window(): void
|
||||
{
|
||||
$store = new ContributionStore(':memory:', retentionDays: 30);
|
||||
$now = time();
|
||||
|
||||
$store->add('github', $now - 10 * 86400, 1);
|
||||
$store->add('github', $now - 40 * 86400, 2);
|
||||
$store->prune();
|
||||
|
||||
$this->assertCount(1, $store->all('github'));
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service\Provider;
|
||||
namespace GitContributionGraph\Tests\Unit\Service\Provider;
|
||||
|
||||
use App\Service\Provider\GitHubProvider;
|
||||
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClient;
|
||||
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface;
|
||||
use IDCI\Bundle\GraphQLClientBundle\Query\GraphQLQuery;
|
||||
use GitContributionGraph\Service\Provider\GitHubProvider;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -23,28 +20,16 @@ final class GitHubProviderTest extends TestCase
|
||||
string $username = 'user',
|
||||
string $token = 'token',
|
||||
?HttpClientInterface $client = null,
|
||||
?GraphQLApiClientRegistryInterface $registry = null,
|
||||
): GitHubProvider {
|
||||
if ($registry === null) {
|
||||
$graphqlQuery = $this->createStub(GraphQLQuery::class);
|
||||
$graphqlQuery->method('getGraphQLQuery')->willReturn('query {}');
|
||||
|
||||
$graphqlClient = $this->createStub(GraphQLApiClient::class);
|
||||
$graphqlClient->method('buildQuery')->willReturn($graphqlQuery);
|
||||
|
||||
$registry = $this->createStub(GraphQLApiClientRegistryInterface::class);
|
||||
$registry->method('get')->willReturn($graphqlClient);
|
||||
}
|
||||
|
||||
return new GitHubProvider(
|
||||
$client ?? $this->createStub(HttpClientInterface::class),
|
||||
$registry,
|
||||
$username,
|
||||
$token,
|
||||
$this->createStub(LoggerInterface::class),
|
||||
);
|
||||
}
|
||||
|
||||
/** @param array<int, array{contributionDays: array<int, array{date: string, contributionCount: int}>}> $weeks */
|
||||
private function stubGraphqlResponse(array $weeks): ResponseInterface
|
||||
{
|
||||
$response = $this->createStub(ResponseInterface::class);
|
||||
@@ -147,4 +132,27 @@ final class GitHubProviderTest extends TestCase
|
||||
|
||||
$this->assertSame([], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_narrows_the_graphql_query_window_when_since_and_until_are_given(): void
|
||||
{
|
||||
$since = new \DateTimeImmutable('2024-01-01');
|
||||
$until = new \DateTimeImmutable('2024-01-31');
|
||||
$capturedQuery = null;
|
||||
|
||||
$client = $this->createMock(HttpClientInterface::class);
|
||||
$client->method('request')->willReturnCallback(
|
||||
function (string $method, string $url, array $options) use (&$capturedQuery): ResponseInterface {
|
||||
$capturedQuery = $options['json']['query'];
|
||||
|
||||
return $this->stubGraphqlResponse([]);
|
||||
}
|
||||
);
|
||||
|
||||
$provider = $this->makeProvider(client: $client);
|
||||
$provider->startFetch($since, $until);
|
||||
|
||||
$this->assertStringContainsString('2024-01-01T00:00:00Z', $capturedQuery);
|
||||
$this->assertStringContainsString('2024-01-31T23:59:59Z', $capturedQuery);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service\Provider;
|
||||
namespace GitContributionGraph\Tests\Unit\Service\Provider;
|
||||
|
||||
use App\Service\Provider\GitLabProvider;
|
||||
use GitContributionGraph\Service\Provider\GitLabProvider;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -31,6 +31,7 @@ final class GitLabProviderTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/** @param array<int, array<string, int|string>> $data raw JSON body, e.g. events or the user lookup */
|
||||
private function stubResponse(array $data): ResponseInterface
|
||||
{
|
||||
$response = $this->createStub(ResponseInterface::class);
|
||||
@@ -128,4 +129,27 @@ final class GitLabProviderTest extends TestCase
|
||||
$this->assertSame(100, $result['2024-06-10']);
|
||||
$this->assertSame(1, $result['2024-06-11']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_sends_after_and_before_query_params_when_since_and_until_are_given(): void
|
||||
{
|
||||
$capturedQuery = null;
|
||||
|
||||
$client = $this->createMock(HttpClientInterface::class);
|
||||
$client->method('request')->willReturnCallback(
|
||||
function (string $method, string $url, array $options = []) use (&$capturedQuery): ResponseInterface {
|
||||
if (str_contains($url, '/events')) {
|
||||
$capturedQuery = $options['query'];
|
||||
}
|
||||
|
||||
return $this->stubResponse(str_contains($url, '/events') ? [] : [['id' => 42]]);
|
||||
}
|
||||
);
|
||||
|
||||
$provider = $this->makeProvider(client: $client);
|
||||
$provider->startFetch(new \DateTimeImmutable('2024-01-01'), new \DateTimeImmutable('2024-01-31'));
|
||||
|
||||
$this->assertSame('2024-01-01', $capturedQuery['after']);
|
||||
$this->assertSame('2024-01-31', $capturedQuery['before']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service\Provider;
|
||||
namespace GitContributionGraph\Tests\Unit\Service\Provider;
|
||||
|
||||
use App\Service\Provider\GiteaProvider;
|
||||
use GitContributionGraph\Service\Provider\GiteaProvider;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -30,6 +30,7 @@ final class GiteaProviderTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/** @param array<int, array{timestamp: int, contributions: int}> $data */
|
||||
private function stubResponse(array $data): ResponseInterface
|
||||
{
|
||||
$response = $this->createStub(ResponseInterface::class);
|
||||
@@ -111,4 +112,44 @@ final class GiteaProviderTest extends TestCase
|
||||
|
||||
$this->assertSame([], $result);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_filters_out_entries_before_since(): void
|
||||
{
|
||||
$since = new \DateTimeImmutable('2024-01-10');
|
||||
$before = $since->modify('-1 day')->getTimestamp();
|
||||
$after = $since->modify('+1 day')->getTimestamp();
|
||||
|
||||
$client = $this->createStub(HttpClientInterface::class);
|
||||
$client->method('request')->willReturn($this->stubResponse([
|
||||
['timestamp' => $before, 'contributions' => 3],
|
||||
['timestamp' => $after, 'contributions' => 2],
|
||||
]));
|
||||
|
||||
$provider = $this->makeProvider(client: $client);
|
||||
$result = $provider->resolveFetch($provider->startFetch($since));
|
||||
|
||||
$this->assertArrayNotHasKey(date('Y-m-d', $before), $result);
|
||||
$this->assertSame(2, $result[date('Y-m-d', $after)]);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_filters_out_entries_after_until(): void
|
||||
{
|
||||
$until = new \DateTimeImmutable('-10 days');
|
||||
$before = $until->modify('-1 day')->getTimestamp();
|
||||
$after = $until->modify('+1 day')->getTimestamp();
|
||||
|
||||
$client = $this->createStub(HttpClientInterface::class);
|
||||
$client->method('request')->willReturn($this->stubResponse([
|
||||
['timestamp' => $before, 'contributions' => 3],
|
||||
['timestamp' => $after, 'contributions' => 2],
|
||||
]));
|
||||
|
||||
$provider = $this->makeProvider(client: $client);
|
||||
$result = $provider->resolveFetch($provider->startFetch(null, $until));
|
||||
|
||||
$this->assertSame(3, $result[date('Y-m-d', $before)]);
|
||||
$this->assertArrayNotHasKey(date('Y-m-d', $after), $result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service\Provider;
|
||||
namespace GitContributionGraph\Tests\Unit\Service\Provider;
|
||||
|
||||
use App\Service\Provider\ProbeTrait;
|
||||
use App\Service\Provider\ProviderErrorCode;
|
||||
use App\Service\Provider\ProviderInterface;
|
||||
use App\Service\Provider\ProviderStatusType;
|
||||
use GitContributionGraph\Service\Provider\ProbeTrait;
|
||||
use GitContributionGraph\Service\Provider\ProviderErrorCode;
|
||||
use GitContributionGraph\Service\Provider\ProviderInterface;
|
||||
use GitContributionGraph\Service\Provider\ProviderStatusType;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -31,7 +31,7 @@ final class ProbeTraitTest extends TestCase
|
||||
public function isConfigured(): bool { return $this->configured; }
|
||||
public function getName(): string { return 'test'; }
|
||||
public function ping(): void { ($this->ping)(); }
|
||||
public function startFetch(): mixed { return null; }
|
||||
public function startFetch(?\DateTimeImmutable $since = null, ?\DateTimeImmutable $until = null): mixed { return null; }
|
||||
public function resolveFetch(mixed $handle): array { return []; }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service\Provider;
|
||||
namespace GitContributionGraph\Tests\Unit\Service\Provider;
|
||||
|
||||
use App\Service\Provider\ProviderErrorCode;
|
||||
use App\Service\Provider\ProviderHealthChecker;
|
||||
use App\Service\Provider\ProviderInterface;
|
||||
use App\Service\Provider\ProviderStatus;
|
||||
use App\Service\Provider\ProviderStatusType;
|
||||
use GitContributionGraph\Service\Provider\ProviderErrorCode;
|
||||
use GitContributionGraph\Service\Provider\ProviderHealthChecker;
|
||||
use GitContributionGraph\Service\Provider\ProviderInterface;
|
||||
use GitContributionGraph\Service\Provider\ProviderStatus;
|
||||
use GitContributionGraph\Service\Provider\ProviderStatusType;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service\Provider;
|
||||
namespace GitContributionGraph\Tests\Unit\Service\Provider;
|
||||
|
||||
use App\Service\Provider\ProviderErrorCode;
|
||||
use App\Service\Provider\ProviderStatus;
|
||||
use App\Service\Provider\ProviderStatusType;
|
||||
use GitContributionGraph\Service\Provider\ProviderErrorCode;
|
||||
use GitContributionGraph\Service\Provider\ProviderStatus;
|
||||
use GitContributionGraph\Service\Provider\ProviderStatusType;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Unit\Service\Renderer;
|
||||
namespace GitContributionGraph\Tests\Unit\Service\Renderer;
|
||||
|
||||
use App\Service\Renderer\SvgRenderer;
|
||||
use GitContributionGraph\Service\Renderer\SvgRenderer;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
@@ -54,6 +54,7 @@ final class SvgRendererTest extends TestCase
|
||||
$this->assertStringContainsString($expectedColor, $svg);
|
||||
}
|
||||
|
||||
/** @return iterable<string, array{string, string}> */
|
||||
public static function theme_background_provider(): iterable
|
||||
{
|
||||
yield 'dark theme' => ['dark', '#0d1117'];
|
||||
@@ -97,6 +98,7 @@ final class SvgRendererTest extends TestCase
|
||||
$this->assertStringContainsString('>' . $label . '<', $svg);
|
||||
}
|
||||
|
||||
/** @return iterable<string, array{string}> */
|
||||
public static function day_of_week_label_provider(): iterable
|
||||
{
|
||||
yield 'Monday' => ['Mon'];
|
||||
|
||||
Reference in New Issue
Block a user