@ref0/workspace 0.10.0 → 0.11.0
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 +143 -1
- package/dist/bin.mjs +19 -7
- package/dist/lint/generic/folder-imports.mjs +84 -0
- package/dist/lint/generic/folder-layers.d.mts +40 -0
- package/dist/lint/generic/folder-layers.mjs +82 -0
- package/dist/lint/generic/imports.mjs +34 -0
- package/dist/lint/generic/package-graph.d.mts +36 -0
- package/dist/lint/generic/package-graph.mjs +111 -0
- package/dist/lint/generic/package-imports.mjs +85 -0
- package/dist/lint/generic/plugin.d.mts +4 -3
- package/dist/lint/generic/plugin.mjs +8 -3
- package/dist/lint/generic/rule-options.mjs +17 -0
- package/dist/lint/generic/workspace-packages.d.mts +29 -0
- package/dist/lint/generic/workspace-packages.mjs +78 -0
- package/dist/lint/graph.d.mts +4 -0
- package/dist/lint/graph.mjs +3 -0
- package/dist/review-record/command.mjs +107 -0
- package/dist/review-record/index.d.mts +3 -0
- package/dist/review-record/index.mjs +3 -0
- package/dist/review-record/review-record.d.mts +33 -0
- package/dist/review-record/review-record.mjs +1253 -0
- package/dist/review-record/settings.d.mts +39 -0
- package/dist/review-record/settings.mjs +100 -0
- package/dist/symbols/symbols.mjs +1 -1
- package/package.json +9 -1
|
@@ -0,0 +1,1253 @@
|
|
|
1
|
+
import { defaults, listPath, settingsFrom, settingsPath } from "./settings.mjs";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { env } from "node:process";
|
|
4
|
+
// src/review-record/review-record.ts
|
|
5
|
+
/**
|
|
6
|
+
* Whether a pull request's review is on record for its merge risk, as the review skill's
|
|
7
|
+
* `merge-risk.md` says. CI's `CI result` runs `ref0-workspace review-record` on a pull request's
|
|
8
|
+
* run, and an agent runs `--count` to learn whether its change is large and its tier floor, as
|
|
9
|
+
* `command.ts` says. What differs per repo is in its settings, `settings.ts`'s, read from the base.
|
|
10
|
+
*
|
|
11
|
+
* The floor is the lowest tier the pull request may name, from its size and paths, as `floorOf`
|
|
12
|
+
* works it out: high past `highTierLines` changed lines, on the base's `review-paths.txt` at high
|
|
13
|
+
* or on a large change, medium past `lowTierLines` or on it at medium or on `testRisks`' list, and
|
|
14
|
+
* low otherwise. Its body names its tier on a `Merge risk:` line, at the floor or above, and the
|
|
15
|
+
* tier needs:
|
|
16
|
+
* - low: no session. Each test file it edits is named on a `Break:` line of the body, the break the
|
|
17
|
+
* builder ran, as test-audit's authoring gate asks.
|
|
18
|
+
* - medium: a session whose first table counts 1 reviewer and 1 round. Its reviewer is a tests one
|
|
19
|
+
* when the floor is from `testRisks`, and a tests one or a `Break:` line for each when it edits
|
|
20
|
+
* test code otherwise.
|
|
21
|
+
* - high: a session, whose first table counts it at `high` with 1 to 4 reviewers.
|
|
22
|
+
* - Each session's reviewer table has a row for each reviewer its first table counts, each with
|
|
23
|
+
* one review type and its scope, since each reviewer is a Codex run of its own.
|
|
24
|
+
* - On a pull request that changes a file a review covers, each session names every type in
|
|
25
|
+
* `consideredTypes` in its reviewer table or, with why, on its `Not run:` line.
|
|
26
|
+
* - A large change, 1,000 or more net lines of production source, needs a session with a code
|
|
27
|
+
* quality reviewer, which is why a large change's floor is high.
|
|
28
|
+
* - A pull request that changes test code, as `testCode` lists it, needs a session with a tests
|
|
29
|
+
* reviewer.
|
|
30
|
+
* - At any tier, a pull request that changes a file the base's `benchedPaths` list ties to a bench
|
|
31
|
+
* case holds a reading for each such case in its body's "Bench and cost" fold: a line that names
|
|
32
|
+
* the case with a run's link, or labelled local, as `benchReadings` reads it. A line that says the
|
|
33
|
+
* case's bundle is unchanged counts only when a run it links is ci-bench's on main and holds a
|
|
34
|
+
* passed job naming that digest, the pull request's number and its head, as `provenUnchanged`
|
|
35
|
+
* says.
|
|
36
|
+
* - Only the base's settings, `review-paths.txt` and bench list count, so a pull request can't take
|
|
37
|
+
* its own files off them; a base with no review list puts every production file at high, and one
|
|
38
|
+
* with no bench list ties no case.
|
|
39
|
+
* - On a medium or high pull request that changes a file a review covers, the newest session whose
|
|
40
|
+
* `Through:` commit is in the head's history counts: its `Through: <sha>` line's, or its first
|
|
41
|
+
* line's `at <sha>` without one. None of the pull request's own commits after it, those the base
|
|
42
|
+
* doesn't hold, may change one of its covered files by hand. A merge changes what differs from
|
|
43
|
+
* `git merge-tree`'s merge of its parents, so a clean merge changes nothing. An agent moves
|
|
44
|
+
* `Through:` to the head when the pushes since were finding fixes, CI fixes or merges from main
|
|
45
|
+
* that it read. A session whose commit the history doesn't hold, as after a force push, fails,
|
|
46
|
+
* unless a newer session counts.
|
|
47
|
+
*
|
|
48
|
+
* A bot's comment doesn't count, and a session counts only from someone who may push to the repo,
|
|
49
|
+
* since anyone may comment once it's public. A pull request whose author may not push needs a
|
|
50
|
+
* session at any tier, as review.md says. It reads GitHub with `gh`, and the head's history with
|
|
51
|
+
* `git` from the checkout that holds it, whole.
|
|
52
|
+
*/
|
|
53
|
+
/** Net lines of production source from which a change is large and needs a code quality reviewer. */
|
|
54
|
+
const largeChange = 1e3;
|
|
55
|
+
/** The merge-risk tiers, lowest first, as the review skill's `merge-risk.md` names them. */
|
|
56
|
+
const tiers = [
|
|
57
|
+
"low",
|
|
58
|
+
"medium",
|
|
59
|
+
"high"
|
|
60
|
+
];
|
|
61
|
+
/** The most files GitHub's `pulls/<n>/files` lists, however it's paged. */
|
|
62
|
+
const githubFileCap = 3e3;
|
|
63
|
+
const rank = (tier) => tiers.indexOf(tier);
|
|
64
|
+
const isTier = (word) => tiers.some((tier) => tier === word);
|
|
65
|
+
const highest = (found) => found.reduce((most, tier) => tier !== void 0 && (most === void 0 || rank(tier) > rank(most)) ? tier : most, void 0);
|
|
66
|
+
/** A session's first line: how it ended, at the commit it names. */
|
|
67
|
+
const sessionLine = /^## Review: (?:clean|open) at ([0-9a-f]{7,40})$/;
|
|
68
|
+
/** The columns of a session's first table, whose one row names its model and effort and counts its reviewers and rounds. */
|
|
69
|
+
const countColumns = [
|
|
70
|
+
"Model",
|
|
71
|
+
"Effort",
|
|
72
|
+
"Reviewers",
|
|
73
|
+
"Rounds"
|
|
74
|
+
];
|
|
75
|
+
/** The columns of a session's reviewer table, which has a row for each reviewer. */
|
|
76
|
+
const reviewerColumns = [
|
|
77
|
+
"Reviewer",
|
|
78
|
+
"Model",
|
|
79
|
+
"What it read",
|
|
80
|
+
"Raised",
|
|
81
|
+
"Real"
|
|
82
|
+
];
|
|
83
|
+
/** A Markdown table row's cells, trimmed, with an escaped `\|` kept inside its cell. */
|
|
84
|
+
const cellsOf = (row) => row.trim().replace(/^\|/, "").replace(/(?<!\\)\|$/, "").split(/(?<!\\)\|/).map((cell) => cell.trim());
|
|
85
|
+
/**
|
|
86
|
+
* The next table from line `from`, when its header is `columns` and its rule has a cell of dashes
|
|
87
|
+
* for each: its rows, each as its cells, and the line after it. Undefined when that table isn't so.
|
|
88
|
+
*/
|
|
89
|
+
const tableAt = (lines, columns, from) => {
|
|
90
|
+
const start = lines.findIndex((line, i) => i >= from && line.startsWith("|"));
|
|
91
|
+
if (start === -1 || cellsOf(lines[start] ?? "").join("|") !== columns.join("|")) return void 0;
|
|
92
|
+
const rule = lines[start + 1] ?? "";
|
|
93
|
+
const dashes = cellsOf(rule);
|
|
94
|
+
if (!rule.startsWith("|") || dashes.length !== columns.length) return void 0;
|
|
95
|
+
if (!dashes.every((cell) => /^:?-{3,}:?$/.test(cell))) return void 0;
|
|
96
|
+
const after = lines.findIndex((line, i) => i > start + 1 && !line.startsWith("|"));
|
|
97
|
+
const end = after === -1 ? lines.length : after;
|
|
98
|
+
return {
|
|
99
|
+
rows: lines.slice(start + 2, end).map(cellsOf),
|
|
100
|
+
end
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* A session's model, reviewers and rounds, from its first table's one row, at high with 1 to 4
|
|
105
|
+
* reviewers: undefined when it doesn't read so.
|
|
106
|
+
*/
|
|
107
|
+
const countsOf = (lines) => {
|
|
108
|
+
const table = tableAt(lines, countColumns, 1);
|
|
109
|
+
if (table?.rows.length !== 1) return void 0;
|
|
110
|
+
const [model = "", effort = "", reviewers = "", rounds = "", ...rest] = table.rows[0] ?? [];
|
|
111
|
+
return rest.length === 0 && model !== "" && effort === "high" && /^[1-4]$/.test(reviewers) && /^[0-9]+$/.test(rounds) ? {
|
|
112
|
+
counted: Number(reviewers),
|
|
113
|
+
rounds: Number(rounds),
|
|
114
|
+
end: table.end
|
|
115
|
+
} : void 0;
|
|
116
|
+
};
|
|
117
|
+
/** Whether a comment is a session: its first line, then its first table at high with 1 to 4 reviewers. */
|
|
118
|
+
const isSession = (comment) => sessionLine.test(comment.split("\n")[0] ?? "") && countsOf(comment.split("\n")) !== void 0;
|
|
119
|
+
/** A session's line that names the commit it vouches for, past its first line's. */
|
|
120
|
+
const throughLine = /^Through: ([0-9a-f]{7,40})$/;
|
|
121
|
+
/** The body's line that names its tier, as merge-risk.md writes it. */
|
|
122
|
+
const mergeRiskLine = /^(?:[-*] )?(?:\*\*)?Merge risk:(?:\*\*)?[ \t]*(?:\*\*)?(low|medium|high)\b/im;
|
|
123
|
+
/** A body's line that records a break a test ran, as test-audit's authoring gate asks. */
|
|
124
|
+
const breakLine = /^(?:[-*] )?(?:\*\*)?Break:(?:\*\*)?[ \t]*\S.*$/gm;
|
|
125
|
+
/** A test's line that starts a row: `it(`, `test.each(`, `describe(` and the like. */
|
|
126
|
+
const testRow = /^\s*(?:it|test|describe|bench)(?:\.\w+)*\s*\(/;
|
|
127
|
+
/** A test's line that skips a row, or runs one alone, which skips the rest. */
|
|
128
|
+
const skipping = /\.(?:skip|todo|only|skipIf|runIf)\b|\b(?:skipIf|runIf)\s*\(/;
|
|
129
|
+
/** A test's line that sets a timeout or a retry, which may loosen it. */
|
|
130
|
+
const loosening = /\b(?:timeout|testTimeout|hookTimeout|retry|retries)\b\s*[:=]/;
|
|
131
|
+
/** A line that ends a call with a number, as `it(name, run, 60_000)` or `}, 60_000);` give a timeout. */
|
|
132
|
+
const positional = /,\s*(\d[\d_]*)(\s*\)[\s;,]*)$/;
|
|
133
|
+
/** A line that `.each(` starts, whose cases a patch can't count. */
|
|
134
|
+
const tableCall = /\.each\s*[(`]/;
|
|
135
|
+
/** A line shaped like a table's case: a string, tuple or object row, a bare number, or a spread. */
|
|
136
|
+
const tableCase = /^\s*(?:[[{].*[\]}]|(['"`]).*\1|-?\d[\d_.]*|\.\.\.[\w$.]+),?\s*$/;
|
|
137
|
+
/** An import's first or last line, which may bring a table's cases in. */
|
|
138
|
+
const importLine = /^\s*(?:import\b|\}\s*from\b)/;
|
|
139
|
+
const fixtures = /\/fixtures\//;
|
|
140
|
+
/**
|
|
141
|
+
* Whether a path is production source, as the review skill counts a large change: one the settings'
|
|
142
|
+
* `productionSource` names, a package's or an app's `src/` unless a repo says otherwise, without its
|
|
143
|
+
* tests and fixtures.
|
|
144
|
+
*/
|
|
145
|
+
const isProductionSource = (path, settings = defaults) => matchesAny(settings.productionSource)(path) && !isTestOrFixture(path, settings);
|
|
146
|
+
/** Whether a path is test code or a fixture, which production source leaves out. */
|
|
147
|
+
const isTestOrFixture = (path, settings) => isTestCode(path, settings) || fixtures.test(path);
|
|
148
|
+
/** Whether a change renames test code or a fixture into production source, which counts whole. */
|
|
149
|
+
const movedIntoProduction = (change, settings) => change.previous_filename !== void 0 && isTestOrFixture(change.previous_filename, settings) && isProductionSource(change.filename, settings);
|
|
150
|
+
/**
|
|
151
|
+
* A change's net lines of production source: lines added less lines deleted, and a file moved into
|
|
152
|
+
* it from test code or a fixture whole, as `lines` counts it at the head, since its lines are new
|
|
153
|
+
* production code for review.
|
|
154
|
+
*/
|
|
155
|
+
const netProductionLines = (changes, lines, settings = defaults) => changes.filter((change) => isProductionSource(change.filename, settings)).reduce((sum, change) => sum + (movedIntoProduction(change, settings) ? lines(change.filename) : change.additions - change.deletions), 0);
|
|
156
|
+
/** A bash glob as a regular expression, where `*` also matches `/`, as `[[ $file == $glob ]]` does. */
|
|
157
|
+
const globPattern = (glob) => new RegExp(`^${glob.replace(/[\\^$.|+()[\]{}*?]/g, "\\$&").replaceAll("\\*", ".*").replaceAll("\\?", ".")}$`);
|
|
158
|
+
/**
|
|
159
|
+
* The tier `review-paths.txt`'s list puts a path at, the highest of the lines that match it, or
|
|
160
|
+
* `undefined` when none does or a `!` line takes it off. A `[high]` or `[medium]` line puts the
|
|
161
|
+
* globs under it at that tier, and a glob before the first is at high, so a list without them
|
|
162
|
+
* fails closed.
|
|
163
|
+
*/
|
|
164
|
+
const tierOf = (list) => {
|
|
165
|
+
const included = [];
|
|
166
|
+
const excluded = [];
|
|
167
|
+
let tier = "high";
|
|
168
|
+
for (const line of list.split("\n").map((text) => text.trim())) {
|
|
169
|
+
if (line === "" || line.startsWith("#")) continue;
|
|
170
|
+
const section = /^\[(.*)\]$/.exec(line)?.[1];
|
|
171
|
+
if (section !== void 0) {
|
|
172
|
+
if (!isTier(section) || section === "low") throw new Error(`review-paths.txt's "${line}" names no tier: use [medium] or [high]`);
|
|
173
|
+
tier = section;
|
|
174
|
+
} else if (line.startsWith("!")) excluded.push(globPattern(line.slice(1)));
|
|
175
|
+
else included.push({
|
|
176
|
+
pattern: globPattern(line),
|
|
177
|
+
tier
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return (path) => excluded.some((pattern) => pattern.test(path)) ? void 0 : highest(included.flatMap((each) => each.pattern.test(path) ? [each.tier] : []));
|
|
181
|
+
};
|
|
182
|
+
/** Whether `review-paths.txt`'s list covers a path, at any tier. */
|
|
183
|
+
const coveredBy = (list) => {
|
|
184
|
+
const tierAt = tierOf(list);
|
|
185
|
+
return (path) => tierAt(path) !== void 0;
|
|
186
|
+
};
|
|
187
|
+
/** Each list of globs' matcher, built once. */
|
|
188
|
+
const matchers = /* @__PURE__ */ new WeakMap();
|
|
189
|
+
/** Whether a path matches one of `globs`, in `review-paths.txt`'s form. */
|
|
190
|
+
const matchesAny = (globs) => {
|
|
191
|
+
const known = matchers.get(globs);
|
|
192
|
+
if (known !== void 0) return known;
|
|
193
|
+
const matches = coveredBy(globs.join("\n"));
|
|
194
|
+
matchers.set(globs, matches);
|
|
195
|
+
return matches;
|
|
196
|
+
};
|
|
197
|
+
/** Whether a path is test code, as the settings' `testCode` lists it. */
|
|
198
|
+
const isTestCode = (path, settings = defaults) => testCodeOf(settings)(path);
|
|
199
|
+
/** The settings' test code, `testCode` and `testHelpers` both, since every helper is test code. */
|
|
200
|
+
const testCodeOf = (settings) => {
|
|
201
|
+
const known = matchers.get(settings);
|
|
202
|
+
if (known !== void 0) return known;
|
|
203
|
+
const matches = coveredBy([...settings.testCode, ...settings.testHelpers].join("\n"));
|
|
204
|
+
matchers.set(settings, matches);
|
|
205
|
+
return matches;
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* The changed files a list matches, with their lines. A file renamed away from one counts its lines
|
|
209
|
+
* there, once, so a move doesn't drop its edits.
|
|
210
|
+
*/
|
|
211
|
+
const filesIn = (changes, matches) => changes.flatMap((change) => {
|
|
212
|
+
const lines = change.additions + change.deletions;
|
|
213
|
+
if (matches(change.filename)) return [{
|
|
214
|
+
file: change.filename,
|
|
215
|
+
lines
|
|
216
|
+
}];
|
|
217
|
+
return change.previous_filename !== void 0 && matches(change.previous_filename) ? [{
|
|
218
|
+
file: change.previous_filename,
|
|
219
|
+
lines
|
|
220
|
+
}] : [];
|
|
221
|
+
});
|
|
222
|
+
/**
|
|
223
|
+
* The test code a change touches, as `filesIn` reads it, with its changed lines: a test file moved
|
|
224
|
+
* with no edits is still one, at 0 lines, so it still needs a tests reviewer or a recorded break.
|
|
225
|
+
*/
|
|
226
|
+
const testChangesOf = (changes, settings) => {
|
|
227
|
+
const files = filesIn(changes, (path) => isTestCode(path, settings));
|
|
228
|
+
return {
|
|
229
|
+
files,
|
|
230
|
+
lines: files.reduce((sum, file) => sum + file.lines, 0)
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
/** Generated files a pull request's size leaves out, though the always-review list may name them. */
|
|
234
|
+
const uncounted = ["pnpm-lock.yaml"];
|
|
235
|
+
/** A pull request's changed lines, additions plus deletions, as the tier limits count them. */
|
|
236
|
+
const changedLines = (changes) => changes.filter((change) => !uncounted.includes(change.filename)).reduce((sum, change) => sum + change.additions + change.deletions, 0);
|
|
237
|
+
/** A patch's hunks, from its first `@@` line, past any header a git diff prints. */
|
|
238
|
+
const hunksOf = (patch) => {
|
|
239
|
+
const hunks = [];
|
|
240
|
+
for (const line of patch.replace(/\r/g, "").split("\n")) {
|
|
241
|
+
if (line.startsWith("@@")) hunks.push({
|
|
242
|
+
header: line,
|
|
243
|
+
kept: [],
|
|
244
|
+
added: [],
|
|
245
|
+
removed: []
|
|
246
|
+
});
|
|
247
|
+
const hunk = hunks.at(-1);
|
|
248
|
+
if (hunk === void 0 || line.startsWith("@@")) continue;
|
|
249
|
+
if (line.startsWith("+")) hunk.added.push(line.slice(1));
|
|
250
|
+
else if (line.startsWith("-")) hunk.removed.push(line.slice(1));
|
|
251
|
+
else hunk.kept.push(line.slice(1));
|
|
252
|
+
}
|
|
253
|
+
return hunks;
|
|
254
|
+
};
|
|
255
|
+
/** A number as a test writes it, `60_000` among them. */
|
|
256
|
+
const numberOf = (digits) => Number(digits.replaceAll("_", ""));
|
|
257
|
+
/**
|
|
258
|
+
* The added lines that raise a positional timeout, the number a call to `it`, `test` or `describe`
|
|
259
|
+
* ends with: a removed line, the same but for its number, held a lower one or none.
|
|
260
|
+
*/
|
|
261
|
+
const raisedTimeouts = (hunk) => hunk.added.filter((line) => {
|
|
262
|
+
const now = positional.exec(line);
|
|
263
|
+
if (now === null) return false;
|
|
264
|
+
const call = line.slice(0, now.index).trim();
|
|
265
|
+
const after = numberOf(now[1] ?? "0");
|
|
266
|
+
return hunk.removed.some((before) => {
|
|
267
|
+
const was = positional.exec(before);
|
|
268
|
+
return was === null ? before.trim() === `${call}${now[2] ?? ""}`.trim() : before.slice(0, was.index).trim() === call && numberOf(was[1] ?? "0") < after;
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
/**
|
|
272
|
+
* The removed lines that may drop a test's case, a table's case, an `.each(` line or an import, with
|
|
273
|
+
* no identical line added back anywhere in the file, as a move within it adds one.
|
|
274
|
+
*/
|
|
275
|
+
const lostCases = (hunks) => {
|
|
276
|
+
const added = new Set(hunks.flatMap((hunk) => hunk.added).map((line) => line.trim()));
|
|
277
|
+
return hunks.flatMap((hunk) => hunk.removed).filter((line) => [
|
|
278
|
+
tableCase,
|
|
279
|
+
tableCall,
|
|
280
|
+
importLine
|
|
281
|
+
].some((pattern) => pattern.test(line)) && !added.has(line.trim()));
|
|
282
|
+
};
|
|
283
|
+
/** At most `count` of a list's items, then how many more. */
|
|
284
|
+
const some = (items, count = 3) => items.length <= count ? items.join(", ") : `${items.slice(0, count).join(", ")} and ${items.length - count} more`;
|
|
285
|
+
/**
|
|
286
|
+
* Why a pull request's tests put it at medium or above, whatever its size, as merge-risk.md's
|
|
287
|
+
* "Weakening a test" and "Tests" say: it deletes a test file, moves one out of test code, or takes
|
|
288
|
+
* away more test rows than it adds; it skips one, with `.skip`, `.todo`, `.only`, `skipIf` or
|
|
289
|
+
* `runIf`; it sets a timeout or a retry, which may loosen one; it adds a test file; or it changes a
|
|
290
|
+
* shared test helper, as `testHelpers` lists them. A test file whose patch GitHub doesn't show is a
|
|
291
|
+
* risk too, since its edits can't be read. Each reason names its file.
|
|
292
|
+
*/
|
|
293
|
+
const testRisks = (changes, settings = defaults) => riskyTests(changes, settings).map((risk) => risk.why);
|
|
294
|
+
/** `testRisks`' reasons, each with the file it's about, whose commits a session's freshness covers. */
|
|
295
|
+
const riskyTests = (changes, settings) => {
|
|
296
|
+
const isTest = (path) => isTestCode(path, settings);
|
|
297
|
+
const isHelper = matchesAny(settings.testHelpers);
|
|
298
|
+
const risks = [];
|
|
299
|
+
const rowFiles = [];
|
|
300
|
+
let rowsAdded = 0;
|
|
301
|
+
let rowsRemoved = 0;
|
|
302
|
+
for (const change of changes) {
|
|
303
|
+
const from = change.previous_filename;
|
|
304
|
+
const file = change.filename;
|
|
305
|
+
if (!isTest(file) && (from === void 0 || !isTest(from))) continue;
|
|
306
|
+
const risk = (why) => risks.push({
|
|
307
|
+
files: [file],
|
|
308
|
+
why
|
|
309
|
+
});
|
|
310
|
+
if (change.status === "removed") risk(`it deletes the test file ${file}`);
|
|
311
|
+
else if (!isTest(file)) risk(`it moves ${from} out of test code, to ${file}`);
|
|
312
|
+
else if (change.status === "added" || from !== void 0 && !isTest(from)) risk(`it adds the test file ${file}`);
|
|
313
|
+
if (change.status !== "added" && change.status !== "removed" && (isHelper(file) || from !== void 0 && isHelper(from))) risk(`it changes the shared test helper ${from ?? file}`);
|
|
314
|
+
if (change.patch === void 0) {
|
|
315
|
+
if (change.status !== "removed") risk(`its diff shows no patch of ${file}, so its edits can't be read`);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const hunks = hunksOf(change.patch);
|
|
319
|
+
const added = hunks.flatMap((hunk) => hunk.added);
|
|
320
|
+
const removed = hunks.flatMap((hunk) => hunk.removed);
|
|
321
|
+
const skips = added.filter((line) => skipping.test(line));
|
|
322
|
+
const loosens = [...added.filter((line) => loosening.test(line)), ...hunks.flatMap(raisedTimeouts)];
|
|
323
|
+
if (skips.length > 0) risk(`it skips a test in ${file}: "${skips[0]?.trim()}"`);
|
|
324
|
+
if (loosens.length > 0) risk(`it sets a timeout or a retry in ${file}: "${loosens[0]?.trim()}"`);
|
|
325
|
+
const lost = change.status === "added" ? [] : lostCases(hunks);
|
|
326
|
+
if (lost.length > 0) risk(`it may drop a case in ${file}: "${lost[0]?.trim()}"`);
|
|
327
|
+
rowsAdded += added.filter((line) => testRow.test(line)).length;
|
|
328
|
+
rowsRemoved += removed.filter((line) => testRow.test(line)).length;
|
|
329
|
+
if (removed.some((line) => testRow.test(line))) rowFiles.push(file);
|
|
330
|
+
}
|
|
331
|
+
if (rowsRemoved > rowsAdded) risks.push({
|
|
332
|
+
files: rowFiles,
|
|
333
|
+
why: `it takes away ${rowsRemoved - rowsAdded} more test rows than it adds`
|
|
334
|
+
});
|
|
335
|
+
return risks;
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
* The lowest tier a pull request may name, from its size, the base's `review-paths.txt` and its
|
|
339
|
+
* tests: high past `highTierLines` changed lines or on the list at high, medium past `lowTierLines`,
|
|
340
|
+
* on the list at medium or with any of `testRisks`, and low otherwise. With no list, every
|
|
341
|
+
* production file is at high. A large change, `net` lines of production source at `largeChange` or
|
|
342
|
+
* more, is at high too, since its code quality reviewer joins a full session.
|
|
343
|
+
*/
|
|
344
|
+
const floorOf = (changes, list, net = 0, settings = defaults) => {
|
|
345
|
+
const { lowTierLines, highTierLines } = settings;
|
|
346
|
+
const isProduction = (path) => isProductionSource(path, settings);
|
|
347
|
+
const tierAt = list === void 0 ? (path) => isProduction(path) ? "high" : void 0 : tierOf(list);
|
|
348
|
+
const lines = changedLines(changes);
|
|
349
|
+
const counted = changes.flatMap((change) => uncounted.includes(change.filename) ? [] : [change.filename]);
|
|
350
|
+
const reasons = [];
|
|
351
|
+
if (net >= 1e3) reasons.push({
|
|
352
|
+
tier: "high",
|
|
353
|
+
why: `${net} net lines of production source, ${largeChange} or more, which a code quality reviewer reads`,
|
|
354
|
+
files: counted.filter(isProduction)
|
|
355
|
+
});
|
|
356
|
+
if (lines > highTierLines) reasons.push({
|
|
357
|
+
tier: "high",
|
|
358
|
+
why: `${lines} changed lines, over ${highTierLines}`,
|
|
359
|
+
files: counted
|
|
360
|
+
});
|
|
361
|
+
else if (lines > lowTierLines) reasons.push({
|
|
362
|
+
tier: "medium",
|
|
363
|
+
why: `${lines} changed lines, over ${lowTierLines}`,
|
|
364
|
+
files: counted
|
|
365
|
+
});
|
|
366
|
+
for (const tier of ["high", "medium"]) {
|
|
367
|
+
const files = changes.flatMap((change) => highest([tierAt(change.filename), change.previous_filename === void 0 ? void 0 : tierAt(change.previous_filename)]) === tier ? [change.previous_filename ?? change.filename] : []);
|
|
368
|
+
if (files.length > 0) reasons.push({
|
|
369
|
+
tier,
|
|
370
|
+
why: `${some(files)} on the base's review-paths.txt at ${tier}${list === void 0 ? ", which the base lacks, so every production file is" : ""}`,
|
|
371
|
+
files: changes.flatMap((change) => files.includes(change.previous_filename ?? change.filename) ? [change.filename] : [])
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
for (const risk of riskyTests(changes, settings)) reasons.push({
|
|
375
|
+
tier: "medium",
|
|
376
|
+
...risk
|
|
377
|
+
});
|
|
378
|
+
const tier = highest(reasons.map((reason) => reason.tier)) ?? "low";
|
|
379
|
+
return {
|
|
380
|
+
tier,
|
|
381
|
+
why: tier === "low" ? [`${lines} changed lines, ${lowTierLines} or fewer, and nothing on the always-review list`] : reasons.flatMap((reason) => reason.tier === tier ? [reason.why] : []),
|
|
382
|
+
files: [...new Set(reasons.flatMap((reason) => reason.files))]
|
|
383
|
+
};
|
|
384
|
+
};
|
|
385
|
+
/** The tier a body's `Merge risk:` line names, or `undefined` without one. */
|
|
386
|
+
const declaredTier = (body) => {
|
|
387
|
+
const named = mergeRiskLine.exec(body)?.[1]?.toLowerCase();
|
|
388
|
+
return named !== void 0 && isTier(named) ? named : void 0;
|
|
389
|
+
};
|
|
390
|
+
/** Whether `line` names `name` whole, not as part of a longer path or name. */
|
|
391
|
+
const names = (line, name) => new RegExp(`(?:^|[^\\w./-])${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\\w.-])`).test(line);
|
|
392
|
+
/**
|
|
393
|
+
* Whether `path`, from the repo's root, is a test's alone: `.test.` in its name, under a `testing/`
|
|
394
|
+
* folder, or a package's `testing.ts` entry, which only the workspace's tests import, and none of
|
|
395
|
+
* the settings' `benchRuns`, the test helpers a bench case runs all the same. No bench case runs
|
|
396
|
+
* one, so no pull request owes a case's reading for it, `benchedCases`.
|
|
397
|
+
*/
|
|
398
|
+
const testsOnly = (path, settings) => !settings.benchRuns.includes(path) && ((path.split("/").at(-1) ?? "").includes(".test.") || path.split("/").includes("testing") || /^packages\/[^/]+\/src\/testing\.ts$/u.test(path));
|
|
399
|
+
/**
|
|
400
|
+
* The bench cases the settings' `benchedPaths` list ties a pull request's changes to, in the list's
|
|
401
|
+
* order, and the changed files that tie them: each case with a line whose glob matches a changed
|
|
402
|
+
* file, or the path a file was renamed from, and every case the list names when a `*` line
|
|
403
|
+
* matches. A test's path alone, `testsOnly`, ties none, since no case runs it. A line that isn't
|
|
404
|
+
* `<case> <glob>` throws.
|
|
405
|
+
*/
|
|
406
|
+
const benchedCases = (list, changes, settings) => {
|
|
407
|
+
const rows = list.split("\n").map((text) => text.trim()).filter((line) => line !== "" && !line.startsWith("#")).map((line) => {
|
|
408
|
+
const [name, glob, ...rest] = line.split(/\s+/);
|
|
409
|
+
if (name === void 0 || glob === void 0 || rest.length > 0) throw new Error(`The bench list's "${line}" isn't "<case> <glob>"`);
|
|
410
|
+
return {
|
|
411
|
+
name,
|
|
412
|
+
pattern: globPattern(glob)
|
|
413
|
+
};
|
|
414
|
+
});
|
|
415
|
+
const paths = changes.flatMap((change) => change.previous_filename === void 0 ? [change.filename] : [change.filename, change.previous_filename]).filter((path) => !testsOnly(path, settings));
|
|
416
|
+
const hit = rows.filter((row) => paths.some((path) => row.pattern.test(path)));
|
|
417
|
+
const cases = (hit.some((row) => row.name === "*") ? rows : hit).map((row) => row.name).filter((name) => name !== "*");
|
|
418
|
+
return {
|
|
419
|
+
cases: [...new Set(cases)],
|
|
420
|
+
files: paths.filter((path) => hit.some((row) => row.pattern.test(path)))
|
|
421
|
+
};
|
|
422
|
+
};
|
|
423
|
+
/** A GitHub Actions run's link, whole. */
|
|
424
|
+
const runLink = /^https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/actions\/runs\/\d+$/i;
|
|
425
|
+
/**
|
|
426
|
+
* The URL a token of a line names: a Markdown link's whole destination, or the token without the
|
|
427
|
+
* brackets and prose punctuation around it, so a run link inside another URL is never its own.
|
|
428
|
+
*/
|
|
429
|
+
const linkOf = (token) => {
|
|
430
|
+
const at = token.indexOf("](");
|
|
431
|
+
if (at >= 0) return token.slice(at + 2).replace(/[.,;:!?]+$/, "").replace(/\)$/, "");
|
|
432
|
+
return token.replace(/^[<([]+/, "").replace(/[>)\].,;:!?]+$/, "");
|
|
433
|
+
};
|
|
434
|
+
/**
|
|
435
|
+
* Whether a line holds a bench reading: the word local, for a local pass, or a GitHub Actions run's
|
|
436
|
+
* link as a token of its own, split at whitespace and table bars.
|
|
437
|
+
*/
|
|
438
|
+
const holdsReading = (line) => /\blocal\b/i.test(line) || line.split(/[\s|]+/).some((token) => runLink.test(linkOf(token)));
|
|
439
|
+
/**
|
|
440
|
+
* Where the body's "Bench and cost" fold opens: a `<details>` tag whose summary reads so, in bold
|
|
441
|
+
* or not. A tag's name ends at `>` or HTML's own whitespace, so `<details-fake>` or a non-breaking
|
|
442
|
+
* space opens no fold.
|
|
443
|
+
*/
|
|
444
|
+
const benchFoldOpen = /<details(?:[\t\n\f\r ][^>]*)?>\s*<summary>\s*(?:<(?:b|strong)>\s*)?Bench and cost\s*(?:<\/(?:b|strong)>\s*)?<\/summary>/i;
|
|
445
|
+
/** The start of a `<details>` or `</details>` tag, its name ended as `benchFoldOpen`'s is. */
|
|
446
|
+
const detailsName = /<(\/?)details(?=[\t\n\f\r >])/iy;
|
|
447
|
+
/** A fenced code block's opening fence, at a line's start: three or more of one fence character. */
|
|
448
|
+
const fenceOpen = / {0,3}(`{3,}|~{3,})/y;
|
|
449
|
+
/** Where the line holding `at` ends, past its `\n`, or the body's end. */
|
|
450
|
+
const lineEnd = (body, at) => {
|
|
451
|
+
const end = body.indexOf("\n", at);
|
|
452
|
+
return end === -1 ? body.length : end + 1;
|
|
453
|
+
};
|
|
454
|
+
/** Past the code block whose opening `fence` ends at `from`: past its closing fence's line, or the body's end. */
|
|
455
|
+
const pastFence = (body, from, fence) => {
|
|
456
|
+
const closing = new RegExp(` {0,3}${fence[0]}{${fence.length},}[\\t ]*\\r?(?:\\n|$)`, "y");
|
|
457
|
+
for (let line = lineEnd(body, from); line < body.length; line = lineEnd(body, line)) {
|
|
458
|
+
closing.lastIndex = line;
|
|
459
|
+
if (closing.test(body)) return closing.lastIndex;
|
|
460
|
+
}
|
|
461
|
+
return body.length;
|
|
462
|
+
};
|
|
463
|
+
/** Past the code span whose opening run of `ticks` backticks ends at `from`, or -1 when no run as long closes it. */
|
|
464
|
+
const pastSpan = (body, from, ticks) => {
|
|
465
|
+
const run = "`".repeat(ticks);
|
|
466
|
+
for (let at = body.indexOf(run, from); at !== -1; at = body.indexOf(run, at)) {
|
|
467
|
+
const end = runEnd(body, at);
|
|
468
|
+
if (end - at === ticks) return end;
|
|
469
|
+
at = end;
|
|
470
|
+
}
|
|
471
|
+
return -1;
|
|
472
|
+
};
|
|
473
|
+
/** Where the run of backticks at `at` ends. */
|
|
474
|
+
const runEnd = (body, at) => {
|
|
475
|
+
let end = at;
|
|
476
|
+
while (body[end] === "`") end += 1;
|
|
477
|
+
return end;
|
|
478
|
+
};
|
|
479
|
+
/**
|
|
480
|
+
* The body's "Bench and cost" fold: what its `<details>` element holds after its summary, to the
|
|
481
|
+
* `</details>` that closes it, past every fold nested inside it, such as pr-fill's test files over
|
|
482
|
+
* 10 s in its cost section (#2368). A tag inside an HTML comment, a fenced code block or a code
|
|
483
|
+
* span is text, as GitHub renders it, and opens or closes nothing. Nothing when the body has no
|
|
484
|
+
* such fold, or it never closes.
|
|
485
|
+
*/
|
|
486
|
+
const benchFold = (body) => {
|
|
487
|
+
const opened = benchFoldOpen.exec(body);
|
|
488
|
+
if (opened === null) return "";
|
|
489
|
+
const start = opened.index + opened[0].length;
|
|
490
|
+
const unclosed = /* @__PURE__ */ new Set();
|
|
491
|
+
let depth = 1;
|
|
492
|
+
let at = start;
|
|
493
|
+
while (at < body.length) {
|
|
494
|
+
fenceOpen.lastIndex = at;
|
|
495
|
+
const fence = body[at - 1] === "\n" ? fenceOpen.exec(body) : null;
|
|
496
|
+
if (fence !== null) {
|
|
497
|
+
at = pastFence(body, fenceOpen.lastIndex, fence[1] ?? "```");
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
if (body.startsWith("<!--", at)) {
|
|
501
|
+
const close = body.indexOf("-->", at + 4);
|
|
502
|
+
if (close === -1) return "";
|
|
503
|
+
at = close + 3;
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
if (body[at] === "`") {
|
|
507
|
+
const end = runEnd(body, at);
|
|
508
|
+
const ticks = end - at;
|
|
509
|
+
const past = unclosed.has(ticks) ? -1 : pastSpan(body, end, ticks);
|
|
510
|
+
if (past === -1) unclosed.add(ticks);
|
|
511
|
+
at = past === -1 ? end : past;
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
detailsName.lastIndex = at;
|
|
515
|
+
const tag = detailsName.exec(body);
|
|
516
|
+
if (tag === null) {
|
|
517
|
+
at += 1;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
const close = body.indexOf(">", detailsName.lastIndex);
|
|
521
|
+
if (close === -1) return "";
|
|
522
|
+
depth += tag[1] === "/" ? -1 : 1;
|
|
523
|
+
if (depth === 0) return body.slice(start, at);
|
|
524
|
+
at = close + 1;
|
|
525
|
+
}
|
|
526
|
+
return "";
|
|
527
|
+
};
|
|
528
|
+
/** ci-bench's workflow, as GitHub names a run's. */
|
|
529
|
+
const benchWorkflow = ".github/workflows/ci-bench.yml";
|
|
530
|
+
/** A line's claim that a case's two bundles are one code, and their digest. */
|
|
531
|
+
const unchangedClaim = /\bbundle unchanged at ([0-9a-f]{64})\b/u;
|
|
532
|
+
/** The runs a line links, by ID. */
|
|
533
|
+
const runsOf = (line) => line.split(/[\s|]+/).map(linkOf).filter((link) => runLink.test(link)).map((link) => Number(link.split("/").at(-1)));
|
|
534
|
+
/**
|
|
535
|
+
* The job ci-bench's `pr` run writes when the bundles of pull request `pull`'s head, `head`, and of
|
|
536
|
+
* its merge base share `digest`: a run's proof that it benched nothing because nothing the bench
|
|
537
|
+
* Worker runs changed. It names the pull request, not the base: the base is the merge base with
|
|
538
|
+
* main when the run starts, which a later merge to main of one of the head's commits moves.
|
|
539
|
+
*/
|
|
540
|
+
const unchangedJob = (digest, pull, head) => `Bundle unchanged at ${digest} for #${pull} at ${head}`;
|
|
541
|
+
/**
|
|
542
|
+
* Whether a run `line` links proves its "bundle unchanged" claim: ci-bench's, on main, with a
|
|
543
|
+
* passed `unchangedJob` of the claim's digest, the pull request's number and its head, so a run of
|
|
544
|
+
* another pull request, another head, another branch or another workflow, or a line with no run,
|
|
545
|
+
* proves nothing. `lookup` reads each run once. A record with no `benchRun`, a local one, can't
|
|
546
|
+
* ask GitHub, and leaves it to CI's.
|
|
547
|
+
*/
|
|
548
|
+
const provenUnchanged = (line, record, lookup) => {
|
|
549
|
+
const digest = unchangedClaim.exec(line)?.[1];
|
|
550
|
+
if (digest === void 0 || record.benchRun === void 0) return true;
|
|
551
|
+
if (record.pull === void 0) return false;
|
|
552
|
+
const job = unchangedJob(digest, record.pull, record.head);
|
|
553
|
+
return runsOf(line).some((id) => {
|
|
554
|
+
const run = lookup(id);
|
|
555
|
+
return run !== void 0 && run.path === ".github/workflows/ci-bench.yml" && run.branch === "main" && run.jobs.some((each) => each.name === job && each.conclusion === "success");
|
|
556
|
+
});
|
|
557
|
+
};
|
|
558
|
+
/**
|
|
559
|
+
* The bench cases the base's `benched-paths.txt` ties the changes to, and why the body's "Bench and
|
|
560
|
+
* cost" fold doesn't hold a reading for each: a line there that names the case whole and holds a
|
|
561
|
+
* run's link or the word local, or a "bundle unchanged" line a run proves, `provenUnchanged`. A
|
|
562
|
+
* base with no list ties none.
|
|
563
|
+
*/
|
|
564
|
+
const benchReadings = (body, record, settings) => {
|
|
565
|
+
if (record.benched === void 0) return {
|
|
566
|
+
cases: [],
|
|
567
|
+
errors: []
|
|
568
|
+
};
|
|
569
|
+
const { cases, files } = benchedCases(record.benched, record.changes, settings);
|
|
570
|
+
const lines = benchFold(body).split("\n");
|
|
571
|
+
const claims = lines.filter((line) => unchangedClaim.test(line));
|
|
572
|
+
const linked = new Set(claims.flatMap(runsOf));
|
|
573
|
+
if (linked.size > 30) return {
|
|
574
|
+
cases,
|
|
575
|
+
errors: [`Its body's "bundle unchanged" lines link ${linked.size} runs, and review-record reads at most 30: link the one run that benched the set, ci-bench's \`pr\` dispatch.`]
|
|
576
|
+
};
|
|
577
|
+
const runs = /* @__PURE__ */ new Map();
|
|
578
|
+
const lookup = (id) => {
|
|
579
|
+
if (!runs.has(id)) runs.set(id, record.benchRun?.(id));
|
|
580
|
+
return runs.get(id);
|
|
581
|
+
};
|
|
582
|
+
const unproven = claims.filter((line) => !provenUnchanged(line, record, lookup));
|
|
583
|
+
const readings = lines.filter((line) => (unchangedClaim.test(line) || holdsReading(line)) && !unproven.includes(line));
|
|
584
|
+
const missing = cases.filter((name) => !readings.some((line) => names(line, name)));
|
|
585
|
+
return {
|
|
586
|
+
cases,
|
|
587
|
+
errors: [...unproven.map((line) => `Its body's "${line.trim()}" says a bundle is unchanged, and no run it links is ci-bench's on main with a passed "${unchangedJob(unchangedClaim.exec(line)?.[1] ?? "", record.pull ?? 0, record.head)}" job: link the ci-bench run of this pull request that wrote it, \`pr\`'s.`), ...missing.length === 0 ? [] : [`It changes benched paths, ${some(files)}, so its body's "Bench and cost" fold holds a reading for each case ${settings.benchedPaths ?? "the bench list"} ties them to, and holds none for ${some(missing, 5)}. Add a line for each that names the case with its ci-bench run's link, or labelled local for a local pass, as .agents/repo/review.md says.`]]
|
|
588
|
+
};
|
|
589
|
+
};
|
|
590
|
+
/**
|
|
591
|
+
* The test files changed at the head that no `Break:` line of the body names: by its full path, or by
|
|
592
|
+
* its name alone when no other changed test file has that name.
|
|
593
|
+
*/
|
|
594
|
+
const unbroken = (body, changes, settings) => {
|
|
595
|
+
const isTest = (path) => isTestCode(path, settings);
|
|
596
|
+
const breaks = body.match(breakLine) ?? [];
|
|
597
|
+
const files = filesIn(changes, isTest).map((each) => each.file);
|
|
598
|
+
const nameOf = (file) => file.split("/").at(-1) ?? file;
|
|
599
|
+
const shared = (file) => files.some((other) => other !== file && nameOf(other) === nameOf(file));
|
|
600
|
+
return filesIn(changes.filter((change) => change.status !== "removed"), isTest).map((each) => each.file).filter((file) => !breaks.some((line) => names(line, file) || !shared(file) && names(line, nameOf(file))));
|
|
601
|
+
};
|
|
602
|
+
/** The entries of a session's line that starts with `label`, split on `; `, or none without one. */
|
|
603
|
+
const entriesOf = (body, label) => {
|
|
604
|
+
const line = body.split("\n").find((text) => text.startsWith(`${label}:`));
|
|
605
|
+
if (line === void 0) return void 0;
|
|
606
|
+
return line.slice(label.length + 1).split("; ").map((entry) => entry.trim()).filter((entry) => entry !== "");
|
|
607
|
+
};
|
|
608
|
+
/** An entry's type, when it starts with one of the review types and a colon, then says something. */
|
|
609
|
+
const typeOf = (entry, { reviewTypes }) => reviewTypes.find((type) => entry.toLowerCase().startsWith(`${type}:`) && entry.slice(type.length + 1).trim() !== "");
|
|
610
|
+
/**
|
|
611
|
+
* The type an entry names that this repo doesn't accept, when it starts `<words>: <text>` with words that
|
|
612
|
+
* aren't one of `reviewTypes`, such as a type the shared review skill added after this list.
|
|
613
|
+
*/
|
|
614
|
+
const unknownTypeOf = (entry, { reviewTypes }) => {
|
|
615
|
+
const named = /^([a-z][a-z -]*):\s*\S/i.exec(entry)?.[1]?.trim().toLowerCase();
|
|
616
|
+
return named !== void 0 && !reviewTypes.includes(named) ? named : void 0;
|
|
617
|
+
};
|
|
618
|
+
/** Why an entry with no review type is refused, by the type it names when it names one. */
|
|
619
|
+
const refusal = (entry, label, form, settings) => {
|
|
620
|
+
const unknown = unknownTypeOf(entry, settings);
|
|
621
|
+
return unknown === void 0 ? void 0 : `the ${label} "${entry}" names the type "${unknown}", which isn't one this repo accepts: add it to reviewTypes in ${settingsPath}, or use one of ${settings.reviewTypes.join(", ")} as "${form}"`;
|
|
622
|
+
};
|
|
623
|
+
/** A session comment's types run and not run, and what's wrong with its lines. */
|
|
624
|
+
const readSession = (comment, covered, settings = defaults) => {
|
|
625
|
+
const body = comment.replace(/\r/g, "");
|
|
626
|
+
const head = body.split("\n")[0] ?? "";
|
|
627
|
+
const lines = body.split("\n");
|
|
628
|
+
const counts = countsOf(lines);
|
|
629
|
+
const counted = counts?.counted ?? 0;
|
|
630
|
+
const problems = [];
|
|
631
|
+
const rows = (counts && tableAt(lines, reviewerColumns, counts.end)?.rows) ?? [];
|
|
632
|
+
const reviewerEntries = rows.flatMap((row) => {
|
|
633
|
+
if (row.length === reviewerColumns.length) return [`${row[0] ?? ""}: ${row[2] ?? ""}`];
|
|
634
|
+
problems.push(`its reviewer row "| ${row.join(" | ")} |" has ${row.length} cells, where its table has ${reviewerColumns.length}: write a "|" inside a cell as "\\|"`);
|
|
635
|
+
return [];
|
|
636
|
+
});
|
|
637
|
+
if (rows.length === 0) problems.push(`it has no "| ${reviewerColumns.join(" | ")} |" table, with a row for each reviewer, next after its first table`);
|
|
638
|
+
else if (rows.length !== counted) problems.push(`its reviewer table has ${rows.length} row${rows.length === 1 ? "" : "s"}, where its first table counts ${counted} reviewers`);
|
|
639
|
+
const reviewers = reviewerEntries.flatMap((entry) => {
|
|
640
|
+
const type = typeOf(entry, settings);
|
|
641
|
+
if (type === void 0) {
|
|
642
|
+
problems.push(refusal(entry, "reviewer", "<type>: <scope>", settings) ?? `the reviewer "${entry}" doesn't name one review type in its Reviewer cell and its scope in What it read: each type is a Codex run of its own`);
|
|
643
|
+
return [];
|
|
644
|
+
}
|
|
645
|
+
return [type];
|
|
646
|
+
});
|
|
647
|
+
const notRun = (entriesOf(body, "Not run") ?? []).flatMap((entry) => {
|
|
648
|
+
const type = typeOf(entry, settings);
|
|
649
|
+
if (type === void 0) {
|
|
650
|
+
problems.push(refusal(entry, "\"Not run:\" entry", "<type>: <why>", settings) ?? `the "Not run:" entry "${entry}" doesn't start "<type>: <why>" with a review type`);
|
|
651
|
+
return [];
|
|
652
|
+
}
|
|
653
|
+
return [type];
|
|
654
|
+
});
|
|
655
|
+
const missing = covered ? settings.consideredTypes.filter((type) => !reviewers.includes(type) && !notRun.includes(type)) : [];
|
|
656
|
+
if (missing.length > 0) problems.push(`it names neither as run nor, on its "Not run:" line with why, ${missing.join(", ")}`);
|
|
657
|
+
const throughs = body.split("\n").filter((line) => line.startsWith("Through:"));
|
|
658
|
+
const through = throughs.length === 1 ? throughLine.exec(throughs[0] ?? "")?.[1] : void 0;
|
|
659
|
+
if (throughs.length > 1) problems.push(`it has ${throughs.length} "Through:" lines, where one names its commit`);
|
|
660
|
+
else if (throughs.length === 1 && through === void 0) problems.push(`its "${throughs[0]}" line doesn't name one commit, as "Through: <sha>"`);
|
|
661
|
+
return {
|
|
662
|
+
head,
|
|
663
|
+
sha: through ?? sessionLine.exec(head)?.[1] ?? "",
|
|
664
|
+
counted,
|
|
665
|
+
rounds: counts?.rounds ?? 0,
|
|
666
|
+
reviewers,
|
|
667
|
+
notRun,
|
|
668
|
+
problems
|
|
669
|
+
};
|
|
670
|
+
};
|
|
671
|
+
/**
|
|
672
|
+
* Why the sessions don't cover the head: the newest session whose `Through:` commit is in the
|
|
673
|
+
* head's history counts, and none of the pull request's own commits after it may change one of its
|
|
674
|
+
* covered files, `covered`, by hand, or a file a review covers that the head no longer holds, as
|
|
675
|
+
* when main renames one after the edit. A session whose commit the checkout doesn't hold, newer
|
|
676
|
+
* than the one that counts, fails, since nothing shows what it reviewed. None in the head's history
|
|
677
|
+
* fails too.
|
|
678
|
+
*/
|
|
679
|
+
const unreviewed = (record, sessions, covered, matches) => {
|
|
680
|
+
const ways = `If those were finding fixes, CI fixes or merges from main and you read them, set the session's "Through:" line to the head, ${record.head}. Otherwise run review at the head and post a new session.`;
|
|
681
|
+
for (const session of sessions.toReversed()) {
|
|
682
|
+
const since = record.since(session.sha);
|
|
683
|
+
if (since === void 0) return [`The session "${session.head}": its "Through:" commit, ${session.sha}, isn't in the repo's history, as after a force push, so nothing shows what it reviewed or what came after it. ${ways}`];
|
|
684
|
+
if (!since.ancestor) continue;
|
|
685
|
+
const changed = since.commits.flatMap((commit) => {
|
|
686
|
+
const files = commit.files.filter((file) => matches(file) && (covered.includes(file) || !record.holds(file)));
|
|
687
|
+
return files.length === 0 ? [] : [{
|
|
688
|
+
commit,
|
|
689
|
+
files
|
|
690
|
+
}];
|
|
691
|
+
});
|
|
692
|
+
if (changed.length === 0) return [];
|
|
693
|
+
const files = [...new Set(changed.flatMap((each) => each.files))].join(" ");
|
|
694
|
+
const commits = changed.map((each) => `${each.commit.sha.slice(0, 9)} "${each.commit.subject}"`).join(", ");
|
|
695
|
+
return [`Files a review covers changed after ${session.sha}, the "Through:" commit of the newest session in the head's history, "${session.head}": ${files}, in ${commits}. ${ways}`];
|
|
696
|
+
}
|
|
697
|
+
return [`No session's "Through:" commit is in the head's history, ${sessions.map((session) => session.sha).join(", ")}, as after a force push. ${ways}`];
|
|
698
|
+
};
|
|
699
|
+
/** A record's net lines of production source and its tier floor, as CI and each subcommand read them. */
|
|
700
|
+
const recordFloor = (record) => {
|
|
701
|
+
const settings = record.settings ?? defaults;
|
|
702
|
+
const net = netProductionLines(record.changes, record.lines, settings);
|
|
703
|
+
return {
|
|
704
|
+
net,
|
|
705
|
+
floor: floorOf(record.changes, record.list, net, settings)
|
|
706
|
+
};
|
|
707
|
+
};
|
|
708
|
+
/** Whether the review is on record: what passed it, or each reason it isn't. */
|
|
709
|
+
const verdict = (record) => {
|
|
710
|
+
const settings = record.settings ?? defaults;
|
|
711
|
+
const body = record.body.replace(/\r/g, "");
|
|
712
|
+
const listed = record.changes.length;
|
|
713
|
+
if (record.changedFiles !== void 0 && (!(listed >= record.changedFiles) || listed >= 3e3)) return {
|
|
714
|
+
ok: false,
|
|
715
|
+
errors: [`GitHub lists ${listed} of the pull request's ${record.changedFiles} changed files, and lists at most ${githubFileCap}, so its floor can't be read from them. Split it into pull requests of fewer files.`]
|
|
716
|
+
};
|
|
717
|
+
const { net, floor } = recordFloor(record);
|
|
718
|
+
const because = floor.why.join("; ");
|
|
719
|
+
const tier = declaredTier(body);
|
|
720
|
+
if (tier === void 0) return {
|
|
721
|
+
ok: false,
|
|
722
|
+
errors: [`The body has no "Merge risk:" line. Its floor is ${floor.tier}, from ${because}. Add "Merge risk: ${floor.tier}", or higher as merge-risk.md's categories judge it, then rerun this job.`]
|
|
723
|
+
};
|
|
724
|
+
if (rank(tier) < rank(floor.tier)) return {
|
|
725
|
+
ok: false,
|
|
726
|
+
errors: [`The body's "Merge risk: ${tier}" is below its floor, ${floor.tier}, from ${because}. Raise it to "Merge risk: ${floor.tier}" or higher, and run the review that tier needs.`]
|
|
727
|
+
};
|
|
728
|
+
const atTier = `Merge risk: ${tier}, at or above its floor, ${floor.tier}`;
|
|
729
|
+
const { files: tests } = testChangesOf(record.changes, settings);
|
|
730
|
+
const missingBreaks = unbroken(body, record.changes, settings);
|
|
731
|
+
const bench = benchReadings(body, record, settings);
|
|
732
|
+
const benched = bench.cases.length > 0 ? `, with a bench reading for each of ${bench.cases.length} cases` : "";
|
|
733
|
+
const lowBreaks = missingBreaks.length > 0 ? [`This low-tier pull request changes test code with no break on record for ${some(missingBreaks)}. Add a "Break:" line to the body for each, naming its file, what you broke in the code and that the test failed, as test-audit's authoring gate asks.`] : [];
|
|
734
|
+
if (tier === "low" && record.outsider !== true) {
|
|
735
|
+
const errors = [...lowBreaks, ...bench.errors];
|
|
736
|
+
if (errors.length > 0) return {
|
|
737
|
+
ok: false,
|
|
738
|
+
errors
|
|
739
|
+
};
|
|
740
|
+
return {
|
|
741
|
+
ok: true,
|
|
742
|
+
why: `${atTier}, so it needs no session: ${because}${tests.length > 0 ? `, with a break on record for each of ${tests.length} test files` : ""}${benched}`
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
const matches = record.list === void 0 ? (path) => isProductionSource(path, settings) : coveredBy(record.list);
|
|
746
|
+
const covered = filesIn(record.changes, matches);
|
|
747
|
+
const comments = record.comments.map((comment) => comment.replace(/\r/g, ""));
|
|
748
|
+
const sessions = comments.filter(isSession).map((comment) => readSession(comment, tier === "high" && covered.length > 0, settings));
|
|
749
|
+
const errors = [...comments.filter((comment) => /^#* *Review:/i.test(comment) && !isSession(comment)).map((comment) => comment.split("\n")[0] ?? "").map((head) => `"${head}" starts like a session but isn't one: copy its first line and first table from the review skill, filled in, at high, with 1 to 4 reviewers.`), ...bench.errors];
|
|
750
|
+
if (sessions.length === 0) {
|
|
751
|
+
if (record.uncounted !== void 0 && record.uncounted.length > 0) errors.push(`No session counts${uncountedNote(record)}.`);
|
|
752
|
+
errors.push({
|
|
753
|
+
low: "No session on record, which a low pull request whose author may not push to the repo needs, from someone who may. Post the review skill's session comment from an account with push access, then rerun this job.",
|
|
754
|
+
medium: "No session on record, which a medium pull request needs. Post the review skill's session comment for one reviewer and one round, whose first line is \"## Review: <end>\" and whose first table counts 1 reviewer and 1 round at high, then rerun this job.",
|
|
755
|
+
high: "No session on record, which a high pull request needs. Post the review skill's session comment, whose first line is \"## Review: <end>\" and whose first table counts its reviewers and rounds at high, then rerun this job."
|
|
756
|
+
}[tier]);
|
|
757
|
+
return {
|
|
758
|
+
ok: false,
|
|
759
|
+
errors
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
for (const session of sessions) for (const problem of session.problems) errors.push(`The session "${session.head}": ${problem}.`);
|
|
763
|
+
const vouched = [.../* @__PURE__ */ new Set([...covered.map((file) => file.file), ...floor.files])];
|
|
764
|
+
const fresh = vouched.length > 0 ? vouched : record.changes.map((change) => change.filename);
|
|
765
|
+
errors.push(...unreviewed(record, sessions, fresh, (path) => matches(path) || fresh.includes(path)));
|
|
766
|
+
const testsReviewed = sessions.some((session) => session.reviewers.includes("tests"));
|
|
767
|
+
if (tier === "medium") {
|
|
768
|
+
for (const session of sessions.filter((each) => each.counted !== 1 || each.rounds !== 1)) errors.push(`The session "${session.head}": a medium pull request's session has 1 reviewer and 1 round, where it counts ${session.counted} and ${session.rounds}. Name "Merge risk: high" for the full session.`);
|
|
769
|
+
const risks = testRisks(record.changes, settings);
|
|
770
|
+
if (risks.length > 0 && !testsReviewed) errors.push(`Its tests put it at medium or above, since ${some(risks)}, so its reviewer is a tests one: give a session's reviewer table a "tests" row.`);
|
|
771
|
+
else if (missingBreaks.length > 0 && !testsReviewed) errors.push(`It changes test code with no tests reviewer and no break on record for ${some(missingBreaks)}. Make the session's reviewer a tests one, or add a "Break:" line to the body for each file.`);
|
|
772
|
+
}
|
|
773
|
+
if (tier === "low") errors.push(...lowBreaks);
|
|
774
|
+
if (tier === "high") {
|
|
775
|
+
if (net >= 1e3 && !sessions.some((session) => session.reviewers.includes("code quality"))) errors.push(`This change adds ${net} net lines of production source, ${largeChange} or more, so a session needs a code quality reviewer in its reviewer table.`);
|
|
776
|
+
if (tests.length > 0 && !testsReviewed) errors.push(`This pull request changes test code: ${some(tests.map((file) => file.file))}. Add a tests reviewer to a session's reviewer table: a high pull request that changes test code needs one.`);
|
|
777
|
+
}
|
|
778
|
+
if (errors.length > 0) return {
|
|
779
|
+
ok: false,
|
|
780
|
+
errors
|
|
781
|
+
};
|
|
782
|
+
return {
|
|
783
|
+
ok: true,
|
|
784
|
+
why: `${atTier}, with ${sessions.length} session${sessions.length === 1 ? "" : "s"} on record${tier === "low" ? ", which it needs since its author may not push to the repo" : ""}${testsReviewed ? ", with a tests reviewer" : ""}${benched}`
|
|
785
|
+
};
|
|
786
|
+
};
|
|
787
|
+
/**
|
|
788
|
+
* A branch's changes since its merge base with `origin/main`, as `git diff --numstat -z -M` counts
|
|
789
|
+
* them: a renamed file names where it was, as GitHub's `pulls/<n>/files` does, and counts only its
|
|
790
|
+
* edits.
|
|
791
|
+
*/
|
|
792
|
+
const changesOf = (numstat) => {
|
|
793
|
+
const fields = numstat.split("\0");
|
|
794
|
+
const changes = [];
|
|
795
|
+
for (let at = 0; at < fields.length; at += 1) {
|
|
796
|
+
const [additions = "-", deletions = "-", ...path] = (fields[at] ?? "").split(" ");
|
|
797
|
+
const filename = path.join(" ");
|
|
798
|
+
if (additions === "") continue;
|
|
799
|
+
const lines = {
|
|
800
|
+
additions: additions === "-" ? 0 : Number(additions),
|
|
801
|
+
deletions: deletions === "-" ? 0 : Number(deletions)
|
|
802
|
+
};
|
|
803
|
+
if (filename !== "") {
|
|
804
|
+
changes.push({
|
|
805
|
+
filename,
|
|
806
|
+
...lines
|
|
807
|
+
});
|
|
808
|
+
continue;
|
|
809
|
+
}
|
|
810
|
+
const [previous_filename = "", renamed = ""] = fields.slice(at + 1, at + 3);
|
|
811
|
+
changes.push({
|
|
812
|
+
filename: renamed,
|
|
813
|
+
previous_filename,
|
|
814
|
+
...lines
|
|
815
|
+
});
|
|
816
|
+
at += 2;
|
|
817
|
+
}
|
|
818
|
+
return changes;
|
|
819
|
+
};
|
|
820
|
+
/** GitHub's word for each status letter `git diff --name-status` prints. */
|
|
821
|
+
const statuses = /* @__PURE__ */ new Map([
|
|
822
|
+
["A", "added"],
|
|
823
|
+
["C", "copied"],
|
|
824
|
+
["D", "removed"],
|
|
825
|
+
["M", "modified"],
|
|
826
|
+
["R", "renamed"],
|
|
827
|
+
["T", "changed"]
|
|
828
|
+
]);
|
|
829
|
+
/**
|
|
830
|
+
* A branch's changes between two commits, as `recordOf` reads them from GitHub: `changesOf`'s, each
|
|
831
|
+
* with its status, and its patch when it's test code, for `--count`.
|
|
832
|
+
*/
|
|
833
|
+
const localChangesOf = (git, from, to, settings) => {
|
|
834
|
+
const fields = printed(git, [
|
|
835
|
+
"diff",
|
|
836
|
+
"--name-status",
|
|
837
|
+
"-z",
|
|
838
|
+
"-M",
|
|
839
|
+
from,
|
|
840
|
+
to
|
|
841
|
+
]).split("\0");
|
|
842
|
+
const statusOf = /* @__PURE__ */ new Map();
|
|
843
|
+
for (let at = 0; at < fields.length; at += 1) {
|
|
844
|
+
const letter = fields[at]?.[0] ?? "";
|
|
845
|
+
if (letter === "") continue;
|
|
846
|
+
const paths = letter === "R" || letter === "C" ? 2 : 1;
|
|
847
|
+
statusOf.set(fields[at + paths] ?? "", statuses.get(letter) ?? "modified");
|
|
848
|
+
at += paths;
|
|
849
|
+
}
|
|
850
|
+
return changesOf(printed(git, [
|
|
851
|
+
"diff",
|
|
852
|
+
"--numstat",
|
|
853
|
+
"-z",
|
|
854
|
+
"-M",
|
|
855
|
+
from,
|
|
856
|
+
to
|
|
857
|
+
])).map((change) => {
|
|
858
|
+
const status = statusOf.get(change.filename) ?? "modified";
|
|
859
|
+
const paths = [change.previous_filename, change.filename].filter((path) => path !== void 0);
|
|
860
|
+
if (!paths.some((path) => isTestCode(path, settings))) return {
|
|
861
|
+
...change,
|
|
862
|
+
status
|
|
863
|
+
};
|
|
864
|
+
const patch = printed(git, [
|
|
865
|
+
"-c",
|
|
866
|
+
"core.quotePath=false",
|
|
867
|
+
"diff",
|
|
868
|
+
"-M",
|
|
869
|
+
"--no-color",
|
|
870
|
+
"--no-ext-diff",
|
|
871
|
+
"--no-textconv",
|
|
872
|
+
from,
|
|
873
|
+
to,
|
|
874
|
+
"--",
|
|
875
|
+
...paths
|
|
876
|
+
]);
|
|
877
|
+
return /^(?:Binary files .* differ|GIT binary patch)$/m.test(patch) ? {
|
|
878
|
+
...change,
|
|
879
|
+
status
|
|
880
|
+
} : {
|
|
881
|
+
...change,
|
|
882
|
+
status,
|
|
883
|
+
patch
|
|
884
|
+
};
|
|
885
|
+
});
|
|
886
|
+
};
|
|
887
|
+
const gh = (args) => {
|
|
888
|
+
const done = spawnSync("gh", ["api", ...args], {
|
|
889
|
+
encoding: "utf8",
|
|
890
|
+
maxBuffer: 67108864
|
|
891
|
+
});
|
|
892
|
+
if (done.status === 0) return done.stdout;
|
|
893
|
+
if (done.stderr.includes("HTTP 404")) return void 0;
|
|
894
|
+
throw new Error(`gh api ${args.join(" ")} failed: ${done.stderr}`);
|
|
895
|
+
};
|
|
896
|
+
/** What GitHub printed, where a 404 is a failure too. */
|
|
897
|
+
const found = (github, args) => {
|
|
898
|
+
const printed = github(args);
|
|
899
|
+
if (printed === void 0) throw new Error(`gh api ${args.join(" ")} found nothing`);
|
|
900
|
+
return printed;
|
|
901
|
+
};
|
|
902
|
+
/** Whether `from` at the merge base of `base` and `head` is the same blob as `to` at `head`. */
|
|
903
|
+
const sameBlob = (git, base, head, from, to) => {
|
|
904
|
+
const blob = (rev) => git([
|
|
905
|
+
"rev-parse",
|
|
906
|
+
"--verify",
|
|
907
|
+
"--quiet",
|
|
908
|
+
rev
|
|
909
|
+
]);
|
|
910
|
+
const [before, after] = [blob(`${printed(git, [
|
|
911
|
+
"merge-base",
|
|
912
|
+
base,
|
|
913
|
+
head
|
|
914
|
+
]).trim()}:${from}`), blob(`${head}:${to}`)];
|
|
915
|
+
return before.status === 0 && after.status === 0 && before.stdout.trim() === after.stdout.trim();
|
|
916
|
+
};
|
|
917
|
+
/** A paginated GitHub API path's items, each printed by `jq` as one line of JSON. */
|
|
918
|
+
const items = (github, path, jq) => {
|
|
919
|
+
return found(github, [
|
|
920
|
+
"--paginate",
|
|
921
|
+
path,
|
|
922
|
+
"--jq",
|
|
923
|
+
jq
|
|
924
|
+
]).split("\n").filter((line) => line !== "").map((line) => JSON.parse(line));
|
|
925
|
+
};
|
|
926
|
+
/** The files a page of GitHub's list of a pull request's files holds, at most. */
|
|
927
|
+
const filesPage = 100;
|
|
928
|
+
/**
|
|
929
|
+
* Pull request `pull`'s changed files, read a page of `filesPage` at a time, as many pages as its
|
|
930
|
+
* `changed_files` count needs. Throws for a pull request of `githubFileCap` files or more, as
|
|
931
|
+
* `verdict` refuses one, before reading any: GitHub's list may stop short of them, and a plan
|
|
932
|
+
* from part of a change would miss its cases.
|
|
933
|
+
*/
|
|
934
|
+
const pullChangesOf = (github, repo, pull) => {
|
|
935
|
+
const count = Number(found(github, [
|
|
936
|
+
`repos/${repo}/pulls/${pull}`,
|
|
937
|
+
"--jq",
|
|
938
|
+
".changed_files"
|
|
939
|
+
]));
|
|
940
|
+
if (!Number.isSafeInteger(count) || count < 0) throw new Error(`GitHub gave no file count for #${pull}`);
|
|
941
|
+
if (count >= 3e3) throw new Error(`#${pull} changes ${count} files, and GitHub lists at most ${githubFileCap}: dispatch ci-bench with its cases by hand`);
|
|
942
|
+
const changes = [];
|
|
943
|
+
for (let page = 1; page <= Math.ceil(count / filesPage); page += 1) {
|
|
944
|
+
const path = `repos/${repo}/pulls/${pull}/files?per_page=${filesPage}&page=${page}`;
|
|
945
|
+
const lines = found(github, [
|
|
946
|
+
path,
|
|
947
|
+
"--jq",
|
|
948
|
+
".[] | {filename, previous_filename} | tojson"
|
|
949
|
+
]);
|
|
950
|
+
for (const line of lines.split("\n").filter((each) => each !== "")) {
|
|
951
|
+
const { filename, previous_filename } = JSON.parse(line);
|
|
952
|
+
changes.push(previous_filename === null ? {
|
|
953
|
+
filename,
|
|
954
|
+
additions: 0,
|
|
955
|
+
deletions: 0
|
|
956
|
+
} : {
|
|
957
|
+
filename,
|
|
958
|
+
previous_filename,
|
|
959
|
+
additions: 0,
|
|
960
|
+
deletions: 0
|
|
961
|
+
});
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return changes;
|
|
965
|
+
};
|
|
966
|
+
/** The jobs GitHub lists of a run on one page: a ci-bench run's are fewer. */
|
|
967
|
+
const runJobsPage = 100;
|
|
968
|
+
/**
|
|
969
|
+
* Run `id` of `repo`, its workflow, branch and jobs, every attempt's, or `undefined` when GitHub has
|
|
970
|
+
* no such run.
|
|
971
|
+
*/
|
|
972
|
+
const benchRunOf = (github, repo, id) => {
|
|
973
|
+
const run = github([
|
|
974
|
+
`repos/${repo}/actions/runs/${id}`,
|
|
975
|
+
"--jq",
|
|
976
|
+
"{path, branch: .head_branch} | tojson"
|
|
977
|
+
]);
|
|
978
|
+
if (run === void 0) return void 0;
|
|
979
|
+
const jobs = github([
|
|
980
|
+
`repos/${repo}/actions/runs/${id}/jobs?filter=all&per_page=${runJobsPage}`,
|
|
981
|
+
"--jq",
|
|
982
|
+
"[.jobs[] | {name, conclusion}] | tojson"
|
|
983
|
+
]);
|
|
984
|
+
if (jobs === void 0) return void 0;
|
|
985
|
+
return {
|
|
986
|
+
...JSON.parse(run),
|
|
987
|
+
jobs: JSON.parse(jobs)
|
|
988
|
+
};
|
|
989
|
+
};
|
|
990
|
+
/**
|
|
991
|
+
* Git in the checkout at `cwd`. When `GH_TOKEN` is set in `gitEnv`, passes a process-scoped
|
|
992
|
+
* `gh auth git-credential` helper so a `--filter=blob:none` partial clone lazily fetches only the
|
|
993
|
+
* blobs `merge-tree` or `cat-file` touches without writing credentials to `.git/config` or passing
|
|
994
|
+
* a token in argv.
|
|
995
|
+
*/
|
|
996
|
+
const gitIn = (cwd, gitEnv = env) => (args) => {
|
|
997
|
+
const auth = gitEnv["GH_TOKEN"] !== void 0 && gitEnv["GH_TOKEN"] !== "" ? [
|
|
998
|
+
"-c",
|
|
999
|
+
"credential.helper=",
|
|
1000
|
+
"-c",
|
|
1001
|
+
"credential.helper=!gh auth git-credential",
|
|
1002
|
+
"-c",
|
|
1003
|
+
"credential.interactive=never"
|
|
1004
|
+
] : [];
|
|
1005
|
+
const done = spawnSync("git", [
|
|
1006
|
+
"-C",
|
|
1007
|
+
cwd,
|
|
1008
|
+
...auth,
|
|
1009
|
+
...args
|
|
1010
|
+
], {
|
|
1011
|
+
encoding: "utf8",
|
|
1012
|
+
env: {
|
|
1013
|
+
...gitEnv,
|
|
1014
|
+
GIT_TERMINAL_PROMPT: "0"
|
|
1015
|
+
},
|
|
1016
|
+
maxBuffer: 67108864
|
|
1017
|
+
});
|
|
1018
|
+
return {
|
|
1019
|
+
status: done.status ?? -1,
|
|
1020
|
+
stdout: done.stdout,
|
|
1021
|
+
stderr: done.stderr
|
|
1022
|
+
};
|
|
1023
|
+
};
|
|
1024
|
+
/** What a git command printed, which throws unless it exits with one of `statuses`. */
|
|
1025
|
+
const printed = (git, args, statuses = [0]) => {
|
|
1026
|
+
const done = git(args);
|
|
1027
|
+
if (!statuses.includes(done.status)) throw new Error(`git ${args.join(" ")} failed: ${done.stderr}`);
|
|
1028
|
+
return done.stdout;
|
|
1029
|
+
};
|
|
1030
|
+
/** A file's lines at a commit, as a diff counts them: a last line with no newline counts too. */
|
|
1031
|
+
const linesAt = (git, commit, path) => {
|
|
1032
|
+
const text = printed(git, [
|
|
1033
|
+
"cat-file",
|
|
1034
|
+
"-p",
|
|
1035
|
+
`${commit}:${path}`
|
|
1036
|
+
]);
|
|
1037
|
+
if (text === "") return 0;
|
|
1038
|
+
return text.split("\n").length - (text.endsWith("\n") ? 1 : 0);
|
|
1039
|
+
};
|
|
1040
|
+
/** The paths that differ between two trees or commits, both sides of a rename included. */
|
|
1041
|
+
const differ = (git, from, to) => printed(git, [
|
|
1042
|
+
"diff-tree",
|
|
1043
|
+
"-r",
|
|
1044
|
+
"--name-only",
|
|
1045
|
+
"--no-renames",
|
|
1046
|
+
from,
|
|
1047
|
+
to
|
|
1048
|
+
]).split("\n").filter((line) => line !== "");
|
|
1049
|
+
/** Whether a commit's tree holds a file at a path, reading the tree on the first question. */
|
|
1050
|
+
const holdsAt = (git, commit) => {
|
|
1051
|
+
let paths;
|
|
1052
|
+
return (path) => {
|
|
1053
|
+
paths ??= new Set(printed(git, [
|
|
1054
|
+
"ls-tree",
|
|
1055
|
+
"-r",
|
|
1056
|
+
"--name-only",
|
|
1057
|
+
commit
|
|
1058
|
+
]).split("\n"));
|
|
1059
|
+
return paths.has(path);
|
|
1060
|
+
};
|
|
1061
|
+
};
|
|
1062
|
+
/**
|
|
1063
|
+
* The head's history after a commit, read from a checkout with the whole commit and tree history,
|
|
1064
|
+
* such as CI's blobless partial clone (`--filter=blob:none`): the commits the head holds and
|
|
1065
|
+
* neither the commit nor the base does, each with the paths it changed by hand. A merge's are where
|
|
1066
|
+
* its tree differs from `git merge-tree`'s merge of its parents: a clean merge has none, and a
|
|
1067
|
+
* conflict resolved by hand has the files it resolved (lazily fetching only the conflicting blobs
|
|
1068
|
+
* from the promisor remote, failing closed if the fetch fails). A merge of three or more parents
|
|
1069
|
+
* has all it changes from its first.
|
|
1070
|
+
*/
|
|
1071
|
+
const historyOf = (git, base, head) => (sha) => {
|
|
1072
|
+
if (git([
|
|
1073
|
+
"rev-parse",
|
|
1074
|
+
"--verify",
|
|
1075
|
+
"--quiet",
|
|
1076
|
+
`${sha}^{commit}`
|
|
1077
|
+
]).status !== 0) return void 0;
|
|
1078
|
+
if (git([
|
|
1079
|
+
"merge-base",
|
|
1080
|
+
"--is-ancestor",
|
|
1081
|
+
sha,
|
|
1082
|
+
head
|
|
1083
|
+
]).status !== 0) return {
|
|
1084
|
+
ancestor: false,
|
|
1085
|
+
commits: []
|
|
1086
|
+
};
|
|
1087
|
+
return {
|
|
1088
|
+
ancestor: true,
|
|
1089
|
+
commits: printed(git, [
|
|
1090
|
+
"log",
|
|
1091
|
+
"--format=%H%x09%P%x09%s",
|
|
1092
|
+
head,
|
|
1093
|
+
"--not",
|
|
1094
|
+
sha,
|
|
1095
|
+
base
|
|
1096
|
+
]).split("\n").filter((line) => line !== "").map((line) => {
|
|
1097
|
+
const [commit = "", parents = "", subject = ""] = line.split(" ");
|
|
1098
|
+
const [first = "", second, ...more] = parents.split(" ");
|
|
1099
|
+
if (second === void 0) return {
|
|
1100
|
+
sha: commit,
|
|
1101
|
+
subject,
|
|
1102
|
+
files: printed(git, [
|
|
1103
|
+
"diff-tree",
|
|
1104
|
+
"-r",
|
|
1105
|
+
"--root",
|
|
1106
|
+
"--no-commit-id",
|
|
1107
|
+
"--name-only",
|
|
1108
|
+
"--no-renames",
|
|
1109
|
+
commit
|
|
1110
|
+
]).split("\n").filter((path) => path !== "")
|
|
1111
|
+
};
|
|
1112
|
+
if (more.length > 0) return {
|
|
1113
|
+
sha: commit,
|
|
1114
|
+
subject,
|
|
1115
|
+
files: differ(git, first, commit)
|
|
1116
|
+
};
|
|
1117
|
+
const merged = printed(git, [
|
|
1118
|
+
"merge-tree",
|
|
1119
|
+
"--write-tree",
|
|
1120
|
+
"--no-messages",
|
|
1121
|
+
first,
|
|
1122
|
+
second
|
|
1123
|
+
], [0, 1]).split("\n")[0] ?? "";
|
|
1124
|
+
return {
|
|
1125
|
+
sha: commit,
|
|
1126
|
+
subject,
|
|
1127
|
+
files: differ(git, merged, `${commit}^{tree}`)
|
|
1128
|
+
};
|
|
1129
|
+
})
|
|
1130
|
+
};
|
|
1131
|
+
};
|
|
1132
|
+
/** The roles GitHub's permission check answers that may push to a repo. */
|
|
1133
|
+
const pushRoles = /* @__PURE__ */ new Set(["admin", "write"]);
|
|
1134
|
+
/** Whether `login` may push to `repo`, as GitHub's permission check answers: none when it has no such account. */
|
|
1135
|
+
const mayPush = (github, repo, login) => {
|
|
1136
|
+
const role = github([
|
|
1137
|
+
`repos/${repo}/collaborators/${login}/permission`,
|
|
1138
|
+
"--jq",
|
|
1139
|
+
".permission"
|
|
1140
|
+
]);
|
|
1141
|
+
return role !== void 0 && pushRoles.has(role.trim());
|
|
1142
|
+
};
|
|
1143
|
+
/** Whether a comment starts like a session, the first line `verdict` reads it by, or a lookalike. */
|
|
1144
|
+
const startsLikeSession = (body) => /^#* *Review:/i.test(body.replace(/\r/g, ""));
|
|
1145
|
+
/**
|
|
1146
|
+
* Pull request `pull`'s people's comments, where one that starts like a session counts only when its
|
|
1147
|
+
* author may push to `repo`. Anyone may comment on a public repo, so a session from someone who
|
|
1148
|
+
* may not push would otherwise stand in for the review. Throws past `sessionAuthorsMost` such authors.
|
|
1149
|
+
*/
|
|
1150
|
+
const countedComments = (github, repo, pull) => {
|
|
1151
|
+
const comments = items(github, `repos/${repo}/issues/${pull}/comments`, ".[] | select(.user.type == \"User\") | {login: .user.login, body} | tojson");
|
|
1152
|
+
const authors = new Set(comments.filter((comment) => startsLikeSession(comment.body)).map((comment) => comment.login));
|
|
1153
|
+
if (authors.size > 10) throw new Error(`#${pull} has comments that start like a session from ${authors.size} people, and review-record reads the push access of at most 10`);
|
|
1154
|
+
const pushers = new Set([...authors].filter((login) => mayPush(github, repo, login)));
|
|
1155
|
+
return {
|
|
1156
|
+
comments: comments.filter((comment) => !startsLikeSession(comment.body) || pushers.has(comment.login)).map((comment) => comment.body),
|
|
1157
|
+
uncounted: [...authors].filter((login) => !pushers.has(login))
|
|
1158
|
+
};
|
|
1159
|
+
};
|
|
1160
|
+
/** The reason a session comment from someone who may not push doesn't count, when there is one. */
|
|
1161
|
+
const uncountedNote = (record) => record.uncounted === void 0 || record.uncounted.length === 0 ? "" : `: the sessions from ${record.uncounted.join(", ")} don't count, since they may not push to the repo`;
|
|
1162
|
+
/** A file at a commit, through GitHub's contents API, or `undefined` when the commit has none. */
|
|
1163
|
+
const contentsAt = (github, repo, path, ref) => github([
|
|
1164
|
+
"-H",
|
|
1165
|
+
"Accept: application/vnd.github.raw+json",
|
|
1166
|
+
`repos/${repo}/contents/${path}?ref=${ref}`
|
|
1167
|
+
]);
|
|
1168
|
+
/**
|
|
1169
|
+
* What a pull request's review on record needs, read from GitHub and the checkout. The settings,
|
|
1170
|
+
* the list of files a review covers and the bench list are the base's alone, or `undefined` when
|
|
1171
|
+
* the base has none: the pull request's own copies are never read, so a change to one counts only
|
|
1172
|
+
* once it has merged.
|
|
1173
|
+
*/
|
|
1174
|
+
const recordOf = (github, git, repo, pull, base, head) => {
|
|
1175
|
+
const settings = settingsFrom(contentsAt(github, repo, settingsPath, base));
|
|
1176
|
+
const isTest = (path) => isTestCode(path, settings);
|
|
1177
|
+
return {
|
|
1178
|
+
settings,
|
|
1179
|
+
changedFiles: Number(found(github, [
|
|
1180
|
+
`repos/${repo}/pulls/${pull}`,
|
|
1181
|
+
"--jq",
|
|
1182
|
+
".changed_files"
|
|
1183
|
+
])),
|
|
1184
|
+
...countedComments(github, repo, pull),
|
|
1185
|
+
outsider: !mayPush(github, repo, found(github, [
|
|
1186
|
+
`repos/${repo}/pulls/${pull}`,
|
|
1187
|
+
"--jq",
|
|
1188
|
+
".user.login"
|
|
1189
|
+
]).trim()),
|
|
1190
|
+
body: found(github, [
|
|
1191
|
+
`repos/${repo}/pulls/${pull}`,
|
|
1192
|
+
"--jq",
|
|
1193
|
+
".body // \"\""
|
|
1194
|
+
]),
|
|
1195
|
+
changes: items(github, `repos/${repo}/pulls/${pull}/files`, ".[] | {filename, additions, deletions, previous_filename, status, patch} | with_entries(select(.value != null)) | tojson").map(({ patch, ...change }) => {
|
|
1196
|
+
const from = change.previous_filename;
|
|
1197
|
+
if (!isTest(change.filename) && (from === void 0 || !isTest(from))) return change;
|
|
1198
|
+
return patch === void 0 && from !== void 0 && sameBlob(git, base, head, from, change.filename) ? {
|
|
1199
|
+
...change,
|
|
1200
|
+
patch: ""
|
|
1201
|
+
} : patch === void 0 ? change : {
|
|
1202
|
+
...change,
|
|
1203
|
+
patch
|
|
1204
|
+
};
|
|
1205
|
+
}),
|
|
1206
|
+
list: contentsAt(github, repo, listPath, base),
|
|
1207
|
+
benched: settings.benchedPaths === void 0 ? void 0 : contentsAt(github, repo, settings.benchedPaths, base),
|
|
1208
|
+
head,
|
|
1209
|
+
pull: Number(pull),
|
|
1210
|
+
benchRun: (id) => benchRunOf(github, repo, id),
|
|
1211
|
+
since: historyOf(git, base, head),
|
|
1212
|
+
holds: holdsAt(git, head),
|
|
1213
|
+
lines: (path) => linesAt(git, head, path)
|
|
1214
|
+
};
|
|
1215
|
+
};
|
|
1216
|
+
/**
|
|
1217
|
+
* The current branch's record, as `recordOf` reads a pull request's: its changes since its merge
|
|
1218
|
+
* base with `origin/main`, and the settings, the lists and the history past `origin/main`'s tip, as
|
|
1219
|
+
* CI reads them at the event's base, with `comments` and `body` in place of GitHub's, for `--count`,
|
|
1220
|
+
* `--tier` and `--check`.
|
|
1221
|
+
*/
|
|
1222
|
+
const branchRecordOf = (git, comments = [], body = "") => {
|
|
1223
|
+
const base = printed(git, [
|
|
1224
|
+
"merge-base",
|
|
1225
|
+
"origin/main",
|
|
1226
|
+
"HEAD"
|
|
1227
|
+
]).trim();
|
|
1228
|
+
const tip = printed(git, ["rev-parse", "origin/main"]).trim();
|
|
1229
|
+
const head = printed(git, ["rev-parse", "HEAD"]).trim();
|
|
1230
|
+
const atTip = (path) => git([
|
|
1231
|
+
"cat-file",
|
|
1232
|
+
"-e",
|
|
1233
|
+
`${tip}:${path}`
|
|
1234
|
+
]).status === 0 ? printed(git, [
|
|
1235
|
+
"cat-file",
|
|
1236
|
+
"-p",
|
|
1237
|
+
`${tip}:${path}`
|
|
1238
|
+
]) : void 0;
|
|
1239
|
+
const settings = settingsFrom(atTip(settingsPath));
|
|
1240
|
+
return {
|
|
1241
|
+
settings,
|
|
1242
|
+
comments,
|
|
1243
|
+
body,
|
|
1244
|
+
changes: localChangesOf(git, base, head, settings),
|
|
1245
|
+
list: atTip(listPath),
|
|
1246
|
+
benched: settings.benchedPaths === void 0 ? void 0 : atTip(settings.benchedPaths),
|
|
1247
|
+
head,
|
|
1248
|
+
since: historyOf(git, tip, head),
|
|
1249
|
+
holds: holdsAt(git, head),
|
|
1250
|
+
lines: (path) => linesAt(git, head, path)
|
|
1251
|
+
};
|
|
1252
|
+
};
|
|
1253
|
+
export { benchRunOf, benchWorkflow, benchedCases, branchRecordOf, changedLines, changesOf, countedComments, coveredBy, floorOf, gh, gitIn, githubFileCap, historyOf, isProductionSource, isTestCode, largeChange, netProductionLines, pullChangesOf, readSession, recordFloor, recordOf, testChangesOf, testRisks, testsOnly, tierOf, tiers, unchangedJob, verdict };
|