1
+#!/usr/bin/env node
2
+import { chmodSync, mkdtempSync, rmSync, writeFileSync, readFileSync } from 'node:fs';
3
+import { tmpdir } from 'node:os';
4
+import { join } from 'node:path';
5
+import { spawnSync } from 'node:child_process';
6
+
7
+const packageJson = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
8
+const tsReleaseWorkflow = readFileSync(
9
+ new URL('../.github/workflows/ts.release.yml', import.meta.url),
10
+ 'utf8'
11
+);
12
+const releaseScriptUrl = new URL('../ts/scripts/changeset-release.sh', import.meta.url);
13
+const releaseScriptPath = releaseScriptUrl.pathname;
14
+const releaseScript = readFileSync(releaseScriptUrl, 'utf8');
15
+const buildCliWorkflow = readFileSync(
16
+ new URL('../.github/workflows/build-cli-binaries.yml', import.meta.url),
17
+ 'utf8'
18
+);
19
+const installHealthCheck = readFileSync(
20
+ new URL('../.github/workflows/cli.install-health-check.yml', import.meta.url),
21
+ 'utf8'
22
+);
23
+const resolveTargetScriptUrl = new URL(
24
+ '../.github/scripts/cli-release/resolve-release-target.sh',
25
+ import.meta.url
26
+);
27
+const resolveTargetScriptPath = resolveTargetScriptUrl.pathname;
28
+const resolveTargetScript = readFileSync(resolveTargetScriptUrl, 'utf8');
29
+const createDraftScript = readFileSync(
30
+ new URL('../.github/scripts/cli-release/create-or-resume-draft.sh', import.meta.url),
31
+ 'utf8'
32
+);
33
+const verifyAssetsScript = readFileSync(
34
+ new URL('../.github/scripts/cli-release/verify-assets.sh', import.meta.url),
35
+ 'utf8'
36
+);
37
+
38
+if (!tsReleaseWorkflow.includes('publish: pnpm changeset:release')) {
39
+ throw new Error('ts.release.yml must use the repository-controlled changeset:release script');
40
+}
41
+
42
+if (packageJson.scripts?.['changeset:release'] !== 'bash ts/scripts/changeset-release.sh') {
43
+ throw new Error('changeset:release must use the CLI-release filtering script');
44
+}
45
+
46
+if (!releaseScript.includes('pnpm changeset publish')) {
47
+ throw new Error('release script must still publish non-CLI changeset packages');
48
+}
49
+
50
+if (!releaseScript.includes('New tag:[[:space:]]*@composio\\/cli@')) {
51
+ throw new Error('release script must filter @composio/cli tag output before changesets/action creates GitHub releases');
52
+}
53
+
54
+// --- build-cli-binaries.yml: the CLI binary workflow is the sole, hardened release writer ---
55
+
56
+// A single failed platform must never publish a partial set: fail-fast: false makes the build
57
+// job `success` only when every matrix leg passes, gating the release job.
58
+if (!buildCliWorkflow.includes('fail-fast: false')) {
59
+ throw new Error('build-cli-binaries.yml build matrix must set fail-fast: false');
60
+}
61
+
62
+// The release must be built as a draft and only flipped to published after verification, so no
63
+// anonymous consumer can observe a release before its assets are attached. The draft and verify
64
+// logic live in standalone scripts checked out from the workflow revision; the workflow must
65
+// invoke them in helper checkout → draft → verify → publish order.
66
+const helperCheckoutIdx = buildCliWorkflow.indexOf('name: Checkout workflow release helpers');
67
+const draftStepIdx = buildCliWorkflow.indexOf(
68
+ 'bash "$RELEASE_HELPERS_DIR/create-or-resume-draft.sh"'
69
+);
70
+const verifyStepIdx = buildCliWorkflow.indexOf('bash "$RELEASE_HELPERS_DIR/verify-assets.sh"');
71
+const publishIdx = buildCliWorkflow.indexOf('gh release edit "$RELEASE_TAG" --draft=false');
72
+
73
+if (!buildCliWorkflow.includes("- '.github/scripts/cli-release/**'")) {
74
+ throw new Error('build-cli-binaries.yml must trigger when CLI release helper scripts change');
75
+}
76
+if (helperCheckoutIdx === -1) {
77
+ throw new Error('build-cli-binaries.yml must checkout workflow release helpers before publishing');
78
+}
79
+if (!buildCliWorkflow.includes('ref: ${{ github.workflow_sha }}')) {
80
+ throw new Error('build-cli-binaries.yml must checkout release helpers from the workflow revision');
81
+}
82
+if (draftStepIdx === -1) {
83
+ throw new Error('build-cli-binaries.yml must create the draft via create-or-resume-draft.sh');
84
+}
85
+if (verifyStepIdx === -1) {
86
+ throw new Error('build-cli-binaries.yml must verify assets via verify-assets.sh');
87
+}
88
+if (publishIdx === -1) {
89
+ throw new Error('build-cli-binaries.yml must publish by flipping the draft (gh release edit --draft=false)');
90
+}
91
+if (!(helperCheckoutIdx < draftStepIdx && draftStepIdx < verifyStepIdx && verifyStepIdx < publishIdx)) {
92
+ throw new Error('build-cli-binaries.yml must order steps helper checkout → draft → verify → publish');
93
+}
94
+
95
+// The draft script must actually create a draft, and the verify gate must require fully-uploaded
96
+// assets (not merely present) — that distinction is what stops a release serving 404s.
97
+if (!createDraftScript.includes('--draft')) {
98
+ throw new Error('create-or-resume-draft.sh must create the release as a draft (--draft)');
99
+}
100
+if (!verifyAssetsScript.includes('select(.state == "uploaded")')) {
101
+ throw new Error('verify-assets.sh must require assets be fully uploaded (state == "uploaded")');
102
+}
103
+
104
+// Beta status must survive the draft→publish flip (regression: the old single create set
105
+// --prerelease at creation; the new flow must set it on the draft).
106
+if (!createDraftScript.includes('flags+=(--prerelease)')) {
107
+ throw new Error('create-or-resume-draft.sh must preserve --prerelease on the draft for beta releases');
108
+}
109
+
110
+// Skills must be packaged BEFORE checksums are generated, so composio-skill.zip is hashed into
111
+// checksums.txt rather than shipping unverifiable.
112
+const packageSkillsIdx = buildCliWorkflow.indexOf('name: Package skill files');
113
+const generateChecksumsIdx = buildCliWorkflow.indexOf('name: Generate checksums');
114
+if (packageSkillsIdx === -1 || generateChecksumsIdx === -1) {
115
+ throw new Error('build-cli-binaries.yml must package skills and generate checksums');
116
+}
117
+if (!(packageSkillsIdx < generateChecksumsIdx)) {
118
+ throw new Error('build-cli-binaries.yml must package skills before generating checksums so the skill zip is checksummed');
119
+}
120
+
121
+// Per-tag concurrency prevents two runs clobbering the same release without serializing betas.
122
+if (!buildCliWorkflow.includes('group: cli-release-${{ needs.prepare.outputs.release_tag }}')) {
123
+ throw new Error('build-cli-binaries.yml release job must use per-tag concurrency keyed on the release tag');
124
+}
125
+
126
+// Release-target resolution lives in a standalone, unit-tested script (see executable tests
127
+// below) rather than inline YAML bash, so the branching logic is reviewable and testable.
128
+if (!buildCliWorkflow.includes('bash .github/scripts/cli-release/resolve-release-target.sh')) {
129
+ throw new Error('build-cli-binaries.yml prepare job must delegate to resolve-release-target.sh');
130
+}
131
+
132
+// The "latest stable" lookup must sort by numeric semver, not lexically: a lexical sort ranks
133
+// @composio/cli@0.2.9 above 0.2.10 and would regress beta versioning once a patch hits 2 digits.
134
+if (!resolveTargetScript.includes('map(tonumber)')) {
135
+ throw new Error('resolve-release-target.sh must sort releases by numeric semver (map(tonumber)), not lexically');
136
+}
137
+if (!resolveTargetScript.includes('--exclude-drafts')) {
138
+ throw new Error('resolve-release-target.sh must exclude draft stable releases from beta base selection');
139
+}
140
+
141
+// --- cli.install-health-check.yml: canary must exercise the failure-prone pinned path ---
142
+
143
+// The bare no-arg `curl | bash` is asset-aware and self-heals to the previous good release, so
144
+// the canary must additionally install the newest PUBLISHED release PINNED to actually catch a
145
+// release shipped with missing assets.
146
+//
147
+// It must resolve the newest *published* GitHub release (drafts excluded), NOT `npm view`: npm's
148
+// latest is bumped minutes before the binary workflow publishes the GitHub release, so pinning to
149
+// it would 404 during the healthy publish gap and false-page.
150
+if (installHealthCheck.includes('$(npm view')) {
151
+ throw new Error('cli.install-health-check.yml must not pin to npm (races ahead of the GitHub release)');
152
+}
153
+if (!installHealthCheck.includes('gh release list')) {
154
+ throw new Error('cli.install-health-check.yml must resolve the newest published release (drafts excluded)');
155
+}
156
+if (!installHealthCheck.includes('--repo "${{ github.repository }}"')) {
157
+ throw new Error('cli.install-health-check.yml must pass repository context to gh release list');
158
+}
159
+if (!installHealthCheck.includes('--limit 1000')) {
160
+ throw new Error('cli.install-health-check.yml must look beyond the gh release list default limit');
161
+}
162
+if (
163
+ !installHealthCheck.includes('--exclude-drafts') ||
164
+ !installHealthCheck.includes('--exclude-pre-releases')
165
+) {
166
+ throw new Error('cli.install-health-check.yml must resolve the newest published release (drafts excluded)');
167
+}
168
+if (installHealthCheck.includes('.[0].tagName')) {
169
+ throw new Error('cli.install-health-check.yml must not let jq null bypass the empty tag guard');
170
+}
171
+if (!installHealthCheck.includes("| sed -n '1p'")) {
172
+ throw new Error('cli.install-health-check.yml must convert no matching release into empty output');
173
+}
174
+if (!installHealthCheck.includes('bash -s -- "${{ steps.resolve.outputs.tag }}"')) {
175
+ throw new Error('cli.install-health-check.yml must install the resolved tag via the pinned path');
176
+}
177
+
178
+const fakeBin = mkdtempSync(join(tmpdir(), 'composio-release-test-'));
179
+try {
180
+ const fakePnpmPath = join(fakeBin, 'pnpm');
181
+ writeFileSync(
182
+ fakePnpmPath,
183
+ `#!/usr/bin/env bash
184
+set -euo pipefail
185
+case "$*" in
186
+ "run build:packages")
187
+ exit 0
188
+ ;;
189
+ "changeset publish")
190
+ echo 'New tag: @composio/core@1.2.3'
191
+ echo 'New tag: @composio/cli@9.9.9'
192
+ echo 'release warning preserved' >&2
193
+ exit 0
194
+ ;;
195
+ *)
196
+ echo "unexpected pnpm invocation: $*" >&2
197
+ exit 1
198
+ ;;
199
+esac
200
+`
201
+ );
202
+ chmodSync(fakePnpmPath, 0o755);
203
+
204
+ const result = spawnSync('bash', [releaseScriptPath], {
205
+ encoding: 'utf8',
206
+ env: { ...process.env, PATH: `${fakeBin}:${process.env.PATH}` },
207
+ });
208
+
209
+ if (result.status !== 0) {
210
+ throw new Error(`release script failed unexpectedly\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`);
211
+ }
212
+
213
+ if (!result.stdout.includes('New tag: @composio/core@1.2.3')) {
214
+ throw new Error('release script must preserve non-CLI changeset tags');
215
+ }
216
+
217
+ if (result.stdout.includes('@composio/cli@9.9.9')) {
218
+ throw new Error('release script must hide @composio/cli tags from changesets/action');
219
+ }
220
+
221
+ if (!result.stderr.includes('release warning preserved')) {
222
+ throw new Error('release script must preserve changeset publish stderr');
223
+ }
224
+} finally {
225
+ rmSync(fakeBin, { recursive: true, force: true });
226
+}
227
+
228
+// --- resolve-release-target.sh: executable tests for the release-target branching ---
229
+//
230
+// The script shells out to `gh` and `curl`; we stub both on PATH and feed fixtures, so these
231
+// exercise the real branching/version logic (not just substring presence). `jq`/`python3` stay
232
+// real because the script's correctness depends on them.
233
+
234
+const FAKE_GH = `#!/usr/bin/env bash
235
+set -euo pipefail
236
+if [[ "\${1:-}" == "release" && "\${2:-}" == "list" ]]; then
237
+ shift 2
238
+ jqexpr=""
239
+ exclude_drafts=false
240
+ limit=""
241
+ while [[ \$# -gt 0 ]]; do
242
+ if [[ "\$1" == "--exclude-drafts" ]]; then exclude_drafts=true; shift; continue; fi
243
+ if [[ "\$1" == "--limit" ]]; then limit="\$2"; shift 2; continue; fi
244
+ if [[ "\$1" == "--jq" ]]; then jqexpr="\$2"; shift 2; continue; fi
245
+ shift
246
+ done
247
+ if [[ "\$exclude_drafts" != "true" ]]; then
248
+ echo "release list must pass --exclude-drafts" >&2
249
+ exit 1
250
+ fi
251
+ if [[ "\$limit" != "1000" ]]; then
252
+ echo "release list must pass --limit 1000" >&2
253
+ exit 1
254
+ fi
255
+ jq -r '[.[] | select(.isDraft != true)] | '"\$jqexpr" "\$GH_RELEASES_FIXTURE"
256
+ exit 0
257
+fi
258
+if [[ "\${1:-}" == "release" && "\${2:-}" == "view" ]]; then
259
+ # An existing release: echo its isDraft flag. Unset fixture ⇒ exit non-zero (release absent).
260
+ if [[ -n "\${GH_VIEW_ISDRAFT:-}" ]]; then
261
+ echo "\$GH_VIEW_ISDRAFT"
262
+ exit 0
263
+ fi
264
+ exit 1
265
+fi
266
+echo "unexpected gh invocation: \$*" >&2
267
+exit 1
268
+`;
269
+
270
+const FAKE_CURL = `#!/usr/bin/env bash
271
+set -euo pipefail
272
+cat "\$CURL_FIXTURE"
273
+`;
274
+
275
+function parseOutputs(text) {
276
+ const outputs = {};
277
+ for (const line of text.split('\n')) {
278
+ const idx = line.indexOf('=');
279
+ if (idx === -1) continue;
280
+ outputs[line.slice(0, idx)] = line.slice(idx + 1);
281
+ }
282
+ return outputs;
283
+}
284
+
285
+function runResolver({ env, releasesFixture, curlFixture, ghViewIsDraft }) {
286
+ const fakeBin = mkdtempSync(join(tmpdir(), 'composio-fakebin-'));
287
+ const workdir = mkdtempSync(join(tmpdir(), 'composio-resolver-'));
288
+ try {
289
+ for (const [name, body] of [['gh', FAKE_GH], ['curl', FAKE_CURL]]) {
290
+ const p = join(fakeBin, name);
291
+ writeFileSync(p, body);
292
+ chmodSync(p, 0o755);
293
+ }
294
+
295
+ const outputPath = join(workdir, 'github_output');
296
+ writeFileSync(outputPath, '');
297
+
298
+ const fixtures = {};
299
+ if (releasesFixture !== undefined) {
300
+ const fixturePath = join(workdir, 'releases.json');
301
+ writeFileSync(fixturePath, JSON.stringify(releasesFixture));
302
+ fixtures.GH_RELEASES_FIXTURE = fixturePath;
303
+ }
304
+ if (curlFixture !== undefined) {
305
+ const fixturePath = join(workdir, 'curl.json');
306
+ writeFileSync(fixturePath, JSON.stringify(curlFixture));
307
+ fixtures.CURL_FIXTURE = fixturePath;
308
+ }
309
+ if (ghViewIsDraft !== undefined) fixtures.GH_VIEW_ISDRAFT = ghViewIsDraft;
310
+
311
+ const result = spawnSync('bash', [resolveTargetScriptPath], {
312
+ encoding: 'utf8',
313
+ env: {
314
+ ...process.env,
315
+ PATH: `${fakeBin}:${process.env.PATH}`,
316
+ GITHUB_OUTPUT: outputPath,
317
+ ...fixtures,
318
+ ...env,
319
+ },
320
+ });
321
+
322
+ const output = readFileSync(outputPath, 'utf8');
323
+ return { ...result, output, outputs: parseOutputs(output) };
324
+ } finally {
325
+ rmSync(fakeBin, { recursive: true, force: true });
326
+ rmSync(workdir, { recursive: true, force: true });
327
+ }
328
+}
329
+
330
+// build-beta bumps off the NUMERIC-latest stable release. The fixture deliberately interleaves
331
+// 0.2.10 and 0.2.9: a lexical sort would pick 0.2.9 and resolve 0.2.10 here — regression lock.
332
+{
333
+ const r = runResolver({
334
+ env: {
335
+ EVENT_NAME: 'workflow_dispatch',
336
+ ACTION_INPUT: 'build-beta',
337
+ REPOSITORY: 'ComposioHQ/composio',
338
+ RUN_NUMBER: '42',
339
+ COMMIT_SHA: 'deadbeef',
340
+ },
341
+ releasesFixture: [
342
+ { tagName: '@composio/cli@0.2.2', isPrerelease: false },
343
+ { tagName: '@composio/cli@0.2.10', isPrerelease: false },
344
+ { tagName: '@composio/cli@0.2.9', isPrerelease: false },
345
+ // Draft stables must not become the base for rolling betas.
346
+ { tagName: '@composio/cli@0.2.11', isPrerelease: false, isDraft: true },
347
+ { tagName: '@composio/cli@0.3.0-beta.1', isPrerelease: true },
348
+ ],
349
+ });
350
+ if (r.status !== 0) {
351
+ throw new Error(`resolve-release-target.sh build-beta failed\nstderr:\n${r.stderr}`);
352
+ }
353
+ if (r.outputs.release_version !== '0.2.11') {
354
+ throw new Error(
355
+ `build-beta must bump off the numeric-latest stable (0.2.10 → 0.2.11), got release_version=${r.outputs.release_version}. A lexical sort would regress.`
356
+ );
357
+ }
358
+ if (r.outputs.release_tag !== '@composio/cli@0.2.11-beta.42') {
359
+ throw new Error(`build-beta release_tag wrong: ${r.outputs.release_tag}`);
360
+ }
361
+ if (r.outputs.prerelease !== 'true' || r.outputs.make_latest !== 'false') {
362
+ throw new Error('build-beta must emit prerelease=true and make_latest=false');
363
+ }
364
+}
365
+
366
+// promote-stable must REFUSE a tag that is already published (isDraft=false) and emit nothing.
367
+{
368
+ const r = runResolver({
369
+ env: {
370
+ EVENT_NAME: 'workflow_dispatch',
371
+ ACTION_INPUT: 'promote-stable',
372
+ BETA_TAG_INPUT: '@composio/cli@0.3.0-beta.5',
373
+ GITHUB_TOKEN: 'fake-token',
374
+ REPOSITORY: 'ComposioHQ/composio',
375
+ RUN_NUMBER: '1',
376
+ COMMIT_SHA: 'unused',
377
+ },
378
+ curlFixture: { prerelease: true, target_commitish: 'abc123' },
379
+ ghViewIsDraft: 'false',
380
+ });
381
+ if (r.status === 0) {
382
+ throw new Error('promote-stable must refuse an already-published stable tag');
383
+ }
384
+ if (!r.stderr.includes('already published')) {
385
+ throw new Error(`promote-stable refusal must explain itself\nstderr:\n${r.stderr}`);
386
+ }
387
+ if (r.output.trim() !== '') {
388
+ throw new Error('a refused promotion must not emit release outputs');
389
+ }
390
+}
391
+
392
+// promote-stable happy path: no existing release ⇒ emit a stable target off the beta's commitish.
393
+{
394
+ const r = runResolver({
395
+ env: {
396
+ EVENT_NAME: 'workflow_dispatch',
397
+ ACTION_INPUT: 'promote-stable',
398
+ BETA_TAG_INPUT: '@composio/cli@0.3.0-beta.5',
399
+ GITHUB_TOKEN: 'fake-token',
400
+ REPOSITORY: 'ComposioHQ/composio',
401
+ RUN_NUMBER: '1',
402
+ COMMIT_SHA: 'unused',
403
+ },
404
+ curlFixture: { prerelease: true, target_commitish: 'abc123' },
405
+ // ghViewIsDraft unset ⇒ `gh release view` exits non-zero ⇒ no existing release to refuse.
406
+ });
407
+ if (r.status !== 0) {
408
+ throw new Error(`resolve-release-target.sh promote-stable failed\nstderr:\n${r.stderr}`);
409
+ }
410
+ if (r.outputs.release_tag !== '@composio/cli@0.3.0' || r.outputs.release_version !== '0.3.0') {
411
+ throw new Error(`promote-stable must strip the -beta suffix, got ${r.outputs.release_tag}`);
412
+ }
413
+ if (r.outputs.prerelease !== 'false' || r.outputs.make_latest !== 'true') {
414
+ throw new Error('promote-stable must emit prerelease=false and make_latest=true');
415
+ }
416
+ if (r.outputs.checkout_ref !== 'abc123') {
417
+ throw new Error(`promote-stable must check out the beta's target_commitish, got ${r.outputs.checkout_ref}`);
418
+ }
419
+}
420
+
421
+console.log('release workflow test passed');