-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
453 lines (405 loc) · 13.9 KB
/
github.ts
File metadata and controls
453 lines (405 loc) · 13.9 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
449
450
451
452
453
// Copyright © 2026 Miguel Tenorio Potrony - AntiD2ta.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { GitHubUser, Repository, StarStatus } from "./types";
import { repoKey } from "./repo-key";
const GITHUB_API = "https://api.github.com";
const CONCURRENT_CHECK_LIMIT = 5;
const STAR_DELAY_MS = 1000;
/**
* Classify a 403 response by reading its body. GitHub returns 403 for both
* secondary rate limits (body contains "rate limit") and permission errors
* (body says "Resource not accessible" or just "Forbidden").
* Returns "rate-limit" or "forbidden".
*/
async function classify403(resp: Response): Promise<"rate-limit" | "forbidden"> {
try {
const body = await resp.json() as { message?: string };
const msg = typeof body.message === "string" ? body.message.toLowerCase() : "";
if (msg.includes("rate limit") || msg.includes("abuse detection")) {
return "rate-limit";
}
} catch {
// Body parse failed — treat as forbidden.
}
return "forbidden";
}
/**
* Handle a 403 or 429 response: 429 is always a rate limit; 403 requires
* body inspection. Logs a diagnostic message and throws the appropriate error.
*/
async function handleForbiddenOrRateLimit(
resp: Response,
context: string,
): Promise<never> {
if (resp.status === 429) {
console.warn(`[github] ${context}: 429 rate limited`);
throw new RateLimitError(resp.headers.get("retry-after"));
}
// 403 — inspect body to distinguish rate limit from permission error.
const kind = await classify403(resp);
if (kind === "rate-limit") {
console.warn(`[github] ${context}: 403 secondary rate limit`);
throw new RateLimitError(resp.headers.get("retry-after"));
}
console.error(`[github] ${context}: 403 permission denied (token may lack required scope)`);
throw new ForbiddenError(context);
}
interface StarCheckResult {
repo: Repository;
status: StarStatus;
}
interface StarAllResult {
starred: number;
failed: number;
}
const BASE_HEADERS: HeadersInit = {
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2026-03-10",
};
function authHeaders(token: string): HeadersInit {
return {
...BASE_HEADERS,
Authorization: `Bearer ${token}`,
};
}
/**
* Wrap fetch so connection failures surface as NetworkError. Browsers reject
* fetch with a TypeError when DNS/TLS/connectivity fails — everything else
* (HTTP status codes) resolves normally. An `AbortError` from a cancelled
* signal is NOT a NetworkError; it's re-thrown as-is so callers can ignore it.
*/
async function networkFetch(
input: RequestInfo | URL,
init?: RequestInit,
): Promise<Response> {
try {
return await fetch(input, init);
} catch (err) {
if (err instanceof DOMException && err.name === "AbortError") throw err;
throw new NetworkError(err);
}
}
/** Fetch the authenticated user's profile. */
export async function getUser(token: string): Promise<GitHubUser> {
const resp = await networkFetch(`${GITHUB_API}/user`, {
headers: authHeaders(token),
});
if (resp.status === 401) {
throw new TokenExpiredError();
}
if (!resp.ok) {
throw new Error(`GitHub API error: ${resp.status}`);
}
return resp.json() as Promise<GitHubUser>;
}
/** Check if the authenticated user has starred a specific repo. */
export async function isStarred(
token: string,
owner: string,
repo: string,
signal?: AbortSignal,
): Promise<boolean> {
const resp = await networkFetch(
`${GITHUB_API}/user/starred/${owner}/${repo}`,
{ headers: authHeaders(token), signal },
);
if (resp.status === 204) return true;
if (resp.status === 404) return false;
if (resp.status === 401) throw new TokenExpiredError();
if (resp.status === 403 || resp.status === 429) {
await handleForbiddenOrRateLimit(resp, `isStarred ${owner}/${repo}`);
}
throw new Error(`GitHub API error: ${resp.status}`);
}
/** Star a repo for the authenticated user. */
export async function starRepo(
token: string,
owner: string,
repo: string,
signal?: AbortSignal,
): Promise<void> {
const resp = await networkFetch(
`${GITHUB_API}/user/starred/${owner}/${repo}`,
{ method: "PUT", headers: authHeaders(token), signal },
);
if (resp.status === 204) return;
if (resp.status === 401) throw new TokenExpiredError();
if (resp.status === 403 || resp.status === 429) {
await handleForbiddenOrRateLimit(resp, `starRepo ${owner}/${repo}`);
}
throw new Error(`GitHub API error: ${resp.status}`);
}
/**
* Check star status for all repos, with concurrency limited to CONCURRENT_CHECK_LIMIT.
* Calls onProgress after each repo is checked.
*/
export async function checkAllStars(
token: string,
repos: Repository[],
onProgress?: (result: StarCheckResult) => void,
): Promise<StarCheckResult[]> {
const results: StarCheckResult[] = [];
const queue = [...repos];
// AbortController cancels in-flight and pending fetches in sibling workers
// as soon as one throws an unrecoverable error (TokenExpired/Network).
const controller = new AbortController();
async function processNext(): Promise<void> {
if (controller.signal.aborted) return;
const repo = queue.shift();
if (!repo) return;
let status: StarStatus;
try {
const starred = await isStarred(
token,
repo.owner,
repo.name,
controller.signal,
);
status = starred ? "starred" : "unstarred";
} catch (err) {
if (err instanceof DOMException && err.name === "AbortError") return;
if (err instanceof TokenExpiredError || err instanceof NetworkError) {
controller.abort();
throw err;
}
status = "failed";
}
const result = { repo, status };
results.push(result);
onProgress?.(result);
await processNext();
}
// Launch CONCURRENT_CHECK_LIMIT workers that pull from the shared queue.
const workers = Array.from(
{ length: Math.min(CONCURRENT_CHECK_LIMIT, repos.length) },
() => processNext(),
);
await Promise.all(workers);
return results;
}
/**
* Star all unstarred repos sequentially with a 1s delay between each.
* Calls onProgress after each attempt. onRateLimit is invoked if a
* secondary-rate-limit pause occurs, with the number of milliseconds waited.
*/
export async function starAllUnstarred(
token: string,
repos: Repository[],
onProgress: (repo: Repository, status: StarStatus) => void,
onRateLimit?: (waitMs: number) => void,
signal?: AbortSignal,
): Promise<StarAllResult> {
let starred = 0;
let failed = 0;
const isAborted = () => signal?.aborted === true;
// Abort during an inter-repo sleep should not be treated as a real error.
// sleepInterruptible resolves early on abort and the loop short-circuits.
const sleepInterruptible = async (ms: number) => {
if (isAborted()) return;
await new Promise<void>((resolve) => {
const t = setTimeout(resolve, ms);
const onAbort = () => {
clearTimeout(t);
resolve();
};
signal?.addEventListener("abort", onAbort, { once: true });
});
};
for (let i = 0; i < repos.length; i++) {
if (isAborted()) break;
const repo = repos[i];
onProgress(repo, "starring");
// Stop-after-current semantics: once `starRepo` is invoked, the PUT is
// allowed to finish regardless of an intervening abort. The signal is
// observed only at loop-boundary + inter-repo sleep. This gives the user
// an honest record of what was starred when they hit "Stop after
// current", rather than reverting a repo that actually landed on
// GitHub's side.
try {
await starRepo(token, repo.owner, repo.name);
starred++;
onProgress(repo, "starred");
} catch (err) {
if (err instanceof TokenExpiredError) throw err;
if (err instanceof NetworkError) throw err;
if (err instanceof ForbiddenError) throw err;
if (err instanceof RateLimitError) {
// Wait for retry-after then retry this repo.
const waitMs = err.retryAfterMs ?? 60_000;
onRateLimit?.(waitMs);
await sleepInterruptible(waitMs);
if (isAborted()) {
onProgress(repo, "unstarred");
break;
}
try {
await starRepo(token, repo.owner, repo.name);
starred++;
onProgress(repo, "starred");
} catch {
failed++;
onProgress(repo, "failed");
}
} else {
failed++;
onProgress(repo, "failed");
}
}
// Delay between stars to avoid abuse detection (except after the last one).
if (i < repos.length - 1) {
await sleepInterruptible(STAR_DELAY_MS);
}
}
return { starred, failed };
}
export interface RepoMeta {
stargazers_count: number;
description: string | null;
}
export type RepoMetaMap = Record<string, RepoMeta>;
/**
* Fetch public metadata for a single repository. Returns null on any error
* (non-200, network failure) so callers can use `Promise.allSettled`-style
* patterns without try/catch.
*/
export async function fetchRepoMeta(
owner: string,
name: string,
token: string | null,
signal?: AbortSignal,
): Promise<RepoMeta | null> {
try {
const headers = token ? authHeaders(token) : BASE_HEADERS;
const resp = await networkFetch(
`${GITHUB_API}/repos/${owner}/${name}`,
{ headers, signal },
);
if (resp.status === 403 || resp.status === 429) {
await handleForbiddenOrRateLimit(resp, `fetchRepoMeta ${owner}/${name}`);
}
if (!resp.ok) return null;
const data = (await resp.json()) as {
stargazers_count?: unknown;
description?: unknown;
};
if (typeof data.stargazers_count !== "number") return null;
return {
stargazers_count: data.stargazers_count,
description: typeof data.description === "string" ? data.description : null,
};
} catch (err) {
// AbortError is caller-initiated cancellation; RateLimitError and
// ForbiddenError signal all remaining requests will also fail.
// Re-throw so callers can react.
if (err instanceof DOMException && err.name === "AbortError") throw err;
if (err instanceof RateLimitError) throw err;
if (err instanceof ForbiddenError) throw err;
return null;
}
}
const VALID_GH_NAME = /^[a-zA-Z0-9._-]+$/;
/**
* Fetch metadata for all repos in a single GraphQL request.
* Requires authentication (GitHub GraphQL API does not support anonymous access).
* Returns a map of "owner/name" → RepoMeta. Throws RateLimitError or ForbiddenError on 403/429.
*/
export async function fetchAllRepoMetaGraphQL(
repos: Repository[],
token: string,
signal?: AbortSignal,
): Promise<RepoMetaMap> {
for (const repo of repos) {
if (!VALID_GH_NAME.test(repo.owner) || !VALID_GH_NAME.test(repo.name)) {
return {};
}
}
const fragments = repos.map((repo, i) =>
`repo${i}: repository(owner: "${repo.owner}", name: "${repo.name}") { stargazerCount description }`,
);
const query = `query { ${fragments.join(" ")} }`;
const resp = await networkFetch(`${GITHUB_API}/graphql`, {
method: "POST",
headers: {
...authHeaders(token),
"Content-Type": "application/json",
},
body: JSON.stringify({ query }),
signal,
});
if (resp.status === 403 || resp.status === 429) {
await handleForbiddenOrRateLimit(resp, "fetchAllRepoMetaGraphQL");
}
if (!resp.ok) return {};
const json = (await resp.json()) as {
data?: Record<
string,
{ stargazerCount: number; description: string | null } | null
>;
};
const result: RepoMetaMap = {};
if (json.data) {
repos.forEach((repo, i) => {
const entry = json.data?.[`repo${i}`];
if (entry && typeof entry.stargazerCount === "number") {
result[repoKey(repo)] = {
stargazers_count: entry.stargazerCount,
description:
typeof entry.description === "string" ? entry.description : null,
};
}
});
}
return result;
}
/** Thrown when the token is expired or revoked (HTTP 401). */
export class TokenExpiredError extends Error {
constructor() {
super("Token expired");
this.name = "TokenExpiredError";
}
}
/** Thrown when the network is unreachable (DNS, TLS, connectivity failures). */
export class NetworkError extends Error {
constructor(cause?: unknown) {
super("Network error", { cause });
this.name = "NetworkError";
}
}
/** Thrown when GitHub's secondary rate limit is hit (HTTP 429 or 403 with rate-limit body). */
export class RateLimitError extends Error {
retryAfterMs: number | null;
constructor(retryAfter: string | null) {
super("Rate limited");
this.name = "RateLimitError";
this.retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1000 : null;
}
}
/**
* Thrown when GitHub returns 403 for permission reasons (not rate limiting).
* Two distinct root causes depending on which token was in use:
* - Read path (isStarred, fetchRepoMeta, fetchAllRepoMetaGraphQL via the
* GitHub App session token): the App install is missing the required
* permission (e.g., Starring: None where Read was expected).
* - Write path (starRepo via the classic-OAuth ephemeral token): almost
* always an organisation-level OAuth app restriction, or a token the user
* revoked mid-flow. Not a GitHub App permission issue — GitHub App tokens
* can't star at all in this architecture (see CLAUDE.md on the hybrid
* OAuth approach).
* Callers should route this error to path-specific recovery advice.
*/
export class ForbiddenError extends Error {
constructor(context: string) {
super(`Permission denied: ${context}`);
this.name = "ForbiddenError";
}
}