Repository navigation
Expand file tree
/
Copy pathContributorsQuery.php
More file actions
120 lines (98 loc) · 4.05 KB
/
Copy pathContributorsQuery.php
File metadata and controls
120 lines (98 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace App\Queries;
use App\Contracts\Services\GitHubProvider;
use App\Models\User;
use Illuminate\Http\Client\HttpClientException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Fluent;
/**
* @phpstan-type Account array{login: string, github_id: string, commits: int}
* @phpstan-type Contributor array{login: string, commits: int, handle: string|null}
*/
final readonly class ContributorsQuery
{
private const int PerPage = 100;
private const int MaxPages = 10;
private const int Ttl = 3600;
private const int FailedTtl = 60;
/**
* GitHub reports them as ordinary accounts, so they are named rather than detected.
*/
private const array Agents = ['ampagent', 'claude'];
public function __construct(private GitHubProvider $github)
{
//
}
/**
* The repository contributors, most commits first, as GitHub counts them, each with their /dev/null handle, or
* null when they aren't on it. They are found by their GitHub account, since a handle can change on either side.
*
* @return list<Contributor>|null
*/
public function get(): ?array
{
$accounts = $this->accounts();
if ($accounts === null) {
return null;
}
$handles = User::query()
->whereIn('github_id', array_column($accounts, 'github_id'))
->get()
->mapWithKeys(fn (User $user): array => [$user->github_id => $user->handle]);
return array_map(fn (array $account): array => [
'login' => $account['login'],
'commits' => $account['commits'],
'handle' => $handles->get($account['github_id']),
], $accounts);
}
/**
* 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
{
return array_column($this->accounts() ?? [], 'commits', 'github_id')[$user->github_id] ?? 0;
}
/**
* The GitHub accounts behind the contributors. A failed read is kept for a minute, so an outage doesn't send every
* visitor to GitHub.
*
* @return list<Account>|null
*/
private function accounts(): ?array
{
$repository = config()->string('services.github.repository');
$key = "about:contributor-accounts:{$repository}";
/** @var list<Account>|false|null $contributors */
$contributors = Cache::get($key);
if ($contributors === null) {
$contributors = [];
try {
for ($page = 1; $page <= self::MaxPages; $page++) {
$accounts = collect($this->github->contributors($repository, $page, self::PerPage));
$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;
}
Cache::put($key, $contributors, $contributors === false ? self::FailedTtl : self::Ttl);
}
return $contributors === false ? null : $contributors;
}
}