Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 106 lines 3.76 KB
Raw
1 import { Config, Effect, Option, Path, Schema, Stdio } from 'effect';
2 import {
3 generateSdkReleaseNotes,
4 generateSdkReleaseReferences,
5 } from '../../docs/scripts/generate-sdk-release-notes';
6 import { applySdkReleasePlan } from './plan';
7 import {
8 ensure,
9 fail,
10 formatJson,
11 readOptionalFile,
12 run,
13 runMain,
14 spawn,
15 withoutReleaseTokens,
16 writeFile,
17 } from './runtime';
18 import {
19 BATCH_PATH,
20 ReleaseBatch,
21 ReleasePatch,
22 channelOf,
23 issueUrl,
24 releaseOutputs,
25 validateBatchDiff,
26 } from './source';
27
28 const git = (cwd: string, ...args: ReadonlyArray<string>) =>
29 run('git', args, { cwd, env: withoutReleaseTokens });
30
31 const nulSeparated = (output: string) => output.split('\0').filter(Boolean);
32
33 /** A changed path's contents at the frozen input base and in the prepared checkout. */
34 const releaseFile = (cwd: string, inputBase: string, file: string) =>
35 Effect.gen(function* () {
36 const path = yield* Path.Path;
37 const original = yield* spawn('git', ['show', `${inputBase}:${file}`], {
38 cwd,
39 env: withoutReleaseTokens,
40 });
41 return {
42 path: file,
43 before: original.exitCode === 0 ? original.stdout : null,
44 after: Option.getOrNull(yield* readOptionalFile(path.resolve(cwd, file))),
45 };
46 });
47
48 /** Writes the versions, lockfile, references and changelogs a stable release PR commits. */
49 const applyStableRelease = (cwd: string, batch: ReleaseBatch) =>
50 Effect.gen(function* () {
51 yield* applySdkReleasePlan(cwd, batch.plan);
52 yield* run('pnpm', ['install', '--lockfile-only', '--ignore-scripts'], {
53 cwd,
54 env: withoutReleaseTokens,
55 });
56 yield* generateSdkReleaseReferences(cwd, batch.plan);
57 yield* generateSdkReleaseNotes(cwd, batch.plan, {
58 releaseId: batch.id,
59 date: batch.date,
60 requestUrl: issueUrl(batch),
61 });
62 });
63
64 /** Applies a planned batch to a clean checkout of its source and returns the changed files. */
65 export const prepareRelease = (cwd: string, batch: ReleaseBatch) =>
66 Effect.gen(function* () {
67 const path = yield* Path.Path;
68 yield* ensure(
69 (yield* git(cwd, 'rev-parse', 'HEAD')).trim() === batch.inputBase,
70 'Preparation checkout does not match the planned source'
71 );
72 yield* ensure(
73 (yield* git(cwd, 'status', '--porcelain')).trim() === '',
74 'Preparation requires a clean source checkout'
75 );
76 // A beta commits only its plan; its candidate build applies the versions.
77 if (channelOf(batch).commitsVersions) yield* applyStableRelease(cwd, batch);
78 yield* writeFile(path.resolve(cwd, BATCH_PATH), formatJson(batch));
79 const changed = new Set([
80 ...nulSeparated(yield* git(cwd, 'diff', '--name-only', '-z', batch.inputBase)),
81 ...nulSeparated(yield* git(cwd, 'ls-files', '--others', '--exclude-standard', '-z')),
82 ]);
83 const files = yield* Effect.forEach(
84 [...changed].sort(),
85 file => releaseFile(cwd, batch.inputBase, file),
86 { concurrency: 8 }
87 );
88 yield* validateBatchDiff(files, batch, yield* releaseOutputs(cwd, batch));
89 return yield* Schema.decodeUnknownEffect(ReleasePatch)({ files });
90 });
91
92 /** `prepare.ts <patch-path>` with the planned batch in `SDK_RELEASE_BATCH`. */
93 const main = Effect.gen(function* () {
94 const [output] = yield* (yield* Stdio.Stdio).args;
95 if (!output) return yield* fail('Expected the patch output path');
96 const token = yield* Config.option(Config.String('SDK_RELEASE_TOKEN'));
97 yield* ensure(
98 Option.isNone(token) || token.value === '',
99 'Preparation must not receive a controller credential'
100 );
101 const batch = yield* Config.schema(Schema.fromJsonString(ReleaseBatch), 'SDK_RELEASE_BATCH');
102 const patch = yield* prepareRelease((yield* Path.Path).resolve('.'), batch);
103 yield* writeFile(output, `${JSON.stringify(patch)}\n`);
104 });
105
106 if (import.meta.main) runMain(main);