-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubClient.php
More file actions
194 lines (164 loc) · 5.8 KB
/
Copy pathGitHubClient.php
File metadata and controls
194 lines (164 loc) · 5.8 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
declare(strict_types=1);
class GitHubClient
{
/** @var non-empty-string */
private readonly string $baseUrl;
private readonly string $userAgent;
public function __construct(string $baseUrl, private readonly string $token, private readonly string $installerVersion = 'unknown')
{
$trimmed = rtrim($baseUrl, '/');
if ('' === $trimmed) {
throw new RuntimeException('GitHub base URL must not be empty.');
}
$this->baseUrl = $trimmed;
$this->userAgent = 'OakEngineInstaller/'.$this->installerVersion;
}
/**
* @return array<int, array{name: string, commit: string}>
*/
public function getBranches(string $repo): array
{
return $this->fetchAllPages("/repos/{$repo}/branches", function (mixed $branch): ?array {
/** @var mixed $branch */
if (!is_array($branch)) {
return null;
}
$name = self::normalizeName($branch['name'] ?? null);
$commit = self::extractSha($branch['commit'] ?? null);
return null !== $name && null !== $commit ? ['name' => $name, 'commit' => $commit] : null;
});
}
/**
* @return array<int, array{name: string, commit: string}>
*/
public function getTags(string $repo): array
{
return $this->fetchAllPages("/repos/{$repo}/tags", function (mixed $tag): ?array {
/** @var mixed $tag */
if (!is_array($tag)) {
return null;
}
$name = self::normalizeName($tag['name'] ?? null);
$commit = self::extractSha($tag['commit'] ?? null);
return null !== $name && null !== $commit ? ['name' => $name, 'commit' => $commit] : null;
});
}
public function downloadArchive(string $repo, string $ref, string $refType = 'branch'): string
{
$url = $this->baseUrl."/repos/{$repo}/zipball/{$ref}";
$result = $this->fetchArchive($url);
if (200 !== $result['httpCode']) {
throw new RuntimeException("GitHub download failed: HTTP {$result['httpCode']}");
}
return $result['body'];
}
/**
* @return array{httpCode: int, body: string}
*/
private function fetchArchive(string $url): array
{
$headers = ['User-Agent: '.$this->userAgent];
if ('' !== $this->token) {
$headers[] = 'Authorization: Bearer '.$this->token;
}
$body = $this->curlDownload($url, $headers);
return ['httpCode' => $body['httpCode'], 'body' => $body['body']];
}
/**
* @param list<string> $headers
*
* @return array{httpCode: int, body: string}
*/
private function curlDownload(string $url, array $headers): array
{
\assert('' !== $url);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 300,
]);
$body = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (403 === $httpCode && '' !== $this->token) {
$headers = ['User-Agent: '.$this->userAgent];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_TIMEOUT => 300,
]);
$body = curl_exec($ch);
$httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
return ['httpCode' => $httpCode, 'body' => is_string($body) ? $body : ''];
}
/**
* @param callable(mixed): ?array{name: string, commit: string} $normalizer
*
* @return array<int, array{name: string, commit: string}>
*/
private function fetchAllPages(string $endpoint, callable $normalizer): array
{
$items = [];
$page = 1;
do {
$response = $this->request($endpoint.'?per_page=100&page='.$page);
if ([] === $response) {
break;
}
foreach ($response as $entry) {
$normalized = $normalizer($entry);
if (null !== $normalized) {
$items[] = $normalized;
}
}
++$page;
} while (100 === count($response));
return $items;
}
/**
* @return array<int|string, mixed>
*/
private function request(string $endpoint): array
{
$url = $this->baseUrl.$endpoint;
$headers = ['User-Agent: '.$this->userAgent, 'Accept: application/vnd.github.v3+json'];
if ('' !== $this->token) {
$headers[] = 'Authorization: Bearer '.$this->token;
}
$result = $this->curlDownload($url, $headers);
if ($result['httpCode'] >= 400) {
throw new RuntimeException("GitHub API Error: HTTP {$result['httpCode']}");
}
$decoded = json_decode($result['body'], true);
return is_array($decoded) ? $decoded : [];
}
private static function normalizeName(mixed $name): ?string
{
if (!is_string($name) && !is_int($name)) {
return null;
}
$trimmed = trim((string) $name);
return '' !== $trimmed ? $trimmed : null;
}
private static function extractSha(mixed $commit): ?string
{
if (!is_array($commit)) {
return null;
}
$sha = $commit['sha'] ?? null;
if (!is_string($sha) && !is_int($sha)) {
return null;
}
$trimmed = trim((string) $sha);
return '' !== $trimmed ? $trimmed : null;
}
}