@patronage/factory-ci 1.0.0-alpha.4 → 1.0.0-alpha.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +712 -10
- package/dist/.build-fingerprint.json +3 -0
- package/dist/alchemy/index.d.ts +1174 -0
- package/dist/alchemy/index.js +2142 -0
- package/dist/bundle-alchemy-entry-CT5hCphA.js +112 -0
- package/dist/execute-alchemy-entry-B397GSk5.d.ts +134 -0
- package/dist/execute-alchemy-entry-CZ7Knz4q.js +160 -0
- package/dist/index.d.ts +1205 -87
- package/dist/index.js +2752 -317
- package/package.json +28 -10
- package/src/actions.ts +13 -0
- package/src/alchemy/child-diagnostics.ts +148 -0
- package/src/alchemy/credential-preflight.ts +428 -0
- package/src/alchemy/destroy-postcondition.ts +103 -0
- package/src/alchemy/evaluate-stack.ts +153 -0
- package/src/alchemy/first-deploy-authorization.ts +194 -0
- package/src/alchemy/index.ts +144 -0
- package/src/alchemy/lifecycle-failure.ts +174 -0
- package/src/alchemy/lifecycle.ts +1330 -0
- package/src/alchemy/local-emulation.ts +89 -0
- package/src/alchemy/plan-effects.ts +510 -0
- package/src/alchemy/retained-identity.ts +248 -0
- package/src/alchemy/secret-name-policy.ts +96 -0
- package/src/alchemy/stage-policy.ts +269 -0
- package/src/alchemy/state-tree.ts +168 -0
- package/src/alchemy/url-implies-auth.ts +264 -0
- package/src/alchemy-baseline.ts +75 -0
- package/src/bundle-alchemy-entry.ts +5 -0
- package/src/candidate-impact-workflow.ts +110 -0
- package/src/candidate-lifecycle.ts +29 -0
- package/src/cursor-oidc.ts +33 -0
- package/src/execute-alchemy-entry.ts +128 -8
- package/src/factory-workflow.ts +83 -40
- package/src/github-app-token.ts +92 -18
- package/src/github-transport.ts +186 -0
- package/src/impact-demand-condition.ts +67 -0
- package/src/index.ts +174 -0
- package/src/lifecycle-contract-lane.ts +113 -0
- package/src/merge-freeze-job.ts +253 -0
- package/src/pinned-action.ts +30 -0
- package/src/pr-status-hud-workflow.ts +96 -0
- package/src/preview-cleanup-topology.ts +115 -0
- package/src/preview-proof-inventory.ts +1092 -0
- package/src/preview-proof-lifecycle.ts +328 -0
- package/src/preview-receipt-workflow.ts +83 -0
- package/src/production-impact-workflow.ts +181 -0
- package/src/proof-reuse-gate.ts +223 -48
- package/src/proof-reuse-presentation.ts +131 -0
- package/src/push-identity-workflow.ts +463 -0
- package/src/runnable-cli.ts +27 -0
- package/src/verify-runner-policy.ts +290 -0
- package/src/vitest-profile-reader.test.ts +208 -0
- package/src/vitest-profile-reader.ts +225 -0
- package/src/vitest-profile.ts +631 -0
|
@@ -0,0 +1,1092 @@
|
|
|
1
|
+
import { parseLocalPreviewStage } from "./disposable-stage.ts";
|
|
2
|
+
import { GitHubApiError } from "./github-app-token.ts";
|
|
3
|
+
import {
|
|
4
|
+
githubJson as sharedGithubJson,
|
|
5
|
+
setGithubTransportWaitForTests,
|
|
6
|
+
} from "./github-transport.ts";
|
|
7
|
+
import type {
|
|
8
|
+
PreviewProofCleanupStatus,
|
|
9
|
+
PreviewProofRegistration,
|
|
10
|
+
} from "./preview-proof-lifecycle.ts";
|
|
11
|
+
import { previewProofLifecycle } from "./preview-proof-lifecycle.ts";
|
|
12
|
+
import { FACTORY_PROOF_GATE_APP_ID } from "./proof-reuse-gate.ts";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Hosted preview-proof inventory (#639): persist and list Factory-App
|
|
16
|
+
* registrations, and record cleanup outcomes, without PR comments.
|
|
17
|
+
*
|
|
18
|
+
* `list` is a read over App-owned commit check-runs and works with a workflow
|
|
19
|
+
* `GITHUB_TOKEN`. `persist` and `recordCleanup` write those check-runs, which
|
|
20
|
+
* GitHub only allows the producing App to update. Pass an installation token
|
|
21
|
+
* the caller minted; this package does not read operator config or hold
|
|
22
|
+
* secrets. Cleanup writes go through `previewProofLifecycle.transition` with
|
|
23
|
+
* `{ type: "cleanup" }`. Outcomes are never inferred from whether a stage
|
|
24
|
+
* still exists.
|
|
25
|
+
*
|
|
26
|
+
* Transient transport failures are retried with bounded backoff and then
|
|
27
|
+
* surface as `PreviewProofTransportError` (#830). A failure from `persist` or
|
|
28
|
+
* `recordCleanup` is an evidence-registration failure, never a failed
|
|
29
|
+
* deployment: the caller retains the healthy disposable stage, records its own
|
|
30
|
+
* proof BLOCKED (never PASS), and re-runs registration at the same head —
|
|
31
|
+
* writes are keyed by stack and stage, so resuming needs no redeploy.
|
|
32
|
+
* Admission stays fail-closed on its own: an unregistered proof never
|
|
33
|
+
* satisfies the preview demand.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** Check-run name that carries one registration JSON payload. Not a gate. */
|
|
37
|
+
export const PREVIEW_PROOF_INVENTORY_CHECK_NAME =
|
|
38
|
+
"patronage-factory/preview-proof";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Least privilege for a job whose `GITHUB_TOKEN` calls `list`. A
|
|
42
|
+
* `permissions` block zeroes every unlisted scope, and `list` reads five
|
|
43
|
+
* endpoints: `pulls/{pr}`, `pulls/{pr}/commits`, and the GraphQL force-push
|
|
44
|
+
* timeline (`pull-requests`), `compare` (`contents`), and
|
|
45
|
+
* `commits/{sha}/check-runs` (`checks`). A job
|
|
46
|
+
* that omits `pull-requests: read` fails with 403 on a same-repository PR
|
|
47
|
+
* and skips every destroy matrix (#859). Give the discovery job this object.
|
|
48
|
+
*/
|
|
49
|
+
export const PREVIEW_PROOF_INVENTORY_LIST_PERMISSIONS = Object.freeze({
|
|
50
|
+
checks: "read",
|
|
51
|
+
contents: "read",
|
|
52
|
+
"pull-requests": "read",
|
|
53
|
+
} as const);
|
|
54
|
+
|
|
55
|
+
const INVENTORY_KIND = "patronage-factory-preview-proof";
|
|
56
|
+
const INVENTORY_SCHEMA_VERSION = 1;
|
|
57
|
+
const DEFAULT_TIMEOUT_MS = 5000;
|
|
58
|
+
const CHECK_RUNS_PER_PAGE = 100;
|
|
59
|
+
const COMMITS_PER_PAGE = 100;
|
|
60
|
+
const MAX_CHECK_RUN_PAGES = 10;
|
|
61
|
+
const MAX_COMMIT_PAGES = 20;
|
|
62
|
+
/** GitHub's documented hard cap on `GET /pulls/{pr}/commits`. */
|
|
63
|
+
const GITHUB_PULL_COMMITS_CAP = 250;
|
|
64
|
+
const FORCE_PUSH_TIMELINE_PER_PAGE = 100;
|
|
65
|
+
const MAX_FORCE_PUSH_PAGES = 20;
|
|
66
|
+
const GRAPHQL_FORCE_PUSH_BEFORE = `query PreviewProofForcePushBefore($owner: String!, $name: String!, $pr: Int!, $first: Int!, $cursor: String) {
|
|
67
|
+
repository(owner: $owner, name: $name) {
|
|
68
|
+
pullRequest(number: $pr) {
|
|
69
|
+
timelineItems(itemTypes: [HEAD_REF_FORCE_PUSHED_EVENT], first: $first, after: $cursor) {
|
|
70
|
+
nodes {
|
|
71
|
+
... on HeadRefForcePushedEvent {
|
|
72
|
+
beforeCommit { oid }
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
pageInfo { endCursor hasNextPage }
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}`;
|
|
80
|
+
// GitHub secondary rate limit is 100 concurrent. 8 stays well under it.
|
|
81
|
+
export const PREVIEW_PROOF_LIST_CONCURRENCY = 8;
|
|
82
|
+
const SHA_PATTERN = /^[0-9a-f]{7,40}$/u;
|
|
83
|
+
const FULL_SHA_PATTERN = /^[0-9a-f]{40}$/u;
|
|
84
|
+
|
|
85
|
+
export interface PreviewProofInventoryStore {
|
|
86
|
+
list: (input: {
|
|
87
|
+
owner: string;
|
|
88
|
+
pr: number;
|
|
89
|
+
repo: string;
|
|
90
|
+
}) => Promise<readonly PreviewProofRegistration[]>;
|
|
91
|
+
put: (input: {
|
|
92
|
+
owner: string;
|
|
93
|
+
registration: PreviewProofRegistration;
|
|
94
|
+
repo: string;
|
|
95
|
+
}) => Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface PreviewProofInventoryTransport {
|
|
99
|
+
/** Injectable `fetch` (tests, or a caller with its own instrumented one). */
|
|
100
|
+
fetch?: typeof fetch;
|
|
101
|
+
/** Per-request timeout; defaults to five seconds. */
|
|
102
|
+
timeoutMs?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Bearer token. `list` accepts a workflow `GITHUB_TOKEN` (read) and needs
|
|
105
|
+
* commit (contents) read for the force-push timeline; the contents-less
|
|
106
|
+
* Factory App token cannot serve it (#825). `persist` and `recordCleanup`
|
|
107
|
+
* must use a Factory App installation token: GitHub refuses GITHUB_TOKEN
|
|
108
|
+
* writes to App-owned check-runs.
|
|
109
|
+
*/
|
|
110
|
+
token: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface PreviewProofInventoryAccess {
|
|
114
|
+
fetch?: typeof fetch;
|
|
115
|
+
store?: PreviewProofInventoryStore;
|
|
116
|
+
timeoutMs?: number;
|
|
117
|
+
token?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface PreviewProofInventoryListInput {
|
|
121
|
+
owner: string;
|
|
122
|
+
pr: number;
|
|
123
|
+
repo: string;
|
|
124
|
+
stack?: string;
|
|
125
|
+
stage?: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface PreviewProofInventoryPersistInput {
|
|
129
|
+
owner: string;
|
|
130
|
+
registration: PreviewProofRegistration;
|
|
131
|
+
repo: string;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export interface PreviewProofInventoryCleanupInput {
|
|
135
|
+
evidence: readonly string[];
|
|
136
|
+
headSha?: string;
|
|
137
|
+
outcome: Exclude<PreviewProofCleanupStatus, "pending">;
|
|
138
|
+
owner: string;
|
|
139
|
+
pr: number;
|
|
140
|
+
repo: string;
|
|
141
|
+
runUrl?: string;
|
|
142
|
+
stack: string;
|
|
143
|
+
stage: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
147
|
+
typeof value === "object" && value !== null;
|
|
148
|
+
|
|
149
|
+
const isCleanupStatus = (value: unknown): value is PreviewProofCleanupStatus =>
|
|
150
|
+
value === "failed" || value === "passed" || value === "pending";
|
|
151
|
+
|
|
152
|
+
const readStringArray = (value: unknown): readonly string[] | null =>
|
|
153
|
+
Array.isArray(value) && value.every((entry) => typeof entry === "string")
|
|
154
|
+
? value
|
|
155
|
+
: null;
|
|
156
|
+
|
|
157
|
+
const readIdentity = (value: Record<string, unknown>) => {
|
|
158
|
+
if (
|
|
159
|
+
typeof value.pr !== "number" ||
|
|
160
|
+
!Number.isInteger(value.pr) ||
|
|
161
|
+
value.pr <= 0 ||
|
|
162
|
+
typeof value.headSha !== "string" ||
|
|
163
|
+
!FULL_SHA_PATTERN.test(value.headSha) ||
|
|
164
|
+
typeof value.stack !== "string" ||
|
|
165
|
+
value.stack.length === 0 ||
|
|
166
|
+
typeof value.stage !== "string" ||
|
|
167
|
+
typeof value.url !== "string" ||
|
|
168
|
+
value.url.length === 0
|
|
169
|
+
) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
const parsedStage = parseLocalPreviewStage(value.stage, {
|
|
173
|
+
headSha: value.headSha,
|
|
174
|
+
pr: value.pr,
|
|
175
|
+
});
|
|
176
|
+
if (parsedStage === null) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
headSha: value.headSha,
|
|
181
|
+
pr: value.pr,
|
|
182
|
+
stack: value.stack,
|
|
183
|
+
stage: parsedStage.stage,
|
|
184
|
+
url: value.url,
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const readCleanup = (value: Record<string, unknown>) => {
|
|
189
|
+
if (!isRecord(value.cleanup) || !isCleanupStatus(value.cleanup.outcome)) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
const evidence = readStringArray(value.cleanup.evidence);
|
|
193
|
+
if (evidence === null) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
return typeof value.cleanup.runUrl === "string" &&
|
|
197
|
+
value.cleanup.runUrl.length > 0
|
|
198
|
+
? {
|
|
199
|
+
evidence,
|
|
200
|
+
outcome: value.cleanup.outcome,
|
|
201
|
+
runUrl: value.cleanup.runUrl,
|
|
202
|
+
}
|
|
203
|
+
: { evidence, outcome: value.cleanup.outcome };
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const readPassingChecks = (value: Record<string, unknown>) => {
|
|
207
|
+
if (!isRecord(value.convergence) || !isRecord(value.smoke)) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
if (
|
|
211
|
+
value.convergence.status !== "passed" ||
|
|
212
|
+
typeof value.convergence.detail !== "string" ||
|
|
213
|
+
value.smoke.outcome !== "passed" ||
|
|
214
|
+
typeof value.smoke.detail !== "string"
|
|
215
|
+
) {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
convergence: {
|
|
220
|
+
detail: value.convergence.detail,
|
|
221
|
+
status: "passed" as const,
|
|
222
|
+
},
|
|
223
|
+
smoke: { detail: value.smoke.detail, outcome: "passed" as const },
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const readProof = (value: Record<string, unknown>, headSha: string) => {
|
|
228
|
+
if (!isRecord(value.proof)) {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
if (
|
|
232
|
+
typeof value.proof.check !== "string" ||
|
|
233
|
+
value.proof.check.trim().length === 0 ||
|
|
234
|
+
value.proof.headSha !== headSha ||
|
|
235
|
+
typeof value.proof.mergeBaseSha !== "string" ||
|
|
236
|
+
!SHA_PATTERN.test(value.proof.mergeBaseSha) ||
|
|
237
|
+
typeof value.proof.patchId !== "string" ||
|
|
238
|
+
!SHA_PATTERN.test(value.proof.patchId)
|
|
239
|
+
) {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
check: value.proof.check,
|
|
244
|
+
headSha: value.proof.headSha,
|
|
245
|
+
mergeBaseSha: value.proof.mergeBaseSha,
|
|
246
|
+
patchId: value.proof.patchId,
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Rebuild a registration from untrusted JSON. Extra keys (comment ids, seed
|
|
252
|
+
* fields, stack unions) are dropped. Invalid identity returns `null`.
|
|
253
|
+
*/
|
|
254
|
+
const readPreviewProofRegistration = (
|
|
255
|
+
value: unknown
|
|
256
|
+
): PreviewProofRegistration | null => {
|
|
257
|
+
if (!isRecord(value) || value.source !== "local-self-certified") {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
const identity = readIdentity(value);
|
|
261
|
+
const cleanup = readCleanup(value);
|
|
262
|
+
const passing = readPassingChecks(value);
|
|
263
|
+
const proof = identity === null ? null : readProof(value, identity.headSha);
|
|
264
|
+
if (
|
|
265
|
+
identity === null ||
|
|
266
|
+
cleanup === null ||
|
|
267
|
+
passing === null ||
|
|
268
|
+
proof === null
|
|
269
|
+
) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
cleanup,
|
|
274
|
+
convergence: passing.convergence,
|
|
275
|
+
headSha: identity.headSha,
|
|
276
|
+
pr: identity.pr,
|
|
277
|
+
proof,
|
|
278
|
+
smoke: passing.smoke,
|
|
279
|
+
source: "local-self-certified",
|
|
280
|
+
stack: identity.stack,
|
|
281
|
+
stage: identity.stage,
|
|
282
|
+
url: identity.url,
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const readInventoryPayload = (
|
|
287
|
+
value: unknown
|
|
288
|
+
): PreviewProofRegistration | null => {
|
|
289
|
+
if (
|
|
290
|
+
!isRecord(value) ||
|
|
291
|
+
value.kind !== INVENTORY_KIND ||
|
|
292
|
+
value.schemaVersion !== INVENTORY_SCHEMA_VERSION
|
|
293
|
+
) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
return readPreviewProofRegistration(value.registration);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const assertPersistableRegistration = (
|
|
300
|
+
registration: PreviewProofRegistration
|
|
301
|
+
): PreviewProofRegistration => {
|
|
302
|
+
const read = readPreviewProofRegistration(registration);
|
|
303
|
+
if (read === null) {
|
|
304
|
+
throw new Error(
|
|
305
|
+
"Preview proof inventory cannot persist an invalid registration."
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
return read;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const registrationKey = (row: {
|
|
312
|
+
readonly stack: string;
|
|
313
|
+
readonly stage: string;
|
|
314
|
+
}): string => `${row.stack}:${row.stage}`;
|
|
315
|
+
|
|
316
|
+
const replaceRegistration = (
|
|
317
|
+
rows: readonly PreviewProofRegistration[],
|
|
318
|
+
next: PreviewProofRegistration
|
|
319
|
+
): PreviewProofRegistration[] => [
|
|
320
|
+
...rows.filter((row) => registrationKey(row) !== registrationKey(next)),
|
|
321
|
+
next,
|
|
322
|
+
];
|
|
323
|
+
|
|
324
|
+
const memoryKey = (owner: string, repo: string, pr: number): string =>
|
|
325
|
+
`${owner}/${repo}#${pr}`;
|
|
326
|
+
|
|
327
|
+
/** Reached by callers as `previewProofInventory.memoryStore`. */
|
|
328
|
+
const memoryStore = (): PreviewProofInventoryStore => {
|
|
329
|
+
const byPr = new Map<string, PreviewProofRegistration[]>();
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
list: (input) =>
|
|
333
|
+
Promise.resolve([
|
|
334
|
+
...(byPr.get(memoryKey(input.owner, input.repo, input.pr)) ?? []),
|
|
335
|
+
]),
|
|
336
|
+
put: (input) => {
|
|
337
|
+
const registration = assertPersistableRegistration(input.registration);
|
|
338
|
+
const key = memoryKey(input.owner, input.repo, registration.pr);
|
|
339
|
+
byPr.set(key, replaceRegistration(byPr.get(key) ?? [], registration));
|
|
340
|
+
return Promise.resolve();
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* The transport gave out — a network-level rejection (EPIPE, DNS, an aborted
|
|
347
|
+
* or timed-out request) or a transient HTTP status — and bounded retries did
|
|
348
|
+
* not recover it. GitHub never judged the content, so this is never a
|
|
349
|
+
* validation verdict and never a deployment failure. Only a bounded reason
|
|
350
|
+
* string is retained: the raw transport error can hold the request and its
|
|
351
|
+
* `Authorization` header, so it is deliberately discarded.
|
|
352
|
+
*/
|
|
353
|
+
export class PreviewProofTransportError extends Error {
|
|
354
|
+
/** How many transport attempts were made before giving up. */
|
|
355
|
+
readonly attempts: number;
|
|
356
|
+
/** The last transient HTTP status, when the server answered at all. */
|
|
357
|
+
readonly status?: number;
|
|
358
|
+
|
|
359
|
+
constructor(reason: string, attempts: number, status?: number) {
|
|
360
|
+
super(
|
|
361
|
+
`Preview proof inventory transport failed after ${attempts} attempt(s): ${reason}`
|
|
362
|
+
);
|
|
363
|
+
this.name = "PreviewProofTransportError";
|
|
364
|
+
this.attempts = attempts;
|
|
365
|
+
if (status !== undefined) {
|
|
366
|
+
this.status = status;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
type TransportWait = (ms: number) => Promise<void>;
|
|
372
|
+
|
|
373
|
+
/** Package-test seam. Not re-exported from `index.ts`. */
|
|
374
|
+
export const setPreviewProofTransportWaitForTests = (
|
|
375
|
+
next?: TransportWait
|
|
376
|
+
): void => {
|
|
377
|
+
setGithubTransportWaitForTests(next);
|
|
378
|
+
};
|
|
379
|
+
const githubJson = (
|
|
380
|
+
request: typeof fetch,
|
|
381
|
+
url: string,
|
|
382
|
+
token: string,
|
|
383
|
+
init: { body?: string; method: "GET" | "PATCH" | "POST" },
|
|
384
|
+
timeoutMs: number
|
|
385
|
+
): Promise<unknown> =>
|
|
386
|
+
sharedGithubJson(
|
|
387
|
+
{ ...init, fetch: request, timeoutMs, token, url },
|
|
388
|
+
{
|
|
389
|
+
refusal: (status, statusText) => new GitHubApiError(status, statusText),
|
|
390
|
+
transport: (failure, attempts) =>
|
|
391
|
+
new PreviewProofTransportError(
|
|
392
|
+
failure.reason,
|
|
393
|
+
attempts,
|
|
394
|
+
failure.status
|
|
395
|
+
),
|
|
396
|
+
}
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
const githubGraphql = async (
|
|
400
|
+
request: typeof fetch,
|
|
401
|
+
token: string,
|
|
402
|
+
timeoutMs: number,
|
|
403
|
+
query: string,
|
|
404
|
+
variables: Record<string, unknown>
|
|
405
|
+
): Promise<unknown> => {
|
|
406
|
+
const served = await githubJson(
|
|
407
|
+
request,
|
|
408
|
+
"https://api.github.com/graphql",
|
|
409
|
+
token,
|
|
410
|
+
{ body: JSON.stringify({ query, variables }), method: "POST" },
|
|
411
|
+
timeoutMs
|
|
412
|
+
);
|
|
413
|
+
if (!isRecord(served)) {
|
|
414
|
+
throw new TypeError("GitHub GraphQL response was not an object");
|
|
415
|
+
}
|
|
416
|
+
if (served.errors !== undefined) {
|
|
417
|
+
const details = Array.isArray(served.errors)
|
|
418
|
+
? served.errors
|
|
419
|
+
.flatMap((entry) =>
|
|
420
|
+
isRecord(entry)
|
|
421
|
+
? [
|
|
422
|
+
[entry.type, entry.message]
|
|
423
|
+
.filter((part) => typeof part === "string")
|
|
424
|
+
.join(": "),
|
|
425
|
+
]
|
|
426
|
+
: []
|
|
427
|
+
)
|
|
428
|
+
.filter((detail) => detail.length > 0)
|
|
429
|
+
.join("; ")
|
|
430
|
+
: "";
|
|
431
|
+
throw new Error(
|
|
432
|
+
details.length > 0
|
|
433
|
+
? `Preview proof inventory GraphQL query failed: ${details}`
|
|
434
|
+
: "Preview proof inventory GraphQL query failed."
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
return served.data;
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const repoApi = (owner: string, repo: string): string =>
|
|
441
|
+
`https://api.github.com/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`;
|
|
442
|
+
|
|
443
|
+
interface ServedCheckRun {
|
|
444
|
+
app?: { id?: null | number } | null;
|
|
445
|
+
head_sha?: string;
|
|
446
|
+
id?: number;
|
|
447
|
+
name?: string;
|
|
448
|
+
output?: { text?: null | string } | null;
|
|
449
|
+
started_at?: null | string;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const isServedCheckRun = (value: unknown): value is ServedCheckRun =>
|
|
453
|
+
isRecord(value);
|
|
454
|
+
|
|
455
|
+
const producedByFactoryApp = (run: ServedCheckRun): boolean =>
|
|
456
|
+
String(run.app?.id ?? "") === FACTORY_PROOF_GATE_APP_ID;
|
|
457
|
+
|
|
458
|
+
const assertFactoryAppWrite = (value: unknown): ServedCheckRun => {
|
|
459
|
+
if (!isServedCheckRun(value) || !producedByFactoryApp(value)) {
|
|
460
|
+
throw new Error(
|
|
461
|
+
"Preview proof inventory write must be owned by the Factory App."
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
return value;
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
const registrationFromCheckRun = (
|
|
468
|
+
run: ServedCheckRun,
|
|
469
|
+
expectedHeadSha?: string
|
|
470
|
+
): PreviewProofRegistration | null => {
|
|
471
|
+
if (
|
|
472
|
+
!producedByFactoryApp(run) ||
|
|
473
|
+
run.name !== PREVIEW_PROOF_INVENTORY_CHECK_NAME ||
|
|
474
|
+
typeof run.head_sha !== "string" ||
|
|
475
|
+
!FULL_SHA_PATTERN.test(run.head_sha)
|
|
476
|
+
) {
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
const text = run.output?.text;
|
|
480
|
+
if (typeof text !== "string" || text.length === 0) {
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
let registration: PreviewProofRegistration | null;
|
|
484
|
+
try {
|
|
485
|
+
registration = readInventoryPayload(JSON.parse(text) as unknown);
|
|
486
|
+
} catch {
|
|
487
|
+
return null;
|
|
488
|
+
}
|
|
489
|
+
if (
|
|
490
|
+
registration === null ||
|
|
491
|
+
run.head_sha !== registration.headSha ||
|
|
492
|
+
(expectedHeadSha !== undefined && run.head_sha !== expectedHeadSha)
|
|
493
|
+
) {
|
|
494
|
+
return null;
|
|
495
|
+
}
|
|
496
|
+
return registration;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
const checkRunOutput = (registration: PreviewProofRegistration) => {
|
|
500
|
+
const title =
|
|
501
|
+
`Preview proof ${registration.stack}/${registration.stage}`.slice(0, 255);
|
|
502
|
+
return {
|
|
503
|
+
summary: `cleanup ${registration.cleanup.outcome}`,
|
|
504
|
+
text: JSON.stringify({
|
|
505
|
+
kind: INVENTORY_KIND,
|
|
506
|
+
registration,
|
|
507
|
+
schemaVersion: INVENTORY_SCHEMA_VERSION,
|
|
508
|
+
}),
|
|
509
|
+
title,
|
|
510
|
+
};
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
const checkRunCreateBody = (registration: PreviewProofRegistration) => ({
|
|
514
|
+
conclusion: "neutral" as const,
|
|
515
|
+
head_sha: registration.headSha,
|
|
516
|
+
name: PREVIEW_PROOF_INVENTORY_CHECK_NAME,
|
|
517
|
+
output: checkRunOutput(registration),
|
|
518
|
+
status: "completed" as const,
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
const checkRunUpdateBody = (registration: PreviewProofRegistration) => ({
|
|
522
|
+
conclusion: "neutral" as const,
|
|
523
|
+
name: PREVIEW_PROOF_INVENTORY_CHECK_NAME,
|
|
524
|
+
output: checkRunOutput(registration),
|
|
525
|
+
status: "completed" as const,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const collectPages = async <T>(
|
|
529
|
+
loadPage: (page: number) => Promise<readonly T[]>,
|
|
530
|
+
pageSize: number,
|
|
531
|
+
maxPages: number,
|
|
532
|
+
exhaustedMessage: string
|
|
533
|
+
): Promise<T[]> => {
|
|
534
|
+
const collected: T[] = [];
|
|
535
|
+
for (let page = 1; page <= maxPages; page += 1) {
|
|
536
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
537
|
+
const rows = await loadPage(page);
|
|
538
|
+
collected.push(...rows);
|
|
539
|
+
if (rows.length < pageSize) {
|
|
540
|
+
return collected;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
throw new Error(exhaustedMessage);
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
const mapPool = async <Item, Result>(
|
|
547
|
+
items: readonly Item[],
|
|
548
|
+
concurrency: number,
|
|
549
|
+
mapper: (item: Item) => Promise<Result>
|
|
550
|
+
): Promise<readonly Result[]> => {
|
|
551
|
+
if (items.length === 0) {
|
|
552
|
+
return [];
|
|
553
|
+
}
|
|
554
|
+
const results: Result[] = [];
|
|
555
|
+
let next = 0;
|
|
556
|
+
await Promise.all(
|
|
557
|
+
Array.from({ length: Math.min(concurrency, items.length) }, async () => {
|
|
558
|
+
while (next < items.length) {
|
|
559
|
+
const index = next;
|
|
560
|
+
next += 1;
|
|
561
|
+
const item = items[index];
|
|
562
|
+
if (item === undefined) {
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
// oxlint-disable-next-line no-await-in-loop -- pool workers take the next SHA only after the current check-run list settles
|
|
566
|
+
results[index] = await mapper(item);
|
|
567
|
+
}
|
|
568
|
+
})
|
|
569
|
+
);
|
|
570
|
+
return results;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const listCompareCommitShas = (
|
|
574
|
+
request: typeof fetch,
|
|
575
|
+
token: string,
|
|
576
|
+
timeoutMs: number,
|
|
577
|
+
owner: string,
|
|
578
|
+
repo: string,
|
|
579
|
+
baseSha: string,
|
|
580
|
+
headSha: string
|
|
581
|
+
): Promise<readonly string[]> =>
|
|
582
|
+
collectPages(
|
|
583
|
+
async (page) => {
|
|
584
|
+
const query = new URLSearchParams({
|
|
585
|
+
page: String(page),
|
|
586
|
+
per_page: String(COMMITS_PER_PAGE),
|
|
587
|
+
});
|
|
588
|
+
const compared = await githubJson(
|
|
589
|
+
request,
|
|
590
|
+
`${repoApi(owner, repo)}/compare/${encodeURIComponent(baseSha)}...${encodeURIComponent(headSha)}?${query.toString()}`,
|
|
591
|
+
token,
|
|
592
|
+
{ method: "GET" },
|
|
593
|
+
timeoutMs
|
|
594
|
+
);
|
|
595
|
+
if (!isRecord(compared) || !Array.isArray(compared.commits)) {
|
|
596
|
+
throw new TypeError("GitHub compare response omitted commits");
|
|
597
|
+
}
|
|
598
|
+
return compared.commits.flatMap((commit) =>
|
|
599
|
+
isRecord(commit) && typeof commit.sha === "string" ? [commit.sha] : []
|
|
600
|
+
);
|
|
601
|
+
},
|
|
602
|
+
COMMITS_PER_PAGE,
|
|
603
|
+
MAX_COMMIT_PAGES,
|
|
604
|
+
"Preview proof inventory compare listing exhausted pagination before GitHub returned a final page."
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
const listForcePushBeforeShas = async (
|
|
608
|
+
request: typeof fetch,
|
|
609
|
+
token: string,
|
|
610
|
+
timeoutMs: number,
|
|
611
|
+
owner: string,
|
|
612
|
+
repo: string,
|
|
613
|
+
pr: number
|
|
614
|
+
): Promise<readonly string[]> => {
|
|
615
|
+
const shas: string[] = [];
|
|
616
|
+
let cursor: string | undefined;
|
|
617
|
+
for (let page = 1; page <= MAX_FORCE_PUSH_PAGES; page += 1) {
|
|
618
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
619
|
+
const data = await githubGraphql(
|
|
620
|
+
request,
|
|
621
|
+
token,
|
|
622
|
+
timeoutMs,
|
|
623
|
+
GRAPHQL_FORCE_PUSH_BEFORE,
|
|
624
|
+
{
|
|
625
|
+
cursor: cursor ?? null,
|
|
626
|
+
first: FORCE_PUSH_TIMELINE_PER_PAGE,
|
|
627
|
+
name: repo,
|
|
628
|
+
owner,
|
|
629
|
+
pr,
|
|
630
|
+
}
|
|
631
|
+
);
|
|
632
|
+
if (
|
|
633
|
+
!isRecord(data) ||
|
|
634
|
+
!isRecord(data.repository) ||
|
|
635
|
+
!isRecord(data.repository.pullRequest) ||
|
|
636
|
+
!isRecord(data.repository.pullRequest.timelineItems)
|
|
637
|
+
) {
|
|
638
|
+
throw new TypeError(
|
|
639
|
+
"GitHub GraphQL force-push timeline omitted timelineItems"
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
const items = data.repository.pullRequest.timelineItems;
|
|
643
|
+
if (!Array.isArray(items.nodes) || !isRecord(items.pageInfo)) {
|
|
644
|
+
throw new TypeError("GitHub GraphQL force-push timeline omitted nodes");
|
|
645
|
+
}
|
|
646
|
+
for (const node of items.nodes) {
|
|
647
|
+
if (
|
|
648
|
+
!isRecord(node) ||
|
|
649
|
+
!isRecord(node.beforeCommit) ||
|
|
650
|
+
typeof node.beforeCommit.oid !== "string" ||
|
|
651
|
+
!FULL_SHA_PATTERN.test(node.beforeCommit.oid)
|
|
652
|
+
) {
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
shas.push(node.beforeCommit.oid);
|
|
656
|
+
}
|
|
657
|
+
if (items.pageInfo.hasNextPage !== true) {
|
|
658
|
+
return shas;
|
|
659
|
+
}
|
|
660
|
+
if (
|
|
661
|
+
typeof items.pageInfo.endCursor !== "string" ||
|
|
662
|
+
items.pageInfo.endCursor.length === 0
|
|
663
|
+
) {
|
|
664
|
+
throw new TypeError(
|
|
665
|
+
"GitHub GraphQL force-push timeline omitted endCursor"
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
cursor = items.pageInfo.endCursor;
|
|
669
|
+
}
|
|
670
|
+
throw new Error(
|
|
671
|
+
"Preview proof inventory force-push timeline exhausted pagination before GitHub returned a final page."
|
|
672
|
+
);
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
const listPullRequestCommitShas = (
|
|
676
|
+
request: typeof fetch,
|
|
677
|
+
token: string,
|
|
678
|
+
timeoutMs: number,
|
|
679
|
+
owner: string,
|
|
680
|
+
repo: string,
|
|
681
|
+
pr: number
|
|
682
|
+
): Promise<readonly string[]> =>
|
|
683
|
+
collectPages(
|
|
684
|
+
async (page) => {
|
|
685
|
+
const query = new URLSearchParams({
|
|
686
|
+
page: String(page),
|
|
687
|
+
per_page: String(COMMITS_PER_PAGE),
|
|
688
|
+
});
|
|
689
|
+
const commits = await githubJson(
|
|
690
|
+
request,
|
|
691
|
+
`${repoApi(owner, repo)}/pulls/${pr}/commits?${query.toString()}`,
|
|
692
|
+
token,
|
|
693
|
+
{ method: "GET" },
|
|
694
|
+
timeoutMs
|
|
695
|
+
);
|
|
696
|
+
if (!Array.isArray(commits)) {
|
|
697
|
+
throw new TypeError(
|
|
698
|
+
"GitHub pull request commits response was not an array"
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
return commits.flatMap((commit) =>
|
|
702
|
+
isRecord(commit) && typeof commit.sha === "string" ? [commit.sha] : []
|
|
703
|
+
);
|
|
704
|
+
},
|
|
705
|
+
COMMITS_PER_PAGE,
|
|
706
|
+
MAX_COMMIT_PAGES,
|
|
707
|
+
"Preview proof inventory commit listing exhausted pagination before GitHub returned a final page."
|
|
708
|
+
);
|
|
709
|
+
|
|
710
|
+
const readPullBaseSha = (pull: Record<string, unknown>): string => {
|
|
711
|
+
if (!isRecord(pull.base) || typeof pull.base.sha !== "string") {
|
|
712
|
+
throw new TypeError("GitHub pull request response omitted base.sha");
|
|
713
|
+
}
|
|
714
|
+
return pull.base.sha;
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Current head, replaced heads from force-push, the commits each replaced head
|
|
719
|
+
* carried, and PR commits. The pull-commits endpoint silently stops at 250 and
|
|
720
|
+
* never lists a rebased-away head, so those two cases use compare pagination
|
|
721
|
+
* and the force-push timeline. A persist, then a fast-forward, then a rebase
|
|
722
|
+
* leaves the persist SHA behind the replaced head, so each replaced head is
|
|
723
|
+
* also walked back to the base (#796).
|
|
724
|
+
*/
|
|
725
|
+
const listCommitShasForPull = async (
|
|
726
|
+
request: typeof fetch,
|
|
727
|
+
token: string,
|
|
728
|
+
timeoutMs: number,
|
|
729
|
+
owner: string,
|
|
730
|
+
repo: string,
|
|
731
|
+
pr: number
|
|
732
|
+
): Promise<readonly string[]> => {
|
|
733
|
+
const pull = await githubJson(
|
|
734
|
+
request,
|
|
735
|
+
`${repoApi(owner, repo)}/pulls/${pr}`,
|
|
736
|
+
token,
|
|
737
|
+
{ method: "GET" },
|
|
738
|
+
timeoutMs
|
|
739
|
+
);
|
|
740
|
+
if (
|
|
741
|
+
!isRecord(pull) ||
|
|
742
|
+
!isRecord(pull.head) ||
|
|
743
|
+
typeof pull.head.sha !== "string"
|
|
744
|
+
) {
|
|
745
|
+
throw new TypeError("GitHub pull request response omitted head.sha");
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const pullCommits = await listPullRequestCommitShas(
|
|
749
|
+
request,
|
|
750
|
+
token,
|
|
751
|
+
timeoutMs,
|
|
752
|
+
owner,
|
|
753
|
+
repo,
|
|
754
|
+
pr
|
|
755
|
+
);
|
|
756
|
+
let older = pullCommits;
|
|
757
|
+
if (pullCommits.length >= GITHUB_PULL_COMMITS_CAP) {
|
|
758
|
+
older = await listCompareCommitShas(
|
|
759
|
+
request,
|
|
760
|
+
token,
|
|
761
|
+
timeoutMs,
|
|
762
|
+
owner,
|
|
763
|
+
repo,
|
|
764
|
+
readPullBaseSha(pull),
|
|
765
|
+
pull.head.sha
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
const previousHeads = await listForcePushBeforeShas(
|
|
770
|
+
request,
|
|
771
|
+
token,
|
|
772
|
+
timeoutMs,
|
|
773
|
+
owner,
|
|
774
|
+
repo,
|
|
775
|
+
pr
|
|
776
|
+
);
|
|
777
|
+
|
|
778
|
+
let replaced: readonly string[] = [];
|
|
779
|
+
if (previousHeads.length > 0) {
|
|
780
|
+
const baseSha = readPullBaseSha(pull);
|
|
781
|
+
const histories = await mapPool(
|
|
782
|
+
previousHeads,
|
|
783
|
+
PREVIEW_PROOF_LIST_CONCURRENCY,
|
|
784
|
+
(previousHead) =>
|
|
785
|
+
listCompareCommitShas(
|
|
786
|
+
request,
|
|
787
|
+
token,
|
|
788
|
+
timeoutMs,
|
|
789
|
+
owner,
|
|
790
|
+
repo,
|
|
791
|
+
baseSha,
|
|
792
|
+
previousHead
|
|
793
|
+
)
|
|
794
|
+
);
|
|
795
|
+
replaced = histories.flatMap((history) => [...history].toReversed());
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
const shas: string[] = [];
|
|
799
|
+
const seen = new Set<string>();
|
|
800
|
+
for (const sha of [
|
|
801
|
+
pull.head.sha,
|
|
802
|
+
...previousHeads,
|
|
803
|
+
...older.toReversed(),
|
|
804
|
+
...replaced,
|
|
805
|
+
]) {
|
|
806
|
+
if (!seen.has(sha)) {
|
|
807
|
+
seen.add(sha);
|
|
808
|
+
shas.push(sha);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
return shas;
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
const listCheckRunsOnSha = (
|
|
815
|
+
request: typeof fetch,
|
|
816
|
+
token: string,
|
|
817
|
+
timeoutMs: number,
|
|
818
|
+
owner: string,
|
|
819
|
+
repo: string,
|
|
820
|
+
sha: string
|
|
821
|
+
): Promise<readonly ServedCheckRun[]> =>
|
|
822
|
+
collectPages(
|
|
823
|
+
async (page) => {
|
|
824
|
+
const query = new URLSearchParams({
|
|
825
|
+
check_name: PREVIEW_PROOF_INVENTORY_CHECK_NAME,
|
|
826
|
+
filter: "all",
|
|
827
|
+
page: String(page),
|
|
828
|
+
per_page: String(CHECK_RUNS_PER_PAGE),
|
|
829
|
+
});
|
|
830
|
+
const served = await githubJson(
|
|
831
|
+
request,
|
|
832
|
+
`${repoApi(owner, repo)}/commits/${encodeURIComponent(sha)}/check-runs?${query.toString()}`,
|
|
833
|
+
token,
|
|
834
|
+
{ method: "GET" },
|
|
835
|
+
timeoutMs
|
|
836
|
+
);
|
|
837
|
+
if (!isRecord(served) || !Array.isArray(served.check_runs)) {
|
|
838
|
+
throw new TypeError("GitHub check-runs response omitted check_runs");
|
|
839
|
+
}
|
|
840
|
+
return served.check_runs.filter((run) => isServedCheckRun(run));
|
|
841
|
+
},
|
|
842
|
+
CHECK_RUNS_PER_PAGE,
|
|
843
|
+
MAX_CHECK_RUN_PAGES,
|
|
844
|
+
"Preview proof inventory check-run listing exhausted pagination before GitHub returned a final page."
|
|
845
|
+
);
|
|
846
|
+
|
|
847
|
+
const compareCheckRunsNewestFirst = (
|
|
848
|
+
left: ServedCheckRun,
|
|
849
|
+
right: ServedCheckRun
|
|
850
|
+
): number => {
|
|
851
|
+
const leftMs = Date.parse(left.started_at ?? "");
|
|
852
|
+
const rightMs = Date.parse(right.started_at ?? "");
|
|
853
|
+
if (Number.isFinite(leftMs) && Number.isFinite(rightMs)) {
|
|
854
|
+
return rightMs - leftMs || (right.id ?? 0) - (left.id ?? 0);
|
|
855
|
+
}
|
|
856
|
+
return (right.id ?? 0) - (left.id ?? 0);
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
const takeNewestRegistrations = (
|
|
860
|
+
runsBySha: readonly {
|
|
861
|
+
readonly runs: readonly ServedCheckRun[];
|
|
862
|
+
readonly sha: string;
|
|
863
|
+
}[],
|
|
864
|
+
pr: number
|
|
865
|
+
): PreviewProofRegistration[] => {
|
|
866
|
+
const byKey = new Map<string, PreviewProofRegistration>();
|
|
867
|
+
for (const { runs, sha } of runsBySha) {
|
|
868
|
+
for (const run of [...runs].toSorted(compareCheckRunsNewestFirst)) {
|
|
869
|
+
const registration = registrationFromCheckRun(run, sha);
|
|
870
|
+
if (registration === null || registration.pr !== pr) {
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
const key = registrationKey(registration);
|
|
874
|
+
if (!byKey.has(key)) {
|
|
875
|
+
byKey.set(key, registration);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
return [...byKey.values()];
|
|
880
|
+
};
|
|
881
|
+
|
|
882
|
+
/** Reached by callers as `previewProofInventory.githubStore`. */
|
|
883
|
+
const githubStore = (
|
|
884
|
+
transport: PreviewProofInventoryTransport
|
|
885
|
+
): PreviewProofInventoryStore => {
|
|
886
|
+
const request = transport.fetch ?? fetch;
|
|
887
|
+
const timeoutMs = transport.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
888
|
+
const { token } = transport;
|
|
889
|
+
|
|
890
|
+
return {
|
|
891
|
+
list: async (input) => {
|
|
892
|
+
const shas = await listCommitShasForPull(
|
|
893
|
+
request,
|
|
894
|
+
token,
|
|
895
|
+
timeoutMs,
|
|
896
|
+
input.owner,
|
|
897
|
+
input.repo,
|
|
898
|
+
input.pr
|
|
899
|
+
);
|
|
900
|
+
const runsBySha = await mapPool(
|
|
901
|
+
shas,
|
|
902
|
+
PREVIEW_PROOF_LIST_CONCURRENCY,
|
|
903
|
+
async (sha) => ({
|
|
904
|
+
runs: await listCheckRunsOnSha(
|
|
905
|
+
request,
|
|
906
|
+
token,
|
|
907
|
+
timeoutMs,
|
|
908
|
+
input.owner,
|
|
909
|
+
input.repo,
|
|
910
|
+
sha
|
|
911
|
+
),
|
|
912
|
+
sha,
|
|
913
|
+
})
|
|
914
|
+
);
|
|
915
|
+
return takeNewestRegistrations(runsBySha, input.pr);
|
|
916
|
+
},
|
|
917
|
+
|
|
918
|
+
put: async (input) => {
|
|
919
|
+
const registration = assertPersistableRegistration(input.registration);
|
|
920
|
+
const runs = await listCheckRunsOnSha(
|
|
921
|
+
request,
|
|
922
|
+
token,
|
|
923
|
+
timeoutMs,
|
|
924
|
+
input.owner,
|
|
925
|
+
input.repo,
|
|
926
|
+
registration.headSha
|
|
927
|
+
);
|
|
928
|
+
const existing = [...runs]
|
|
929
|
+
.toSorted(compareCheckRunsNewestFirst)
|
|
930
|
+
.find((run) => {
|
|
931
|
+
const row = registrationFromCheckRun(run, registration.headSha);
|
|
932
|
+
return (
|
|
933
|
+
row !== null &&
|
|
934
|
+
row.pr === registration.pr &&
|
|
935
|
+
row.stack === registration.stack &&
|
|
936
|
+
row.stage === registration.stage &&
|
|
937
|
+
typeof run.id === "number"
|
|
938
|
+
);
|
|
939
|
+
});
|
|
940
|
+
if (existing?.id === undefined) {
|
|
941
|
+
assertFactoryAppWrite(
|
|
942
|
+
await githubJson(
|
|
943
|
+
request,
|
|
944
|
+
`${repoApi(input.owner, input.repo)}/check-runs`,
|
|
945
|
+
token,
|
|
946
|
+
{
|
|
947
|
+
body: JSON.stringify(checkRunCreateBody(registration)),
|
|
948
|
+
method: "POST",
|
|
949
|
+
},
|
|
950
|
+
timeoutMs
|
|
951
|
+
)
|
|
952
|
+
);
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
assertFactoryAppWrite(
|
|
956
|
+
await githubJson(
|
|
957
|
+
request,
|
|
958
|
+
`${repoApi(input.owner, input.repo)}/check-runs/${existing.id}`,
|
|
959
|
+
token,
|
|
960
|
+
{
|
|
961
|
+
body: JSON.stringify(checkRunUpdateBody(registration)),
|
|
962
|
+
method: "PATCH",
|
|
963
|
+
},
|
|
964
|
+
timeoutMs
|
|
965
|
+
)
|
|
966
|
+
);
|
|
967
|
+
},
|
|
968
|
+
};
|
|
969
|
+
};
|
|
970
|
+
|
|
971
|
+
const resolveStore = (
|
|
972
|
+
access: PreviewProofInventoryAccess
|
|
973
|
+
): PreviewProofInventoryStore => {
|
|
974
|
+
if (access.store !== undefined) {
|
|
975
|
+
return access.store;
|
|
976
|
+
}
|
|
977
|
+
if (access.token !== undefined && access.token.length > 0) {
|
|
978
|
+
return githubStore({
|
|
979
|
+
fetch: access.fetch,
|
|
980
|
+
timeoutMs: access.timeoutMs,
|
|
981
|
+
token: access.token,
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
throw new Error(
|
|
985
|
+
"Preview proof inventory requires a store or a GitHub token."
|
|
986
|
+
);
|
|
987
|
+
};
|
|
988
|
+
|
|
989
|
+
const list = async (
|
|
990
|
+
input: PreviewProofInventoryListInput,
|
|
991
|
+
access: PreviewProofInventoryAccess
|
|
992
|
+
): Promise<readonly PreviewProofRegistration[]> => {
|
|
993
|
+
const rows = await resolveStore(access).list({
|
|
994
|
+
owner: input.owner,
|
|
995
|
+
pr: input.pr,
|
|
996
|
+
repo: input.repo,
|
|
997
|
+
});
|
|
998
|
+
return rows.filter(
|
|
999
|
+
(row) =>
|
|
1000
|
+
(input.stack === undefined || row.stack === input.stack) &&
|
|
1001
|
+
(input.stage === undefined || row.stage === input.stage)
|
|
1002
|
+
);
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
const persist = async (
|
|
1006
|
+
input: PreviewProofInventoryPersistInput,
|
|
1007
|
+
access: PreviewProofInventoryAccess
|
|
1008
|
+
): Promise<void> => {
|
|
1009
|
+
const registration = assertPersistableRegistration(input.registration);
|
|
1010
|
+
await resolveStore(access).put({
|
|
1011
|
+
owner: input.owner,
|
|
1012
|
+
registration,
|
|
1013
|
+
repo: input.repo,
|
|
1014
|
+
});
|
|
1015
|
+
};
|
|
1016
|
+
|
|
1017
|
+
const recordCleanup = async (
|
|
1018
|
+
input: PreviewProofInventoryCleanupInput,
|
|
1019
|
+
access: PreviewProofInventoryAccess
|
|
1020
|
+
): Promise<PreviewProofRegistration> => {
|
|
1021
|
+
const parsedStage = parseLocalPreviewStage(
|
|
1022
|
+
input.stage,
|
|
1023
|
+
input.headSha === undefined
|
|
1024
|
+
? { pr: input.pr }
|
|
1025
|
+
: { headSha: input.headSha, pr: input.pr }
|
|
1026
|
+
);
|
|
1027
|
+
if (parsedStage === null) {
|
|
1028
|
+
throw new Error(
|
|
1029
|
+
`Preview proof stage must be a disposable local-pr stage owned by PR #${input.pr} at this head.`
|
|
1030
|
+
);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
const store = resolveStore(access);
|
|
1034
|
+
const listed = await store.list({
|
|
1035
|
+
owner: input.owner,
|
|
1036
|
+
pr: input.pr,
|
|
1037
|
+
repo: input.repo,
|
|
1038
|
+
});
|
|
1039
|
+
const existing = listed.find(
|
|
1040
|
+
(row) =>
|
|
1041
|
+
row.pr === input.pr &&
|
|
1042
|
+
row.stack === input.stack &&
|
|
1043
|
+
row.stage === parsedStage.stage
|
|
1044
|
+
);
|
|
1045
|
+
if (
|
|
1046
|
+
input.headSha !== undefined &&
|
|
1047
|
+
existing !== undefined &&
|
|
1048
|
+
existing.headSha !== input.headSha
|
|
1049
|
+
) {
|
|
1050
|
+
throw new Error(
|
|
1051
|
+
"Preview proof identity does not match the cleanup target."
|
|
1052
|
+
);
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
const next = previewProofLifecycle.transition(
|
|
1056
|
+
{ registrations: listed },
|
|
1057
|
+
{
|
|
1058
|
+
evidence: input.evidence,
|
|
1059
|
+
outcome: input.outcome,
|
|
1060
|
+
pr: input.pr,
|
|
1061
|
+
...(input.runUrl === undefined ? {} : { runUrl: input.runUrl }),
|
|
1062
|
+
stack: input.stack,
|
|
1063
|
+
stage: parsedStage.stage,
|
|
1064
|
+
type: "cleanup",
|
|
1065
|
+
}
|
|
1066
|
+
);
|
|
1067
|
+
const [updated] = previewProofLifecycle.resolve(next, {
|
|
1068
|
+
pr: input.pr,
|
|
1069
|
+
stack: input.stack,
|
|
1070
|
+
stage: parsedStage.stage,
|
|
1071
|
+
});
|
|
1072
|
+
if (updated === undefined) {
|
|
1073
|
+
throw new Error(
|
|
1074
|
+
`Preview proof cleanup requires a registration for ${input.stack}/${parsedStage.stage}.`
|
|
1075
|
+
);
|
|
1076
|
+
}
|
|
1077
|
+
await store.put({
|
|
1078
|
+
owner: input.owner,
|
|
1079
|
+
registration: updated,
|
|
1080
|
+
repo: input.repo,
|
|
1081
|
+
});
|
|
1082
|
+
return updated;
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1085
|
+
export const previewProofInventory = {
|
|
1086
|
+
githubStore,
|
|
1087
|
+
list,
|
|
1088
|
+
listPermissions: PREVIEW_PROOF_INVENTORY_LIST_PERMISSIONS,
|
|
1089
|
+
memoryStore,
|
|
1090
|
+
persist,
|
|
1091
|
+
recordCleanup,
|
|
1092
|
+
} as const;
|