From b971e4d1a784a45cef5f872e24829525c7e26d11 Mon Sep 17 00:00:00 2001 From: Kjell Kind Date: Wed, 28 Oct 2026 21:09:26 +0100 Subject: [PATCH 1/2] feat: git blame achievement for a commit in the /dev/null repo Unlocks when the user's GitHub account has at least one commit in the repository, as GitHub's contributor list counts it. Achievements only read the cached list, which the scheduler refreshes every half hour, so a profile or a commit never waits on GitHub. Co-Authored-By: Claude Opus 5.5 --- KNOWLEDGE_BASE.md | 5 +- app/Enums/Achievement.php | 5 + app/Queries/ContributorsQuery.php | 118 ++++++++++++------ app/Queries/UserAchievementsQuery.php | 7 +- .../js/components/achievements-dialog.tsx | 2 + .../js/components/achievements-shelf.tsx | 2 + routes/console.php | 4 + tests/Unit/Queries/ContributorsQueryTest.php | 56 +++++++++ .../Queries/UserAchievementsQueryTest.php | 36 +++++- 9 files changed, 190 insertions(+), 45 deletions(-) diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 26b649c54..667f5dc3a 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -125,7 +125,7 @@ Sources: [go-live menu](resources/js/components/profile-actions-menu.tsx), [live ### Achievement badges and XP -Profiles have **15 achievement badges**. Earned badges appear in a single row, with **+N more** when more have been earned than fit. Hover over the row on desktop or tap it on mobile to toggle a popover with every badge, including locked badges and their progress. Profiles with no earned badges show **View achievements** instead. Select a badge in the popover, or the profile's rank or unlocked count, to open the detailed achievements dialog. +Profiles have **16 achievement badges**. Earned badges appear in a single row, with **+N more** when more have been earned than fit. Hover over the row on desktop or tap it on mobile to toggle a popover with every badge, including locked badges and their progress. Profiles with no earned badges show **View achievements** instead. Select a badge in the popover, or the profile's rank or unlocked count, to open the detailed achievements dialog. | Badge | When it unlocks | | --- | --- | @@ -144,10 +144,11 @@ Profiles have **15 achievement badges**. Earned badges appear in a single row, w | **luddite** | Stop following Tom while he still follows you. This deducts **100 XP**; following him again removes the badge and penalty. Accounts Tom has never befriended do not qualify. | | **shell shocked** | “Survived the php2shell downtime of Oct '26. Earns 100 XP.” — accounts created before **October 21, 2026 at 14:00 UTC** receive this badge and **100 bonus XP**. Accounts created at or after that moment do not qualify. | | **super-saiyan** | Have **9,000 followers or more** on /dev/null. “Y—your follower count. It... it’s over 9000!!!1!” | +| **git blame** | Have **at least one commit** in the [/dev/null repository](https://github.com/dev-null-sh/dev-null.sh) on GitHub, matched by your GitHub account rather than your handle. “Landed a commit in the /dev/null repo itself. git blame now points at you.” It uses the same contributor list as the About page, so it can take an hour (and however long GitHub takes to update its counts) to appear after a merge, and it is locked while GitHub cannot be reached. | Commit counts include replies and quotes. Commit-based badges and XP use non-deleted commits visible to the person viewing the profile, so an author may see progress from pending or rejected commits that other viewers cannot see. Streak badges use the **longest** streak, not the current streak: missing a day does not by itself remove them. Days follow the activity graph's app-timezone grouping, not the user's selected display timezone. -Badges are calculated from current account data, not permanently recorded awards. Removing qualifying activity can reduce progress or lock a badge again; for example, unstarring a commit can remove **stargazer**. **Shell shocked** depends only on when the account was created, so deleting activity does not remove it. When a commit posted on the web unlocks a badge, a toast names it and confetti drops; clicking an unlocked badge drops confetti again. Unlocking a badge does not normally give bonus XP; **shell shocked** contributes **100 XP** once. XP otherwise comes from **10 per commit**, **15 per day in the longest streak**, **5 per star received**, **5 per reply to another developer**, and **2 per star given to another developer**. The **luddite** penalty applies once while the badge is unlocked, not once per unfollow, and total XP cannot fall below zero. A qualifying reply contributes both commit XP and reply XP; pastéis do not add XP. +Badges are calculated from current account data, not permanently recorded awards. Removing qualifying activity can reduce progress or lock a badge again; for example, unstarring a commit can remove **stargazer**. **Shell shocked** depends only on when the account was created, so deleting activity does not remove it. **Git blame** depends only on GitHub's contributor list, not on anything posted on /dev/null. When a commit posted on the web unlocks a badge, a toast names it and confetti drops; clicking an unlocked badge drops confetti again. Unlocking a badge does not normally give bonus XP; **shell shocked** contributes **100 XP** once. XP otherwise comes from **10 per commit**, **15 per day in the longest streak**, **5 per star received**, **5 per reply to another developer**, and **2 per star given to another developer**. The **luddite** penalty applies once while the badge is unlocked, not once per unfollow, and total XP cannot fall below zero. A qualifying reply contributes both commit XP and reply XP; pastéis do not add XP. XP also determines your Unix rank: **user** at 0 XP, **daemon** at 50, **operator** at 150, **maintainer** at 400, **wheel** at 1,000, and **root** at 2,500. The achievements dialog's **Ranks** tab shows the levels and progress. Level-up notifications use your public progress, so pending or rejected commits do not trigger them. Each level above the starting rank can notify you only once, even if you lose and regain the XP; jumping ahead does not send a notification for every skipped level. diff --git a/app/Enums/Achievement.php b/app/Enums/Achievement.php index 8a878196c..2359a4111 100644 --- a/app/Enums/Achievement.php +++ b/app/Enums/Achievement.php @@ -21,6 +21,7 @@ enum Achievement: string case Luddite = 'luddite'; case ShellShocked = 'shell_shocked'; case SuperSaiyan = 'super_saiyan'; + case GitBlame = 'git_blame'; public function label(): string { @@ -40,6 +41,7 @@ public function label(): string self::Luddite => 'luddite', self::ShellShocked => 'shell shocked', self::SuperSaiyan => 'super-saiyan', + self::GitBlame => 'git blame', }; } @@ -61,6 +63,7 @@ public function description(): string self::Luddite => 'Unfollowed Tom. Costs 100 XP until you follow him again.', self::ShellShocked => "Survived the php2shell downtime of Oct '26. Earns 100 XP.", self::SuperSaiyan => 'Y—your follower count. It... it’s over 9000!!!1!', + self::GitBlame => 'Landed a commit in the /dev/null repo itself. git blame now points at you.', }; } @@ -82,6 +85,7 @@ public function icon(): string self::Luddite => 'bot-off', self::ShellShocked => 'bomb', self::SuperSaiyan => 'dragon-ball', + self::GitBlame => 'git-commit-horizontal', }; } @@ -103,6 +107,7 @@ public function target(): int self::Luddite => 1, self::ShellShocked => 1, self::SuperSaiyan => 9000, + self::GitBlame => 1, }; } } diff --git a/app/Queries/ContributorsQuery.php b/app/Queries/ContributorsQuery.php index 5bbaba6d4..1484c49b7 100644 --- a/app/Queries/ContributorsQuery.php +++ b/app/Queries/ContributorsQuery.php @@ -60,6 +60,31 @@ public function get(): ?array ], $accounts); } + /** + * How many commits the user's GitHub account has in the repository, as GitHub counted them when the list was last + * read. It only reads the cache, which the scheduler keeps warm, so a profile or a commit never waits on GitHub. + * Zero when the account has none, or the list hasn't been read. + */ + public function commits(User $user): int + { + /** @var list|false|null $accounts */ + $accounts = Cache::get($this->key()); + + return array_column(is_array($accounts) ? $accounts : [], 'commits', 'github_id')[$user->github_id] ?? 0; + } + + /** + * Reads the list from GitHub again before the cached one expires. A failed read keeps the list there is. + */ + public function refresh(): void + { + $accounts = $this->fetch(); + + if ($accounts !== null) { + Cache::put($this->key(), $accounts, self::Ttl); + } + } + /** * The GitHub accounts behind the contributors. A failed read is kept for a minute, so an outage doesn't send every * visitor to GitHub. @@ -68,53 +93,64 @@ public function get(): ?array */ private function accounts(): ?array { - $repository = config()->string('services.github.repository'); - $key = "about:contributor-accounts:{$repository}"; - /** @var list|false|null $contributors */ - $contributors = Cache::get($key); + $contributors = Cache::get($this->key()); if ($contributors === null) { - $contributors = []; - - try { - for ($page = 1; $page <= self::MaxPages; $page++) { - $accounts = $this->github->request()->get("repos/{$repository}/contributors", [ - 'per_page' => self::PerPage, - 'page' => $page, - ])->throw()->collect(); - - if ($accounts->isNotEmpty() && ! array_is_list($accounts->all())) { - $contributors = false; - - break; - } - - $contributors = [...$contributors, ...$accounts->where('type', 'User') - ->mapInto(Fluent::class) - ->filter(fn (Fluent $account): bool => $account->string('login')->isNotEmpty() - && is_int($account->get('id')) - && is_int($account->get('contributions')) - && ! in_array($account->string('login')->lower()->value(), self::Agents, true)) - ->map(fn (Fluent $account): array => [ - 'login' => $account->string('login')->value(), - 'github_id' => (string) $account->integer('id'), - 'commits' => $account->integer('contributions'), - ]) - ->values() - ->all()]; - - if ($accounts->count() < self::PerPage) { - break; - } - } - } catch (HttpClientException) { - $contributors = false; - } + $contributors = $this->fetch() ?? false; - Cache::put($key, $contributors, $contributors === false ? self::FailedTtl : self::Ttl); + Cache::put($this->key(), $contributors, $contributors === false ? self::FailedTtl : self::Ttl); } return $contributors === false ? null : $contributors; } + + /** + * @return list|null + */ + private function fetch(): ?array + { + $repository = config()->string('services.github.repository'); + $contributors = []; + + try { + for ($page = 1; $page <= self::MaxPages; $page++) { + $accounts = $this->github->request()->get("repos/{$repository}/contributors", [ + 'per_page' => self::PerPage, + 'page' => $page, + ])->throw()->collect(); + + if ($accounts->isNotEmpty() && ! array_is_list($accounts->all())) { + return null; + } + + $contributors = [...$contributors, ...$accounts->where('type', 'User') + ->mapInto(Fluent::class) + ->filter(fn (Fluent $account): bool => $account->string('login')->isNotEmpty() + && is_int($account->get('id')) + && is_int($account->get('contributions')) + && ! in_array($account->string('login')->lower()->value(), self::Agents, true)) + ->map(fn (Fluent $account): array => [ + 'login' => $account->string('login')->value(), + 'github_id' => (string) $account->integer('id'), + 'commits' => $account->integer('contributions'), + ]) + ->values() + ->all()]; + + if ($accounts->count() < self::PerPage) { + break; + } + } + } catch (HttpClientException) { + return null; + } + + return $contributors; + } + + private function key(): string + { + return 'about:contributor-accounts:'.config()->string('services.github.repository'); + } } diff --git a/app/Queries/UserAchievementsQuery.php b/app/Queries/UserAchievementsQuery.php index 0c27f7ef7..2029c081d 100644 --- a/app/Queries/UserAchievementsQuery.php +++ b/app/Queries/UserAchievementsQuery.php @@ -35,7 +35,7 @@ */ final readonly class UserAchievementsQuery { - public function __construct(private UserStreakQuery $streaks) + public function __construct(private UserStreakQuery $streaks, private ContributorsQuery $contributors) { // } @@ -90,6 +90,7 @@ public function get(User $user, ?User $viewer, ?array $streaks = null): array $hasUnfollowedTom = $user->followers()->where('github_id', CreateTomFriendship::GITHUB_ID)->exists() && ! $user->following()->where('github_id', CreateTomFriendship::GITHUB_ID)->exists(); $isShellShocked = $user->created_at->lt('2026-10-21 14:00:00 UTC'); + $repositoryCommits = $this->contributors->commits($user); // XP breakdown: 10 per commit, 15 per streak day, 5 per star received, 5 per reply, 2 per star given $commitsXp = $commitsCount * 10; @@ -188,6 +189,10 @@ public function get(User $user, ?User $viewer, ?array $streaks = null): array 'progress' => min($user->followers()->count(), Achievement::SuperSaiyan->target()), 'target' => Achievement::SuperSaiyan->target(), ], + Achievement::GitBlame->value => [ + 'progress' => min($repositoryCommits, Achievement::GitBlame->target()), + 'target' => Achievement::GitBlame->target(), + ], ]; $badges = array_map(function (Achievement $achievement) use ($metrics): array { diff --git a/resources/js/components/achievements-dialog.tsx b/resources/js/components/achievements-dialog.tsx index 392a93367..84519258a 100644 --- a/resources/js/components/achievements-dialog.tsx +++ b/resources/js/components/achievements-dialog.tsx @@ -5,6 +5,7 @@ import { Cookie, FileCode, Flame, + GitCommitHorizontal, GitMerge, Key, Server, @@ -46,6 +47,7 @@ const iconMap: Record> = { 'bot-off': BotOff, bomb: Bomb, 'dragon-ball': DragonBallIcon, + 'git-commit-horizontal': GitCommitHorizontal, }; export function AchievementsDialog({ diff --git a/resources/js/components/achievements-shelf.tsx b/resources/js/components/achievements-shelf.tsx index e7e812987..2400b2819 100644 --- a/resources/js/components/achievements-shelf.tsx +++ b/resources/js/components/achievements-shelf.tsx @@ -7,6 +7,7 @@ import { Cookie, FileCode, Flame, + GitCommitHorizontal, GitMerge, Key, Server, @@ -51,6 +52,7 @@ const iconMap: Record> = { 'bot-off': BotOff, bomb: Bomb, 'dragon-ball': DragonBallIcon, + 'git-commit-horizontal': GitCommitHorizontal, }; export function AchievementsShelf({ diff --git a/routes/console.php b/routes/console.php index 4b6746e1b..86e996e21 100644 --- a/routes/console.php +++ b/routes/console.php @@ -4,6 +4,7 @@ use App\Jobs\EmbedKnowledgeBase; use App\Jobs\EndLiveStreams; +use App\Queries\ContributorsQuery; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schedule; @@ -11,6 +12,9 @@ Schedule::job(new EmbedKnowledgeBase())->hourly(); Schedule::job(new EndLiveStreams())->everyFiveMinutes(); +// Refreshed before the hour it is cached runs out, so the git blame badge never has to ask GitHub on a page load +Schedule::call(fn (ContributorsQuery $contributors) => $contributors->refresh())->name('refresh-contributors')->everyThirtyMinutes(); + Schedule::call(function (): void { $store = 'cache.stores.'.config()->string('cache.default'); diff --git a/tests/Unit/Queries/ContributorsQueryTest.php b/tests/Unit/Queries/ContributorsQueryTest.php index 0ea6058e1..d1ed2d3d4 100644 --- a/tests/Unit/Queries/ContributorsQueryTest.php +++ b/tests/Unit/Queries/ContributorsQueryTest.php @@ -2,7 +2,11 @@ declare(strict_types=1); +use App\Models\User; use App\Queries\ContributorsQuery; +use Illuminate\Console\Scheduling\CallbackEvent; +use Illuminate\Console\Scheduling\Event; +use Illuminate\Console\Scheduling\Schedule; use Illuminate\Http\Client\ConnectionException; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; @@ -131,3 +135,55 @@ expect(resolve(ContributorsQuery::class)->get())->toBe([['login' => 'recovered', 'commits' => 12, 'handle' => null]]); Http::assertSentCount(3); }); + +it('counts the commits of a user by their GitHub account from the refreshed list, and none for anyone else', function (): void { + fakeGitHubContributors([gitHubContributor('xHeaven', 187), gitHubContributor('jubianchi', 1)]); + $query = resolve(ContributorsQuery::class); + $contributor = User::factory()->make(['github_id' => (string) crc32('jubianchi')]); + + expect($query->commits($contributor))->toBe(0); + Http::assertNothingSent(); + + $query->refresh(); + + expect($query->commits($contributor))->toBe(1) + ->and($query->commits(User::factory()->make(['handle' => 'xHeaven'])))->toBe(0); +}); + +it('keeps the list it has when a refresh fails, and replaces it when one succeeds', function (): void { + Http::fake(['api.github.com/repos/'.config()->string('services.github.repository').'/contributors*' => Http::sequence() + ->push([gitHubContributor('xHeaven', 187)]) + ->pushStatus(503) + ->push([gitHubContributor('xHeaven', 188)])]); + $query = resolve(ContributorsQuery::class); + $contributor = User::factory()->make(['github_id' => (string) crc32('xHeaven')]); + + $query->refresh(); + $query->refresh(); + + expect($query->commits($contributor))->toBe(187); + + $query->refresh(); + + expect($query->commits($contributor))->toBe(188) + ->and($query->get())->toBe([['login' => 'xHeaven', 'commits' => 188, 'handle' => null]]); + Http::assertSentCount(3); +}); + +it('counts no commits while GitHub has not answered', function (): void { + Http::fake(['api.github.com/*' => Http::response(status: 500)]); + $query = resolve(ContributorsQuery::class); + + $query->refresh(); + $query->get(); + + expect($query->commits(User::factory()->make(['github_id' => (string) crc32('xHeaven')])))->toBe(0); +}); + +it('is refreshed every half hour, well within the hour it is cached', function (): void { + $event = collect(resolve(Schedule::class)->events()) + ->sole(fn (Event $event): bool => $event->description === 'refresh-contributors'); + + expect($event)->toBeInstanceOf(CallbackEvent::class) + ->expression->toBe('*/30 * * * *'); +}); diff --git a/tests/Unit/Queries/UserAchievementsQueryTest.php b/tests/Unit/Queries/UserAchievementsQueryTest.php index 5200495fd..eb41bf683 100644 --- a/tests/Unit/Queries/UserAchievementsQueryTest.php +++ b/tests/Unit/Queries/UserAchievementsQueryTest.php @@ -9,7 +9,9 @@ use App\Models\Commit; use App\Models\SshKey; use App\Models\User; +use App\Queries\ContributorsQuery; use App\Queries\UserAchievementsQuery; +use Illuminate\Support\Facades\Http; beforeEach(function (): void { $this->travelTo(now()->setDate(2026, 10, 22)->setTime(12, 0)); @@ -28,7 +30,7 @@ ->and($result['ranks'])->toHaveCount(6) ->and($result['xp_breakdown']['commits']['xp'])->toBe(0) ->and($result['xp_breakdown']['shell_shocked']['xp'])->toBe(0) - ->and($result['badges'])->toHaveCount(15); + ->and($result['badges'])->toHaveCount(16); foreach ($result['badges'] as $badge) { expect($badge['unlocked'])->toBeFalse() @@ -360,3 +362,35 @@ 'exactly at cutoff' => ['2026-10-21 14:00:00', false, 0, 1], 'just after cutoff' => ['2026-10-21 14:00:01', false, 0, 1], ]); + +it('unlocks git blame with a commit in the /dev/null repository, matching the GitHub account', function (): void { + $contributor = User::factory()->create(['github_id' => (string) crc32('xHeaven'), 'handle' => 'renamed']); + $other = User::factory()->create(['handle' => 'xHeaven']); + fakeGitHubContributors([gitHubContributor('xHeaven', 187)]); + resolve(ContributorsQuery::class)->refresh(); + + $query = resolve(UserAchievementsQuery::class); + $result = $query->get($contributor, null); + + expect(collect($result['badges'])->firstWhere('id', 'git_blame'))->toBe([ + 'id' => 'git_blame', + 'name' => 'git blame', + 'description' => 'Landed a commit in the /dev/null repo itself. git blame now points at you.', + 'icon' => 'git-commit-horizontal', + 'unlocked' => true, + 'progress' => 1, + 'target' => 1, + ])->and($result['xp'])->toBe(0) + ->and(collect($query->get($other, null)['badges'])->firstWhere('id', 'git_blame')['unlocked'])->toBeFalse(); +}); + +it('does not ask GitHub for git blame, leaving it locked until the contributors are refreshed', function (): void { + $user = User::factory()->create(['github_id' => (string) crc32('xHeaven')]); + fakeGitHubContributors([gitHubContributor('xHeaven', 187)]); + + $badge = collect(resolve(UserAchievementsQuery::class)->get($user, null)['badges'])->firstWhere('id', 'git_blame'); + + expect($badge['unlocked'])->toBeFalse() + ->and($badge['progress'])->toBe(0); + Http::assertNothingSent(); +}); From 3a1ddeed3cc333ee150c927e1eb6b539cff2c301 Mon Sep 17 00:00:00 2001 From: Kjell Kind Date: Wed, 28 Oct 2026 23:35:13 +0100 Subject: [PATCH 2/2] refactor: read git blame from the About page's contributor list Drops the cache-only read, refresh() and the scheduled task. The badge now goes through the same cached fetch the About page uses, so an earned badge stays for as long as that list is cached. Tests start from an empty cached list so pages showing achievements don't ask GitHub. Co-Authored-By: Claude Opus 5.5 --- KNOWLEDGE_BASE.md | 2 +- app/Queries/ContributorsQuery.php | 115 +++++++----------- routes/console.php | 4 - .../Controllers/AboutControllerTest.php | 2 + tests/Pest.php | 11 ++ tests/Unit/Queries/ContributorsQueryTest.php | 53 ++------ .../Queries/UserAchievementsQueryTest.php | 9 +- 7 files changed, 71 insertions(+), 125 deletions(-) diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 667f5dc3a..001c80c30 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -144,7 +144,7 @@ Profiles have **16 achievement badges**. Earned badges appear in a single row, w | **luddite** | Stop following Tom while he still follows you. This deducts **100 XP**; following him again removes the badge and penalty. Accounts Tom has never befriended do not qualify. | | **shell shocked** | “Survived the php2shell downtime of Oct '26. Earns 100 XP.” — accounts created before **October 21, 2026 at 14:00 UTC** receive this badge and **100 bonus XP**. Accounts created at or after that moment do not qualify. | | **super-saiyan** | Have **9,000 followers or more** on /dev/null. “Y—your follower count. It... it’s over 9000!!!1!” | -| **git blame** | Have **at least one commit** in the [/dev/null repository](https://github.com/dev-null-sh/dev-null.sh) on GitHub, matched by your GitHub account rather than your handle. “Landed a commit in the /dev/null repo itself. git blame now points at you.” It uses the same contributor list as the About page, so it can take an hour (and however long GitHub takes to update its counts) to appear after a merge, and it is locked while GitHub cannot be reached. | +| **git blame** | Have **at least one commit** in the [/dev/null repository](https://github.com/dev-null-sh/dev-null.sh) on GitHub, matched by your GitHub account rather than your handle. “Landed a commit in the /dev/null repo itself. git blame now points at you.” It uses the same contributor list as the About page, cached for an hour, so it can take that hour (and however long GitHub takes to update its counts) to appear after a merge. An earned badge stays while that list is cached; it only locks when the list runs out while GitHub can't be reached, until GitHub answers again. | Commit counts include replies and quotes. Commit-based badges and XP use non-deleted commits visible to the person viewing the profile, so an author may see progress from pending or rejected commits that other viewers cannot see. Streak badges use the **longest** streak, not the current streak: missing a day does not by itself remove them. Days follow the activity graph's app-timezone grouping, not the user's selected display timezone. diff --git a/app/Queries/ContributorsQuery.php b/app/Queries/ContributorsQuery.php index 1484c49b7..7123d4fcb 100644 --- a/app/Queries/ContributorsQuery.php +++ b/app/Queries/ContributorsQuery.php @@ -61,28 +61,12 @@ public function get(): ?array } /** - * How many commits the user's GitHub account has in the repository, as GitHub counted them when the list was last - * read. It only reads the cache, which the scheduler keeps warm, so a profile or a commit never waits on GitHub. - * Zero when the account has none, or the list hasn't been read. + * How many commits the user's GitHub account has in the repository, as GitHub counts them, from the same list the + * About page shows. Zero when it has none, or GitHub didn't answer. */ public function commits(User $user): int { - /** @var list|false|null $accounts */ - $accounts = Cache::get($this->key()); - - return array_column(is_array($accounts) ? $accounts : [], 'commits', 'github_id')[$user->github_id] ?? 0; - } - - /** - * Reads the list from GitHub again before the cached one expires. A failed read keeps the list there is. - */ - public function refresh(): void - { - $accounts = $this->fetch(); - - if ($accounts !== null) { - Cache::put($this->key(), $accounts, self::Ttl); - } + return array_column($this->accounts() ?? [], 'commits', 'github_id')[$user->github_id] ?? 0; } /** @@ -92,65 +76,54 @@ public function refresh(): void * @return list|null */ private function accounts(): ?array - { - /** @var list|false|null $contributors */ - $contributors = Cache::get($this->key()); - - if ($contributors === null) { - $contributors = $this->fetch() ?? false; - - Cache::put($this->key(), $contributors, $contributors === false ? self::FailedTtl : self::Ttl); - } - - return $contributors === false ? null : $contributors; - } - - /** - * @return list|null - */ - private function fetch(): ?array { $repository = config()->string('services.github.repository'); - $contributors = []; - - try { - for ($page = 1; $page <= self::MaxPages; $page++) { - $accounts = $this->github->request()->get("repos/{$repository}/contributors", [ - 'per_page' => self::PerPage, - 'page' => $page, - ])->throw()->collect(); + $key = "about:contributor-accounts:{$repository}"; - if ($accounts->isNotEmpty() && ! array_is_list($accounts->all())) { - return null; - } + /** @var list|false|null $contributors */ + $contributors = Cache::get($key); - $contributors = [...$contributors, ...$accounts->where('type', 'User') - ->mapInto(Fluent::class) - ->filter(fn (Fluent $account): bool => $account->string('login')->isNotEmpty() - && is_int($account->get('id')) - && is_int($account->get('contributions')) - && ! in_array($account->string('login')->lower()->value(), self::Agents, true)) - ->map(fn (Fluent $account): array => [ - 'login' => $account->string('login')->value(), - 'github_id' => (string) $account->integer('id'), - 'commits' => $account->integer('contributions'), - ]) - ->values() - ->all()]; - - if ($accounts->count() < self::PerPage) { - break; + if ($contributors === null) { + $contributors = []; + + try { + for ($page = 1; $page <= self::MaxPages; $page++) { + $accounts = $this->github->request()->get("repos/{$repository}/contributors", [ + 'per_page' => self::PerPage, + 'page' => $page, + ])->throw()->collect(); + + if ($accounts->isNotEmpty() && ! array_is_list($accounts->all())) { + $contributors = false; + + break; + } + + $contributors = [...$contributors, ...$accounts->where('type', 'User') + ->mapInto(Fluent::class) + ->filter(fn (Fluent $account): bool => $account->string('login')->isNotEmpty() + && is_int($account->get('id')) + && is_int($account->get('contributions')) + && ! in_array($account->string('login')->lower()->value(), self::Agents, true)) + ->map(fn (Fluent $account): array => [ + 'login' => $account->string('login')->value(), + 'github_id' => (string) $account->integer('id'), + 'commits' => $account->integer('contributions'), + ]) + ->values() + ->all()]; + + if ($accounts->count() < self::PerPage) { + break; + } } + } catch (HttpClientException) { + $contributors = false; } - } catch (HttpClientException) { - return null; - } - return $contributors; - } + Cache::put($key, $contributors, $contributors === false ? self::FailedTtl : self::Ttl); + } - private function key(): string - { - return 'about:contributor-accounts:'.config()->string('services.github.repository'); + return $contributors === false ? null : $contributors; } } diff --git a/routes/console.php b/routes/console.php index 86e996e21..4b6746e1b 100644 --- a/routes/console.php +++ b/routes/console.php @@ -4,7 +4,6 @@ use App\Jobs\EmbedKnowledgeBase; use App\Jobs\EndLiveStreams; -use App\Queries\ContributorsQuery; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schedule; @@ -12,9 +11,6 @@ Schedule::job(new EmbedKnowledgeBase())->hourly(); Schedule::job(new EndLiveStreams())->everyFiveMinutes(); -// Refreshed before the hour it is cached runs out, so the git blame badge never has to ask GitHub on a page load -Schedule::call(fn (ContributorsQuery $contributors) => $contributors->refresh())->name('refresh-contributors')->everyThirtyMinutes(); - Schedule::call(function (): void { $store = 'cache.stores.'.config()->string('cache.default'); diff --git a/tests/Feature/Controllers/AboutControllerTest.php b/tests/Feature/Controllers/AboutControllerTest.php index a4a03a0d8..a854755fb 100644 --- a/tests/Feature/Controllers/AboutControllerTest.php +++ b/tests/Feature/Controllers/AboutControllerTest.php @@ -4,6 +4,7 @@ use App\Models\User; use Illuminate\Http\Client\ConnectionException; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; it('renders the about page for guests', function (): void { @@ -55,6 +56,7 @@ }); it('leaves the contributors out while GitHub is unreachable', function (): void { + Cache::forget(gitHubContributorsCacheKey()); Http::fake(fn () => throw new ConnectionException('timeout')); $this->get(route('about.show')) diff --git a/tests/Pest.php b/tests/Pest.php index ac43ed690..8002b0065 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -15,6 +15,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Client\Request; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Process; @@ -30,6 +31,9 @@ Str::createRandomStringsNormally(); Str::createUuidsNormally(); Http::preventStrayRequests(); + // Achievements read the repository contributors, so every page that shows them would ask GitHub; + // fakeGitHubContributors() clears this to serve a list instead + Cache::put(gitHubContributorsCacheKey(), []); Process::preventStrayProcesses(); Sleep::fake(); @@ -155,6 +159,11 @@ function gitHubContributor(string $login, int $commits, string $type = 'User', ? return ['login' => $login, 'id' => $id ?? crc32($login), 'contributions' => $commits, 'type' => $type]; } +function gitHubContributorsCacheKey(): string +{ + return 'about:contributor-accounts:'.config()->string('services.github.repository'); +} + /** * Serves the contributors of the repository like GitHub does: most commits first, a page at a time. * @@ -162,6 +171,8 @@ function gitHubContributor(string $login, int $commits, string $type = 'User', ? */ function fakeGitHubContributors(array $contributors): void { + Cache::forget(gitHubContributorsCacheKey()); + Http::fake(['api.github.com/repos/'.config()->string('services.github.repository').'/contributors*' => function (Request $request) use ($contributors): PromiseInterface { $perPage = (int) $request['per_page']; diff --git a/tests/Unit/Queries/ContributorsQueryTest.php b/tests/Unit/Queries/ContributorsQueryTest.php index d1ed2d3d4..2c0ac0175 100644 --- a/tests/Unit/Queries/ContributorsQueryTest.php +++ b/tests/Unit/Queries/ContributorsQueryTest.php @@ -4,13 +4,14 @@ use App\Models\User; use App\Queries\ContributorsQuery; -use Illuminate\Console\Scheduling\CallbackEvent; -use Illuminate\Console\Scheduling\Event; -use Illuminate\Console\Scheduling\Schedule; use Illuminate\Http\Client\ConnectionException; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; +beforeEach(function (): void { + Cache::forget(gitHubContributorsCacheKey()); +}); + it('lists everyone with a commit, as GitHub counts them', function (): void { fakeGitHubContributors([gitHubContributor('xHeaven', 187), gitHubContributor('jubianchi', 1)]); @@ -136,54 +137,18 @@ Http::assertSentCount(3); }); -it('counts the commits of a user by their GitHub account from the refreshed list, and none for anyone else', function (): void { +it('counts the commits of a user by their GitHub account, and none for anyone else', function (): void { fakeGitHubContributors([gitHubContributor('xHeaven', 187), gitHubContributor('jubianchi', 1)]); $query = resolve(ContributorsQuery::class); - $contributor = User::factory()->make(['github_id' => (string) crc32('jubianchi')]); - - expect($query->commits($contributor))->toBe(0); - Http::assertNothingSent(); - $query->refresh(); - - expect($query->commits($contributor))->toBe(1) + expect($query->commits(User::factory()->make(['github_id' => (string) crc32('jubianchi')])))->toBe(1) ->and($query->commits(User::factory()->make(['handle' => 'xHeaven'])))->toBe(0); -}); - -it('keeps the list it has when a refresh fails, and replaces it when one succeeds', function (): void { - Http::fake(['api.github.com/repos/'.config()->string('services.github.repository').'/contributors*' => Http::sequence() - ->push([gitHubContributor('xHeaven', 187)]) - ->pushStatus(503) - ->push([gitHubContributor('xHeaven', 188)])]); - $query = resolve(ContributorsQuery::class); - $contributor = User::factory()->make(['github_id' => (string) crc32('xHeaven')]); - - $query->refresh(); - $query->refresh(); - expect($query->commits($contributor))->toBe(187); - - $query->refresh(); - - expect($query->commits($contributor))->toBe(188) - ->and($query->get())->toBe([['login' => 'xHeaven', 'commits' => 188, 'handle' => null]]); - Http::assertSentCount(3); + Http::assertSentCount(1); }); -it('counts no commits while GitHub has not answered', function (): void { +it('counts no commits while GitHub does not answer', function (): void { Http::fake(['api.github.com/*' => Http::response(status: 500)]); - $query = resolve(ContributorsQuery::class); - - $query->refresh(); - $query->get(); - - expect($query->commits(User::factory()->make(['github_id' => (string) crc32('xHeaven')])))->toBe(0); -}); - -it('is refreshed every half hour, well within the hour it is cached', function (): void { - $event = collect(resolve(Schedule::class)->events()) - ->sole(fn (Event $event): bool => $event->description === 'refresh-contributors'); - expect($event)->toBeInstanceOf(CallbackEvent::class) - ->expression->toBe('*/30 * * * *'); + expect(resolve(ContributorsQuery::class)->commits(User::factory()->make(['github_id' => (string) crc32('xHeaven')])))->toBe(0); }); diff --git a/tests/Unit/Queries/UserAchievementsQueryTest.php b/tests/Unit/Queries/UserAchievementsQueryTest.php index eb41bf683..dabb106e9 100644 --- a/tests/Unit/Queries/UserAchievementsQueryTest.php +++ b/tests/Unit/Queries/UserAchievementsQueryTest.php @@ -9,8 +9,8 @@ use App\Models\Commit; use App\Models\SshKey; use App\Models\User; -use App\Queries\ContributorsQuery; use App\Queries\UserAchievementsQuery; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; beforeEach(function (): void { @@ -367,7 +367,6 @@ $contributor = User::factory()->create(['github_id' => (string) crc32('xHeaven'), 'handle' => 'renamed']); $other = User::factory()->create(['handle' => 'xHeaven']); fakeGitHubContributors([gitHubContributor('xHeaven', 187)]); - resolve(ContributorsQuery::class)->refresh(); $query = resolve(UserAchievementsQuery::class); $result = $query->get($contributor, null); @@ -384,13 +383,13 @@ ->and(collect($query->get($other, null)['badges'])->firstWhere('id', 'git_blame')['unlocked'])->toBeFalse(); }); -it('does not ask GitHub for git blame, leaving it locked until the contributors are refreshed', function (): void { +it('keeps git blame locked while GitHub does not answer', function (): void { $user = User::factory()->create(['github_id' => (string) crc32('xHeaven')]); - fakeGitHubContributors([gitHubContributor('xHeaven', 187)]); + Cache::forget(gitHubContributorsCacheKey()); + Http::fake(['api.github.com/*' => Http::response(status: 500)]); $badge = collect(resolve(UserAchievementsQuery::class)->get($user, null)['badges'])->firstWhere('id', 'git_blame'); expect($badge['unlocked'])->toBeFalse() ->and($badge['progress'])->toBe(0); - Http::assertNothingSent(); });