forked from github/codespaces-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
448 lines (371 loc) · 12.6 KB
/
github.js
File metadata and controls
448 lines (371 loc) · 12.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import { buildAnalysisResult, preRankImportanceScore } from "./analysis.js";
import { mapWithConcurrency, withTimeout } from "./utils.js";
const GITHUB_API_ROOT = "https://api.github.com";
const GITHUB_GRAPHQL_ENDPOINT = "https://api.github.com/graphql";
const MAX_REPO_PAGES = 3;
const MAX_DEEP_REPOS = 30;
const CONCURRENCY_LIMIT = 5;
const REQUEST_TIMEOUT_MS = Number(process.env.GITHUB_REQUEST_TIMEOUT_MS || 10_000);
const GITHUB_CACHE_TTL_MS = Number(process.env.GITHUB_CACHE_TTL_MS || 120_000);
const githubResponseCache = new Map();
export class GitHubError extends Error {
constructor(type, message, details = {}) {
super(message);
this.name = "GitHubError";
this.type = type;
this.details = details;
}
}
export function parseProfileInput(rawInput) {
const value = (rawInput || "").trim();
if (!value) {
return { ok: false, error: "Enter a GitHub username or profile URL." };
}
let candidate = value;
if (/github\.com\//i.test(candidate) && !/^https?:\/\//i.test(candidate)) {
candidate = `https://${candidate}`;
}
if (/^https?:\/\//i.test(candidate)) {
try {
const url = new URL(candidate);
const host = url.hostname.toLowerCase();
if (host !== "github.com" && host !== "www.github.com") {
return { ok: false, error: "URL must be a valid github.com profile URL." };
}
const segments = url.pathname.split("/").filter(Boolean);
if (!segments.length) {
return { ok: false, error: "GitHub URL is missing a username." };
}
candidate = segments[0];
} catch {
return { ok: false, error: "Invalid URL format. Use a username or github.com profile URL." };
}
}
candidate = candidate.replace(/^@+/, "").trim();
if (candidate.endsWith(".git")) {
candidate = candidate.slice(0, -4);
}
const isValid = /^[A-Za-z0-9-]{1,39}$/.test(candidate) && !candidate.startsWith("-") && !candidate.endsWith("-");
if (!isValid) {
return { ok: false, error: "Invalid GitHub username format." };
}
return { ok: true, username: candidate.toLowerCase() };
}
export async function runAnalysisPipeline(username, token, signal) {
const profileResponse = await githubRequest(`/users/${encodeURIComponent(username)}`, { token, signal });
const profile = profileResponse.data;
const allRepos = await fetchAllRepositories(profile.login, token, signal);
const scorableRepos = allRepos.filter((repo) => !repo.fork);
const deepEnrichment = await enrichTopRepositories(profile.login, scorableRepos, token, signal);
const deepMap = new Map(deepEnrichment.items.map((item) => [item.id, item]));
const enrichedScorableRepos = scorableRepos.map((repo) => {
const extra = deepMap.get(repo.id);
return {
...repo,
_languageBytes: extra ? extra.languageBytes : null,
_languageChecked: extra ? extra.languageChecked : false,
_hasReadme: extra ? extra.hasReadme : null,
_readmeChecked: extra ? extra.readmeChecked : false
};
});
const contributions = await fetchContributionSignals(profile.login, token, signal);
const pinnedFromGraphql = token ? await fetchPinnedRepositories(profile.login, token, signal) : null;
return buildAnalysisResult({
profile,
scorableRepos: enrichedScorableRepos,
contributions,
partial: deepEnrichment.partial,
pinnedFromGraphql
});
}
async function fetchAllRepositories(username, token, signal) {
const repos = [];
for (let page = 1; page <= MAX_REPO_PAGES; page += 1) {
const path = `/users/${encodeURIComponent(username)}/repos?type=owner&sort=updated&per_page=100&page=${page}`;
const response = await githubRequest(path, { token, signal });
const data = Array.isArray(response.data) ? response.data : [];
repos.push(...data);
if (data.length < 100) {
break;
}
}
return repos;
}
async function enrichTopRepositories(owner, scorableRepos, token, signal) {
const candidates = [...scorableRepos]
.sort((a, b) => preRankImportanceScore(b) - preRankImportanceScore(a))
.slice(0, MAX_DEEP_REPOS);
const partial = {
deepRepoCount: candidates.length,
languageChecked: 0,
readmeChecked: 0,
languageFailures: 0,
readmeFailures: 0
};
const items = await mapWithConcurrency(candidates, CONCURRENCY_LIMIT, async (repo) => {
return fetchRepoDeepMeta(owner, repo, token, signal, partial);
});
return { items, partial };
}
async function fetchRepoDeepMeta(owner, repo, token, signal, partial) {
const details = {
id: repo.id,
languageBytes: null,
languageChecked: false,
hasReadme: null,
readmeChecked: false
};
try {
const languagesResponse = await githubRequest(
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo.name)}/languages`,
{ token, signal }
);
details.languageBytes = languagesResponse.data && typeof languagesResponse.data === "object" ? languagesResponse.data : {};
details.languageChecked = true;
partial.languageChecked += 1;
} catch (error) {
if (error.name === "AbortError") {
throw error;
}
partial.languageFailures += 1;
}
try {
await githubRequest(
`/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo.name)}/readme`,
{ token, signal }
);
details.hasReadme = true;
details.readmeChecked = true;
partial.readmeChecked += 1;
} catch (error) {
if (error.name === "AbortError") {
throw error;
}
if (error instanceof GitHubError && error.type === "NotFound") {
details.hasReadme = false;
details.readmeChecked = true;
partial.readmeChecked += 1;
} else {
partial.readmeFailures += 1;
}
}
return details;
}
async function fetchContributionSignals(username, token, signal) {
const [prCount, issueCount] = await Promise.all([
fetchSearchCount(`author:${username} type:pr`, token, signal),
fetchSearchCount(`author:${username} type:issue`, token, signal)
]);
return { prCount, issueCount };
}
async function fetchSearchCount(query, token, signal) {
const path = `/search/issues?q=${encodeURIComponent(query)}&per_page=1`;
const response = await githubRequest(path, { token, signal });
return Number(response.data && response.data.total_count) || 0;
}
async function fetchPinnedRepositories(username, token, signal) {
const query = `
query ($login: String!) {
user(login: $login) {
pinnedItems(first: 6, types: REPOSITORY) {
nodes {
... on Repository {
name
url
stargazerCount
}
}
}
}
}
`;
try {
const data = await githubGraphQL(query, { login: username }, token, signal);
const nodes = data && data.user && data.user.pinnedItems ? data.user.pinnedItems.nodes : [];
if (!Array.isArray(nodes)) {
return null;
}
return nodes
.filter((node) => node && node.name && node.url)
.map((node) => ({
name: node.name,
url: node.url,
stars: Number(node.stargazerCount) || 0
}));
} catch (error) {
if (error.name === "AbortError") {
throw error;
}
return null;
}
}
async function githubRequest(path, options = {}) {
const {
token = "",
method = "GET",
signal,
retry = 1,
body,
accept = "application/vnd.github+json"
} = options;
const url = path.startsWith("http") ? path : `${GITHUB_API_ROOT}${path}`;
const cacheKey = method === "GET" && !body ? url : null;
if (cacheKey) {
const cached = getFromCache(cacheKey);
if (cached) {
return cached;
}
}
let attempt = 0;
while (attempt <= retry) {
attempt += 1;
const timeout = withTimeout(signal, REQUEST_TIMEOUT_MS);
try {
const response = await fetch(url, {
method,
signal: timeout.signal,
headers: {
Accept: accept,
"X-GitHub-Api-Version": "2022-11-28",
...(token ? { Authorization: `Bearer ${token}` } : {})
},
...(body ? { body: JSON.stringify(body) } : {})
});
const rateInfo = extractRateInfo(response.headers);
const contentType = response.headers.get("content-type") || "";
if (!response.ok) {
let errorPayload = null;
try {
errorPayload = contentType.includes("application/json")
? await response.json()
: { message: await response.text() };
} catch {
errorPayload = { message: `GitHub API error (${response.status})` };
}
const message =
(errorPayload && typeof errorPayload.message === "string" && errorPayload.message.trim()) ||
`GitHub API error (${response.status})`;
if (response.status === 404) {
throw new GitHubError("NotFound", message, { status: response.status, rateInfo });
}
if (response.status === 401) {
throw new GitHubError("Unauthorized", message, { status: response.status, rateInfo });
}
const rateLimited =
response.status === 429 ||
(response.status === 403 && (rateInfo.remaining === 0 || /rate limit/i.test(message)));
if (rateLimited) {
throw new GitHubError("RateLimited", message, {
status: response.status,
resetAt: rateInfo.resetAt,
rateInfo
});
}
throw new GitHubError("Api", message, { status: response.status, rateInfo });
}
if (response.status === 204) {
const result = { data: null, headers: response.headers, rateInfo };
if (cacheKey) {
setToCache(cacheKey, result);
}
return result;
}
const data = contentType.includes("application/json") ? await response.json() : await response.text();
const result = { data, headers: response.headers, rateInfo };
if (cacheKey) {
setToCache(cacheKey, result);
}
return result;
} catch (error) {
if (error.name === "AbortError") {
throw error;
}
if (error instanceof GitHubError) {
throw error;
}
if (attempt > retry) {
throw new GitHubError("Network", "Failed to reach GitHub API.", { cause: error });
}
} finally {
timeout.dispose();
}
}
throw new GitHubError("Network", "Failed to reach GitHub API.");
}
async function githubGraphQL(query, variables, token, signal) {
if (!token) {
throw new GitHubError("Unauthorized", "GitHub token is required for GraphQL requests.");
}
const timeout = withTimeout(signal, REQUEST_TIMEOUT_MS);
let response;
try {
response = await fetch(GITHUB_GRAPHQL_ENDPOINT, {
method: "POST",
signal: timeout.signal,
headers: {
Accept: "application/vnd.github+json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": "2022-11-28"
},
body: JSON.stringify({ query, variables })
});
} catch (error) {
if (error.name === "AbortError") {
throw error;
}
throw new GitHubError("Network", "Failed to reach GitHub GraphQL API.", { cause: error });
} finally {
timeout.dispose();
}
const rateInfo = extractRateInfo(response.headers);
const payload = await response.json();
if (!response.ok) {
if (response.status === 401) {
throw new GitHubError("Unauthorized", "GitHub token is invalid for GraphQL API.", {
status: response.status,
rateInfo
});
}
const message = (payload && payload.message) || `GitHub GraphQL error (${response.status})`;
throw new GitHubError("Api", message, { status: response.status, rateInfo });
}
if (payload.errors && payload.errors.length) {
throw new GitHubError("Api", payload.errors[0].message || "GraphQL query failed.", {
errors: payload.errors,
rateInfo
});
}
return payload.data;
}
function extractRateInfo(headers) {
const remaining = Number(headers.get("x-ratelimit-remaining"));
const resetEpoch = Number(headers.get("x-ratelimit-reset"));
return {
remaining: Number.isFinite(remaining) ? remaining : null,
resetAt: Number.isFinite(resetEpoch) && resetEpoch > 0 ? new Date(resetEpoch * 1000).toISOString() : null
};
}
function getFromCache(key) {
const item = githubResponseCache.get(key);
if (!item) {
return null;
}
if (Date.now() > item.expiresAt) {
githubResponseCache.delete(key);
return null;
}
return item.value;
}
function setToCache(key, value) {
githubResponseCache.set(key, {
value,
expiresAt: Date.now() + GITHUB_CACHE_TTL_MS
});
if (githubResponseCache.size > 500) {
const oldestKey = githubResponseCache.keys().next().value;
if (oldestKey) {
githubResponseCache.delete(oldestKey);
}
}
}