-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
581 lines (506 loc) Β· 18 KB
/
index.ts
File metadata and controls
581 lines (506 loc) Β· 18 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
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
import todo from "todo";
import date from "./date.ts";
import downloadPages from "./downloadPages.ts";
import extract from "./extract.ts";
import headers from "./headers.ts";
import login from "./login.ts";
import time from "./time.ts";
// Get the annoying `ExperimentalWarning` about `fetch` out of the wayβ¦
await fetch("https://example.com");
// Fetch all 300 events GitHub API will provide:
// https://docs.github.com/en/rest/activity/events#list-public-events
const events: any[] = await downloadPages(
`${process.env.GITHUB_API_URL}/users/${login}/events?per_page=1000`
);
console.log("Downloaded", events.length, "events");
// Fetch all repository artifacts used to carry cached data between runs
// https://docs.github.com/en/rest/actions/artifacts#list-artifacts-for-a-repository
const { artifacts } = await fetch(
`${process.env.GITHUB_API_URL}/repos/${process.env.GITHUB_REPOSITORY}/actions/artifacts`,
{ headers }
).then((response) => response.json());
console.log("Downloaded", artifacts.length, "artifacts");
// Recover remembered followers for later comparison and change detection
const followersArtifact = artifacts.find(
(artifact: any) => artifact.name === "followers.json"
);
const staleFollowers = followersArtifact
? await fetch(followersArtifact.archive_download_url, { headers })
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => extract(Buffer.from(arrayBuffer)))
.then((buffer) => JSON.parse(buffer as any))
: [];
// Fetch current followers for later comparison and change detection
const freshFollowers = await downloadPages(
`${process.env.GITHUB_API_URL}/users/${login}/followers?page_page=100`
);
// Get the unique names of both stale and fresh followers to get the whole set
const logins = [
...staleFollowers.map((follower: any) => follower.login),
...freshFollowers.map((follower: any) => follower.login),
].filter((login, index, array) => array.indexOf(login) === index);
const followers = [];
const stamp = new Date().toISOString().slice(0, 19) + "Z";
for (const login of logins) {
const followed_at =
staleFollowers.find((follower: any) => follower.login === login)
?.followed_at ?? stamp;
const unfollowed_at = freshFollowers.find(
(follower: any) => follower.login === login
)
? undefined
: staleFollowers.find((follower: any) => follower.login === login)
?.unfollowed_at ?? stamp;
followers.push({ login, followed_at, unfollowed_at });
}
await Bun.write("followers.json", JSON.stringify(followers, null, 2));
// Keep a list of accounts found to be dead to skip in unfollow-follow events
const deadLogins: string[] = [];
// Generate synthetic follower events only if GitHub API returned events
// (skip if empty due to transient API issue)
if (events.length > 0) {
const cutoff = events[events.length - 1].created_at;
for (const follower of followers) {
// Generate follower event (unfollowed) if the user unfollowed earlier than the oldest GitHub activity event returned
if (follower.unfollowed_at?.localeCompare(cutoff) >= 0) {
// Skip accounts known to be dead already
if (deadLogins.includes(follower.login)) {
continue;
}
// Check if the account is dead and if so, mark it as such and skip
// Use the non-API endpoint because the API is not always accurate on this
const { status } = await fetch(
process.env.GITHUB_SERVER_URL + "/" + follower.login
);
if (status === 404) {
deadLogins.push(follower.login);
continue;
}
let duration;
if (follower.followed_at !== "0000-00-00T00:00:00Z") {
duration = ~~(
(new Date(follower.unfollowed_at).valueOf() -
new Date(follower.followed_at).valueOf()) /
(1000 * 3600 * 24)
);
}
events.push({
actor: { login },
created_at: follower.unfollowed_at,
type: "FollowerEvent",
payload: { action: "unfollowed", unfollower: follower.login, duration },
});
}
// Generate follower event (followed) if the user followed earlier than the oldest GitHub activity event returned
if (follower.followed_at?.localeCompare(cutoff) >= 0) {
// Skip accounts known to be dead (marked as such by unfollow event)
if (deadLogins.includes(follower.login)) {
continue;
}
events.push({
actor: { login },
created_at: follower.followed_at,
type: "FollowerEvent",
payload: { action: "followed", newFollower: follower.login },
});
}
}
}
// Fetch repositories for star and fork change detection
const repositories = await downloadPages(
`${process.env.GITHUB_API_URL}/users/${login}/repos?per_page=100`
);
for (const [field, order] of [
["name", "asc"],
["name", "desc"],
["updated_at", "asc"],
["updated_at", "desc"],
["pushed_at", "asc"],
["pushed_at", "desc"],
["size", "asc"],
["size", "desc"],
]) {
console.group(`Generating list by ${field} ${order}β¦`);
repositories.sort((a, b) => {
// Use strict equality checks not `!field` to accept zeros
const aField = a[field];
if (aField === null || aField === undefined) {
console.log(`Empty '${field}' field:`);
console.log(a);
}
// Use strict equality checks not `!field` to accept zeros
const bField = b[field];
if (bField === null || bField === undefined) {
console.log(`Empty '${field}' field:`);
console.log(b);
}
const sortOrder = `${typeof aField}-${typeof bField}-${order}`;
switch (sortOrder) {
case "string-string-asc":
return aField.localeCompare(bField);
case "string-string-desc":
return bField.localeCompare(aField);
case "number-number-asc":
return aField - bField;
case "number-number-desc":
return bField - aField;
default:
throw new Error(
`No or incorrect sort order was specified: '${sortOrder}'. Pass sortOrder! Field: ${field}. aField: ${aField}. bField: ${bField}.`
);
}
});
let markdown = `# By \`${field}\` (${order})\n\n`;
markdown += new Date().toISOString() + "\n\n";
for (const repository of repositories) {
markdown += `## [${repository.name}](${repository.html_url})\n\n`;
markdown += `βοΈ ${repository[field]}\n\n`;
markdown += `π· ${repository.topics.join(", ")}\n\n`;
markdown += `π ${repository.description}\n\n`;
}
await Bun.write(`by-${field}-${order}.md`, markdown);
console.groupEnd();
}
const todosArtifact = artifacts.find(
(artifact: any) => artifact.name === "todos.json"
);
const todos = todosArtifact
? await fetch(todosArtifact.archive_download_url, { headers })
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => extract(Buffer.from(arrayBuffer)))
.then((buffer) => JSON.parse(buffer as any))
: [];
// Extract tracked attributes of each repository (used for change detection)
const repositoriesArtifact = artifacts.find(
(artifact: any) => artifact.name === "repositories.json"
);
const _repositories = repositoriesArtifact
? await fetch(repositoriesArtifact.archive_download_url, { headers })
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => extract(Buffer.from(arrayBuffer)))
.then((buffer) => JSON.parse(buffer as any))
: [];
const deletedRepositories = Object.keys(_repositories).filter(
(name) => !repositories.find((repository) => repository.name === name)
);
for (const deletedRepository of deletedRepositories) {
console.log("Deleted", deletedRepository);
// TODO: Mark the repository as deleted until cutoff so we can generate repository-deleted event
//delete _repositories[deletedRepository];
}
let _stars = 0;
for (const repository of repositories) {
// Note that `watchers_count` is the same as `stargazers_count` and the real
// value for watches, `subscribers_count` is not available for the bulk repo
// endpoint.
// Note that `open_issues_count` mixes together issues and pull requests and
// is not distringuishable without fetching individual repo's details.
const {
name,
full_name,
stargazers_count: stars,
forks_count: forks,
pushed_at,
} = repository;
_stars += stars;
// TODO: Drop entries that are older than the cutoff and no longer contribute
// Record the changes only if there are any to speak of - ignore non-changes
let stats = _repositories[name];
if (!stats) {
stats = _repositories[name] = {};
}
const stat = stats[Object.keys(stats).pop()!];
if (!stat || stars !== stat.stars || forks !== stat.forks) {
stats[stamp] = { stars, forks };
}
// Update the repository readme todos if it was pushed to since the last capture
if (
full_name !== process.env.GITHUB_REPOSITORY &&
todos[name]?.stamp !== pushed_at
) {
const readme = todos[name]?.readme ?? "readme.md";
let content;
// Download the readme at the remembered or default name
try {
content = await fetch(
`${process.env.GITHUB_API_URL}/repos/${login}/${name}/contents/${readme}`,
{ headers }
).then((response) => response.json());
if (content.message === "Not Found") {
throw new Error(content);
}
} catch (error) {
// Download the readme at the alternate name or fail
const oppositeReadme = readme === "readme.md" ? "README.md" : "readme.md";
content = await fetch(
`${process.env.GITHUB_API_URL}/repos/${login}/${name}/contents/${oppositeReadme}`,
{ headers }
).then((response) => response.json());
}
if (content.message?.startsWith("API rate limit exceeded")) {
throw new Error(content.message);
}
if (content.message === "This repository is empty.") {
continue;
}
if (content.message?.startsWith("Not Found")) {
console.log(name, "readme not found");
continue;
}
content = Buffer.from(content.content, "base64");
await Bun.write(name + "." + readme, content);
if (!todos[name]) {
todos[name] ??= {};
}
todos[name].stamp = pushed_at;
todos[name].todos = [];
for await (const { text } of todo(
"." as unknown as undefined,
name + "." + readme
)) {
todos[name].todos.push(text);
}
await Bun.file(name + "." + readme).delete();
}
}
for (const name in todos) {
if (!repositories.find((repository) => repository.name === name)) {
delete todos[name];
}
}
await Bun.write(
"todos.json",
JSON.stringify(Object.fromEntries(Object.entries(todos).sort()), null, 2)
);
// Sort the repositories object by key before persisting it to the change
await Bun.write(
"repositories.json",
JSON.stringify(
Object.fromEntries(Object.entries(_repositories).sort()),
null,
2
)
);
// Compare changes between repository attributes and generate events for them
// Note that repo creations and deletions are handled by GitHub Activity API
// Skip if no events returned from API (transient GitHub API issue)
if (events.length > 0) {
const cutoff = events[events.length - 1].created_at;
for (const repository in _repositories) {
let _stat;
const stats = _repositories[repository];
for (const stamp in stats) {
const stat = stats[stamp];
// Set the first stat as the comparison basis and continue
if (!_stat) {
_stat = stat;
continue;
}
if (stat.stars !== _stat.stars && stamp?.localeCompare(cutoff) >= 0) {
events.push({
actor: { login },
created_at: stamp,
type: "RepositoryEvent",
repo: { name: `${login}/${repository}` },
payload: { action: "starred", old: _stat.stars, new: stat.stars },
});
}
if (stat.forks !== _stat.forks && stamp?.localeCompare(cutoff) >= 0) {
events.push({
actor: { login },
created_at: stamp,
type: "RepositoryEvent",
repo: { name: `${login}/${repository}` },
payload: { action: "forked", old: _stat.forks, new: stat.forks },
});
}
_stat = stat;
}
}
}
const issuesAndPrs = [
...(await downloadPages(
`${process.env.GITHUB_API_URL}/search/issues?q=org:${login}+is:open&per_page=100`
)),
].reduce((issuesAndPrs, page) => [...issuesAndPrs, ...page.items], []);
const issues = issuesAndPrs
.filter((issueOrPr: any) => !issueOrPr.pull_request)
.map((issue: any) => ({
repo: issue.html_url.split("/")[4],
user: issue.user.login === login ? "πββοΈ" : `π€ ${issue.user.login}`,
title: issue.title,
url: issue.html_url,
}));
const issueGroups = issues.reduce((groups: any, issue: any) => {
groups[issue.repo] = groups[issue.repo] ?? [];
groups[issue.repo].push(issue);
return groups;
}, {});
const issuesMarkDown =
"# Issues\n\n" +
Object.keys(issueGroups)
.sort()
.map((group) => issueGroups[group])
.map(
(group) =>
`## ${group[0].repo}\n\n${group
.map(
(issue: any) => `- [${issue.user}: ${issue.title}](${issue.url})`
)
.join("\n")}`
)
.join("\n\n") +
"\n";
await Bun.write("issues.md", issuesMarkDown);
const prs = issuesAndPrs
.filter((issueOrPr: any) => issueOrPr.pull_request)
.map((pr: any) => ({
repo: pr.html_url.split("/")[4],
title: pr.title,
url: pr.html_url,
}));
const prGroups = prs.reduce((groups: any, pr: any) => {
groups[pr.repo] = groups[pr.repo] ?? [];
groups[pr.repo].push(pr);
return groups;
}, {});
const prsMarkDown =
"# Pull Requests\n\n" +
Object.keys(prGroups)
.sort()
.map((group) => prGroups[group])
.map(
(group) =>
`## ${group[0].repo}\n\n${group
.map((pr: any) => `- [${pr.title}](${pr.url})`)
.join("\n")}`
)
.join("\n\n") +
"\n";
await Bun.write("prs.md", prsMarkDown);
const forkPrs = [
...(await downloadPages(
`${process.env.GITHUB_API_URL}/search/issues?q=is:pr+is:open+author:${login}+-org:${login}&per_page=100`
)),
].reduce((forkPrs, page) => [...forkPrs, ...page.items], []);
const forkPrRepos = forkPrs.map((pr: any) =>
pr.html_url.split("/").slice(3, 5).join("/")
);
const forks = repositories.filter((repository) => repository.fork);
const identicalForks = [];
for (const fork of forks) {
const { parent } = await fetch(fork.url, { headers }).then((response) =>
response.json()
);
if (!forkPrRepos.includes(parent.full_name)) {
console.log("Marked", fork.name, "as identical");
identicalForks.push(fork.name);
}
}
const nbsp = " ";
const forksMarkDown =
forks.length === 0
? `No${nbsp}forks${nbsp}π΄`
: forks.length === 1
? `[One${nbsp}fork:${nbsp}\`${forks[0].name}\`${nbsp}π΄](${
forks[0].html_url
})${identicalForks.length > 0 ? " α§ " : ""}`
: `[${forks.length}${nbsp}forks${nbsp}π΄](${
process.env.GITHUB_SERVER_URL
}/${login}?tab=repositories&q=&type=fork)${
identicalForks.length > 0 ? " α§ " : ""
}`;
const identicalForksMarkDown =
identicalForks.length === 0
? ""
: identicalForks.length === 1
? `\n[One${nbsp}identical${nbsp}fork:${nbsp}\`${identicalForks[0]}\`${nbsp}π΄β οΈ](${process.env.GITHUB_SERVER_URL}/${login}/${identicalForks[0]})`
: `\n[${identicalForks.length}${nbsp}identical${nbsp}forks${nbsp}π΄β οΈ](identical-forks.md)`;
const identicalForksContentMarkDown =
"# Identical forks\n\n" +
identicalForks
.map(
(fork) =>
`- [${fork}](${process.env.GITHUB_SERVER_URL}/${login}/${fork}) ([Go to Settings > Delete](${process.env.GITHUB_SERVER_URL}/${login}/${fork}/settings#danger-zone))`
)
.join("\n\n") +
"\n";
await Bun.write("identical-forks.md", identicalForksContentMarkDown);
if (identicalForks.length === 0) {
await Bun.file("identical-forks.md").delete();
}
const followerCount = followers.filter(
(follower) => follower.followed_at && !follower.unfollowed_at
).length;
let markdown = `
<div align="center">
<img src="${process.env.GITHUB_SERVER_URL}/${
process.env.GITHUB_REPOSITORY
}/actions/workflows/main.yml/badge.svg">
</div>
<div align="center">
[${followerCount} follower${followerCount === 1 ? "" : "s"} π€](${
process.env.GITHUB_SERVER_URL
}/${login}?tab=followers) α§
${_stars} star${_stars === 1 ? "" : "s"} βοΈ α§
[${repositories.length} repositorie${
repositories.length === 1 ? "" : "s"
} π](${process.env.GITHUB_SERVER_URL}/${login}?tab=repositories) α§
[${issues.length} issue${
issues.length === 1 ? "" : "s"
} π«](issues.md) α§
[${prs.length || "No"} PR${prs.length === 1 ? "" : "s"} π](prs.md) α§
[${Object.keys(todos).length} todo${
Object.keys(todos).length === 1 ? "" : "s"
} πͺ](todos.json) α§
${forksMarkDown}${identicalForksMarkDown}
</div>
`;
let heading;
// Sort the events so that any added virtual events are sorted in correctly
events.sort((a, b) => b.created_at.localeCompare(a.created_at));
for (const event of events) {
if (event.actor.login !== login) {
throw new Error("A non-me event has happened.");
}
if (
event.repo?.name?.startsWith(login) &&
!repositories.find(
(repository) =>
repository.name === event.repo.name.slice(login.length + "/".length)
)
) {
console.log(
"Skipped",
event.type,
"of deleted repository",
event.repo?.name || event
);
continue;
}
const _date = new Date(event.created_at);
const _heading = date(_date);
if (heading !== _heading) {
if (heading) {
markdown += "\n";
markdown += "</details>\n";
markdown += "\n";
}
markdown += `<details${!heading ? " open" : ""}>\n`;
markdown += `<summary>${_heading}${
_heading === "Today" ? ` (${time(new Date())})` : ""
}</summary>\n`;
markdown += "\n";
heading = _heading;
}
markdown += `- \`${time(_date)}\`\n `;
// https://docs.github.com/en/developers/webhooks-and-events/github-event-types
if (!/\w+/.test(event.type)) {
throw new Error("Misformatted event " + event.type);
}
markdown += (await import("./write" + event.type + ".ts")).default(event);
markdown += "\n";
}
markdown += "\n";
markdown += "</details>\n";
await Bun.write("readme.md", markdown);