The best way to control Claude Code, Codex and more from any computer or phone

main
ts 386 lines 13.2 KB
Raw
1 import * as dataform from "@distilled.cloud/gcp/dataform_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 expandRepository,
13 forEachOwnedRepository,
14 hasAlchemyLabelMap,
15 listReleaseConfigs,
16 mergeOwnedVars,
17 normalizeLocation,
18 parseResourceName,
19 replaceOnIdentity,
20 retryTransient,
21 sameJson,
22 sameText,
23 toPhysicalId,
24 updateMaskOf,
25 userLabels,
26 waitUntilExists,
27 waitUntilGone,
28 } from "./internal.ts";
29
30 export type NotebookRuntimeOptions = {
31 /** Colab runtime template resource name. */
32 aiPlatformNotebookRuntimeTemplate?: string;
33 /** GCS bucket for notebook output (`gs://bucket`). */
34 gcsOutputBucket?: string;
35 };
36
37 export type CodeCompilationConfig = {
38 /** Prefix prepended to table names. */
39 tablePrefix?: string;
40 /** Default notebook runtime options. */
41 defaultNotebookRuntimeOptions?: NotebookRuntimeOptions;
42 /** Default BigQuery location. Defaults to `US`. */
43 defaultLocation?: string;
44 /** Prefix prepended to built-in assertion names. */
45 builtinAssertionNamePrefix?: string;
46 /**
47 * User-defined compilation variables. Alchemy ownership keys
48 * (`alchemy-stack`, `alchemy-stage`, `alchemy-id`) are merged in
49 * automatically because release configs have no labels field.
50 */
51 vars?: Record<string, string>;
52 /** Default schema for assertions. */
53 assertionSchema?: string;
54 /** Suffix appended to database (project id) names. */
55 databaseSuffix?: string;
56 /** Suffix appended to schema (dataset) names. */
57 schemaSuffix?: string;
58 /** Default schema (BigQuery dataset id). */
59 defaultSchema?: string;
60 /** Default database (Google Cloud project id). */
61 defaultDatabase?: string;
62 };
63
64 export type RepositoriesReleaseConfigProps = {
65 /**
66 * Parent repository. Full name
67 * `projects/{project}/locations/{location}/repositories/{repository}`
68 * or the repository id (combined with `location`). Immutable —
69 * changing it replaces the release config.
70 */
71 repository: string;
72 /**
73 * Region used when `repository` is a bare id.
74 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
75 */
76 location?: string;
77 /**
78 * Release config id. If omitted, a unique RFC1035 name is generated.
79 * Immutable — changing it replaces the release config.
80 */
81 releaseConfigId?: string;
82 /**
83 * Git commit, tag, or branch compiled by this release
84 * (`main`, `12ade345`, `tag1`).
85 */
86 gitCommitish: string;
87 /**
88 * IANA time zone used when interpreting `cronSchedule`.
89 * @default "UTC"
90 */
91 timeZone?: string;
92 /**
93 * Cron schedule for automatic compilation-result creation.
94 */
95 cronSchedule?: string;
96 /**
97 * Disable automatic compilation-result creation.
98 * @default false
99 */
100 disabled?: boolean;
101 /**
102 * Compilation overrides. Alchemy ownership vars are merged in.
103 */
104 codeCompilationConfig?: CodeCompilationConfig;
105 /**
106 * Currently released compilation result
107 * (`.../repositories/{repository}/compilationResults/{result}`).
108 */
109 releaseCompilationResult?: string;
110 };
111
112 export type RepositoriesReleaseConfig = Resource<
113 "GCP.Dataform.RepositoriesReleaseConfig",
114 RepositoriesReleaseConfigProps,
115 {
116 /** Full resource name `.../repositories/{repository}/releaseConfigs/{releaseConfig}`. */
117 name: string;
118 /** Release config id (last path segment). */
119 releaseConfigId: string;
120 /** Parent repository resource name. */
121 repository: string;
122 /** Project id. */
123 project: string;
124 /** Location id. */
125 location: string;
126 /** Git commitish. */
127 gitCommitish: string;
128 /** Cron schedule, if set. */
129 cronSchedule: string | undefined;
130 /** Time zone. */
131 timeZone: string | undefined;
132 /** Whether automatic releases are disabled. */
133 disabled: boolean;
134 /** User compilation vars (Alchemy ownership keys stripped). */
135 vars: Record<string, string>;
136 /** Currently released compilation result, if any. */
137 releaseCompilationResult: string | undefined;
138 },
139 never,
140 Providers
141 >;
142
143 /**
144 * A Dataform release configuration — compiles a Git commitish on a
145 * schedule (or on demand) into a compilation result.
146 *
147 * Release configs have no labels field, so Alchemy stamps ownership
148 * into `codeCompilationConfig.vars`. Changing `releaseConfigId`,
149 * `repository`, or `location` replaces the config. Commitish, schedule,
150 * disabled flag, vars, and the current compilation result update in
151 * place.
152 *
153 * ### Creating a Release Config
154 * **Example:** Compile `main`
155 * ```typescript
156 * const release = yield* GCP.Dataform.RepositoriesReleaseConfig("Prod", {
157 * repository: repo.name,
158 * gitCommitish: "main",
159 * });
160 * ```
161 *
162 * **Example:** Nightly release
163 * ```typescript
164 * const release = yield* GCP.Dataform.RepositoriesReleaseConfig("Nightly", {
165 * repository: repo.name,
166 * gitCommitish: "main",
167 * cronSchedule: "0 8 * * *",
168 * timeZone: "America/Los_Angeles",
169 * });
170 * ```
171 *
172 * @resource
173 * @category Dataform
174 */
175 export const RepositoriesReleaseConfig = Resource<RepositoriesReleaseConfig>(
176 "GCP.Dataform.RepositoriesReleaseConfig",
177 );
178
179 export class RepositoriesReleaseConfigNotResolved extends Data.TaggedError(
180 "GCP.Dataform.RepositoriesReleaseConfigNotResolved",
181 )<{
182 resourceName: string;
183 }> {}
184
185 const resourceName = (repository: string, releaseConfigId: string) =>
186 `${repository}/releaseConfigs/${releaseConfigId}`;
187
188 const compilationOf = (
189 config: CodeCompilationConfig | undefined,
190 ownership: Record<string, string>,
191 ): dataform.CodeCompilationConfig => ({
192 tablePrefix: config?.tablePrefix,
193 defaultNotebookRuntimeOptions: config?.defaultNotebookRuntimeOptions,
194 defaultLocation: config?.defaultLocation,
195 builtinAssertionNamePrefix: config?.builtinAssertionNamePrefix,
196 vars: mergeOwnedVars(config?.vars, ownership),
197 assertionSchema: config?.assertionSchema,
198 databaseSuffix: config?.databaseSuffix,
199 schemaSuffix: config?.schemaSuffix,
200 defaultSchema: config?.defaultSchema,
201 defaultDatabase: config?.defaultDatabase,
202 });
203
204 const toAttrs = (config: dataform.ReleaseConfig, project: string, region: string) => {
205 const name = config.name ?? "";
206 const parsed = parseResourceName(name, "releaseConfigs", region);
207 return {
208 name,
209 releaseConfigId: parsed.id,
210 repository: parsed.parent,
211 project: parsed.project || project,
212 location: parsed.location,
213 gitCommitish: config.gitCommitish ?? "",
214 cronSchedule: config.cronSchedule,
215 timeZone: config.timeZone,
216 disabled: config.disabled === true,
217 vars: userLabels(config.codeCompilationConfig?.vars),
218 releaseCompilationResult: config.releaseCompilationResult,
219 };
220 };
221
222 const getByName = (name: string) =>
223 name.length === 0 || name.includes("//")
224 ? Effect.undefined
225 : dataform
226 .getProjectsLocationsRepositoriesReleaseConfigs({ name })
227 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
228
229 const isOwnedConfig = (config: dataform.ReleaseConfig) =>
230 hasAlchemyLabelMap(config.codeCompilationConfig?.vars);
231
232 export const RepositoriesReleaseConfigProvider = () =>
233 Provider.succeed(RepositoriesReleaseConfig, {
234 stables: ["name", "releaseConfigId", "repository", "project", "location"],
235
236 diff: Effect.fn(function* ({ news, olds, output }) {
237 if (!isResolved(news)) return undefined;
238 const env = yield* GcpEnvironment.current;
239 const location = normalizeLocation(
240 news.location ?? olds?.location ?? output?.location,
241 env.region,
242 );
243 return replaceOnIdentity({
244 previousId: olds?.releaseConfigId ?? output?.releaseConfigId,
245 nextId: news.releaseConfigId ?? olds?.releaseConfigId ?? output?.releaseConfigId,
246 previousLocation: olds?.location ?? output?.location,
247 nextLocation: news.location ?? olds?.location ?? output?.location,
248 previousParent: olds?.repository ?? output?.repository,
249 nextParent: expandRepository(news.repository, env.project, location),
250 });
251 }),
252
253 read: Effect.fn(function* ({ id, olds, output }) {
254 const env = yield* GcpEnvironment.current;
255 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
256 const repository = expandRepository(
257 olds?.repository ??
258 output?.repository ??
259 parseResourceName(output?.name ?? "", "releaseConfigs", env.region).parent,
260 env.project,
261 location,
262 );
263 const releaseConfigId = yield* toPhysicalId(
264 id,
265 olds?.releaseConfigId,
266 output?.releaseConfigId,
267 );
268 const name = output?.name ?? resourceName(repository, releaseConfigId);
269 const existing = yield* getByName(name);
270 if (existing === undefined) return undefined;
271 const attrs = toAttrs(existing, env.project, env.region);
272 const labeled = yield* hasAlchemyLabels(id, existing.codeCompilationConfig?.vars);
273 return labeled || isOwnedConfig(existing) ? attrs : Unowned(attrs);
274 }),
275
276 list: () =>
277 Effect.gen(function* () {
278 const env = yield* GcpEnvironment.current;
279 const configs = yield* forEachOwnedRepository(env.project, env.region, (repo) =>
280 listReleaseConfigs(repo.name ?? ""),
281 );
282 return configs.map((item) => toAttrs(item, env.project, env.region));
283 }),
284
285 reconcile: Effect.fn(function* ({ id, news, output }) {
286 const env = yield* GcpEnvironment.current;
287 const location = normalizeLocation(news.location ?? output?.location, env.region);
288 const repository = expandRepository(news.repository, env.project, location);
289 const releaseConfigId = yield* toPhysicalId(
290 id,
291 news.releaseConfigId,
292 output?.releaseConfigId,
293 );
294 const name = resourceName(repository, releaseConfigId);
295 const ownership = yield* createInternalLabels(id);
296 const codeCompilationConfig = compilationOf(news.codeCompilationConfig, ownership);
297 const disabled = news.disabled === true;
298
299 let current = yield* getByName(output?.name ?? name);
300
301 if (current === undefined) {
302 const created = yield* retryTransient(
303 dataform.createProjectsLocationsRepositoriesReleaseConfigs({
304 parent: repository,
305 releaseConfigId,
306 body: {
307 gitCommitish: news.gitCommitish,
308 timeZone: news.timeZone,
309 cronSchedule: news.cronSchedule,
310 disabled,
311 codeCompilationConfig,
312 releaseCompilationResult: news.releaseCompilationResult,
313 },
314 }),
315 ).pipe(Effect.catchTag("Conflict", () => getByName(name)));
316 current = created ?? undefined;
317 if (current === undefined) {
318 current = yield* waitUntilExists(getByName(name), name);
319 }
320 }
321
322 if (current === undefined) {
323 return yield* new RepositoriesReleaseConfigNotResolved({ resourceName: name });
324 }
325
326 const currentName = current.name ?? name;
327 const gitChanged = !sameText(current.gitCommitish, news.gitCommitish);
328 const zoneChanged = !sameText(current.timeZone, news.timeZone);
329 const cronChanged = !sameText(current.cronSchedule, news.cronSchedule);
330 const disabledChanged = (current.disabled === true) !== disabled;
331 const compilationChanged = !sameJson(
332 {
333 ...current.codeCompilationConfig,
334 vars: mergeOwnedVars(userLabels(current.codeCompilationConfig?.vars), ownership),
335 },
336 codeCompilationConfig,
337 );
338 const resultChanged = !sameText(
339 current.releaseCompilationResult,
340 news.releaseCompilationResult,
341 );
342
343 if (
344 gitChanged ||
345 zoneChanged ||
346 cronChanged ||
347 disabledChanged ||
348 compilationChanged ||
349 resultChanged
350 ) {
351 current = yield* retryTransient(
352 dataform.patchProjectsLocationsRepositoriesReleaseConfigs({
353 name: currentName,
354 updateMask: updateMaskOf(
355 gitChanged ? "gitCommitish" : undefined,
356 zoneChanged ? "timeZone" : undefined,
357 cronChanged ? "cronSchedule" : undefined,
358 disabledChanged ? "disabled" : undefined,
359 compilationChanged ? "codeCompilationConfig" : undefined,
360 resultChanged ? "releaseCompilationResult" : undefined,
361 ),
362 body: {
363 gitCommitish: news.gitCommitish,
364 timeZone: news.timeZone,
365 cronSchedule: news.cronSchedule,
366 disabled,
367 codeCompilationConfig,
368 releaseCompilationResult: news.releaseCompilationResult,
369 },
370 }),
371 );
372 }
373
374 return toAttrs(current, env.project, env.region);
375 }),
376
377 delete: Effect.fn(function* ({ output }) {
378 if (!output.name) return;
379 yield* retryTransient(
380 dataform.deleteProjectsLocationsRepositoriesReleaseConfigs({
381 name: output.name,
382 }),
383 ).pipe(Effect.catchTag("NotFound", () => Effect.void));
384 yield* waitUntilGone(getByName(output.name));
385 }),
386 });