Repository navigation
Expand file tree
/
Copy pathRepositoryInsightsQuery.php
More file actions
149 lines (129 loc) · 5.72 KB
/
Copy pathRepositoryInsightsQuery.php
File metadata and controls
149 lines (129 loc) · 5.72 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
namespace App\Queries;
use App\Contracts\Services\GitHubProvider;
use Illuminate\Http\Client\HttpClientException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Fluent;
final readonly class RepositoryInsightsQuery
{
public function __construct(private GitHubProvider $github, private ChangelogQuery $changelog)
{
//
}
/**
* @param 'contributors'|'recent_changes'|'opportunities'|'overview'|'languages' $metric
* @return array<string, mixed>|null
*/
public function get(string $metric): ?array
{
$repository = config()->string('services.github.repository');
return Cache::remember("repository-insights:{$repository}:{$metric}", 900, function () use ($repository, $metric): ?array {
try {
$data = match ($metric) {
'recent_changes' => $this->recentChanges(),
'contributors' => $this->contributors($repository),
'opportunities' => $this->opportunities($repository),
'overview' => $this->overview($repository),
'languages' => [
'definition' => 'Repository languages measured in bytes of code, not people or skill.',
'complete' => true,
'languages' => $this->github->languages($repository),
],
};
} catch (HttpClientException) {
return null;
}
if ($data === null) {
return null;
}
return [
'metric' => $metric,
'repository' => $repository,
'source_url' => 'https://github.com/'.$repository,
'as_of' => now()->toIso8601String(),
'freshness' => 'Cached for 15 minutes. GitHub contributor counts can lag by hours; changelog pages are cached for another 30 minutes. This is not a live deployment status.',
...$data,
];
});
}
/**
* @return array<string, mixed>|null
*/
private function recentChanges(): ?array
{
$changelog = $this->changelog->get('UTC');
if ($changelog === null) {
return null;
}
return [
'definition' => 'Up to ten recent commits on production, from the changelog. Commit time is not a verified deployment time.',
'complete' => false,
'coverage' => 'Changelog scans at most 1,000 commits for the three latest UTC days with commits on production. This list is not an exhaustive count or guaranteed latest under truncation.',
'commits' => collect($changelog['commits'])->take(10)->map(fn (array $commit): array => [
'title' => mb_substr($commit['title'], 0, 300),
'url' => $commit['url'],
'author' => $commit['author']['login'],
'committed_at' => $commit['committed_at'],
])->all(),
];
}
/**
* @return array<string, mixed>
*/
private function contributors(string $repository): array
{
return [
'definition' => 'Up to ten top GitHub accounts by GitHub-reported all-time commit count, excluding bots and anonymous contributors. Counts do not measure contribution quality.',
'complete' => false,
'coverage' => 'First 100 contributors in GitHub commit-count order, then filtered to users and limited to ten. Not an exhaustive contributor count.',
'contributors' => collect($this->github->contributors($repository, 1, 100))->where('type', 'User')->take(10)->mapInto(Fluent::class)->map(fn (Fluent $contributor): array => [
'username' => $contributor->string('login')->value(),
'commits' => $contributor->integer('contributions'),
'url' => $contributor->string('html_url')->value(),
])->values()->all(),
];
}
/**
* Null when GitHub's search didn't finish, so the list may be missing issues.
*
* @return array<string, mixed>|null
*/
private function opportunities(string $repository): ?array
{
$results = $this->github->searchIssues('repo:'.$repository.' is:issue is:open label:"good first issue","help wanted"', 10, 'updated');
if ($results['incomplete']) {
return null;
}
return [
'definition' => 'Up to ten recently updated open issues labelled good first issue or help wanted.',
'complete' => false,
'coverage' => 'A limited issue list, not all available work or a commitment that an issue is unassigned.',
'issues' => collect($results['items'])->mapInto(Fluent::class)->map(fn (Fluent $issue): array => [
'title' => $issue->string('title')->substr(0, 300)->value(),
'url' => $issue->string('html_url')->value(),
])->all(),
];
}
/**
* Null when GitHub's search didn't finish, so a count may be short.
*
* @return array<string, mixed>|null
*/
private function overview(string $repository): ?array
{
$data = [
'definition' => 'Repository stars and forks, with open issues and open pull requests counted separately by GitHub search.',
'complete' => true,
...$this->github->repository($repository),
];
foreach (['open_issues' => 'issue', 'open_pull_requests' => 'pr'] as $key => $type) {
$results = $this->github->searchIssues("repo:{$repository} is:{$type} is:open", 1, null);
if ($results['incomplete']) {
return null;
}
$data[$key] = $results['total'];
}
return $data;
}
}