Repository navigation
feat: add an upstream badge for /dev/null contributors #741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Akiyo974
wants to merge
6
commits into
dev-null-sh:production
Choose a base branch
from
Akiyo974:feat/upstream-badge
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0fc1b7
feat: add an upstream badge for /dev/null contributors
be251cc
chore: rerun CI
2f59796
Merge remote-tracking branch 'upstream/production' into feat/upstream…
401cb7c
fix: refresh the upstream contributors once at a time
483f5d9
Merge remote-tracking branch 'upstream/production' into feat/upstream…
69e98c7
fix: keep the refresh lock for as long as a GitHub lookup can take
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Queries; | ||
|
|
||
| use App\Models\User; | ||
| use App\Services\GitHub; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Throwable; | ||
|
|
||
| /** | ||
| * Who has a commit on /dev/null's production branch, which is where every merged pull request lands. GitHub counts it | ||
| * under the author's account, matched by its immutable ID so a renamed username keeps the badge. | ||
| */ | ||
| final readonly class UpstreamContributorsQuery | ||
| { | ||
| private const string Key = 'upstream-contributors'; | ||
|
|
||
| private const int MaxPages = 10; | ||
|
|
||
| /** | ||
| * Longer than a refresh can take: up to ten pages, each request given five seconds, with room to spare. A lock that | ||
| * ran out mid-refresh would let a second one start alongside it. | ||
| */ | ||
| private const int RefreshLockSeconds = 120; | ||
|
|
||
| public function __construct(private GitHub $github) | ||
| { | ||
| // | ||
| } | ||
|
|
||
| public function includes(User $user): bool | ||
| { | ||
| return in_array($user->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<string> | ||
| */ | ||
| private function ids(): array | ||
| { | ||
| /** @var list<string>|null $ids */ | ||
| $ids = Cache::get(self::Key); | ||
|
|
||
| if ($ids !== null) { | ||
| return $ids; | ||
| } | ||
|
|
||
| /** @var list<string>|false $refreshed */ | ||
| $refreshed = Cache::lock(self::Key.':refresh', self::RefreshLockSeconds)->get($this->refresh(...)); | ||
|
|
||
| if ($refreshed !== false) { | ||
| return $refreshed; | ||
| } | ||
|
|
||
| /** @var list<string> $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<string> | ||
| */ | ||
| private function refresh(): array | ||
| { | ||
| /** @var list<string>|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<string> $ids */ | ||
| $ids = Cache::get(self::Key.':last', []); | ||
| Cache::put(self::Key, $ids, now()->addMinute()); | ||
| } | ||
|
|
||
| return $ids; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Models\User; | ||
| use App\Queries\UpstreamContributorsQuery; | ||
| use Illuminate\Cache\Events\CacheMissed; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Illuminate\Support\Facades\Event; | ||
| use Illuminate\Support\Facades\Http; | ||
|
|
||
| /** | ||
| * @param list<int> $ids | ||
| * @return list<array{id: int, login: string, type: string}> | ||
| */ | ||
| 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(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.