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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
export class GitHubHTTPError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.name = "GitHubHTTPError";
this.status = status;
}
}
interface TreeItem {
contentType: "directory" | "file";
name: string;
path: string;
}
interface OverviewFile {
displayName: string;
richText?: string;
loaded: boolean;
}
interface IssueNode {
number: number;
title: string;
author?: {
login: string;
};
createdAt: string;
state: string;
labels?: {
edges: {
node: {
name: string;
};
}[];
};
}
interface IssueIndexPageQuery {
queryName: "IssueIndexPageQuery";
result: {
data: {
repository: {
search: {
edges: {
node: IssueNode;
}[];
};
};
};
};
}
type StylingDirective = [number, number, string];
interface Commit {
oid: string;
url: string;
authoredDate: string;
committedDate: string;
shortMessage: string;
bodyMessageHtml: string;
authors: {
login: string;
displayName: string;
avatarUrl: string;
path: string;
}[];
}
export interface CommitGroup {
title: string;
commits: Commit[];
}
interface RawPayload {
repo: {
ownerLogin: string;
name: string;
};
refInfo: { name: string };
path: string;
tree?: {
items: TreeItem[];
};
blob?: {
headerInfo: {
blobSize: string;
lineInfo: {
truncatedLoc: string | null;
truncatedSloc: string | null;
};
};
language: string | null;
image: boolean;
rawLines: string[] | null;
colorizedLines: string[] | null;
richText: string | null;
stylingDirectives: StylingDirective[][] | null;
};
overview?: {
overviewFiles?: OverviewFile[];
};
commitGroups?: CommitGroup[];
// Actually there may be other query types.
preloadedQueries?: IssueIndexPageQuery[];
}
function escapeHtml(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function applyStyling(line: string, directives: StylingDirective[]): string {
let result = "";
let lastPos = 0;
for (const [start, end, className] of directives) {
result += escapeHtml(line.substring(lastPos, start));
result += `<span class="${className}">${escapeHtml(line.substring(start, end))}</span>`;
lastPos = end;
}
result += escapeHtml(line.substring(lastPos));
return result;
}
interface GitHubCommon {
repo: {
owner: string;
name: string;
};
}
interface GitHubNav extends GitHubCommon {
branch: string;
path: string;
}
export interface GitHubTree extends GitHubNav {
items: TreeItem[];
overviewHtml?: Record<string, string>;
}
export interface RepoInfo {
description: string | null;
website: string | null;
stars: string | null;
watchers: string | null;
forks: string | null;
numReleases?: number;
}
export interface GitHubRepo extends GitHubTree {
info: RepoInfo;
}
export interface GitHubBlob extends GitHubNav {
language: string | null;
size: string;
image: boolean;
textLines: string[] | null;
htmlLines: string[] | null;
htmlContent: string | null;
}
interface Issue {
number: number;
title: string;
state: string;
createdAt: string;
numReplies?: number;
numPRs?: number;
}
export interface GitHubIssues extends GitHubCommon {
issues: Issue[];
}
export interface GitHubCommits extends GitHubNav {
commitGroups: CommitGroup[];
}
interface Release {
tagName: string;
title: string;
publishedAt: string;
bodyHtml: string;
}
export interface GitHubReleases extends GitHubCommon {
releases: Release[];
}
async function fetchGitHubPage(path: string): Promise<string> {
// GitHub throttles/blocks requests without realistic browser headers.
// These headers make the request appear as a standard browser visit.
const headers = {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
};
const response = await fetch(`https://github.com/${path}`, { headers });
if (!response.ok) throw new GitHubHTTPError(response.status, response.statusText);
return response.text();
}
function parsePayload(html: string, dataTarget: string, prop: string[]): RawPayload | null {
const regex = new RegExp(
`<script type="application/json" data-target="${dataTarget}">([^<]+)</script>`,
"g",
);
match: for (const match of html.matchAll(regex)) {
let data;
try {
data = JSON.parse(match[1]);
} catch {
continue;
}
for (const key of prop) {
data = data[key];
if (data === undefined) continue match;
}
return data;
}
return null;
}
function parseRepoInfo(html: string): RepoInfo {
const descMatch = html.match(/<p[^>]*class="[^"]*f4[^"]*"[^>]*>([\s\S]*?)<\/p>/);
const description = descMatch?.[1]?.replace(/<[^>]+>/g, "").trim() || null;
const websiteMatch = html.match(/<a[^>]*rel="noopener noreferrer nofollow"[^>]*href="([^"]+)"/);
const starsMatch = html.match(/id="repo-stars-counter-star"[^>]*>([^<]+)/);
const watchersMatch = html.match(/<strong>(\d+)<\/strong>\s*watching/);
const forksMatch = html.match(/id="repo-network-counter"[^>]*>([^<]+)/);
let numReleases: number | undefined;
const releaseCountMatch = html.match(
/<a[^>]*href="[^"]*\/releases"[^>]*>[\s\S]*?<span[^>]*title="(\d+)"[^>]*(?:class="[^"]*Counter|data-view-component="true")[^>]*>/,
);
if (releaseCountMatch) {
numReleases = parseInt(releaseCountMatch[1], 10);
}
return {
description,
website: websiteMatch?.[1] || null,
stars: starsMatch?.[1]?.trim() || null,
watchers: watchersMatch?.[1] || null,
forks: forksMatch?.[1]?.trim() || null,
numReleases,
};
}
function extractOverviewHtml(payload: RawPayload): Record<string, string> | undefined {
const result: Record<string, string> = {};
for (const file of payload.overview?.overviewFiles ?? []) {
if (file.loaded && file.richText !== undefined) {
result[file.displayName] = file.richText;
}
}
return Object.keys(result).length > 0 ? result : undefined;
}
function extractGitHub<T>(payload: RawPayload, extra: T): GitHubNav & T {
return {
repo: {
owner: payload.repo.ownerLogin,
name: payload.repo.name,
},
branch: payload.refInfo.name,
path: payload.path,
...extra,
};
}
export async function getGitHubRepo(owner: string, repo: string): Promise<GitHubRepo> {
const html = await fetchGitHubPage(`${owner}/${repo}`);
const payload = parsePayload(html, "react-partial.embeddedData", ["props", "initialPayload"]);
if (payload?.tree === undefined) {
throw new Error("Could not find tree data in embedded JSON");
}
const info = parseRepoInfo(html);
const overviewHtml = extractOverviewHtml(payload);
return extractGitHub(payload, { items: payload.tree.items, info, overviewHtml });
}
export async function getGitHubTree(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<GitHubTree> {
const html = await fetchGitHubPage(`${owner}/${repo}/tree/${branch}/${path}`);
// GitHub uses different formats for root vs subdirectories.
const payload =
// Root directory format.
parsePayload(html, "react-partial.embeddedData", ["props", "initialPayload"]) ||
// Subdirectory format.
parsePayload(html, "react-app.embeddedData", ["payload"]);
if (payload?.tree === undefined) {
throw new Error("Could not find tree data in embedded JSON");
}
const overviewHtml = extractOverviewHtml(payload);
return extractGitHub(payload, { items: payload.tree.items, overviewHtml });
}
export async function getGitHubBlob(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<GitHubBlob> {
const html = await fetchGitHubPage(`${owner}/${repo}/blob/${branch}/${path}`);
const payload = parsePayload(html, "react-app.embeddedData", ["payload"]);
if (payload?.blob === undefined) {
throw new Error("Could not find blob data in embedded JSON");
}
const blob = payload.blob;
const { headerInfo, stylingDirectives } = blob;
let htmlLines = blob.colorizedLines;
if (!htmlLines && stylingDirectives && blob.rawLines) {
htmlLines = blob.rawLines.map((line, i) => applyStyling(line, stylingDirectives[i]));
}
let size = headerInfo.blobSize;
if (headerInfo.lineInfo.truncatedLoc) {
size += ` / ${headerInfo.lineInfo.truncatedLoc} lines`;
}
if (headerInfo.lineInfo.truncatedSloc) {
size += ` / ${headerInfo.lineInfo.truncatedSloc} loc`;
}
return extractGitHub(payload, {
language: blob.language || null,
size,
image: blob.image,
textLines: blob.rawLines,
htmlLines,
htmlContent: blob.richText,
});
}
export async function getGitHubIssues(owner: string, repo: string): Promise<GitHubIssues> {
const html = await fetchGitHubPage(`${owner}/${repo}/issues`);
const payload = parsePayload(html, "react-app.embeddedData", ["payload"]);
let issuesQuery: IssueIndexPageQuery | undefined;
for (const query of payload?.preloadedQueries ?? []) {
if (query.queryName === "IssueIndexPageQuery") {
issuesQuery = query;
}
}
if (issuesQuery === undefined) {
throw new Error("Could not find IssueIndexPageQuery in embedded JSON");
}
const issues: Issue[] = issuesQuery.result.data.repository.search.edges.map((edge) => {
const { number, title, state, createdAt } = edge.node;
return { number, title, state, createdAt };
});
return {
repo: {
owner,
name: repo,
},
issues,
};
}
export async function getGitHubCommits(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<GitHubCommits> {
const urlPath = path
? `${owner}/${repo}/commits/${branch}/${path}`
: `${owner}/${repo}/commits/${branch}`;
const html = await fetchGitHubPage(urlPath);
const payload = parsePayload(html, "react-app.embeddedData", ["payload"]);
if (payload?.commitGroups === undefined) {
throw new Error("Could not find commit history in embedded JSON");
}
return {
repo: {
owner,
name: repo,
},
branch,
path,
commitGroups: payload.commitGroups,
};
}
export async function getGitHubReleases(owner: string, repo: string): Promise<GitHubReleases> {
const html = await fetchGitHubPage(`${owner}/${repo}/releases`);
// Releases page doesn't have embedded JSON, so we parse HTML directly
const releases: Release[] = [];
// Find all h2 tags with version numbers
const h2Regex = /<h2[^>]*id="([^"]*)"[^>]*>(v[^<]*)<\/h2>/g;
for (const h2Match of html.matchAll(h2Regex)) {
const id = h2Match[1];
const title = h2Match[2].trim();
// Find the section containing this h2
const sectionRegex = new RegExp(
`<section[^>]*>\\s*<h2[^>]*id="${id}"[^>]*>[^<]*<\\/h2>([\\s\\S]*?)<\\/section>`,
"s",
);
const sectionMatch = html.match(sectionRegex);
if (!sectionMatch) continue;
const sectionHtml = sectionMatch[1];
// Extract tag name from link
const tagMatch = sectionHtml.match(/<a[^>]*href="[^"]*\/tree\/([^"]+)"[^>]*>/);
const tagName = tagMatch?.[1] || title;
// Extract published date
const dateMatch = sectionHtml.match(/<relative-time[^>]*datetime="([^"]*)"[^>]*>/);
const publishedAt = dateMatch?.[1] || "";
// Extract body HTML (markdown content)
const bodyMatch = sectionHtml.match(/<div[^>]*class="[^"]*markdown-body[^"]*"[^>]*>([\s\S]*?)<\/div>/);
const bodyHtml = bodyMatch?.[1]?.trim() || "";
releases.push({
tagName,
title,
publishedAt,
bodyHtml,
});
}
return {
repo: {
owner,
name: repo,
},
releases,
};
}
|