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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
|
import { parseDocument } from "htmlparser2";
import { isText, type AnyNode, type Element } from "domhandler";
import { getAttributeValue, getInnerHTML, textContent } from "domutils";
import * as cssSelect from "css-select";
export class GitHubHTTPError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.name = "GitHubHTTPError";
this.status = status;
}
}
export abstract class RedirectError extends Error {
location: string;
constructor(location: string) {
super(`Redirect to ${location}`);
this.name = this.constructor.name;
this.location = location;
}
}
export class InternalRedirectError extends RedirectError {}
export class ExternalRedirectError extends RedirectError {}
interface TreeItem {
contentType: "directory" | "file";
name: string;
path: string;
}
interface OverviewFile {
displayName: string;
// null when GitHub includes the file without rendered content.
richText?: string | null;
}
interface IssueNode {
number: number;
title?: string;
titleHtml?: string;
author?: {
login: string;
};
createdAt: string;
state: string;
labels?: {
edges: {
node: IssueLabelNode;
}[];
};
}
interface IssueIndexPageQuery {
queryName: "IssueIndexPageQuery";
result: {
data: {
repository: {
search: {
edges: {
node: IssueNode;
}[];
};
};
};
};
}
interface IssueActor {
login: string;
}
interface IssueLabelNode {
name: string;
color: string;
description: string | null;
}
// One entry of an issue's timeline. Every event type has its own fields, so all
// but the ones every item carries are optional here.
interface IssueTimelineNode {
__typename: string;
createdAt: string;
databaseId?: number;
// Comments only; events name whoever performed them as the actor.
author?: IssueActor | null;
url?: string;
authorAssociation?: string;
bodyHTML?: string;
actor?: IssueActor | null;
label?: IssueLabelNode;
assignee?: IssueActor | null;
blockedUser?: IssueActor | null;
milestoneTitle?: string;
stateReason?: string | null;
lockReason?: string | null;
previousTitle?: string;
currentTitle?: string;
commit?: { url?: string; abbreviatedOid?: string } | null;
}
interface IssueViewerNode {
number: number;
databaseId: number;
// Titles are Markdown, so they can carry code spans and emoji.
titleHTML: string;
state: string;
stateReason: string | null;
createdAt: string;
author: IssueActor | null;
bodyHTML: string;
labels?: { edges: { node: IssueLabelNode }[] };
assignedActors?: { nodes: IssueActor[] };
milestone?: { title: string; url: string } | null;
// The oldest page of the timeline; GitHub fetches the rest as you scroll.
frontTimelineItems: {
totalCount: number;
edges: { node: IssueTimelineNode }[];
};
}
interface IssueViewerViewQuery {
queryName: "IssueViewerViewQuery";
result: {
data: {
repository: {
issue: IssueViewerNode;
};
};
};
}
type PreloadedQuery = IssueIndexPageQuery | IssueViewerViewQuery;
type StylingDirective = [number, number, string];
export 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 CodeViewLayoutRoute {
repo: {
ownerLogin: string;
name: string;
};
refInfo: {
name: string;
};
path: string;
}
interface CodeViewRepoRoute {
tree: {
items: TreeItem[];
};
overview?: {
overviewFiles?: OverviewFile[];
};
}
interface CodeViewTreeRoute {
tree: {
items: TreeItem[];
readme?: OverviewFile;
};
}
interface BlobHeaderInfo {
blobSize: string;
lineInfo?: {
truncatedLoc: string | null;
truncatedSloc: string | null;
};
}
interface CodeViewBlobLayoutRoute {
blob: {
headerInfo: BlobHeaderInfo;
language: string | null;
image: boolean;
};
}
interface CodeViewBlobLayoutRouteStyledBlob {
rawLines: string[] | null;
colorizedLines: string[] | null;
stylingDirectives: StylingDirective[][] | null;
}
interface CommitsRefRoute {
commitGroups: CommitGroup[];
}
interface CodeViewBlobRoute {
richText: string | null;
renderedFileInfo?: { renderFileType: string | null } | null;
}
interface SidebarAbout {
description?: string | null;
website?: string | null;
stargazerCount?: number;
forksCount?: number;
// A section is false when the repository has none of that thing.
sections?: { releases?: { releaseCount?: number } | false };
}
interface AppPayload {
sidebarAbout?: SidebarAbout;
codeViewLayoutRoute?: CodeViewLayoutRoute;
codeViewRepoRoute?: CodeViewRepoRoute;
codeViewTreeRoute?: CodeViewTreeRoute;
codeViewBlobLayoutRoute?: CodeViewBlobLayoutRoute;
"codeViewBlobLayoutRoute.StyledBlob"?: CodeViewBlobLayoutRouteStyledBlob;
codeViewBlobRoute?: CodeViewBlobRoute;
commitsRefRoute?: CommitsRefRoute;
// Actually there may be other query types.
preloadedQueries?: PreloadedQuery[];
}
function escapeHtml(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// Renders directives[i..] within [rangeStart, rangeEnd], producing nested spans.
// Returns the rendered content and the index of the first directive not consumed.
function renderRange(
line: string,
directives: StylingDirective[],
i: number,
rangeStart: number,
rangeEnd: number,
): [string, number] {
let result = "";
let pos = rangeStart;
while (i < directives.length) {
const [start, end, className] = directives[i];
// Stop at directives that start outside or extend past this range.
if (start >= rangeEnd || end > rangeEnd) break;
// Combine classes for directives with identical ranges.
let classes = className;
let j = i + 1;
while (j < directives.length && directives[j][0] === start && directives[j][1] === end) {
classes += " " + directives[j][2];
j++;
}
result += escapeHtml(line.substring(pos, start));
const [innerContent, nextI] = renderRange(line, directives, j, start, end);
result += `<span class="${classes}">${innerContent}</span>`;
pos = end;
i = nextI;
}
result += escapeHtml(line.substring(pos, rangeEnd));
return [result, i];
}
function applyStyling(line: string, directives: StylingDirective[]): string {
// Sort by start ascending, then end descending so outer spans precede nested ones.
const sorted = [...directives].sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : b[1] - a[1]));
const [result] = renderRange(line, sorted, 0, 0, line.length);
return result;
}
interface GitHubCommon {
repo: {
owner: string;
name: string;
};
}
interface GitHubRepoPage extends GitHubCommon {
// Enabled features, like issues or wiki.
tabs: string[] | null;
}
interface GitHubNav extends GitHubRepoPage {
branch: string;
path: string;
}
export interface GitHubTree extends GitHubNav {
items: TreeItem[];
overviewHtml?: Record<string, string | null>;
}
export interface RepoInfo {
description: string | null;
website: string | null;
stars: 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;
renderFileType: string | null;
textLines: string[] | null;
htmlLines: string[] | null;
htmlContent: string | null;
}
export interface IssueLabel {
name: string;
// GitHub's own hex color for the label, without a leading "#".
color: string;
description: string | null;
}
interface Issue {
number: number;
title: string;
// OPEN or CLOSED for an issue; a pull request is also DRAFT or MERGED.
state: string;
createdAt: string;
labels: IssueLabel[];
numReplies?: number;
numPRs?: number;
}
export interface GitHubIssues extends GitHubRepoPage {
issues: Issue[];
q?: string;
}
export interface GitHubPulls extends GitHubRepoPage {
pulls: Issue[];
q?: string;
}
export interface IssueComment {
// What GitHub's permalinks point at, e.g. "issuecomment-3090316111".
anchor: string;
author: string | null;
// The badge beside the author: Owner, Member, Contributor, ...
association: string | null;
createdAt: string;
bodyHtml: string;
}
export interface IssueEvent {
// What GitHub's permalinks point at, e.g. "event-25375447940".
anchor: string;
actor: string | null;
createdAt: string;
// What happened, in GitHub's own words, e.g. "closed this as completed".
description: string;
// What the description ends on, when it names one: a label, or anything else
// worth linking (the assignee, the commit that referenced the issue, ...).
label: IssueLabel | null;
link: { text: string; href: string } | null;
}
export type IssueTimelineItem =
| ({ kind: "comment" } & IssueComment)
| ({ kind: "event" } & IssueEvent);
export interface GitHubIssue extends GitHubRepoPage {
number: number;
titleHtml: string;
state: string;
// Why a closed issue was closed: COMPLETED, NOT_PLANNED, DUPLICATE.
stateReason: string | null;
createdAt: string;
labels: IssueLabel[];
assignees: string[];
milestone: { title: string; href: string } | null;
// The issue itself, rendered like the comments that follow it.
body: IssueComment;
timeline: IssueTimelineItem[];
// Timeline items GitHub fetches as you scroll rather than sending with the
// page, so we never see them.
hiddenItems: number;
}
interface DiscussionCategory {
slug: string;
name: string;
// Every category has an emoji, but GitHub only prints it in some places.
emoji: string | null;
}
interface DiscussionSummary {
number: number;
// Titles are Markdown, so they can carry code spans and emoji.
titleHtml: string;
author: string | null;
createdAt: string | null;
category: DiscussionCategory | null;
answered: boolean;
}
export interface DiscussionComment {
// What GitHub's permalinks point at, e.g. "discussioncomment-9174663".
anchor: string;
author: string | null;
createdAt: string | null;
// The badges beside the author: Author, Maintainer, Collaborator, ...
labels: string[];
bodyHtml: string;
// How GitHub words a comment it keeps collapsed, e.g. "This comment has been
// hidden." or "This comment was marked as off-topic."; null when the comment
// is shown as usual. A collapsed comment arrives with no author or body.
hiddenReason: string | null;
isAnswer: boolean;
replies: DiscussionComment[];
// Replies GitHub keeps collapsed behind a "show more" button of its own.
hiddenReplies: number;
}
export interface GitHubDiscussions extends GitHubRepoPage {
discussions: DiscussionSummary[];
categories: DiscussionCategory[];
// The category the list is narrowed to, if any.
category: DiscussionCategory | null;
q?: string;
}
export interface GitHubDiscussion extends GitHubRepoPage {
number: number;
titleHtml: string;
author: string | null;
category: DiscussionCategory | null;
// The comment a Q&A discussion is answered by, as an anchor on this page.
answerAnchor: string | null;
body: DiscussionComment;
comments: DiscussionComment[];
}
export interface GitHubCommits extends GitHubNav {
commitGroups: CommitGroup[];
}
export interface LatestCommit {
oid: string;
date: string;
shortMessageHtml: string;
authors: {
login: string;
displayName: string;
avatarUrl: string;
}[];
}
export interface GitHubLatestCommit extends GitHubCommon {
commit: LatestCommit;
}
export interface Language {
// "Other" for the aggregate bucket GitHub reports without a name.
name: string;
percentage: number;
color: string | null;
}
export interface LatestRelease {
tag: string;
publishedAt: string | null;
}
export interface GitHubSidebar extends GitHubCommon {
languages: Language[];
latestRelease: LatestRelease | null;
}
export interface Ref {
name: string;
isDefault?: boolean;
date?: string;
authorLogin?: string;
}
export interface GitHubRefs extends GitHubCommon {
type: "branches" | "tags";
refs: Ref[];
hasMore: boolean;
}
interface Release {
tagName: string;
title: string;
publishedAt: string;
bodyHtml: string;
assets?: ReleaseAsset[];
}
export interface ReleaseAsset {
name: string;
url: string;
size?: string;
publishedAt?: string;
}
export interface GitHubReleases extends GitHubRepoPage {
releases: Release[];
}
export interface GitHubRelease extends GitHubRepoPage {
release: Release;
}
export interface WikiPage {
name: string;
href: string;
updatedAt?: string;
}
export interface GitHubWiki extends GitHubRepoPage {
title: string;
updatedBy: string | null;
updatedAt: string | null;
bodyHtml: string;
sidebarHtml: string | null;
footerHtml: string | null;
pages: WikiPage[];
}
export interface GitHubWikiPages extends GitHubRepoPage {
pages: WikiPage[];
}
interface OwnerRepo {
owner: string;
name: string;
description?: string;
language?: string;
stars?: string;
forks?: string;
}
interface OwnerNavItem {
text: string;
href: string;
}
export interface GitHubOwner {
type: "user" | "organization";
login: string;
name: string | null;
bio: string | null;
avatarUrl: string | null;
followers: string | null;
// Users only; always null for organizations.
following: string | null;
company: string | null;
location: string | null;
links: { text: string; href: string }[];
nav: OwnerNavItem[];
pinned: OwnerRepo[];
// The repository list on organization overviews; empty for users.
repos: OwnerRepo[];
}
// GitHub throttles/blocks requests without realistic browser headers.
// These headers make the request appear as a standard browser visit.
function browserHeaders(accept: string): Record<string, string> {
return {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
Accept: accept,
"Accept-Language": "en-US,en;q=0.5",
};
}
async function fetchGitHubResponse(path: string, accept: string): Promise<Response> {
const headers = browserHeaders(accept);
const response = await fetch(`https://github.com/${path}`, { headers, redirect: "manual" });
if (response.status === 301 || response.status === 302) {
const location = response.headers.get("location");
if (location) {
const url = new URL(location);
if (url.hostname === "github.com") {
throw new InternalRedirectError(url.pathname);
}
}
}
if (!response.ok) throw new GitHubHTTPError(response.status, response.statusText);
return response;
}
async function fetchGitHubPage(path: string): Promise<string> {
const response = await fetchGitHubResponse(
path,
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
);
return response.text();
}
// Some routes (e.g. latest-commit) are the same endpoints the React UI itself
// calls to lazily fill in data; they 406 without an explicit JSON Accept.
async function fetchGitHubJson<T>(path: string): Promise<T> {
const response = await fetchGitHubResponse(path, "application/json");
return (await response.json()) as T;
}
function parseEmbeddedPayload<T>(html: string): T | null {
const regex = new RegExp(
`<script type="application/json" data-target="react-app.embeddedData">([^<]+)</script>`,
"g",
);
match: for (const match of html.matchAll(regex)) {
let data;
try {
data = JSON.parse(match[1]);
} catch {
continue;
}
if (data.payload === undefined) continue match;
return data.payload;
}
return null;
}
function parsePayload(html: string): AppPayload | null {
return parseEmbeddedPayload<AppPayload>(html);
}
function extractOverviewHtml(
overviewFiles: OverviewFile[] = [],
): Record<string, string | null> | undefined {
const result: Record<string, string | null> = {};
for (const file of overviewFiles) {
if (file.richText !== undefined) {
result[file.displayName] = file.richText;
}
}
return Object.keys(result).length > 0 ? result : undefined;
}
function parseRepoTabs(html: string): string[] | null {
const nav = /<nav\b[^>]*\baria-label="Repository"[^>]*>([\s\S]*?)<\/nav>/.exec(html);
if (!nav) return null;
return Array.from(nav[1].matchAll(/\bid="([a-z-]+)-tab"/g), (match) => match[1]);
}
function extractGitHub<T>(html: string, payload: AppPayload | null, extra: T): GitHubNav & T {
const layoutRoute = payload?.codeViewLayoutRoute;
if (layoutRoute === undefined) {
throw new Error("Missing codeViewLayoutRoute");
}
return {
repo: {
owner: layoutRoute.repo.ownerLogin,
name: layoutRoute.repo.name,
},
tabs: parseRepoTabs(html),
branch: layoutRoute.refInfo.name,
path: layoutRoute.path,
...extra,
};
}
export async function getGitHubRepo(owner: string, repo: string): Promise<GitHubRepo> {
const html = await fetchGitHubPage(`${owner}/${repo}`);
const payload = parsePayload(html);
const repoRoute = payload?.codeViewRepoRoute;
if (repoRoute?.tree === undefined) {
throw new Error("Could not find tree data in embedded JSON");
}
const about = payload?.sidebarAbout;
const releases = about?.sections?.releases;
const info = {
description: about?.description ?? null,
website: about?.website ?? null,
stars: about?.stargazerCount?.toLocaleString("en-US") ?? null,
forks: about?.forksCount?.toLocaleString("en-US") ?? null,
numReleases: releases === false ? undefined : releases?.releaseCount,
};
const overviewHtml = extractOverviewHtml(repoRoute.overview?.overviewFiles);
return extractGitHub(html, payload, { items: repoRoute.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}`);
const payload = parsePayload(html);
const treeRoute = payload?.codeViewTreeRoute;
if (treeRoute?.tree === undefined) {
throw new Error("Could not find tree data in embedded JSON");
}
const overviewHtml = extractOverviewHtml(treeRoute.tree.readme ? [treeRoute.tree.readme] : []);
return extractGitHub(html, payload, { items: treeRoute.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);
const blob = payload?.codeViewBlobLayoutRoute?.blob;
const styledBlob = payload?.["codeViewBlobLayoutRoute.StyledBlob"];
if (blob === undefined) {
throw new Error("Could not find blob data in embedded JSON");
}
let htmlLines = styledBlob?.colorizedLines ?? null;
const stylingDirectives = styledBlob?.stylingDirectives;
if (!htmlLines && stylingDirectives && styledBlob?.rawLines) {
htmlLines = styledBlob.rawLines.map((line, i) => applyStyling(line, stylingDirectives[i]));
}
let size = blob.headerInfo.blobSize;
if (blob.headerInfo.lineInfo?.truncatedLoc) {
size += ` / ${blob.headerInfo.lineInfo.truncatedLoc} lines`;
}
if (blob.headerInfo.lineInfo?.truncatedSloc) {
size += ` / ${blob.headerInfo.lineInfo.truncatedSloc} loc`;
}
return extractGitHub(html, payload, {
language: blob.language || null,
size,
image: blob.image,
renderFileType: payload?.codeViewBlobRoute?.renderedFileInfo?.renderFileType ?? null,
textLines: styledBlob?.rawLines ?? null,
htmlLines,
htmlContent: payload?.codeViewBlobRoute?.richText ?? null,
});
}
// For proxying embeds.
export async function getGitHubRaw(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<Response> {
const url = `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${path}`;
const response = await fetch(url, { headers: browserHeaders("*/*") });
if (!response.ok) throw new GitHubHTTPError(response.status, response.statusText);
return response;
}
function parseIssueLabel(label: IssueLabelNode): IssueLabel {
return { name: label.name, color: label.color, description: label.description ?? null };
}
// A label in a list row wears its color as the RGB channels GitHub's own CSS
// mixes with, rather than as the hex its issue payloads carry, and keeps its
// description in a tooltip pointing back at it.
function parseRowLabel(label: Element, row: Element): IssueLabel {
const style = getAttributeValue(label, "style") ?? "";
const channels: number[] = [];
for (const channel of ["r", "g", "b"]) {
const value = new RegExp(`--label-${channel}:\\s*(\\d+)`).exec(style);
if (value) channels.push(parseInt(value[1], 10));
}
const id = getAttributeValue(label, "id");
const tooltip = id ? cssSelect.selectOne(`tool-tip[for="${id}"]`, row) : null;
return {
name: getAttributeValue(label, "data-name") ?? textContent(label).trim(),
// GitHub's own default gray, for a label whose color it did not spell out.
color:
channels.length === 3
? channels.map((c) => c.toString(16).padStart(2, "0")).join("")
: "ededed",
description: tooltip ? textContent(tooltip).trim() || null : null,
};
}
export async function getGitHubIssues(
owner: string,
repo: string,
// We only support is:issue searches (no PR results).
{ q }: { q?: string } = {},
): Promise<GitHubIssues> {
const suffix = q ? `?q=${encodeURIComponent(q)}` : "";
const html = await fetchGitHubPage(`${owner}/${repo}/issues${suffix}`);
const payload = parsePayload(html);
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, titleHtml, state, createdAt, labels } = edge.node;
const finalTitle = (title || titleHtml || "").replace(/<[^>]+>/g, "");
return {
number,
title: finalTitle,
state,
createdAt,
labels: (labels?.edges ?? []).map((label) => parseIssueLabel(label.node)),
};
});
return { repo: { owner, name: repo }, tabs: parseRepoTabs(html), issues, q };
}
// GitHub's payloads carry absolute URLs; the same paths on our host are ours.
function githubPath(url: string | undefined): string | null {
const parsed = url ? URL.parse(url) : null;
return parsed?.hostname === "github.com" ? parsed.pathname : null;
}
// "FIRST_TIME_CONTRIBUTOR" is a badge; "NONE" is everybody else, and no badge.
function issueAssociation(association: string | undefined): string | null {
if (!association || association === "NONE") return null;
const words = association.toLowerCase().replace(/_/g, " ");
return words.charAt(0).toUpperCase() + words.slice(1);
}
function parseIssueComment(node: IssueTimelineNode): IssueComment {
return {
anchor: node.url?.split("#")[1] ?? "",
author: node.author?.login ?? null,
association: issueAssociation(node.authorAssociation),
createdAt: node.createdAt,
bodyHtml: node.bodyHTML ?? "",
};
}
function userLink(user: IssueActor | null | undefined): { text: string; href: string } | null {
return user ? { text: user.login, href: `/${user.login}` } : null;
}
// GitHub's timeline carries more event types than it shows, and shows more than
// are worth a line here; anything this does not describe is left out.
function parseIssueEvent(node: IssueTimelineNode): IssueEvent | null {
const event = {
anchor: node.databaseId ? `event-${node.databaseId}` : "",
actor: node.actor?.login ?? null,
createdAt: node.createdAt,
label: null,
link: null,
};
switch (node.__typename) {
case "ClosedEvent":
return {
...event,
description:
node.stateReason === "NOT_PLANNED"
? "closed this as not planned"
: node.stateReason === "DUPLICATE"
? "closed this as a duplicate"
: "closed this as completed",
};
case "ReopenedEvent":
return { ...event, description: "reopened this" };
case "LabeledEvent":
case "UnlabeledEvent":
if (!node.label) return null;
return {
...event,
description: node.__typename === "LabeledEvent" ? "added the" : "removed the",
label: parseIssueLabel(node.label),
};
case "AssignedEvent":
case "UnassignedEvent": {
const assigned = node.__typename === "AssignedEvent";
// GitHub words an assignment to oneself without naming anybody.
if (node.assignee && node.assignee.login === event.actor) {
return {
...event,
description: assigned ? "self-assigned this" : "removed their assignment",
};
}
return {
...event,
description: assigned ? "assigned this to" : "unassigned",
link: userLink(node.assignee),
};
}
case "MilestonedEvent":
return { ...event, description: `added this to the ${node.milestoneTitle} milestone` };
case "DemilestonedEvent":
return { ...event, description: `removed this from the ${node.milestoneTitle} milestone` };
case "RenamedTitleEvent":
return { ...event, description: `changed the title to ${node.currentTitle}` };
case "LockedEvent":
return {
...event,
description: node.lockReason
? `locked this conversation as ${node.lockReason.toLowerCase().replace(/_/g, " ")}`
: "locked this conversation",
};
case "UnlockedEvent":
return { ...event, description: "unlocked this conversation" };
case "PinnedEvent":
return { ...event, description: "pinned this" };
case "UnpinnedEvent":
return { ...event, description: "unpinned this" };
case "ReferencedEvent": {
const href = githubPath(node.commit?.url);
if (!href || !node.commit?.abbreviatedOid) return null;
return {
...event,
description: "referenced this in commit",
link: { text: node.commit.abbreviatedOid, href },
};
}
case "UserBlockedEvent":
return { ...event, description: "blocked", link: userLink(node.blockedUser) };
default:
return null;
}
}
export async function getGitHubIssue(
owner: string,
repo: string,
number: number,
): Promise<GitHubIssue> {
const html = await fetchGitHubPage(`${owner}/${repo}/issues/${number}`);
const payload = parsePayload(html);
let issue: IssueViewerNode | undefined;
for (const query of payload?.preloadedQueries ?? []) {
if (query.queryName === "IssueViewerViewQuery") {
issue = query.result.data.repository.issue;
}
}
if (issue === undefined) {
throw new Error("Could not find IssueViewerViewQuery in embedded JSON");
}
const timeline: IssueTimelineItem[] = [];
for (const edge of issue.frontTimelineItems.edges) {
if (edge.node.__typename === "IssueComment") {
timeline.push({ kind: "comment", ...parseIssueComment(edge.node) });
continue;
}
const event = parseIssueEvent(edge.node);
if (event) timeline.push({ kind: "event", ...event });
}
const labels: IssueLabel[] = [];
for (const edge of issue.labels?.edges ?? []) {
labels.push(parseIssueLabel(edge.node));
}
const assignees: string[] = [];
for (const assignee of issue.assignedActors?.nodes ?? []) {
assignees.push(assignee.login);
}
const milestoneHref = githubPath(issue.milestone?.url);
return {
repo: { owner, name: repo },
tabs: parseRepoTabs(html),
number: issue.number,
titleHtml: issue.titleHTML,
state: issue.state,
stateReason: issue.stateReason,
createdAt: issue.createdAt,
labels,
assignees,
milestone:
issue.milestone && milestoneHref
? { title: issue.milestone.title, href: milestoneHref }
: null,
body: {
// The issue's own permalink, which GitHub anchors on its database id.
anchor: `issue-${issue.databaseId}`,
author: issue.author?.login ?? null,
association: null,
createdAt: issue.createdAt,
bodyHtml: issue.bodyHTML,
},
timeline,
hiddenItems: issue.frontTimelineItems.totalCount - issue.frontTimelineItems.edges.length,
};
}
export async function getGitHubPulls(
owner: string,
repo: string,
{ q }: { q?: string } = {},
): Promise<GitHubPulls> {
const suffix = q ? `?q=${encodeURIComponent(q)}` : "";
const html = await fetchGitHubPage(`${owner}/${repo}/pulls${suffix}`);
const document = parseDocument(html);
const pulls: Issue[] = [];
for (const row of cssSelect.selectAll(".js-issue-row", document) as unknown as Element[]) {
const id = getAttributeValue(row, "id"); // "issue_NNN"
const number = id ? parseInt(id.replace("issue_", ""), 10) : 0;
if (!number) continue;
const titleLink = cssSelect.selectOne("a.markdown-title", row);
const title = titleLink ? textContent(titleLink).trim() : "";
const timeElem = cssSelect.selectOne("relative-time", row);
const createdAt = timeElem ? getAttributeValue(timeElem, "datetime") || "" : "";
const stateSpan = cssSelect.selectOne(".tooltipped[aria-label]", row) as Element | null;
const ariaLabel = stateSpan ? (getAttributeValue(stateSpan, "aria-label") ?? "") : "";
const state = ariaLabel.includes("Merged")
? "MERGED"
: ariaLabel.includes("Closed")
? "CLOSED"
: ariaLabel.includes("Draft")
? "DRAFT"
: "OPEN";
const labels: IssueLabel[] = [];
for (const label of cssSelect.selectAll("a.IssueLabel", row) as unknown as Element[]) {
labels.push(parseRowLabel(label, row));
}
pulls.push({ number, title, state, createdAt, labels });
}
return { repo: { owner, name: repo }, tabs: parseRepoTabs(html), pulls, q };
}
// A category link names the category on its own in a discussion's header and
// in a list row; in the sidebar it wraps an emoji and a label of its own.
function parseDiscussionCategory(link: Element | null): DiscussionCategory | null {
const href = link ? getAttributeValue(link, "href") : undefined;
// A list row's category link carries the filter it would apply as a query.
const slug = href?.split("?")[0].split("/").pop();
if (!link || !slug) return null;
const label = cssSelect.selectOne(".ActionList-item-label", link) ?? link;
const emoji = cssSelect.selectOne("g-emoji", link);
return {
slug,
name: textContent(label).replace(/\s+/g, " ").trim(),
emoji: emoji ? textContent(emoji).trim() : null,
};
}
function parseDiscussionCategories(document: AnyNode): DiscussionCategory[] {
const categories: DiscussionCategory[] = [];
const links = cssSelect.selectAll(
'nav[aria-label="Categories"] a[href*="/discussions/categories/"]',
document,
) as unknown as Element[];
for (const link of links) {
const category = parseDiscussionCategory(link);
if (category) categories.push(category);
}
return categories;
}
function parseDiscussionSummary(row: Element): DiscussionSummary | null {
const link = cssSelect.selectOne('a[data-hovercard-type="discussion"]', row);
const href = link ? getAttributeValue(link, "href") : undefined;
const number = parseInt(href?.split("/").pop() ?? "", 10);
if (!link || !number) return null;
const time = cssSelect.selectOne("relative-time", row);
return {
number,
titleHtml: getInnerHTML(link).trim(),
author: selectText('a[aria-label$="(author)"]', row),
createdAt: time ? (getAttributeValue(time, "datetime") ?? null) : null,
category: parseDiscussionCategory(cssSelect.selectOne('a[aria-label$="(category)"]', row)),
// An answered thread is called out in green beside its category.
answered: cssSelect.selectOne(".color-fg-success", row) !== null,
};
}
export async function getGitHubDiscussions(
owner: string,
repo: string,
{ q, category }: { q?: string; category?: string } = {},
): Promise<GitHubDiscussions> {
const base = category
? `${owner}/${repo}/discussions/categories/${category}`
: `${owner}/${repo}/discussions`;
const suffix = q ? `?discussions_q=${encodeURIComponent(q)}` : "";
const html = await fetchGitHubPage(`${base}${suffix}`);
const document = parseDocument(html);
const discussions: DiscussionSummary[] = [];
for (const row of cssSelect.selectAll(
"li.js-navigation-item",
document,
) as unknown as Element[]) {
const summary = parseDiscussionSummary(row);
if (summary) discussions.push(summary);
}
const categories = parseDiscussionCategories(document);
return {
repo: { owner, name: repo },
tabs: parseRepoTabs(html),
discussions,
categories,
category: categories.find((c) => c.slug === category) ?? null,
q,
};
}
// A comment and a reply to it are the same markup, with the replies nested
// inside the comment they answer.
function parseDiscussionComment(item: Element): DiscussionComment {
const target = cssSelect.selectOne(".discussions-timeline-scroll-target[id]", item);
const header = cssSelect.selectOne(".timeline-comment-header-text", item);
const time = header ? cssSelect.selectOne("relative-time", header) : null;
const body = cssSelect.selectOne(".js-comment-body", item);
// A comment GitHub collapsed has its reason where the header would be, and
// nothing else: no author, no time, no body. The header of a comment shown
// as usual comes first, so only a collapsed one owns the first match here;
// in the others it belongs to a nested reply.
const minimized = cssSelect.selectOne(".minimized-comment .timeline-comment-header-text", item);
const hiddenReason =
header !== null && header === minimized
? textContent(header).replace(/\s+/g, " ").trim()
: null;
const labels: string[] = [];
if (header) {
for (const label of cssSelect.selectAll(".Label", header) as unknown as Element[]) {
labels.push(textContent(label).replace(/\s+/g, " ").trim());
}
}
const replies: DiscussionComment[] = [];
for (const reply of cssSelect.selectAll(
".discussion-nested-comment-timeline-item",
item,
) as unknown as Element[]) {
replies.push(parseDiscussionComment(reply));
}
// Long threads arrive with the older replies behind a "Show 5 previous
// replies" button, which needs GitHub's JavaScript to load them.
let hiddenReplies = 0;
for (const button of cssSelect.selectAll(
".discussion-nested-comment-paging-form .Button-label",
item,
) as unknown as Element[]) {
const count = /(\d+)/.exec(textContent(button));
if (count) hiddenReplies += parseInt(count[1], 10);
}
return {
anchor: target ? (getAttributeValue(target, "id") ?? "") : "",
author: header ? selectText('a[data-hovercard-type="user"]', header) : null,
createdAt: time ? (getAttributeValue(time, "datetime") ?? null) : null,
labels,
bodyHtml: hiddenReason ? "" : body ? getInnerHTML(body).trim() : "",
hiddenReason,
isAnswer: cssSelect.selectOne(".discussion-footer-answered-badge", item) !== null,
replies,
hiddenReplies,
};
}
export async function getGitHubDiscussion(
owner: string,
repo: string,
number: number,
): Promise<GitHubDiscussion> {
const html = await fetchGitHubPage(`${owner}/${repo}/discussions/${number}`);
const document = parseDocument(html);
const header = cssSelect.selectOne("#partial-discussion-header", document) as Element | null;
if (!header) {
throw new Error("Could not find the discussion header");
}
// The discussion's own post opens the timeline; the rest are its comments.
const items = cssSelect.selectAll(".discussion-timeline-item", document) as unknown as Element[];
if (items.length === 0) {
throw new Error("Could not find the discussion body");
}
const [body, ...comments] = items.map(parseDiscussionComment);
const title = cssSelect.selectOne(".js-issue-title", header);
// Who opened the discussion, and where, sits under the title; an answer, when
// there is one, is in a state badge above it.
const meta = cssSelect.selectOne("div.color-fg-muted", header) as Element | null;
const answer = cssSelect.selectOne(".js-discussions-goto-answer-button", header);
return {
repo: { owner, name: repo },
tabs: parseRepoTabs(html),
number,
titleHtml: title ? getInnerHTML(title).trim() : "",
author: meta ? selectText("a[href]", meta) : null,
category: parseDiscussionCategory(
meta ? cssSelect.selectOne('a[href*="/discussions/categories/"]', meta) : null,
),
answerAnchor: answer ? (getAttributeValue(answer, "href") ?? "").replace(/^#/, "") : null,
body,
comments,
};
}
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);
const commitsRoute = payload?.commitsRefRoute;
if (commitsRoute === undefined) {
throw new Error("Could not find commit history in embedded JSON");
}
return {
repo: {
owner,
name: repo,
},
tabs: parseRepoTabs(html),
branch,
path,
commitGroups: commitsRoute.commitGroups,
};
}
interface LatestCommitPayload {
oid: string;
date: string;
shortMessageHtmlLink: string;
authors: {
login: string;
displayName: string;
avatarUrl: string;
}[];
}
// This is the same endpoint the React UI itself calls to lazily fill in the
// commit bar above a file listing, rather than the much heavier /commits
// history page: it returns just the single latest commit as JSON.
export async function getGitHubLatestCommit(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<GitHubLatestCommit> {
const urlPath = path
? `${owner}/${repo}/latest-commit/${branch}/${path}`
: `${owner}/${repo}/latest-commit/${branch}`;
const payload = await fetchGitHubJson<LatestCommitPayload>(urlPath);
return {
repo: { owner, name: repo },
commit: {
oid: payload.oid,
date: payload.date,
shortMessageHtml: payload.shortMessageHtmlLink,
authors: payload.authors,
},
};
}
interface SidebarPayload {
languages?: {
languages: {
// Absent for the "Other" bucket.
name?: string;
percentage: number;
color?: string;
}[];
};
releases?: {
// The name is the release title, which need not be the tag; the path is.
latestRelease?: { name?: string; path: string; publishedAt?: string } | null;
};
}
// The repository sidebar (languages, releases, contributors, ...) is not
// rendered into the repo page at all; GitHub's own UI lazily fetches it as
// JSON from here.
export async function getGitHubSidebar(owner: string, repo: string): Promise<GitHubSidebar> {
const payload = await fetchGitHubJson<SidebarPayload>(`${owner}/${repo}/_sidebar`);
const languages: Language[] = [];
for (const entry of payload.languages?.languages ?? []) {
languages.push({
name: entry.name ?? "Other",
percentage: entry.percentage,
color: entry.color ?? null,
});
}
const release = payload.releases?.latestRelease;
const latestRelease = release
? {
tag: decodeURIComponent(release.path.split("/releases/tag/")[1] ?? release.name ?? ""),
publishedAt: release.publishedAt ?? null,
}
: null;
return { repo: { owner, name: repo }, languages, latestRelease };
}
interface BranchRefEntry {
name: string;
isDefault: boolean;
authoredDate?: string;
author?: { login: string };
}
interface BranchesPayload {
branches: {
default?: BranchRefEntry;
yours?: BranchRefEntry[];
active?: BranchRefEntry[];
stale?: BranchRefEntry[];
};
hasMore?: Record<string, boolean>;
}
async function getGitHubBranchRefs(owner: string, repo: string): Promise<GitHubRefs> {
const html = await fetchGitHubPage(`${owner}/${repo}/branches`);
const payload = parseEmbeddedPayload<BranchesPayload>(html);
if (payload?.branches === undefined) {
throw new Error("Could not find branches data in embedded JSON");
}
const { default: defaultBranch, yours = [], active = [], stale = [] } = payload.branches;
const entries = [...(defaultBranch ? [defaultBranch] : []), ...yours, ...active, ...stale];
const seen = new Set<string>();
const refs: Ref[] = [];
for (const entry of entries) {
if (seen.has(entry.name)) continue;
seen.add(entry.name);
refs.push({
name: entry.name,
isDefault: entry.isDefault || undefined,
date: entry.authoredDate,
authorLogin: entry.author?.login,
});
}
const hasMore = Object.values(payload.hasMore ?? {}).some(Boolean);
return { repo: { owner, name: repo }, type: "branches", refs, hasMore };
}
async function getGitHubTagRefs(owner: string, repo: string): Promise<GitHubRefs> {
const html = await fetchGitHubPage(`${owner}/${repo}/tags`);
const document = parseDocument(html);
const refs: Ref[] = [];
for (const row of cssSelect.selectAll(".Box-row", document) as unknown as Element[]) {
const nameElem = cssSelect.selectOne("h2 a", row);
if (!nameElem) continue;
const name = textContent(nameElem).trim();
if (!name) continue;
// The row has two <relative-time> elements: a release/tag creation
// timestamp, then the tagged commit's authored date. We want the latter.
const timeElems = cssSelect.selectAll("relative-time", row);
const timeElem = timeElems[1] as Element | undefined;
const date = timeElem ? getAttributeValue(timeElem, "datetime") || undefined : undefined;
refs.push({ name, date });
}
const hasMore = cssSelect.selectOne(".pagination a", document) !== null;
return { repo: { owner, name: repo }, type: "tags", refs, hasMore };
}
export async function getGitHubRefs(
owner: string,
repo: string,
type: "branches" | "tags",
): Promise<GitHubRefs> {
return type === "branches" ? getGitHubBranchRefs(owner, repo) : getGitHubTagRefs(owner, repo);
}
function parseRelease(section: Element, title: string): Release {
// Extract tag name from link
const tagLink = cssSelect.selectOne('a[href*="/tree/"]', section);
let tagName = title;
if (tagLink) {
const href = getAttributeValue(tagLink, "href");
if (href) {
tagName = href.split("/").pop() || title;
}
}
// Extract published date
const relativeTime = cssSelect.selectOne("relative-time", section);
const publishedAt = relativeTime ? getAttributeValue(relativeTime, "datetime") || "" : "";
// Extract body HTML (markdown content)
const body = cssSelect.selectOne(".markdown-body", section);
const bodyHtml = body ? getInnerHTML(body).trim() : "";
return {
tagName,
title,
publishedAt,
bodyHtml,
};
}
export async function getGitHubReleases(owner: string, repo: string): Promise<GitHubReleases> {
const html = await fetchGitHubPage(`${owner}/${repo}/releases`);
const document = parseDocument(html);
const releases: Release[] = [];
// Not really sure the right way to express this type.
const sections = cssSelect.selectAll("section", document) as unknown as Element[];
for (const section of sections) {
const h2 = cssSelect.selectOne("h2[id]", section);
if (!h2) continue;
const title = textContent(h2).trim();
releases.push(parseRelease(section, title));
}
return {
repo: {
owner,
name: repo,
},
tabs: parseRepoTabs(html),
releases,
};
}
export async function getGitHubReleaseBase(
owner: string,
repo: string,
tag: string,
): Promise<GitHubRelease> {
const html = await fetchGitHubPage(`${owner}/${repo}/releases/tag/${tag}`);
const document = parseDocument(html);
const box = cssSelect.selectOne(".Box:has(h1)", document) as Element | null;
if (!box) throw new Error("Could not find release box");
const h1 = cssSelect.selectOne("h1", box);
const title = h1 ? textContent(h1).trim() : "";
const release = parseRelease(box, title);
return {
repo: {
owner,
name: repo,
},
tabs: parseRepoTabs(html),
release,
};
}
export async function getGitHubReleaseAssets(
owner: string,
repo: string,
tag: string,
): Promise<ReleaseAsset[]> {
const html = await fetchGitHubPage(`${owner}/${repo}/releases/expanded_assets/${tag}`);
const document = parseDocument(html);
const assets: ReleaseAsset[] = [];
for (const row of cssSelect.selectAll("li", document) as unknown as Element[]) {
const link = cssSelect.selectOne("a[href]", row);
if (!link) continue;
const url = getAttributeValue(link, "href") || "";
// Filename is in the link, often split into multiple spans
const name = textContent(link).replace(/\s+/g, " ").trim();
let size: string | undefined;
let publishedAt: string | undefined;
for (const s of cssSelect.selectAll("span.color-fg-muted.text-right", row)) {
// Size is in a span with color-fg-muted and text-right, but avoid the one with relative-time
const timeElem = cssSelect.selectOne("relative-time", row);
if (timeElem) {
publishedAt = timeElem ? getAttributeValue(timeElem, "datetime") || "" : "";
} else {
const text = textContent(s).trim();
if (text) size = text;
}
}
assets.push({
name,
url: url.startsWith("/") ? `https://github.com${url}` : url,
size,
publishedAt,
});
}
return assets;
}
export async function getGitHubRelease(
owner: string,
repo: string,
tag: string,
): Promise<GitHubRelease> {
const [base, assets] = await Promise.all([
getGitHubReleaseBase(owner, repo, tag),
getGitHubReleaseAssets(owner, repo, tag),
]);
base.release.assets = assets;
return base;
}
function selectText(selector: string, context: AnyNode): string | null {
const elem = cssSelect.selectOne(selector, context);
if (!elem) return null;
return textContent(elem).replace(/\s+/g, " ").trim() || null;
}
function parseRepoItems(items: Element[]): OwnerRepo[] {
const repos: OwnerRepo[] = [];
for (const item of items) {
const link = cssSelect.selectOne(
"a:has(span.repo), a[itemprop~='codeRepository']",
item,
) as Element | null;
const href = link ? getAttributeValue(link, "href") : undefined;
if (!href) continue;
const [owner, name] = href.replace(/^\//, "").split("/");
if (!owner || !name) continue;
repos.push({
owner,
name,
description: selectText("p.pinned-item-desc, [itemprop='description']", item) ?? undefined,
language: selectText("[itemprop='programmingLanguage']", item) ?? undefined,
stars: selectText("a[href$='/stargazers']", item) ?? undefined,
forks: selectText("a[href$='/forks']", item) ?? undefined,
});
}
return repos;
}
function parsePinnedRepos(document: AnyNode): OwnerRepo[] {
return parseRepoItems(
cssSelect.selectAll(".pinned-item-list-item", document) as unknown as Element[],
);
}
// The tabs we surface from the profile page's own navigation. Stars and
// Overview are deliberately excluded.
const OWNER_NAV_TABS = ["Repositories", "Projects", "Packages", "People", "Sponsoring"];
function parseOwnerNav(document: AnyNode): OwnerNavItem[] {
const nav: OwnerNavItem[] = [];
const seen = new Set<string>();
const anchors = cssSelect.selectAll("a.UnderlineNav-item", document) as unknown as Element[];
for (const anchor of anchors) {
const href = getAttributeValue(anchor, "href");
// Label text is e.g. "Repositories 12" including the counter.
const text = textContent(anchor).replace(/\s+/g, " ").trim().split(" ")[0];
if (!href || seen.has(href) || !OWNER_NAV_TABS.includes(text)) continue;
seen.add(href);
nav.push({ text, href });
}
return nav;
}
function parseUser(document: AnyNode, login: string): GitHubOwner {
const avatarElem = cssSelect.selectOne("a[itemprop='image'] img", document) as Element | null;
const avatarUrl = avatarElem ? getAttributeValue(avatarElem, "src") || null : null;
const followers =
selectText("a[href$='tab=followers']", document)?.replace(/ followers?$/, "") ?? null;
const following =
selectText("a[href$='tab=following']", document)?.replace(/ following$/, "") ?? null;
const links: { text: string; href: string }[] = [];
const linkItems = cssSelect.selectAll(
"li[itemprop='url'], li[itemprop='social']",
document,
) as unknown as Element[];
for (const item of linkItems) {
const anchor = cssSelect.selectOne("a[href]", item) as Element | null;
const href = anchor ? getAttributeValue(anchor, "href") : undefined;
if (!anchor || !href) continue;
links.push({ text: textContent(anchor).replace(/\s+/g, " ").trim(), href });
}
return {
type: "user",
login,
name: selectText(".p-name.vcard-fullname", document),
bio: selectText(".p-note.user-profile-bio", document),
avatarUrl,
followers,
following,
company: selectText("li[itemprop='worksFor']", document),
location: selectText("li[itemprop='homeLocation']", document),
links,
nav: parseOwnerNav(document),
pinned: parsePinnedRepos(document),
repos: [],
};
}
function parseOrganization(document: AnyNode, owner: string): GitHubOwner {
const avatarElem = cssSelect.selectOne(
".orghead img[itemprop='image']",
document,
) as Element | null;
const avatarUrl = avatarElem ? getAttributeValue(avatarElem, "src") || null : null;
// The login only appears in the avatar's "@login" alt text.
const alt = avatarElem ? getAttributeValue(avatarElem, "alt") : undefined;
const login = alt?.replace(/^@/, "") || owner;
const followers =
selectText("a[href$='/followers']", document)?.replace(/ followers?$/, "") ?? null;
const links: { text: string; href: string }[] = [];
const anchors = cssSelect.selectAll(
".orghead a[itemprop='url'], .orghead [itemprop='email'] a",
document,
) as unknown as Element[];
for (const anchor of anchors) {
const href = getAttributeValue(anchor, "href");
if (!href) continue;
links.push({ text: textContent(anchor).replace(/\s+/g, " ").trim(), href });
}
return {
type: "organization",
login,
name: selectText(".orghead h1", document),
bio: selectText(".orghead h1 + div", document),
avatarUrl,
followers,
following: null,
company: null,
location: selectText(".orghead [itemprop='location']", document),
links,
nav: parseOwnerNav(document),
pinned: parsePinnedRepos(document),
repos: parseRepoItems(
cssSelect.selectAll("#org-repositories [itemprop='owns']", document) as unknown as Element[],
),
};
}
export async function getGitHubOwner(owner: string): Promise<GitHubOwner> {
const html = await fetchGitHubPage(owner);
const document = parseDocument(html);
const login = selectText(".p-nickname.vcard-username", document);
if (login) {
return parseUser(document, login);
}
if (cssSelect.selectOne(".orghead", document)) {
return parseOrganization(document, owner);
}
// Not a profile page (marketing pages like github.com/features, etc.).
throw new ExternalRedirectError(`https://github.com/${owner}`);
}
// The text directly inside an element, ignoring any nested elements' text.
function directText(elem: Element): string {
return elem.children
.filter(isText)
.map((node) => node.data)
.join("")
.replace(/\s+/g, " ")
.trim();
}
// The sidebar holds every wiki page, which are revealed by JavaScript.
function parseWikiSidebarPages(document: AnyNode): WikiPage[] {
const pages: WikiPage[] = [];
const anchors = cssSelect.selectAll(
".js-wiki-sidebar-page-container > div > a[href]",
document,
) as unknown as Element[];
for (const anchor of anchors) {
const href = getAttributeValue(anchor, "href");
const name = textContent(anchor).replace(/\s+/g, " ").trim();
if (href && name) pages.push({ name, href });
}
return pages;
}
function wikiHtml(selector: string, document: AnyNode): string | null {
const elem = cssSelect.selectOne(selector, document);
if (!elem) return null;
const html = getInnerHTML(elem).trim();
return html || null;
}
export async function getGitHubWiki(
owner: string,
repo: string,
// Empty for the wiki's home page, which GitHub serves off /wiki itself.
page = "",
// A revision SHA renders that older version of the page.
oid = "",
): Promise<GitHubWiki> {
// Page names keep characters that are not path-safe (C#, [DEV] ...), so the
// slug has to be re-encoded rather than interpolated the way a ref is.
const path = [`${owner}/${repo}/wiki`, page && encodeURIComponent(page), oid]
.filter(Boolean)
.join("/");
const html = await fetchGitHubPage(path);
const document = parseDocument(html);
const heading = cssSelect.selectOne("h1.gh-header-title", document) as Element | null;
if (!heading) {
throw new Error("Could not find the wiki page heading");
}
// "<author> edited this page <date> · <n> revisions", where the author is the
// only part not wrapped in an element of its own.
const meta = cssSelect.selectOne(".gh-header-meta", document) as Element | null;
const updatedBy = meta ? (/^(.+?) edited this page\b/.exec(directText(meta))?.[1] ?? null) : null;
const time = meta ? cssSelect.selectOne("relative-time", meta) : null;
return {
repo: { owner, name: repo },
tabs: parseRepoTabs(html),
title: directText(heading),
updatedBy,
updatedAt: time ? getAttributeValue(time, "datetime") || null : null,
bodyHtml: wikiHtml("#wiki-body .markdown-body", document) ?? "",
// Wikis can define _Sidebar and _Footer pages, rendered alongside every page.
sidebarHtml: wikiHtml(".wiki-custom-sidebar", document),
footerHtml: wikiHtml("#wiki-footer .markdown-body", document),
pages: parseWikiSidebarPages(document),
};
}
export async function getGitHubWikiPages(owner: string, repo: string): Promise<GitHubWikiPages> {
const html = await fetchGitHubPage(`${owner}/${repo}/wiki/_pages`);
const document = parseDocument(html);
const pages: WikiPage[] = [];
for (const row of cssSelect.selectAll(
"#wiki-content li.Box-row",
document,
) as unknown as Element[]) {
const anchor = cssSelect.selectOne("a[href]", row) as Element | null;
const href = anchor ? getAttributeValue(anchor, "href") : undefined;
const name = anchor ? textContent(anchor).replace(/\s+/g, " ").trim() : "";
if (!href || !name) continue;
const time = cssSelect.selectOne("relative-time", row) as Element | null;
pages.push({ name, href, updatedAt: time ? getAttributeValue(time, "datetime") : undefined });
}
return { repo: { owner, name: repo }, tabs: parseRepoTabs(html), pages };
}
|