@ref0/workspace 0.9.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.
@@ -0,0 +1,107 @@
1
+ import { benchedCases, branchRecordOf, gh, gitIn, largeChange, pullChangesOf, recordFloor, recordOf, testChangesOf, verdict } from "./review-record.mjs";
2
+ import { defaults, settingsFrom, settingsPath } from "./settings.mjs";
3
+ import { readFileSync } from "node:fs";
4
+ import { join } from "node:path";
5
+ // src/review-record/command.ts
6
+ /**
7
+ * `ref0-workspace review-record`: whether a pull request's review is on record for its merge risk,
8
+ * as `review-record.ts` judges it, and what an agent asks of its branch before it posts one.
9
+ *
10
+ * ref0-workspace review-record CI: reads PR, BASE, HEAD, GITHUB_REPOSITORY and GH_TOKEN
11
+ * ref0-workspace review-record --count the branch's net lines of production source, and its floor
12
+ * ref0-workspace review-record --tier the branch's floor, and what sets it, as a Merge risk line
13
+ * ref0-workspace review-record --check <comment> [<body>] CI's checks on a session, before posting it
14
+ * ref0-workspace review-record --benched ci-bench: the cases PR's changes tie to, from this checkout's list
15
+ *
16
+ * CI runs it at the version the base's `pnpm-workspace.yaml` catalog names, in a blobless clone of
17
+ * the base and the head, and it reads the base's settings and lists from GitHub, so a pull request
18
+ * can't change the gate that judges it.
19
+ */
20
+ const usage = [
21
+ "usage: ref0-workspace review-record CI: reads PR, BASE, HEAD, GITHUB_REPOSITORY",
22
+ " ref0-workspace review-record --count net lines of production source, and the floor",
23
+ " ref0-workspace review-record --tier the branch's tier floor, and what sets it",
24
+ " ref0-workspace review-record --check <comment> [<body>] CI's checks on a session comment and a body",
25
+ " ref0-workspace review-record --benched ci-bench: the cases PR's changes tie to, from this checkout's list"
26
+ ].join("\n");
27
+ /** A tier floor as one line to paste into a body or a brief. */
28
+ const floorLine = (floor) => `Merge risk: ${floor.tier}, from ${floor.why.join("; ")}.`;
29
+ /** A file under `root`, or `undefined` when there's none. */
30
+ const fileAt = (root, path) => {
31
+ try {
32
+ return readFileSync(join(root, path), "utf8");
33
+ } catch {
34
+ return;
35
+ }
36
+ };
37
+ /** The branch's net lines of production source on stdout, and what they and its floor need on stderr. */
38
+ const count = (root) => {
39
+ const record = branchRecordOf(gitIn(root));
40
+ const { net, floor } = recordFloor(record);
41
+ const { files: tests } = testChangesOf(record.changes, record.settings ?? defaults);
42
+ console.log(net);
43
+ console.error(net >= 1e3 ? `Large: ${largeChange} or more net lines of production source, so a session needs a code quality reviewer.` : `Not large: under ${largeChange} net lines of production source.`);
44
+ console.error(`${floorLine(floor)} Name it or higher on the body's "Merge risk:" line.`);
45
+ if (tests.length > 0) console.error(`Tests: ${tests.length} files of test code changed, so the body names each on a "Break:" line at low or with a medium session that has no tests reviewer, and a high session needs a tests reviewer.`);
46
+ return 0;
47
+ };
48
+ /** A session comment and a body, from their files, judged as CI would judge them on this branch. */
49
+ const check = (root, files) => {
50
+ const [comment = "", body = comment] = files.map((file) => readFileSync(file, "utf8"));
51
+ const result = verdict(branchRecordOf(gitIn(root), [comment], body));
52
+ if (result.ok) {
53
+ console.log(result.why);
54
+ return 0;
55
+ }
56
+ for (const error of result.errors) console.log(error);
57
+ return 1;
58
+ };
59
+ /**
60
+ * ci-bench's plan: the cases pull request `PR` of `GITHUB_REPOSITORY` changes the benched paths of,
61
+ * one a line, read from this checkout's settings and bench list, main's in ci-bench.
62
+ */
63
+ const benched = (root, env) => {
64
+ const { PR: pull, GITHUB_REPOSITORY: repo } = env;
65
+ if (!pull || !repo) {
66
+ console.error(usage);
67
+ return 2;
68
+ }
69
+ const settings = settingsFrom(fileAt(root, settingsPath));
70
+ if (settings.benchedPaths === void 0) {
71
+ console.error(`${settingsPath} names no benchedPaths, so no case is benched`);
72
+ return 2;
73
+ }
74
+ const list = readFileSync(join(root, settings.benchedPaths), "utf8");
75
+ console.log(benchedCases(list, pullChangesOf(gh, repo, pull), settings).cases.join("\n"));
76
+ return 0;
77
+ };
78
+ /** CI's run: pull request `PR`'s review on record, each reason it isn't as an Actions error. */
79
+ const ci = (root, env) => {
80
+ const { PR: pull, BASE: base, HEAD: head, GITHUB_REPOSITORY: repo } = env;
81
+ if (!pull || !base || !head || !repo) {
82
+ console.error(usage);
83
+ return 2;
84
+ }
85
+ const result = verdict(recordOf(gh, gitIn(root, env), repo, pull, base, head));
86
+ if (result.ok) {
87
+ console.log(result.why);
88
+ return 0;
89
+ }
90
+ for (const error of result.errors) console.log(`::error::${error}`);
91
+ return 1;
92
+ };
93
+ /** Runs the subcommand `args` names in the checkout at `root`, and answers its exit code. */
94
+ const reviewRecord = (args, root, env) => {
95
+ const [flag, ...rest] = args;
96
+ if (flag === void 0) return ci(root, env);
97
+ if (flag === "--tier" && rest.length === 0) {
98
+ console.log(floorLine(recordFloor(branchRecordOf(gitIn(root))).floor));
99
+ return 0;
100
+ }
101
+ if (flag === "--count" && rest.length === 0) return count(root);
102
+ if (flag === "--check" && (rest.length === 1 || rest.length === 2)) return check(root, rest);
103
+ if (flag === "--benched" && rest.length === 0) return benched(root, env);
104
+ console.error(usage);
105
+ return 2;
106
+ };
107
+ export { reviewRecord, usage };
@@ -0,0 +1,3 @@
1
+ import { Change, benchedCases, testsOnly } from "./review-record.mjs";
2
+ import { Settings, settingsFrom, settingsPath } from "./settings.mjs";
3
+ export { type Change, type Settings, benchedCases, settingsFrom, settingsPath, testsOnly };
@@ -0,0 +1,3 @@
1
+ import { benchedCases, testsOnly } from "./review-record.mjs";
2
+ import { settingsFrom, settingsPath } from "./settings.mjs";
3
+ export { benchedCases, settingsFrom, settingsPath, testsOnly };
@@ -0,0 +1,33 @@
1
+ import { Settings } from "./settings.mjs";
2
+ /** A changed file, as GitHub's `pulls/<n>/files` lists it, in the fields read here. */
3
+ export interface Change {
4
+ readonly filename: string;
5
+ readonly additions: number;
6
+ readonly deletions: number;
7
+ readonly previous_filename?: string;
8
+ /** GitHub's: `added`, `removed`, `modified`, `renamed` and the like. */
9
+ readonly status?: string;
10
+ /**
11
+ * Its hunks, kept for test code alone, from the first `@@` line: GitHub leaves it out of a file
12
+ * too big to show, and `testRisks` then reads the file as a risk.
13
+ */
14
+ readonly patch?: string;
15
+ }
16
+ /**
17
+ * Whether `path`, from the repo's root, is a test's alone: `.test.` in its name, under a `testing/`
18
+ * folder, or a package's `testing.ts` entry, which only the workspace's tests import, and none of
19
+ * the settings' `benchRuns`, the test helpers a bench case runs all the same. No bench case runs
20
+ * one, so no pull request owes a case's reading for it, `benchedCases`.
21
+ */
22
+ export declare const testsOnly: (path: string, settings: Settings) => boolean;
23
+ /**
24
+ * The bench cases the settings' `benchedPaths` list ties a pull request's changes to, in the list's
25
+ * order, and the changed files that tie them: each case with a line whose glob matches a changed
26
+ * file, or the path a file was renamed from, and every case the list names when a `*` line
27
+ * matches. A test's path alone, `testsOnly`, ties none, since no case runs it. A line that isn't
28
+ * `<case> <glob>` throws.
29
+ */
30
+ export declare const benchedCases: (list: string, changes: ReadonlyArray<Change>, settings: Settings) => {
31
+ cases: string[];
32
+ files: string[];
33
+ };