-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacts.ts
More file actions
536 lines (499 loc) · 16 KB
/
facts.ts
File metadata and controls
536 lines (499 loc) · 16 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
// SPDX-FileCopyrightText: 2026 Emily <hello@emily.moe>
//
// SPDX-License-Identifier: MIT
import { assert, assertExists } from "@std/assert";
import { Octokit, RequestError } from "octokit";
import type { OctokitPlugin } from "@octokit/core/types";
import type { RequestParameters } from "@octokit/types";
import {
type ActorInfoFragment,
BasicFactsDocument,
CommittersDocument,
type InputMaybe,
MergeBotMergesSinceDocument,
PullRequestForMergeCommitDocument,
type PullRequestInfoFragment,
type RepositoryInfoFragment,
type Scalars,
type TypedDocumentString,
UnmergedPullRequestsDocument,
} from "./graphql/graphql.ts";
import {
type Activity,
nixosOrgRiskPeriodEnd,
riskPeriodStart,
tokenRiskPeriodEnd,
} from "./axioms.ts";
export type ID = Scalars["ID"]["output"];
export type Facts = {
readonly affectedRepositories: Map<ID, RepositoryInfoFragment>;
readonly affectedUser: ActorInfoFragment;
readonly webFlowUser: ActorInfoFragment;
readonly rRyantmUser: ActorInfoFragment;
readonly nixpkgsCIActor: ActorInfoFragment;
readonly githubActionsActor: ActorInfoFragment;
readonly committers: Map<ID, ActorInfoFragment>;
readonly trustedOpenPGPPublicKeys: Map<ID, string[]>;
readonly riskyPullRequests: Map<ID, PullRequestInfoFragment>;
readonly riskyRepositoryActivity: Map<ID, Activity[]>;
};
export type JSONFacts = {
readonly [Field in keyof Facts]: Facts[Field] extends Map<ID, infer V>
? Record<ID, V>
: Facts[Field];
};
function toJSONFacts(facts: Facts): JSONFacts {
const toObject: <V>(m: Map<ID, V>) => Record<ID, V> = (m) =>
Object.fromEntries(
m.entries().toArray().toSorted(([id1, _v1], [id2, _v2]) =>
id1.localeCompare(id2, "en")
),
);
return {
affectedRepositories: toObject(facts.affectedRepositories),
affectedUser: facts.affectedUser,
webFlowUser: facts.webFlowUser,
rRyantmUser: facts.rRyantmUser,
nixpkgsCIActor: facts.nixpkgsCIActor,
githubActionsActor: facts.githubActionsActor,
committers: toObject(facts.committers),
trustedOpenPGPPublicKeys: toObject(facts.trustedOpenPGPPublicKeys),
riskyPullRequests: toObject(facts.riskyPullRequests),
riskyRepositoryActivity: toObject(facts.riskyRepositoryActivity),
};
}
export function fromJSONFacts(facts: JSONFacts): Facts {
return {
affectedRepositories: new Map(Object.entries(facts.affectedRepositories)),
affectedUser: facts.affectedUser,
webFlowUser: facts.webFlowUser,
rRyantmUser: facts.rRyantmUser,
nixpkgsCIActor: facts.nixpkgsCIActor,
githubActionsActor: facts.githubActionsActor,
committers: new Map(Object.entries(facts.committers)),
trustedOpenPGPPublicKeys: new Map(
Object.entries(facts.trustedOpenPGPPublicKeys),
),
riskyPullRequests: new Map(Object.entries(facts.riskyPullRequests)),
riskyRepositoryActivity: new Map(
Object.entries(facts.riskyRepositoryActivity),
),
};
}
// Sometimes the GitHub API freaks out and returns an empty response
// with no error from the GraphQL endpoint. This hacks around that by
// pretending it’s a rate limit issue.
const retryHack: OctokitPlugin = (octokit) => {
octokit.hook.after("request", (response, options) => {
if (
response.url === "https://api.github.com/graphql" &&
response.status === 200 &&
response.data === ""
) {
// The throttling plugin matches the error message against a
// regular expression to detect secondary rate limits, so the
// string here is load‐bearing…
throw new RequestError("Fake secondary rate limit", 403, {
request: options,
response: { ...response, status: 403 },
});
}
});
};
// We need our plugin to come before the stock throttling plugin
// included with the high‐level `octokit.js` wrapper library, which
// means we have to inject it ourselves rather than appending to the
// list with `Octokit.plugin()`.
const MyOctokit = class extends Octokit {
static override plugins = [
retryHack,
...Octokit.plugins,
];
};
async function getGitHubCliToken(): Promise<string> {
const command = new Deno.Command("gh", { args: ["auth", "token"] });
const { success, stdout, stderr } = await command.output();
assert(success, `Getting token from gh(1) failed: ${stderr}`);
return new TextDecoder().decode(stdout);
}
async function getOctokit(): Promise<Octokit> {
return new MyOctokit({
auth: Deno.env.get("GITHUB_TOKEN") ?? await getGitHubCliToken(),
request: {
retries: 10,
},
});
}
function execute<Result, Vars>(
octokit: Octokit,
query: TypedDocumentString<Result, Vars>,
parameters: RequestParameters & Vars,
): Promise<Result> {
return octokit.graphql(query.toString(), parameters);
}
function paginate<
Result,
Vars extends { readonly cursor: InputMaybe<string> },
>(
octokit: Octokit,
query: TypedDocumentString<Result, Vars>,
parameters: RequestParameters & Omit<Vars, "cursor">,
): AsyncIterable<Result> {
return octokit.graphql.paginate.iterator(query.toString(), parameters);
}
async function fetchUserIDByRESTUsername(
{ octokit, username }: { octokit: Octokit; username: string },
): Promise<ID> {
return (await octokit.rest.users.getByUsername({ username })).data.node_id;
}
async function fetchCommitters(
octokit: Octokit,
): Promise<Map<ID, ActorInfoFragment>> {
return new Map(
(await Array.fromAsync(paginate(octokit, CommittersDocument, {})))
.values()
.flatMap(({ organization }) => {
const nodes = organization?.team?.members.nodes;
assertExists(nodes);
return nodes.map((member) => {
assertExists(member);
return [member.id, member];
});
}),
);
}
async function fetchOpenPGPPublicKeys(
{ octokit, user: { login } }: {
octokit: Octokit;
user: ActorInfoFragment;
},
): Promise<string[]> {
const iterator = octokit.paginate.iterator(
octokit.rest.users.listGpgKeysForUser,
{ username: login },
);
return (await Array.fromAsync(iterator)).flatMap(({ data }) =>
data.map(({ raw_key }) => {
assertExists(raw_key);
return raw_key;
})
);
}
async function* fetchRiskyRepositoryActivity(
{ octokit, repository: { owner, name }, affectedUser, periodEnd }: {
octokit: Octokit;
repository: RepositoryInfoFragment;
affectedUser: ActorInfoFragment;
periodEnd: Temporal.Instant;
},
): AsyncGenerator<Activity> {
const iterator = octokit.paginate.iterator(
octokit.rest.repos.listActivities,
{
owner: owner.login,
repo: name,
actor: affectedUser.login,
per_page: 100,
},
);
for await (const { data } of iterator) {
yield* data.filter(({ timestamp }) =>
riskPeriodStart.until(timestamp).sign >= 0 &&
periodEnd.since(timestamp).sign > 0
);
}
}
async function fetchPullRequestForMergeCommit(
{ octokit, repository: { id: repositoryID }, oid }: {
octokit: Octokit;
repository: RepositoryInfoFragment;
oid: string;
},
): Promise<PullRequestInfoFragment> {
const { node } = await execute(
octokit,
PullRequestForMergeCommitDocument,
{
repositoryID,
oid,
},
);
assert(node?.__typename === "Repository");
assert(node.object?.__typename === "Commit");
const { associatedPullRequests } = node.object;
assertExists(associatedPullRequests);
const { nodes, pageInfo } = associatedPullRequests;
assert(!pageInfo.hasNextPage);
const pullRequest = nodes?.[0];
assertExists(pullRequest);
return pullRequest;
}
async function* fetchRiskyMergeBotMergedPullRequests(
{ octokit, mergeBotActorIDs }: {
octokit: Octokit;
mergeBotActorIDs: Set<ID>;
},
): AsyncGenerator<PullRequestInfoFragment> {
const iterator = paginate(octokit, MergeBotMergesSinceDocument, {
periodStart: riskPeriodStart.toString(),
});
for await (const { repository } of iterator) {
const nodes = repository?.issue?.timelineItems.nodes;
assertExists(nodes);
yield* nodes.flatMap((timelineItem) => {
assertExists(timelineItem);
return timelineItem.__typename === "CrossReferencedEvent" &&
timelineItem.source.__typename === "PullRequest" &&
timelineItem.actor !== null &&
mergeBotActorIDs.has(timelineItem.actor.id) &&
tokenRiskPeriodEnd.since(timelineItem.source.createdAt).sign > 0
? [timelineItem.source]
: [];
});
}
}
async function* fetchRiskyUnmergedPullRequests(
{ octokit, repository: { id: repositoryID } }: {
octokit: Octokit;
repository: RepositoryInfoFragment;
},
): AsyncGenerator<PullRequestInfoFragment> {
const iterator = paginate(octokit, UnmergedPullRequestsDocument, {
repositoryID,
});
for await (const { node } of iterator) {
assert(node?.__typename === "Repository");
const nodes = node?.pullRequests.nodes;
assertExists(nodes);
for (const pullRequest of nodes) {
assertExists(pullRequest);
if (riskPeriodStart.until(pullRequest.updatedAt).sign < 0) {
return;
}
if (tokenRiskPeriodEnd.since(pullRequest.createdAt).sign > 0) {
yield pullRequest;
}
}
}
}
class Progress {
#stderr: WritableStreamDefaultWriter<Uint8Array> = Deno.stderr.writable
.getWriter();
#encoder: TextEncoder = new TextEncoder();
#progress: number = 0;
async update(newProgress: number): Promise<void> {
this.#progress = Math.max(this.#progress, newProgress);
await this.#stderr.ready;
await this.#stderr.write(
this.#encoder.encode(
"\r\x1B[K" + "█".repeat(Math.floor(this.#progress * 80)) + "\r",
),
);
}
async finish(): Promise<void> {
await this.#stderr.ready;
await this.#stderr.write(this.#encoder.encode("\r\x1B[K"));
await this.#stderr.ready;
this.#stderr.releaseLock();
}
}
async function collectParallelWithProgress<T>(
promises: Promise<T>[],
): Promise<T[]> {
const progress = new Progress();
const results = await Promise.all(promises.map(async (promise, i) => {
const result = await promise;
await progress.update(i / promises.length);
return result;
}));
await progress.finish();
return results;
}
async function collectPullRequestsParallel<T extends PullRequestInfoFragment>(
promises: Promise<T>[],
): Promise<Map<ID, T>> {
return new Map(
await collectParallelWithProgress(promises.map(async (promise) => {
const pullRequest = await promise;
return [pullRequest.id, pullRequest] as const;
})),
);
}
async function collectPullRequestsSequential<T extends PullRequestInfoFragment>(
{ periodStart, periodEnd, pullRequests }: {
periodStart: Temporal.Instant;
periodEnd: Temporal.Instant;
pullRequests: AsyncIterable<T>;
},
): Promise<Map<ID, T>> {
const map = new Map();
const progress = new Progress();
const periodDuration = periodEnd.since(periodStart).total("seconds");
for await (const pullRequest of pullRequests) {
map.set(pullRequest.id, pullRequest);
await progress.update(
periodEnd.since(pullRequest.updatedAt).total("seconds") / periodDuration,
);
}
await progress.finish();
return map;
}
async function fetchFacts(octokit: Octokit): Promise<Facts> {
console.log("Fetching basic information");
const nixpkgsCIActorID = await fetchUserIDByRESTUsername({
octokit,
username: "nixpkgs-ci[bot]",
});
const githubActionsActorID = await fetchUserIDByRESTUsername({
octokit,
username: "github-actions[bot]",
});
const {
nixpkgsRepository,
nixosHardwareRepository,
nixosWeeklyRepository,
nixIdeaRepository,
nixPillsRepository,
affectedUser,
webFlowUser,
rRyantmUser,
nixpkgsCIActor,
githubActionsActor,
} = await execute(octokit, BasicFactsDocument, {
nixpkgsCIActorID,
githubActionsActorID,
});
assertExists(nixpkgsRepository);
assertExists(nixosHardwareRepository);
assertExists(nixosWeeklyRepository);
assertExists(nixIdeaRepository);
assertExists(nixPillsRepository);
assertExists(affectedUser);
assertExists(webFlowUser);
assertExists(rRyantmUser);
assert(nixpkgsCIActor?.__typename === "Bot");
assert(githubActionsActor?.__typename === "Bot");
const affectedRepositories = new Map(
[
nixpkgsRepository,
nixosHardwareRepository,
nixosWeeklyRepository,
nixIdeaRepository,
nixPillsRepository,
].values().map((repository) => [repository.id, repository]),
);
console.log("Fetching trusted OpenPGP public keys");
const trustedOpenPGPPublicKeys = new Map(
await Promise.all(
[webFlowUser, affectedUser].map(async (user) =>
[user.id, await fetchOpenPGPPublicKeys({ octokit, user })] as const
),
),
);
console.log("Fetching Nixpkgs committers");
const committers = await fetchCommitters(octokit);
console.log("Fetching affected NixOS organization repository activity");
const riskyAffectedRepositoryActivity = new Map(
await Array.fromAsync(
affectedRepositories.entries().map(async ([id, repository]) => [
id,
await Array.fromAsync(fetchRiskyRepositoryActivity({
octokit,
repository,
affectedUser,
periodEnd: nixosOrgRiskPeriodEnd,
})),
]),
),
);
console.log("Fetching directly merged pull requests");
const riskyDirectlyMergedPullRequests = await collectPullRequestsParallel(
riskyAffectedRepositoryActivity.entries().flatMap(
([id, riskyActivities]) => {
const repository = affectedRepositories.get(id);
assertExists(repository);
return riskyActivities.values().filter(({ activity_type }) =>
activity_type === "pr_merge"
).map(({ after }) =>
fetchPullRequestForMergeCommit({
octokit,
repository,
oid: after,
})
);
},
).toArray(),
);
console.log("Fetching merge bot merged pull requests");
const nixpkgsMergeBotActorID = await fetchUserIDByRESTUsername({
octokit,
username: "nixpkgs-merge-bot[bot]",
});
const riskyMergeBotMergedPullRequests = await collectPullRequestsSequential({
periodStart: riskPeriodStart,
periodEnd: tokenRiskPeriodEnd,
pullRequests: fetchRiskyMergeBotMergedPullRequests({
octokit,
mergeBotActorIDs: new Set([nixpkgsCIActor.id, nixpkgsMergeBotActorID]),
}),
});
console.log("Fetching unmerged pull requests");
const riskyUnmergedPullRequests = await collectPullRequestsSequential(
{
periodStart: riskPeriodStart,
periodEnd: tokenRiskPeriodEnd,
pullRequests: (async function* () {
for (const repository of affectedRepositories.values()) {
yield* fetchRiskyUnmergedPullRequests({ octokit, repository });
}
})(),
},
);
const riskyPullRequests = new Map([
...riskyDirectlyMergedPullRequests.entries(),
...riskyMergeBotMergedPullRequests.entries(),
...riskyUnmergedPullRequests.entries(),
]);
console.log("Fetching pull request repository activity");
const riskyPullRequestRepositories: Map<ID, RepositoryInfoFragment> = new Map(
riskyPullRequests.values().flatMap(({ headRepository }) =>
headRepository === null || affectedRepositories.has(headRepository.id)
? []
: [[headRepository.id, headRepository]]
),
);
const riskyRepositoryActivity = new Map([
...riskyAffectedRepositoryActivity.entries(),
...await collectParallelWithProgress(
riskyPullRequestRepositories.entries().map(async ([id, repository]) =>
[
id,
await Array.fromAsync(fetchRiskyRepositoryActivity({
octokit,
repository,
affectedUser,
periodEnd: tokenRiskPeriodEnd,
})),
] as const
).toArray(),
),
]);
return {
affectedRepositories,
affectedUser,
webFlowUser,
rRyantmUser,
nixpkgsCIActor,
githubActionsActor,
committers,
trustedOpenPGPPublicKeys,
riskyPullRequests,
riskyRepositoryActivity,
};
}
if (import.meta.main) {
const octokit = await getOctokit();
const facts = await fetchFacts(octokit);
const json = JSON.stringify(toJSONFacts(facts));
await Deno.writeTextFile("facts.json", json + "\n");
}