diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 34ff6978d..bdbd108fb 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -116,7 +116,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 | | --- | --- | @@ -132,6 +132,7 @@ Profiles have **15 achievement badges**. Earned badges appear in a single row, w | **pipe** | Write **5 replies to other developers' commits**, excluding self-replies. Replies still count when their parent commit is soft-deleted. | | **authorized_keys** | Have at least one public SSH key linked from GitHub. | | **sweet tooth** | Receive **100 pastéis across your account**, not on a single commit. Pastéis mode does not need to be enabled, and the total survives deletion of the awarded commits. | +| **upstream** | Have a commit on /dev/null's own production branch, which is where every merged pull request lands. GitHub's contributor list is checked by account ID, so a renamed username keeps it, and refreshed at most once an hour. | | **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!” | @@ -142,7 +143,7 @@ Badges are calculated from current account data, not permanently recorded awards 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. -Sources: [badge definitions](app/Enums/Achievement.php), [unlock conditions and XP calculation](app/Queries/UserAchievementsQuery.php), [streak calculation](app/Queries/UserStreakQuery.php), [Unix ranks](app/Enums/UnixRank.php), [level-up notifications](app/Actions/CreateLevelNotification.php), [achievements dialog](resources/js/components/achievements-dialog.tsx). +Sources: [badge definitions](app/Enums/Achievement.php), [unlock conditions and XP calculation](app/Queries/UserAchievementsQuery.php), [streak calculation](app/Queries/UserStreakQuery.php), [Unix ranks](app/Enums/UnixRank.php), [level-up notifications](app/Actions/CreateLevelNotification.php), [upstream contributors](app/Queries/UpstreamContributorsQuery.php), [achievements dialog](resources/js/components/achievements-dialog.tsx). ### Following developers and suggestions diff --git a/app/Enums/Achievement.php b/app/Enums/Achievement.php index 8a878196c..ec3c41d36 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 Upstream = 'upstream'; public function label(): string { @@ -40,6 +41,7 @@ public function label(): string self::Luddite => 'luddite', self::ShellShocked => 'shell shocked', self::SuperSaiyan => 'super-saiyan', + self::Upstream => 'upstream', }; } @@ -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::Upstream => 'Got a pull request merged into /dev/null itself.', }; } @@ -82,6 +85,7 @@ public function icon(): string self::Luddite => 'bot-off', self::ShellShocked => 'bomb', self::SuperSaiyan => 'dragon-ball', + self::Upstream => 'git-pull-request', }; } @@ -103,6 +107,7 @@ public function target(): int self::Luddite => 1, self::ShellShocked => 1, self::SuperSaiyan => 9000, + self::Upstream => 1, }; } } diff --git a/app/Queries/UpstreamContributorsQuery.php b/app/Queries/UpstreamContributorsQuery.php new file mode 100644 index 000000000..bbfecca74 --- /dev/null +++ b/app/Queries/UpstreamContributorsQuery.php @@ -0,0 +1,93 @@ +github_id, $this->ids(), true); + } + + /** + * Cached for an hour. When GitHub doesn't answer, the last list it gave is used, and asked for again a minute later. + * Only one request refreshes the list at a time: the others keep the last list meanwhile instead of all asking + * GitHub at once. + * + * @return list + */ + private function ids(): array + { + /** @var list|null $ids */ + $ids = Cache::get(self::Key); + + if ($ids !== null) { + return $ids; + } + + /** @var list|false $refreshed */ + $refreshed = Cache::lock(self::Key.':refresh', self::RefreshLockSeconds)->get($this->refresh(...)); + + if ($refreshed !== false) { + return $refreshed; + } + + /** @var list $last */ + $last = Cache::get(self::Key.':last', []); + + return $last; + } + + /** + * Another request may have refreshed the list while this one waited for its turn, so the cache is read again first. + * + * @return list + */ + private function refresh(): array + { + /** @var list|null $ids */ + $ids = Cache::get(self::Key); + + if ($ids !== null) { + return $ids; + } + + try { + $ids = $this->github->accountIds('repos/'.config()->string('services.github.repository').'/contributors', self::MaxPages); + Cache::put(self::Key.':last', $ids); + Cache::put(self::Key, $ids, now()->addHour()); + } catch (Throwable) { + /** @var list $ids */ + $ids = Cache::get(self::Key.':last', []); + Cache::put(self::Key, $ids, now()->addMinute()); + } + + return $ids; + } +} diff --git a/app/Queries/UserAchievementsQuery.php b/app/Queries/UserAchievementsQuery.php index 0c27f7ef7..16720d7d6 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 UpstreamContributorsQuery $upstream) { // } @@ -188,6 +188,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::Upstream->value => [ + 'progress' => $this->upstream->includes($user) ? 1 : 0, + 'target' => Achievement::Upstream->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 431af14b2..816525f28 100644 --- a/resources/js/components/achievements-dialog.tsx +++ b/resources/js/components/achievements-dialog.tsx @@ -6,6 +6,7 @@ import { FileCode, Flame, GitMerge, + GitPullRequest, Key, Server, Shield, @@ -46,6 +47,7 @@ const iconMap: Record> = { 'bot-off': BotOff, bomb: Bomb, 'dragon-ball': DragonBallIcon, + 'git-pull-request': GitPullRequest, }; export function AchievementsDialog({ diff --git a/resources/js/components/achievements-shelf.tsx b/resources/js/components/achievements-shelf.tsx index e7e812987..805c1784a 100644 --- a/resources/js/components/achievements-shelf.tsx +++ b/resources/js/components/achievements-shelf.tsx @@ -8,6 +8,7 @@ import { FileCode, Flame, GitMerge, + GitPullRequest, Key, Server, Shield, @@ -51,6 +52,7 @@ const iconMap: Record> = { 'bot-off': BotOff, bomb: Bomb, 'dragon-ball': DragonBallIcon, + 'git-pull-request': GitPullRequest, }; export function AchievementsShelf({ diff --git a/tests/Unit/Queries/UpstreamContributorsQueryTest.php b/tests/Unit/Queries/UpstreamContributorsQueryTest.php new file mode 100644 index 000000000..32be98923 --- /dev/null +++ b/tests/Unit/Queries/UpstreamContributorsQueryTest.php @@ -0,0 +1,113 @@ + $ids + * @return list + */ +function contributorsResponse(array $ids): array +{ + return array_map(fn (int $id): array => ['id' => $id, 'login' => "contributor-{$id}", 'type' => 'User'], $ids); +} + +it('finds contributors by their github account id, not their username', function (): void { + $contributor = User::factory()->create(['github_id' => '123', 'username' => 'renamed-since']); + $other = User::factory()->create(['github_id' => '456']); + + Http::fake([ + 'api.github.com/repos/dev-null-sh/dev-null.sh/contributors*' => Http::response(contributorsResponse([123, 789])), + ]); + + $query = resolve(UpstreamContributorsQuery::class); + + expect($query->includes($contributor))->toBeTrue() + ->and($query->includes($other))->toBeFalse(); + + Http::assertSentCount(1); +}); + +it('reads every page of contributors and keeps them for an hour', function (): void { + $lastPage = User::factory()->create(['github_id' => '1000']); + + Http::fake([ + 'api.github.com/repos/dev-null-sh/dev-null.sh/contributors*' => Http::sequence() + ->push(contributorsResponse(range(1, 100))) + ->push(contributorsResponse([1000])), + ]); + + $query = resolve(UpstreamContributorsQuery::class); + + expect($query->includes($lastPage))->toBeTrue(); + + $this->travel(59)->minutes(); + + expect($query->includes($lastPage))->toBeTrue(); + + Http::assertSentCount(2); +}); + +it('keeps the last known contributors while github does not answer', function (): void { + $contributor = User::factory()->create(['github_id' => '123']); + + Http::fake([ + 'api.github.com/repos/dev-null-sh/dev-null.sh/contributors*' => Http::sequence() + ->push(contributorsResponse([123])) + ->push('Unavailable', 503) + ->push(contributorsResponse([])), + ]); + + $query = resolve(UpstreamContributorsQuery::class); + + expect($query->includes($contributor))->toBeTrue(); + + $this->travel(61)->minutes(); + + expect($query->includes($contributor))->toBeTrue(); + + $this->travel(2)->minutes(); + + expect($query->includes($contributor))->toBeFalse(); + + Http::assertSentCount(3); +}); + +it('has nobody when github has never answered', function (): void { + Http::fake([ + 'api.github.com/repos/dev-null-sh/dev-null.sh/contributors*' => Http::response('Unavailable', 503), + ]); + + expect(resolve(UpstreamContributorsQuery::class)->includes(User::factory()->create()))->toBeFalse(); +}); + +it('keeps the last known contributors without asking github while another request refreshes them', function (): void { + $contributor = User::factory()->create(['github_id' => '123']); + Cache::put('upstream-contributors:last', ['123']); + Cache::lock('upstream-contributors:refresh', 30)->get(); + Http::fake(); + + expect(resolve(UpstreamContributorsQuery::class)->includes($contributor))->toBeTrue(); + + Http::assertNothingSent(); +}); + +it('uses the list another request refreshed while it waited for its turn', function (): void { + $contributor = User::factory()->create(['github_id' => '123']); + Http::fake(); + + // Another request finishes its refresh just after this one found the list missing. + Event::listen(CacheMissed::class, function (CacheMissed $event): void { + Cache::put($event->key, ['123'], 60); + }); + + expect(resolve(UpstreamContributorsQuery::class)->includes($contributor))->toBeTrue(); + + Http::assertNothingSent(); +}); diff --git a/tests/Unit/Queries/UserAchievementsQueryTest.php b/tests/Unit/Queries/UserAchievementsQueryTest.php index ebd27bf12..848e4e8f7 100644 --- a/tests/Unit/Queries/UserAchievementsQueryTest.php +++ b/tests/Unit/Queries/UserAchievementsQueryTest.php @@ -10,6 +10,7 @@ use App\Models\SshKey; use App\Models\User; use App\Queries\UserAchievementsQuery; +use Illuminate\Support\Facades\Http; beforeEach(function (): void { $this->travelTo(now()->setDate(2026, 10, 22)->setTime(12, 0)); @@ -28,7 +29,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() @@ -66,6 +67,23 @@ ->and($badge['progress'])->toBe(1); }); +it('unlocks upstream badge for developers with a commit on /dev/null itself', function (): void { + $contributor = User::factory()->create(['github_id' => '123']); + $user = User::factory()->create(['github_id' => '456']); + + Http::fake([ + 'api.github.com/repos/dev-null-sh/dev-null.sh/contributors*' => Http::response([['id' => 123, 'login' => 'octocat', 'type' => 'User']]), + ]); + + $badge = fn (User $user): array => collect(resolve(UserAchievementsQuery::class)->get($user, $user)['badges']) + ->firstWhere('id', Achievement::Upstream->value); + + expect($badge($contributor)['unlocked'])->toBeTrue() + ->and($badge($contributor)['icon'])->toBe('git-pull-request') + ->and($badge($user)['unlocked'])->toBeFalse() + ->and($badge($user)['progress'])->toBe(0); +}); + it('tracks stars given to other developers and excludes self-starring', function (): void { $user = User::factory()->create(); $other = User::factory()->create();