@decohere/ci-tools 0.0.1-dev.20261105.99 → 0.0.1-dev.20261106.139

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decohere/ci-tools",
3
- "version": "0.0.1-dev.20261105.99",
3
+ "version": "0.0.1-dev.20261106.139",
4
4
  "description": "CI utility scripts for badge generation, coverage reporting, pages artifact assembly, and GitHub API tooling",
5
5
  "keywords": [
6
6
  "typescript",
@@ -41,11 +41,9 @@
41
41
  "./changeset/check-status": "./src/changeset/check-status.mjs",
42
42
  "./github/create-issue": "./src/github/create-issue.mjs",
43
43
  "./github/ensure-metadata": "./src/github/ensure-metadata.mjs",
44
- "./github/create-or-update-pr": "./src/github/create-or-update-pr.mjs",
45
44
  "./github/publish-verify": "./src/github/publish-verify.mjs",
46
45
  "./github/first-publish-check": "./src/github/first-publish-check.mjs",
47
- "./github/npm-bootstrap": "./src/github/npm-bootstrap.mjs",
48
- "./github/smoke-create-launcher": "./src/github/smoke-create-launcher.mjs"
46
+ "./github/npm-bootstrap": "./src/github/npm-bootstrap.mjs"
49
47
  },
50
48
  "scripts": {
51
49
  "test": "vitest run"
@@ -0,0 +1,162 @@
1
+ #!/usr/bin/env node
2
+ // When a run fails, put each failed step's reason on the run's summary page. GitHub only shows
3
+ // "Process completed with exit code 1" there, and the reason stays in the log. Standard library
4
+ // only: it has to work when the install or build is what failed.
5
+ import { appendFileSync } from "node:fs";
6
+ import { pathToFileURL } from "node:url";
7
+ import { stripVTControlCharacters } from "node:util";
8
+
9
+ const NO_REASON = "No reason found in the log.";
10
+ const MAX_LINES = 40;
11
+ const MAX_REASON = 1000;
12
+ const EXIT_LINE = /^##\[error\]Process completed with exit code \d+\.?$/;
13
+ const NOISE = /^\s*(ELIFECYCLE|npm error (A complete log|code|path|command)|npm ERR!)/;
14
+ const STACK_FRAME = /^\s*(at |❯ )/;
15
+ const TEST_TOTALS = /^\s*(Test Files|Tests|Errors|Start at|Duration)\s/;
16
+ const NAMED_ERROR = /^\s*(\w*error\b\S*:|fatal:|FAIL\b)/i;
17
+ const FILE_POSITION = /^\s*\S+:\d+(:\d+)?:? /;
18
+
19
+ function cleanLines(log) {
20
+ return log.split("\n").map((line) =>
21
+ stripVTControlCharacters(line)
22
+ .replace(/^\d{4}-\d{2}-\d{2}T[\d:.]+Z ?/, "")
23
+ .replace(/\r$/, "")
24
+ .replace(/^\ufeff/, ""),
25
+ );
26
+ }
27
+
28
+ /** The failed step's reason and last lines, from a job's raw log. */
29
+ export function extractFailure(log) {
30
+ const all = cleanLines(log);
31
+
32
+ let end = all.findLastIndex((line) => EXIT_LINE.test(line));
33
+ if (end === -1) end = all.findLastIndex((line) => line.startsWith("##[error]"));
34
+ if (end === -1) return { reason: NO_REASON, lines: [] };
35
+
36
+ const start = all.findLastIndex(
37
+ (line, index) => index < end && line.startsWith("##[group]Run"),
38
+ );
39
+ const headerEnd = all.findIndex((line, index) => index > start && line === "##[endgroup]");
40
+ const inclusive = EXIT_LINE.test(all[end]) ? end : end + 1;
41
+ const body = all.slice(headerEnd === -1 ? start + 1 : headerEnd + 1, inclusive);
42
+
43
+ const marked = body.find((line) => line.startsWith("##[error]"));
44
+ const lines = body
45
+ .filter((line) => !/^##\[(?!error\])/.test(line))
46
+ .map((line) => line.replace(/^##\[error\]/, ""));
47
+
48
+ return {
49
+ reason: marked?.slice("##[error]".length) ?? pickReason(lines) ?? NO_REASON,
50
+ lines: lines.slice(-MAX_LINES),
51
+ };
52
+ }
53
+
54
+ /** The line most likely to say what went wrong. A step's last line is often a stack or totals. */
55
+ function pickReason(lines) {
56
+ const candidates = lines.filter(
57
+ (line) =>
58
+ /[a-z]/i.test(line) &&
59
+ !NOISE.test(line) &&
60
+ !STACK_FRAME.test(line) &&
61
+ !TEST_TOTALS.test(line),
62
+ );
63
+ const best =
64
+ candidates.findLast((line) => NAMED_ERROR.test(line)) ??
65
+ candidates.findLast((line) => FILE_POSITION.test(line)) ??
66
+ candidates.at(-1);
67
+ return best?.trim();
68
+ }
69
+
70
+ function encodeData(text) {
71
+ return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
72
+ }
73
+
74
+ function encodeProperty(text) {
75
+ return encodeData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
76
+ }
77
+
78
+ /** A one-line annotation. Encoded, so text from a log can't act as a workflow command. */
79
+ export function errorCommand(title, reason) {
80
+ return `::error title=${encodeProperty(title)}::${encodeData(reason.slice(0, MAX_REASON))}`;
81
+ }
82
+
83
+ export function formatSummary(failures) {
84
+ const sections = failures.map(({ job, step, url, reason, lines }) => {
85
+ const longestFence = Math.max(
86
+ 2,
87
+ ...lines.map((line) => /^`+/.exec(line)?.[0].length ?? 0),
88
+ );
89
+ const fence = "`".repeat(longestFence + 1);
90
+ return [
91
+ `### ${job} / ${step}`,
92
+ "",
93
+ reason,
94
+ "",
95
+ `[Full log](${url})`,
96
+ "",
97
+ fence,
98
+ ...lines,
99
+ fence,
100
+ "",
101
+ ].join("\n");
102
+ });
103
+ return ["## Why this run failed", "", ...sections].join("\n");
104
+ }
105
+
106
+ async function github(path, token, accept = "application/vnd.github+json") {
107
+ const response = await fetch(`https://api.github.com/${path}`, {
108
+ headers: { authorization: `Bearer ${token}`, accept },
109
+ });
110
+ if (!response.ok) throw new Error(`GET ${path}: ${response.status} ${response.statusText}`);
111
+ return response;
112
+ }
113
+
114
+ async function failedJobs(repo, runId, attempt, token) {
115
+ const jobs = [];
116
+ for (let page = 1; ; page++) {
117
+ const response = await github(
118
+ `repos/${repo}/actions/runs/${runId}/attempts/${attempt}/jobs?per_page=100&page=${page}`,
119
+ token,
120
+ );
121
+ const batch = (await response.json()).jobs;
122
+ jobs.push(...batch);
123
+ if (batch.length < 100) break;
124
+ }
125
+ return jobs.filter((job) => job.conclusion === "failure");
126
+ }
127
+
128
+ async function main() {
129
+ const {
130
+ GITHUB_REPOSITORY: repo,
131
+ GITHUB_RUN_ID: runId,
132
+ GITHUB_RUN_ATTEMPT: attempt = "1",
133
+ GH_TOKEN: token,
134
+ GITHUB_STEP_SUMMARY: summaryPath,
135
+ } = process.env;
136
+ if (!repo || !runId || !token) {
137
+ throw new Error("GITHUB_REPOSITORY, GITHUB_RUN_ID and GH_TOKEN are required");
138
+ }
139
+
140
+ const failures = [];
141
+ for (const job of await failedJobs(repo, runId, attempt, token)) {
142
+ const step = job.steps?.find((candidate) => candidate.conclusion === "failure")?.name;
143
+ const response = await github(`repos/${repo}/actions/jobs/${job.id}/logs`, token);
144
+ const { reason, lines } = extractFailure(await response.text());
145
+ failures.push({
146
+ job: job.name,
147
+ step: step ?? "unknown step",
148
+ url: job.html_url,
149
+ reason,
150
+ lines,
151
+ });
152
+ }
153
+
154
+ for (const { job, step, reason } of failures) {
155
+ console.log(errorCommand(`${job} / ${step}`, reason));
156
+ }
157
+ if (summaryPath && failures.length > 0) appendFileSync(summaryPath, formatSummary(failures));
158
+ }
159
+
160
+ if (process.argv[1] != null && import.meta.url === pathToFileURL(process.argv[1]).href) {
161
+ await main();
162
+ }
@@ -43,6 +43,13 @@ export function promoteLatest(options) {
43
43
  }
44
44
  log(`Promoting ${version} to latest on ${packages.length} packages`);
45
45
 
46
+ // Once a stable version is out, latest belongs to releases: see "Stable release" in docs/ci.
47
+ const current = /^latest: (.+)$/m.exec(npm("dist-tag", "ls", LAUNCH_PACKAGE).stdout)?.[1];
48
+ if (current != null && !current.includes("-") && version.includes("-")) {
49
+ log(`latest is the stable ${current}, and ${version} is a dev version. Nothing moved.`);
50
+ return 1;
51
+ }
52
+
46
53
  const tag = run("git", ["tag", "--list", `v${version}`]).stdout.trim();
47
54
  if (tag !== `v${version}`) {
48
55
  log(`${version} has no v${version} tag, so it didn't fully ship. Nothing moved.`);
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Gives a tested build its stable version by changing only `package.json` files, so what ships is
4
+ * the code that was tested. Refuses if the old version appears anywhere else, because then the
5
+ * version couldn't be changed in those files alone. Checks everything before writing anything.
6
+ *
7
+ * node restamp-build.mjs <build dir> <tested version> <stable version>
8
+ */
9
+ import { readdirSync, readFileSync, writeFileSync } from "node:fs";
10
+ import { join, relative, resolve } from "node:path";
11
+ import { pathToFileURL } from "node:url";
12
+
13
+ const STABLE = /^\d+\.\d+\.\d+$/;
14
+ const DEPENDENCY_KEYS = [
15
+ "dependencies",
16
+ "devDependencies",
17
+ "peerDependencies",
18
+ "optionalDependencies",
19
+ ];
20
+
21
+ function listFiles(dir) {
22
+ return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
23
+ const path = join(dir, entry.name);
24
+ return entry.isDirectory() ? listFiles(path) : [path];
25
+ });
26
+ }
27
+
28
+ /**
29
+ * @param {{ dir: string, from: string, to: string }} options
30
+ * @returns {{ packages: string[] }}
31
+ */
32
+ export function restampBuild({ dir, from, to }) {
33
+ if (!STABLE.test(to)) {
34
+ throw new Error(`${to} isn't a stable version: expected major.minor.patch`);
35
+ }
36
+
37
+ const files = listFiles(dir);
38
+ const stamped = [];
39
+ const elsewhere = [];
40
+ for (const file of files) {
41
+ const text = readFileSync(file, "utf8");
42
+ if (file.endsWith("/package.json")) {
43
+ const manifest = JSON.parse(text);
44
+ if (manifest.version !== from) {
45
+ throw new Error(`${manifest.name} is at ${manifest.version}, not ${from}`);
46
+ }
47
+ manifest.version = to;
48
+ for (const key of DEPENDENCY_KEYS) {
49
+ for (const name of Object.keys(manifest[key] ?? {})) {
50
+ if (name.startsWith("@decohere/") && manifest[key][name] === from) {
51
+ manifest[key][name] = to;
52
+ }
53
+ }
54
+ }
55
+ const output = `${JSON.stringify(manifest, null, 2)}\n`;
56
+ if (output.includes(from)) elsewhere.push(relative(dir, file));
57
+ stamped.push({ file, name: manifest.name, output });
58
+ } else if (text.includes(from)) {
59
+ elsewhere.push(relative(dir, file));
60
+ }
61
+ }
62
+
63
+ if (stamped.length === 0) {
64
+ throw new Error(`No packages in ${dir}`);
65
+ }
66
+ if (elsewhere.length > 0) {
67
+ throw new Error(`${from} also appears in: ${elsewhere.join(", ")}`);
68
+ }
69
+
70
+ for (const { file, output } of stamped) {
71
+ writeFileSync(file, output);
72
+ }
73
+ return { packages: stamped.map((item) => item.name).sort() };
74
+ }
75
+
76
+ if (
77
+ process.argv[1] != null &&
78
+ import.meta.url === pathToFileURL(resolve(process.argv[1])).href
79
+ ) {
80
+ const [dir, from, to] = process.argv.slice(2);
81
+ if (!dir || !from || !to) {
82
+ console.error("Usage: restamp-build.mjs <build dir> <tested version> <stable version>");
83
+ process.exit(1);
84
+ }
85
+ const { packages } = restampBuild({ dir, from, to });
86
+ console.log(`${packages.length} packages now at ${to}`);
87
+ }
@@ -0,0 +1,240 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import {
4
+ errorCommand,
5
+ extractFailure,
6
+ formatSummary,
7
+ } from "../../src/github/failure-report.mjs";
8
+
9
+ const fs = await vi.importActual("node:fs");
10
+ const WORKFLOWS = fileURLToPath(new URL("../../../../.github/workflows", import.meta.url));
11
+
12
+ const stamp = "2026-11-06T09:12:21.3065221Z ";
13
+ const log = (...lines) => lines.map((line) => `${stamp}${line}`).join("\n");
14
+
15
+ // Shape and text taken from a real failed Promote to latest run.
16
+ const PROMOTE = log(
17
+ "##[group]Run npm install -g npm@12.2.2",
18
+ "npm install -g npm@12.2.2",
19
+ "##[endgroup]",
20
+ "removed 8 packages, and changed 77 packages in 4s",
21
+ "##[group]Run node projects/ci-tools/src/release/promote-latest.mjs",
22
+ "node projects/ci-tools/src/release/promote-latest.mjs",
23
+ "env:",
24
+ " PROMOTE_VERSION: 0.0.1-dev.20261105.119",
25
+ "##[endgroup]",
26
+ "Promoting 0.0.1-dev.20261105.119 to latest on 18 packages",
27
+ "0.0.1-dev.20261105.119 is 12.8 hours old. Wait, or run with allow_young. Nothing moved.",
28
+ "##[error]Process completed with exit code 1.",
29
+ "Post job cleanup.",
30
+ "[command]/usr/bin/git version",
31
+ "##[warning]Node.js 20 is deprecated.",
32
+ );
33
+
34
+ // Shape and text taken from a real failed test shard, colours included.
35
+ const TESTS = log(
36
+ "##[group]Run pnpm test --shard=2/3",
37
+ "pnpm test --shard=2/3",
38
+ "##[endgroup]",
39
+ "\u001b[33m✓\u001b[39m flux serve > keeps running",
40
+ "##[error]Error: forge build failed:",
41
+ "\u001b[2m Test Files \u001b[22m \u001b[31m1 failed\u001b[39m | 65 passed (66)",
42
+ " ELIFECYCLE Test failed. See above for more details.",
43
+ "##[error]Process completed with exit code 1.",
44
+ "Post job cleanup.",
45
+ "##[start-action display=Run actions/setup-node@v4;id=x]",
46
+ );
47
+
48
+ describe("extractFailure", () => {
49
+ it("finds the reason a script printed before exiting", () => {
50
+ const { reason, lines } = extractFailure(PROMOTE);
51
+
52
+ expect(reason).toBe(
53
+ "0.0.1-dev.20261105.119 is 12.8 hours old. Wait, or run with allow_young. Nothing moved.",
54
+ );
55
+ expect(lines).toContain("Promoting 0.0.1-dev.20261105.119 to latest on 18 packages");
56
+ });
57
+
58
+ it("keeps only the failed step, not earlier steps, its env block or the cleanup", () => {
59
+ const { lines } = extractFailure(PROMOTE);
60
+ const text = lines.join("\n");
61
+
62
+ expect(text).not.toContain("removed 8 packages");
63
+ expect(text).not.toContain("PROMOTE_VERSION");
64
+ expect(text).not.toContain("Post job cleanup");
65
+ expect(text).not.toContain("Process completed");
66
+ });
67
+
68
+ it("prefers an error the tool marked over the last line, and removes colours", () => {
69
+ const { reason, lines } = extractFailure(TESTS);
70
+
71
+ expect(reason).toBe("Error: forge build failed:");
72
+ expect(lines).toContain(" Test Files 1 failed | 65 passed (66)");
73
+ expect(lines.join("\n")).not.toContain("\u001b");
74
+ expect(lines.join("\n")).not.toContain("##[");
75
+ });
76
+
77
+ it("skips package manager noise when choosing the last line", () => {
78
+ const { reason } = extractFailure(
79
+ log(
80
+ "##[group]Run pnpm build",
81
+ "##[endgroup]",
82
+ "TS2307: Cannot find module",
83
+ " ELIFECYCLE Command failed with exit code 2.",
84
+ "npm error A complete log of this run can be found in: /tmp/x.log",
85
+ "##[error]Process completed with exit code 2.",
86
+ ),
87
+ );
88
+
89
+ expect(reason).toBe("TS2307: Cannot find module");
90
+ });
91
+
92
+ it("finds the error above a stack trace (real Docker failure)", () => {
93
+ const { reason } = extractFailure(
94
+ log(
95
+ "##[group]Run forge docker",
96
+ "##[endgroup]",
97
+ "#12 ERROR: process exited",
98
+ "Dockerfile:27",
99
+ "ERROR: failed to build: failed to solve: exit code: 1",
100
+ " at DockerCommand.run (command.ts:72:8)",
101
+ " }",
102
+ "##[error]Process completed with exit code 1.",
103
+ ),
104
+ );
105
+
106
+ expect(reason).toBe("ERROR: failed to build: failed to solve: exit code: 1");
107
+ });
108
+
109
+ it("finds the error above the test totals (real unhandled Vitest error)", () => {
110
+ const { reason } = extractFailure(
111
+ log(
112
+ "##[group]Run pnpm e2e",
113
+ "##[endgroup]",
114
+ 'Error: [vitest-worker]: Timeout calling "onTaskUpdate"',
115
+ " ❯ Object.onTimeoutError node_modules/vitest/rpc.js:53:10",
116
+ " Test Files 1 passed (1)",
117
+ " Errors 1 error",
118
+ " Duration 90.38s (transform 73ms)",
119
+ "##[error]Process completed with exit code 1.",
120
+ ),
121
+ );
122
+
123
+ expect(reason).toBe('Error: [vitest-worker]: Timeout calling "onTaskUpdate"');
124
+ });
125
+
126
+ it("finds the file and line above a code excerpt (real lint failure)", () => {
127
+ const { reason } = extractFailure(
128
+ log(
129
+ "##[group]Run actionlint",
130
+ "##[endgroup]",
131
+ ".github/workflows/publish.yml:415:9: shellcheck reported issue: SC2016",
132
+ " |",
133
+ "415 | run: |",
134
+ " | ^~~~",
135
+ "##[error]Process completed with exit code 1.",
136
+ ),
137
+ );
138
+
139
+ expect(reason).toBe(
140
+ ".github/workflows/publish.yml:415:9: shellcheck reported issue: SC2016",
141
+ );
142
+ });
143
+
144
+ it("handles an action that failed with its own error and no exit code line", () => {
145
+ const { reason } = extractFailure(
146
+ log(
147
+ "##[group]Run actions/upload-artifact@v4",
148
+ "##[endgroup]",
149
+ "##[error]No files found",
150
+ ),
151
+ );
152
+
153
+ expect(reason).toBe("No files found");
154
+ });
155
+
156
+ it("keeps the last lines of a long log", () => {
157
+ const many = Array.from({ length: 100 }, (_, index) => `line ${index}`);
158
+ const { lines } = extractFailure(
159
+ log(
160
+ "##[group]Run x",
161
+ "##[endgroup]",
162
+ ...many,
163
+ "##[error]Process completed with exit code 1.",
164
+ ),
165
+ );
166
+
167
+ expect(lines).toHaveLength(40);
168
+ expect(lines.at(-1)).toBe("line 99");
169
+ });
170
+
171
+ it("says so when a log has nothing to go on", () => {
172
+ expect(extractFailure("").reason).toBe("No reason found in the log.");
173
+ });
174
+ });
175
+
176
+ describe("errorCommand", () => {
177
+ it("encodes text so a log line can't act as a workflow command", () => {
178
+ const command = errorCommand("a, b: c", "bad\n::set-env name=X::1 100%");
179
+
180
+ expect(command).toBe("::error title=a%2C b%3A c::bad%0A::set-env name=X::1 100%25");
181
+ expect(command.split("\n")).toHaveLength(1);
182
+ });
183
+
184
+ it("shortens a very long reason", () => {
185
+ expect(errorCommand("job", "x".repeat(5000)).length).toBeLessThan(1100);
186
+ });
187
+ });
188
+
189
+ describe("formatSummary", () => {
190
+ const failure = {
191
+ job: "promote",
192
+ step: "Move latest",
193
+ url: "https://x/1",
194
+ reason: "Too new",
195
+ };
196
+
197
+ it("names the job and step, gives the reason and links the log", () => {
198
+ const summary = formatSummary([{ ...failure, lines: ["a", "b"] }]);
199
+
200
+ expect(summary).toContain("### promote / Move latest");
201
+ expect(summary).toContain("Too new");
202
+ expect(summary).toContain("https://x/1");
203
+ expect(summary).toContain("a\nb");
204
+ });
205
+
206
+ it("uses a code fence the log can't close", () => {
207
+ const summary = formatSummary([{ ...failure, lines: ["```", "pretend heading"] }]);
208
+
209
+ expect(summary).toContain("````\n```\npretend heading\n````");
210
+ });
211
+ });
212
+
213
+ describe("every workflow reports its failures", () => {
214
+ const workflows = fs.readdirSync(WORKFLOWS).filter((name) => name.endsWith(".yml"));
215
+
216
+ it.each(workflows.filter((name) => name !== "ci.yml"))(
217
+ "%s has a failure-report job that waits for every other job",
218
+ (name) => {
219
+ const yml = fs.readFileSync(`${WORKFLOWS}/${name}`, "utf8");
220
+ const jobs = yml.slice(yml.indexOf("\njobs:"));
221
+ const names = [...jobs.matchAll(/^ {2}([a-zA-Z0-9_-]+):$/gm)].map((match) => match[1]);
222
+ const needs = /^ {2}failure-report:\n {4}needs: \[(.+)\]$/m.exec(jobs)?.[1].split(", ");
223
+
224
+ expect(needs?.toSorted()).toEqual(
225
+ names.filter((job) => job !== "failure-report").toSorted(),
226
+ );
227
+ expect(jobs).toContain("uses: ./.github/actions/failure-report");
228
+ },
229
+ );
230
+
231
+ it("ci.yml reports from its required job, without changing that job's result", () => {
232
+ const yml = fs.readFileSync(`${WORKFLOWS}/ci.yml`, "utf8");
233
+ const ci = yml.slice(yml.indexOf("\n ci:\n"));
234
+
235
+ expect(ci).toMatch(
236
+ /uses: \.\/\.github\/actions\/failure-report\n\s+if: contains\(needs\.\*\.result, 'failure'\)\n\s+continue-on-error: true/,
237
+ );
238
+ expect(ci).toContain("actions: read");
239
+ });
240
+ });
@@ -0,0 +1,68 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { describe, expect, it, vi } from "vitest";
3
+
4
+ const fs = await vi.importActual("node:fs");
5
+ const PUBLISH_YML = fileURLToPath(
6
+ new URL("../../../../.github/workflows/publish.yml", import.meta.url),
7
+ );
8
+
9
+ describe("publish.yml", () => {
10
+ const yml = fs.readFileSync(PUBLISH_YML, "utf8");
11
+
12
+ it("defaults a stable release to a dry run", () => {
13
+ const dryRun = yml.slice(yml.indexOf("dry_run:"), yml.indexOf("\nconcurrency:"));
14
+
15
+ expect(dryRun).toMatch(/default: true/);
16
+ });
17
+
18
+ it("stops a stable release before publishing on a dry run, or if its checks fail", () => {
19
+ const verify = yml.slice(yml.indexOf("\n verify:"), yml.indexOf("\n npm:"));
20
+
21
+ expect(verify).toContain("inputs.dry_run != true");
22
+ expect(verify).toContain("needs.release-build.result == 'success'");
23
+ });
24
+
25
+ it("ships a stable release from the tested build, without rebuilding it", () => {
26
+ const build = yml.slice(yml.indexOf("\n release-build:"), yml.indexOf("\n verify:"));
27
+
28
+ expect(build).toContain("forge-release-beta-");
29
+ expect(build).toContain("restamp-build.mjs");
30
+ expect(build).not.toContain("forge --log-level info build");
31
+ });
32
+
33
+ it("only tags a version once npm and the images have shipped", () => {
34
+ const tag = yml.slice(yml.indexOf("\n tag:"));
35
+ const needs = /needs: \[(.+)\]/.exec(tag)?.[1].split(", ");
36
+
37
+ expect(needs).toContain("npm");
38
+ expect(needs).toContain("check-images");
39
+ });
40
+
41
+ it("checks the images with no login, as a stranger would", () => {
42
+ const job = yml.slice(yml.indexOf("\n check-images:"), yml.indexOf("\n tag:"));
43
+
44
+ expect(job).toContain("permissions: {}");
45
+ expect(job).toContain("docker manifest inspect");
46
+ expect(job).not.toContain("docker login");
47
+ });
48
+
49
+ it("reads npm without a token: the packages are public and publishing is OIDC", () => {
50
+ const workflows = fileURLToPath(new URL("../../../../.github/", import.meta.url));
51
+ const files = fs
52
+ .readdirSync(workflows, { recursive: true })
53
+ .filter((file) => /\.ya?ml$/.test(file));
54
+ expect(files.length).toBeGreaterThan(0);
55
+ for (const file of files) {
56
+ expect(fs.readFileSync(`${workflows}${file}`, "utf8"), file).not.toMatch(
57
+ /NODE_AUTH_TOKEN|_authToken/,
58
+ );
59
+ }
60
+ });
61
+
62
+ it("grants the tag job actions:read so publish-prepare can download CI artifacts", () => {
63
+ const tag = yml.slice(yml.indexOf("\n tag:"));
64
+ const perms = tag.slice(tag.indexOf("permissions:"), tag.indexOf("steps:"));
65
+ expect(perms).toMatch(/contents:\s*write/);
66
+ expect(perms).toMatch(/actions:\s*read/);
67
+ });
68
+ });
@@ -1,143 +0,0 @@
1
- import { execSync } from "node:child_process";
2
- import { readFileSync } from "node:fs";
3
-
4
- function getArg(name, fallback) {
5
- const equals = process.argv.find((arg) => arg.startsWith(`--${name}=`));
6
- if (equals) return equals.slice(name.length + 3);
7
- const index = process.argv.indexOf(`--${name}`);
8
- if (index >= 0) return process.argv[index + 1];
9
- return fallback;
10
- }
11
-
12
- function hasFlag(name) {
13
- return process.argv.includes(`--${name}`);
14
- }
15
-
16
- function run(command) {
17
- return execSync(command, { encoding: "utf8" }).trim();
18
- }
19
-
20
- function parseRepoFromRemoteUrl(url) {
21
- const sshMatch = url.match(/github\.com:([^/]+)\/([^/.]+)(?:\.git)?$/);
22
- if (sshMatch) return { owner: sshMatch[1], repo: sshMatch[2] };
23
- const httpsMatch = url.match(/github\.com\/([^/]+)\/([^/.]+)(?:\.git)?$/);
24
- if (httpsMatch) return { owner: httpsMatch[1], repo: httpsMatch[2] };
25
- throw new Error(`Unable to parse GitHub owner/repo from remote URL: ${url}`);
26
- }
27
-
28
- async function request(apiBaseUrl, token, method, path, body) {
29
- const response = await fetch(`${apiBaseUrl}${path}`, {
30
- method,
31
- headers: {
32
- Authorization: `Bearer ${token}`,
33
- Accept: "application/vnd.github+json",
34
- "X-GitHub-Api-Version": "2022-11-28",
35
- "Content-Type": "application/json",
36
- },
37
- body: body == null ? undefined : JSON.stringify(body),
38
- });
39
- if (!response.ok) {
40
- const message = await response.text();
41
- throw new Error(`GitHub API ${method} ${path} failed (${response.status}): ${message}`);
42
- }
43
- return response.status === 204 ? undefined : response.json();
44
- }
45
-
46
- if (hasFlag("help")) {
47
- console.log(`Usage: node create-or-update-pr.mjs [options]
48
-
49
- Required:
50
- --title <title> PR title
51
- --body <markdown> PR body markdown
52
- or
53
- --body-file <path> Read PR body markdown from file
54
-
55
- Optional:
56
- --base <branch> Base branch (default: main)
57
- --head <branch> Head branch (default: current git branch)
58
- --draft Create as draft
59
- --labels <a,b,c> Apply labels
60
- --reviewers <u1,u2> Request reviewers
61
- --dry-run Print intended operation without calling API
62
- --help Show this help
63
-
64
- Auth: Set GITHUB_TOKEN or GH_TOKEN
65
- `);
66
- process.exit(0);
67
- }
68
-
69
- const token = process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN;
70
- if (token == null || token.length === 0) throw new Error("Missing GITHUB_TOKEN or GH_TOKEN");
71
-
72
- const apiBaseUrl = process.env.GITHUB_API_URL ?? "https://api.github.com";
73
- const base = getArg("base", "main");
74
- const head = getArg("head", run("git rev-parse --abbrev-ref HEAD"));
75
- const title = getArg("title");
76
- const bodyFile = getArg("body-file");
77
- const body = getArg("body", bodyFile ? readFileSync(bodyFile, "utf8") : undefined);
78
- const draft = hasFlag("draft");
79
- const dryRun = hasFlag("dry-run");
80
- const labels = (getArg("labels") ?? "")
81
- .split(",")
82
- .map((v) => v.trim())
83
- .filter(Boolean);
84
- const reviewers = (getArg("reviewers") ?? "")
85
- .split(",")
86
- .map((v) => v.trim())
87
- .filter(Boolean);
88
-
89
- if (!title) throw new Error("Missing required argument: --title");
90
- if (!body) throw new Error("Missing required argument: --body or --body-file");
91
-
92
- const remoteUrl = run("git config --get remote.origin.url");
93
- const parsedRepo = parseRepoFromRemoteUrl(remoteUrl);
94
- const owner = getArg("owner", parsedRepo.owner);
95
- const repo = getArg("repo", parsedRepo.repo);
96
-
97
- const findPath = `/repos/${owner}/${repo}/pulls?state=open&head=${encodeURIComponent(`${owner}:${head}`)}&base=${encodeURIComponent(base)}`;
98
-
99
- if (dryRun) {
100
- console.log(
101
- JSON.stringify({ owner, repo, base, head, title, draft, labels, reviewers }, null, 2),
102
- );
103
- process.exit(0);
104
- }
105
-
106
- const existing = await request(apiBaseUrl, token, "GET", findPath);
107
- const pr = existing[0]
108
- ? await request(
109
- apiBaseUrl,
110
- token,
111
- "PATCH",
112
- `/repos/${owner}/${repo}/pulls/${existing[0].number}`,
113
- { title, body, base },
114
- )
115
- : await request(apiBaseUrl, token, "POST", `/repos/${owner}/${repo}/pulls`, {
116
- title,
117
- body,
118
- base,
119
- head,
120
- draft,
121
- });
122
-
123
- if (labels.length > 0) {
124
- await request(
125
- apiBaseUrl,
126
- token,
127
- "POST",
128
- `/repos/${owner}/${repo}/issues/${pr.number}/labels`,
129
- { labels },
130
- );
131
- }
132
-
133
- if (reviewers.length > 0) {
134
- await request(
135
- apiBaseUrl,
136
- token,
137
- "POST",
138
- `/repos/${owner}/${repo}/pulls/${pr.number}/requested_reviewers`,
139
- { reviewers },
140
- );
141
- }
142
-
143
- console.log(`${existing[0] ? "Updated" : "Created"} PR #${pr.number}: ${pr.html_url}`);
@@ -1,85 +0,0 @@
1
- #!/usr/bin/env node
2
- import { spawnSync } from "node:child_process";
3
- import { mkdtempSync, writeFileSync } from "node:fs";
4
- import { tmpdir } from "node:os";
5
- import { join, resolve } from "node:path";
6
- import { pathToFileURL } from "node:url";
7
-
8
- /**
9
- * Run `npx @decohere/create@version --help` from an empty temp directory.
10
- * Must not use the repo cwd — after `@decohere/cli` is pinned, pnpm installs
11
- * `@decohere/create` into the workspace tree and npm's npx misses the bin.
12
- * No token: the packages are public, so this installs the launcher the way a user does.
13
- * Anonymous reads go through npm's cache, which can lag minutes behind a publish, so it
14
- * first waits until npm serves the version (`attempts` tries, `intervalMs` apart).
15
- *
16
- * @param {{
17
- * createVersion: string,
18
- * attempts?: number,
19
- * intervalMs?: number,
20
- * sleep?: (ms: number) => void,
21
- * mkdtempSync?: typeof mkdtempSync,
22
- * writeFileSync?: typeof writeFileSync,
23
- * spawnSync?: typeof spawnSync,
24
- * tmpdir?: () => string,
25
- * stdio?: import("node:child_process").StdioOptions,
26
- * }} options
27
- */
28
- export function runCreateLauncherSmoke(options) {
29
- const version = options.createVersion?.trim() ?? "";
30
- if (!version) {
31
- throw new Error("CREATE_VERSION is required");
32
- }
33
-
34
- const mkdtemp = options.mkdtempSync ?? mkdtempSync;
35
- const writeFile = options.writeFileSync ?? writeFileSync;
36
- const spawn = options.spawnSync ?? spawnSync;
37
- const tmp = options.tmpdir ?? tmpdir;
38
- const sleep = options.sleep ?? sleepSync;
39
- const attempts = options.attempts ?? 20;
40
- const intervalMs = options.intervalMs ?? 15_000;
41
-
42
- const workDir = mkdtemp(join(tmp(), "decohere-create-smoke-"));
43
- const npmrc = join(workDir, ".npmrc");
44
- writeFile(npmrc, "@decohere:registry=https://registry.npmjs.org/\n");
45
-
46
- const env = { ...process.env, NPM_CONFIG_USERCONFIG: npmrc };
47
- const spec = `@decohere/create@${version}`;
48
-
49
- for (let attempt = 1; ; attempt++) {
50
- const view = spawn("npm", ["view", spec, "version"], { cwd: workDir, env, stdio: "pipe" });
51
- if (view.status === 0) break;
52
- if (attempt >= attempts) {
53
- console.error(`${spec} is still not served by npm after ${attempts} tries`);
54
- return { workDir, status: 1 };
55
- }
56
- console.log(`${spec} not served by npm yet (try ${attempt} of ${attempts}); waiting`);
57
- sleep(intervalMs);
58
- }
59
-
60
- const result = spawn("npx", ["--yes", spec, "--help"], {
61
- cwd: workDir,
62
- env,
63
- stdio: options.stdio ?? "inherit",
64
- });
65
-
66
- return {
67
- workDir,
68
- status: result.status ?? 1,
69
- };
70
- }
71
-
72
- /** @param {number} ms */
73
- function sleepSync(ms) {
74
- Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
75
- }
76
-
77
- const isCli =
78
- process.argv[1] != null && import.meta.url === pathToFileURL(resolve(process.argv[1])).href;
79
-
80
- if (isCli) {
81
- const { status } = runCreateLauncherSmoke({
82
- createVersion: process.env.CREATE_VERSION ?? "",
83
- });
84
- process.exit(status);
85
- }
@@ -1,27 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Capture final version metadata from package.json
4
- * Outputs version and is_prerelease flags for GitHub Actions
5
- */
6
-
7
- import { readFileSync } from "node:fs";
8
- import { join } from "node:path";
9
-
10
- try {
11
- const packageJson = JSON.parse(readFileSync(join(process.cwd(), "package.json"), "utf8"));
12
- const version = packageJson.version;
13
-
14
- if (!version) {
15
- console.error("No version found in package.json");
16
- process.exit(1);
17
- }
18
-
19
- console.log(`version=${version}`);
20
-
21
- // Check if prerelease (has hyphen in version)
22
- const isPrerelease = version.includes("-");
23
- console.log(`is_prerelease=${isPrerelease}`);
24
- } catch (e) {
25
- console.error(`Failed to capture version: ${e.message}`);
26
- process.exit(1);
27
- }
@@ -1,32 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Generate npm package links for all @decohere packages
4
- * Outputs markdown formatted links for GitHub release notes
5
- */
6
-
7
- import { readFileSync } from "node:fs";
8
- import { join } from "node:path";
9
- import { listProjectDirs } from "../lib/workspace-packages.mjs";
10
-
11
- const VERSION = process.argv[2] || process.env.VERSION;
12
-
13
- if (!VERSION) {
14
- console.error("VERSION not provided as argument or env var");
15
- process.exit(1);
16
- }
17
-
18
- let links = "## Published Packages\n\n";
19
-
20
- try {
21
- for (const projectDir of listProjectDirs(process.cwd())) {
22
- const pkg = JSON.parse(readFileSync(join(projectDir, "package.json"), "utf8"));
23
- if (pkg.name?.startsWith("@decohere/")) {
24
- links += `- [\`${pkg.name}@${VERSION}\`](https://www.npmjs.com/package/${pkg.name}/v/${VERSION})\n`;
25
- }
26
- }
27
-
28
- console.log(links);
29
- } catch (e) {
30
- console.error(`Failed to generate npm links: ${e.message}`);
31
- process.exit(1);
32
- }
@@ -1,85 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Publish prerelease version commit via PR
4
- * Validates working tree, pushes prerelease commit to a release branch,
5
- * and creates/updates a PR targeting the protected base branch.
6
- */
7
-
8
- import { execSync } from "node:child_process";
9
-
10
- function run(command) {
11
- return execSync(command, { encoding: "utf8" }).trim();
12
- }
13
-
14
- function runInherit(command, env) {
15
- execSync(command, { stdio: "inherit", env });
16
- }
17
-
18
- try {
19
- // Check if working tree is dirty
20
- const status = run("git status --porcelain");
21
- if (status.trim().length > 0) {
22
- console.error(
23
- "::error::Working tree is dirty after version/dependency sync; refusing to publish with uncommitted changes.",
24
- );
25
- console.log(status);
26
- process.exit(1);
27
- }
28
-
29
- // Compare HEAD with origin and create a PR branch targeting an explicit base branch.
30
- const head = run("git rev-parse HEAD");
31
- const sourceBranch = process.env.GITHUB_REF_NAME || run("git rev-parse --abbrev-ref HEAD");
32
- const channel = process.env.RELEASE_CHANNEL ?? "beta";
33
- const baseBranch = process.env.RELEASE_BASE_BRANCH || sourceBranch;
34
- const releasePrBranch = `release/${channel}-${sourceBranch}-to-${baseBranch}`;
35
- const token = process.env.GITHUB_TOKEN ?? process.env.GH_TOKEN;
36
-
37
- try {
38
- const origin = run(`git rev-parse origin/${sourceBranch}`);
39
-
40
- if (head !== origin) {
41
- console.log(
42
- `Publishing release commit via PR branch origin/${releasePrBranch} (base: origin/${baseBranch})`,
43
- );
44
- runInherit(`git push origin HEAD:${releasePrBranch} --force`, process.env);
45
-
46
- if (token == null || token.length === 0) {
47
- throw new Error("Missing GITHUB_TOKEN/GH_TOKEN for PR upsert");
48
- }
49
-
50
- const version = process.env.RELEASE_VERSION ?? "(unknown)";
51
- const ownerRepo = run("git config --get remote.origin.url")
52
- .replace(/^.*github\.com[:/]/, "")
53
- .replace(/\.git$/, "");
54
-
55
- runInherit(
56
- [
57
- "node projects/ci-tools/src/github/create-or-update-pr.mjs",
58
- `--title "chore(release): ${channel} ${version}"`,
59
- `--base "${baseBranch}"`,
60
- `--head "${releasePrBranch}"`,
61
- `--body "Automated ${channel} version bump and dependency sync.\\n\\n- Channel: ${channel}\\n- Version: ${version}\\n- Source branch: ${sourceBranch}\\n- Base branch: ${baseBranch}\\n- Source commit: ${head}\\n\\nGenerated by pull-request workflow."`,
62
- `--owner "${ownerRepo.split("/")[0]}"`,
63
- `--repo "${ownerRepo.split("/")[1]}"`,
64
- ].join(" "),
65
- { ...process.env, GITHUB_TOKEN: token },
66
- );
67
- } else {
68
- console.log("No prerelease version commit to push.");
69
- }
70
- } catch (err) {
71
- const message = err?.message ?? "";
72
- if (
73
- message.includes(`origin/${sourceBranch}`) &&
74
- message.includes("Needed a single revision")
75
- ) {
76
- console.log(`Remote branch origin/${sourceBranch} not found, skipping push.`);
77
- } else {
78
- console.error("::error::Failed to publish release commit via PR branch.");
79
- throw err;
80
- }
81
- }
82
- } catch (e) {
83
- console.error(`Push failed: ${e.message}`);
84
- process.exit(1);
85
- }
@@ -1,26 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Sync cross-library dependency versions
4
- * Conditionally runs sync with or without commit based on DRY_RUN flag
5
- */
6
-
7
- import { execSync } from "node:child_process";
8
-
9
- const DRY_RUN = process.env.DRY_RUN === "true";
10
- const DECOHERE_CONFIG = process.env.DECOHERE_CONFIG || ".decohere.js";
11
-
12
- const baseCmd = `pnpm forge --log-level info --config-file "${DECOHERE_CONFIG}" dependencies --configuration release sync`;
13
-
14
- try {
15
- if (DRY_RUN) {
16
- console.log(`[DRY RUN] ${baseCmd}`);
17
- execSync(baseCmd, { stdio: "inherit" });
18
- } else {
19
- const commitCmd = `${baseCmd} --commit --message "chore: version packages and sync cross-library dependencies [skip ci]"`;
20
- console.log(`Syncing with commit...`);
21
- execSync(commitCmd, { stdio: "inherit" });
22
- }
23
- } catch (e) {
24
- console.error(`Dependency sync failed: ${e.message}`);
25
- process.exit(1);
26
- }
@@ -1,132 +0,0 @@
1
- import { fileURLToPath } from "node:url";
2
- import { describe, expect, it, vi } from "vitest";
3
- import { runCreateLauncherSmoke } from "../../src/github/smoke-create-launcher.mjs";
4
-
5
- const fs = await vi.importActual("node:fs");
6
- const PUBLISH_YML = fileURLToPath(
7
- new URL("../../../../.github/workflows/publish.yml", import.meta.url),
8
- );
9
-
10
- describe("runCreateLauncherSmoke", () => {
11
- it("execs npx from a temp directory, not the repo root", () => {
12
- /** @type {{ cmd: string, args: string[], opts: { cwd?: string } }[]} */
13
- const calls = [];
14
- const written = [];
15
-
16
- const result = runCreateLauncherSmoke({
17
- createVersion: "0.0.1-dev.80",
18
- tmpdir: () => "/tmp",
19
- mkdtempSync: (prefix) => `${prefix}abc`,
20
- writeFileSync: (path, body) => {
21
- written.push({ path, body });
22
- },
23
- spawnSync: (cmd, args, opts) => {
24
- calls.push({ cmd, args, opts });
25
- return { status: 0 };
26
- },
27
- stdio: "pipe",
28
- });
29
-
30
- expect(result.workDir).toBe("/tmp/decohere-create-smoke-abc");
31
- expect(result.workDir).not.toBe(process.cwd());
32
- const npx = calls.at(-1);
33
- expect(npx.cmd).toBe("npx");
34
- expect(npx.args).toEqual(["--yes", "@decohere/create@0.0.1-dev.80", "--help"]);
35
- expect(npx.opts.cwd).toBe("/tmp/decohere-create-smoke-abc");
36
- expect(npx.opts.cwd).not.toBe(process.cwd());
37
- expect(written).toEqual([
38
- {
39
- path: "/tmp/decohere-create-smoke-abc/.npmrc",
40
- body: "@decohere:registry=https://registry.npmjs.org/\n",
41
- },
42
- ]);
43
- // The packages are public: the smoke installs them the way a user does, with no token.
44
- expect(npx.opts.env.NODE_AUTH_TOKEN).toBeUndefined();
45
- });
46
-
47
- // npm's public cache can lag minutes behind a publish: the first token-free Publish ran npx
48
- // 90 seconds after publishing and got ETARGET for a version the registry already had.
49
- it("waits until npm serves the new version before running npx", () => {
50
- /** @type {{ cmd: string, args: string[] }[]} */
51
- const calls = [];
52
- const sleeps = [];
53
- let views = 0;
54
-
55
- const result = runCreateLauncherSmoke({
56
- createVersion: "0.0.1-dev.127",
57
- tmpdir: () => "/tmp",
58
- mkdtempSync: (prefix) => `${prefix}abc`,
59
- writeFileSync: () => {},
60
- spawnSync: (cmd, args) => {
61
- calls.push({ cmd, args });
62
- if (cmd === "npm") return { status: ++views < 3 ? 1 : 0 };
63
- return { status: 0 };
64
- },
65
- sleep: (ms) => sleeps.push(ms),
66
- stdio: "pipe",
67
- });
68
-
69
- expect(result.status).toBe(0);
70
- expect(calls.map((call) => call.cmd)).toEqual(["npm", "npm", "npm", "npx"]);
71
- expect(calls[0].args).toEqual(["view", "@decohere/create@0.0.1-dev.127", "version"]);
72
- expect(sleeps).toHaveLength(2);
73
- });
74
-
75
- it("fails without running npx when npm never serves the version", () => {
76
- /** @type {string[]} */
77
- const commands = [];
78
-
79
- const result = runCreateLauncherSmoke({
80
- createVersion: "0.0.1-dev.127",
81
- tmpdir: () => "/tmp",
82
- mkdtempSync: (prefix) => `${prefix}abc`,
83
- writeFileSync: () => {},
84
- spawnSync: (cmd) => {
85
- commands.push(cmd);
86
- return { status: 1 };
87
- },
88
- sleep: () => {},
89
- attempts: 3,
90
- stdio: "pipe",
91
- });
92
-
93
- expect(result.status).toBe(1);
94
- expect(commands).toEqual(["npm", "npm", "npm"]);
95
- });
96
-
97
- it("fails closed without a version", () => {
98
- expect(() => runCreateLauncherSmoke({ createVersion: "" })).toThrow(
99
- "CREATE_VERSION is required",
100
- );
101
- });
102
- });
103
-
104
- describe("publish.yml create smoke + tag", () => {
105
- const yml = fs.readFileSync(PUBLISH_YML, "utf8");
106
-
107
- it("wires create smoke through the isolated launcher", () => {
108
- expect(yml).toContain("node projects/ci-tools/src/github/smoke-create-launcher.mjs");
109
- expect(yml).toContain("CREATE_VERSION:");
110
- expect(yml).not.toContain("npx --yes");
111
- });
112
-
113
- it("reads npm without a token: the packages are public and publishing is OIDC", () => {
114
- const workflows = fileURLToPath(new URL("../../../../.github/", import.meta.url));
115
- const files = fs
116
- .readdirSync(workflows, { recursive: true })
117
- .filter((file) => /\.ya?ml$/.test(file));
118
- expect(files.length).toBeGreaterThan(0);
119
- for (const file of files) {
120
- expect(fs.readFileSync(`${workflows}${file}`, "utf8"), file).not.toMatch(
121
- /NODE_AUTH_TOKEN|_authToken/,
122
- );
123
- }
124
- });
125
-
126
- it("grants the tag job actions:read so publish-prepare can download CI artifacts", () => {
127
- const tag = yml.slice(yml.indexOf("\n tag:"));
128
- const perms = tag.slice(tag.indexOf("permissions:"), tag.indexOf("steps:"));
129
- expect(perms).toMatch(/contents:\s*write/);
130
- expect(perms).toMatch(/actions:\s*read/);
131
- });
132
- });