Repository navigation
Expand file tree
/
Copy pathRepositoryHistoryQuery.php
More file actions
82 lines (70 loc) · 4.22 KB
/
Copy pathRepositoryHistoryQuery.php
File metadata and controls
82 lines (70 loc) · 4.22 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
<?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;
final readonly class RepositoryHistoryQuery
{
public function __construct(private GitHubProvider $github)
{
//
}
/** @return array<string, mixed>|null */
public function get(string $query): ?array
{
$repository = config()->string('services.github.repository');
$terms = str($query)->explode(' ')->map(fn (string $term): string => '"'.$term.'"')->implode(' ');
$result = Cache::remember("repository-history:v2:{$repository}:".hash('sha256', $query), 900, function () use ($repository, $terms): ?array {
try {
$pullRequests = $this->github->searchIssues('repo:'.$repository.' is:pr is:merged base:'.ChangelogQuery::Branch.' in:title,body '.$terms, 5, null);
$commits = $this->github->searchCommits('repo:'.$repository.' merge:false '.$terms, 5);
if ($pullRequests['incomplete'] || $commits['incomplete']) {
return null;
}
} catch (HttpClientException) {
return null;
}
return [
'repository' => $repository,
'as_of' => now()->toIso8601String(),
'freshness' => 'Cached for 15 minutes. GitHub search indexing can lag.',
'complete' => false,
'coverage' => 'Up to five best matches each: merged production PR titles and descriptions, and non-merge Git commit messages on the default branch, across all indexed dates. All keywords must match, in any order; this is lexical search, not semantic, code or diff search. No matches does not mean the feature does not exist. Results may be follow-up fixes rather than the original implementation.',
'attribution' => 'PR authors opened the PR; commit authors are GitHub-linked author accounts, not committers or mergers. Neither proves sole implementation or includes all co-authors. A null account is unknown; never infer identity from names or emails. Cite the matching PR or commit, distinguish these roles, and state uncertainty when the evidence is inconclusive. A merge is not proof of deployment.',
'pull_requests' => collect($pullRequests['items'])->take(5)->mapInto(Fluent::class)->map(fn (Fluent $pullRequest): array => [
'title' => $pullRequest->string('title')->substr(0, 300)->value(),
'url' => $pullRequest->string('html_url')->value(),
'pr_author' => $pullRequest->get('user.login'),
'github_id' => $pullRequest->get('user.id'),
'merged_at' => $pullRequest->get('pull_request.merged_at'),
])->all(),
'commits' => collect($commits['items'])->take(5)->mapInto(Fluent::class)->map(fn (Fluent $commit): array => [
'title' => $commit->string('commit.message')->before("\n")->substr(0, 300)->value(),
'url' => $commit->string('html_url')->value(),
'commit_author' => $commit->get('author.login'),
'github_id' => $commit->get('author.id'),
'authored_at' => $commit->get('commit.author.date'),
])->all(),
];
});
if ($result === null) {
return null;
}
$authors = User::query()
->whereNotNull('onboarded_at')
->whereIn('github_id', collect($result['pull_requests'])->merge($result['commits'])->pluck('github_id')->filter()->unique())
->get()
->reject(fn (User $user): bool => str_starts_with($user->handle, '_'))
->mapWithKeys(fn (User $user): array => [$user->github_id => $user->mention()]);
foreach (['pull_requests', 'commits'] as $key) {
$result[$key] = collect($result[$key])->mapInto(Fluent::class)->map(fn (Fluent $item): array => [
...$item->except('github_id'),
'mention' => $authors->get($item->string('github_id')->value()),
])->all();
}
return $result;
}
}