-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
423 lines (343 loc) Β· 17.2 KB
/
index.js
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
// This is a test edit from the Github iOS app
import fs from 'fs';
import todo from 'todo';
import date from './date.js';
import downloadPages from './downloadPages.js';
import extract from './extract.js';
import headers from './headers.js';
import login from './login.js';
import time from './time.js';
// Get the annoying `ExperimentalWarning` about `fetch` out of the wayβ¦
await fetch('https://example.com');
// Run an experiment with GQL which might make some actions consume less limit
const query = `
query MyQuery {
repositoryOwner(login: "${login}") {
login repositories(first: 100, affiliations: OWNER) {
edges {
node {
name watchers {
totalCount
}
}
}
}
}
}
`;
// TODO: Paginate using https://github.com/JefferyHus/graphql-pagination
// Note that there is no `Link` response header to be able to use
const response = await fetch(process.env.GITHUB_GRAPHQL_URL, { body: JSON.stringify({ query }), method: 'POST', headers });
console.log(JSON.stringify(await response.json(), null, 2));
// Fetch all 300 events GitHub API will provide:
// https://docs.github.com/en/rest/activity/events#list-public-events
/** @type {{ actor: { login: string; }; created_at: string; type: string; payload: unknown; repo: { name: string; }; }[]} */
const events = 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 staleFollowers = await fetch(artifacts.find(artifact => artifact.name === 'followers.json').archive_download_url, { headers })
.then(response => response.arrayBuffer())
.then(arrayBuffer => extract(Buffer.from(arrayBuffer)))
.then(buffer => JSON.parse(buffer))
;
// 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 => follower.login),
...freshFollowers.map(follower => 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 => follower.login === login)?.followed_at ?? stamp;
const unfollowed_at = freshFollowers.find(follower => follower.login === login) ? undefined : (staleFollowers.find(follower => follower.login === login)?.unfollowed_at ?? stamp);
followers.push({ login, followed_at, unfollowed_at });
}
await fs.promises.writeFile('followers.json', JSON.stringify(followers, null, 2));
// Keep a list of accounts found to be dead to skip in unfollow-follow events
const deadLogins = [];
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) - new Date(follower.followed_at)) / (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 fs.promises.writeFile(`by-${field}-${order}.md`, markdown);
console.groupEnd();
}
const todos = await fetch(artifacts.find(artifact => artifact.name === 'todos.json').archive_download_url, { headers })
.then(response => response.arrayBuffer())
.then(arrayBuffer => extract(Buffer.from(arrayBuffer)))
.then(buffer => JSON.parse(buffer))
;
// Extract tracked attributes of each repository (used for change detection)
const _repositories = await fetch(artifacts.find(artifact => artifact.name === 'repositories.json').archive_download_url, { headers })
.then(response => response.arrayBuffer())
.then(arrayBuffer => extract(Buffer.from(arrayBuffer)))
.then(buffer => JSON.parse(buffer))
;
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);
}
}
// Download the readme at the alternate name or fail
catch (error) {
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 fs.promises.writeFile(name + '.' + readme, content);
if (!todos[name]) {
todos[name] ??= {};
}
todos[name].stamp = pushed_at;
todos[name].todos = [];
for await (const { text } of todo('.', name + '.' + readme)) {
todos[name].todos.push(text);
}
await fs.promises.unlink(name + '.' + readme);
}
}
for (const name in todos) {
if (!repositories.find(repository => repository.name === name)) {
delete todos[name];
}
}
await fs.promises.writeFile('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 fs.promises.writeFile('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
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 => !issueOrPr.pull_request).map(issue => ({
repo: issue.html_url.split('/')[4],
title: issue.title,
url: issue.html_url,
}));
const issueGroups = issues.reduce((groups, issue) => { 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 => `- [${issue.title}](${issue.url})`).join('\n')}`)
.join('\n\n') + '\n'
;
await fs.promises.writeFile('issues.md', issuesMarkDown);
const prs = issuesAndPrs.filter(issueOrPr => issueOrPr.pull_request).map(pr => ({
repo: pr.html_url.split('/')[4],
title: pr.title,
url: pr.html_url,
}));
const prGroups = prs.reduce((groups, pr) => { 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 => `- [${pr.title}](${pr.url})`).join('\n')}`)
.join('\n\n') + '\n'
;
await fs.promises.writeFile('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 => 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.json)`
;
await fs.promises.writeFile('identical-forks.json', JSON.stringify(identicalForks, null, 2));
if (identicalForks.length === 0) {
await fs.promises.unlink('identical-forks.json');
}
const followerCount = followers.filter(follower => follower.followed_at && !follower.unfollowed_at).length;
let markdown = `![](banner.svg)
<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} 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 + '.js')).default(event);
markdown += '\n';
}
markdown += '\n';
markdown += '</details>\n';
await fs.promises.writeFile('readme.md', markdown);