@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 551 lines 18.1 KB
Raw
1 import * as cloudbuild from "@distilled.cloud/gcp/cloudbuild_v2";
2 import type { GcpOpContext } from "@distilled.cloud/gcp/Protocol";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import { isResolved } from "alchemy/Diff";
5 import { createPhysicalName } from "alchemy/PhysicalName";
6 import * as Provider from "alchemy/Provider";
7 import { Resource } from "alchemy/Resource";
8 import { tagRecord } from "alchemy/Tags";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13 import { GcpEnvironment } from "../Environment.ts";
14 import {
15 createInternalLabels,
16 hasAlchemyLabels,
17 stripInternalLabels,
18 toLabels,
19 } from "../Labels.ts";
20 import { waitForOperation as waitForLongRunningOperation } from "../Operation.ts";
21 import type { Providers } from "../Providers.ts";
22
23 const MAX_NAME_LENGTH = 63;
24
25 export type RepositoryProps = {
26 /**
27 * Parent Cloud Build connection. Full name
28 * `projects/{project}/locations/{location}/connections/{connection}` or
29 * the connection id (combined with `location`). Immutable — changing it
30 * replaces the repository.
31 */
32 connection: string;
33 /**
34 * Cloud Build location (`us-central1`, `us-east1`, …). Used when
35 * `connection` is a bare id. Immutable — changing it replaces the
36 * repository. `US-CENTRAL1` is accepted and normalized to `us-central1`.
37 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
38 */
39 location?: string;
40 /**
41 * Repository id (the `{repository}` segment of
42 * `projects/{project}/locations/{location}/connections/{connection}/repositories/{repository}`).
43 * If omitted, a unique name is generated from the stack, stage, and
44 * logical id. Unique within the connection. Immutable — changing it
45 * replaces the repository.
46 */
47 repositoryId?: string;
48 /**
49 * Git clone HTTPS URI (e.g. `https://github.com/{owner}/{repo}.git`).
50 * Immutable — changing it replaces the repository.
51 */
52 remoteUri: string;
53 /**
54 * User annotations. Cloud Build repositories have no labels, so Alchemy
55 * ownership (`alchemy-stack` / `alchemy-stage` / `alchemy-id`) is stored
56 * here for `list` / nuke. The API has no update method — changing
57 * annotations replaces the repository.
58 */
59 annotations?: Record<string, string>;
60 };
61
62 export type Repository = Resource<
63 "GCP.CloudBuild.Repository",
64 RepositoryProps,
65 {
66 /** Full resource name `projects/{project}/locations/{location}/connections/{connection}/repositories/{repository}`. */
67 name: string;
68 /** Repository id (last path segment). */
69 repositoryId: string;
70 /** Parent connection resource name. */
71 connection: string;
72 /** Connection id (last path segment of the parent). */
73 connectionId: string;
74 /** Project id. */
75 project: string;
76 /** Location id (`us-central1`, …). */
77 location: string;
78 /** Git clone HTTPS URI. */
79 remoteUri: string;
80 /** User annotations (Alchemy ownership keys stripped). */
81 annotations: Record<string, string>;
82 /** External id of the webhook created for the repository. */
83 webhookId: string | undefined;
84 /** Server checksum of the resource. */
85 etag: string | undefined;
86 /** RFC3339 creation timestamp. */
87 createTime: string | undefined;
88 /** RFC3339 last-update timestamp. */
89 updateTime: string | undefined;
90 },
91 never,
92 Providers
93 >;
94
95 /**
96 * A Cloud Build v2 repository — links a remote Git repository to a
97 * {@link Connection}.
98 *
99 * Changing `repositoryId`, `connection`, `location`, `remoteUri`, or
100 * `annotations` replaces the repository. There is no update API; every
101 * user-facing field is immutable.
102 *
103 * Cloud Build repositories have no labels. Alchemy stores ownership in
104 * annotations (`alchemy-stack` / `alchemy-stage` / `alchemy-id`) so
105 * `list` / `pnpm nuke:gcp` can find them.
106 *
107 * ### Creating a Repository
108 * **Example:** Generated name
109 * ```typescript
110 * const github = yield* GCP.CloudBuild.Connection("Github", {
111 * githubConfig: {},
112 * });
113 * const source = yield* GCP.CloudBuild.Repository("Source", {
114 * connection: github.name,
115 * remoteUri: "https://github.com/{owner}/{repo}.git",
116 * });
117 * ```
118 *
119 * **Example:** Named repository with annotations
120 * ```typescript
121 * const source = yield* GCP.CloudBuild.Repository("Source", {
122 * connection: github.name,
123 * repositoryId: "app-source",
124 * remoteUri: "https://github.com/{owner}/{repo}.git",
125 * annotations: { env: "prod" },
126 * });
127 * ```
128 *
129 * ### Fetching Git Refs
130 * **Example:** List branches
131 * ```typescript
132 * const fetchGitRefs = yield* GCP.CloudBuild.FetchGitRefs(source);
133 * const { refNames } = yield* fetchGitRefs({ refType: "BRANCH" });
134 * ```
135 *
136 * ### Cloning with a Token
137 * **Example:** Fetch a read token
138 * ```typescript
139 * const accessReadToken = yield* GCP.CloudBuild.AccessReadToken(source);
140 * const { token } = yield* accessReadToken();
141 * ```
142 *
143 * @resource
144 * @category CloudBuild
145 */
146 export const Repository = Resource<Repository>("GCP.CloudBuild.Repository");
147
148 export class RepositoryNotResolved extends Data.TaggedError(
149 "GCP.CloudBuild.RepositoryNotResolved",
150 )<{
151 resourceName: string;
152 }> {}
153
154 export class RepositoryStillExists extends Data.TaggedError(
155 "GCP.CloudBuild.RepositoryStillExists",
156 )<{
157 resourceName: string;
158 }> {}
159
160 const lastSegment = (value: string) => {
161 const trimmed = value.replace(/\/+$/, "");
162 const parts = trimmed.split("/");
163 return parts[parts.length - 1] || trimmed;
164 };
165
166 const normalizeLocation = (location: string | undefined, fallback: string) =>
167 lastSegment(location ?? fallback).toLowerCase();
168
169 const parseName = (name: string) => {
170 const parts = name.split("/").filter((part) => part.length > 0);
171 const repositoriesAt = parts.lastIndexOf("repositories");
172 const connectionsAt = parts.lastIndexOf("connections");
173 const locationsAt = parts.lastIndexOf("locations");
174 const projectsAt = parts.lastIndexOf("projects");
175 const connection = connectionsAt >= 0 ? parts.slice(0, connectionsAt + 2).join("/") : "";
176 return {
177 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
178 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
179 connection,
180 connectionId: connectionsAt >= 0 && parts[connectionsAt + 1] ? parts[connectionsAt + 1]! : "",
181 repositoryId:
182 repositoriesAt >= 0 && parts[repositoriesAt + 1]
183 ? parts[repositoriesAt + 1]!
184 : lastSegment(name),
185 };
186 };
187
188 const resolveParent = (
189 project: string,
190 connection: string,
191 location: string | undefined,
192 defaultLocation: string,
193 ) => {
194 if (connection.includes("/")) {
195 const parsed = parseName(
196 connection.includes("/repositories/")
197 ? connection
198 : `${connection.replace(/\/+$/, "")}/repositories/_`,
199 );
200 return {
201 parent: parsed.connection,
202 location: parsed.location,
203 project: parsed.project || project,
204 connectionId: parsed.connectionId,
205 };
206 }
207 const loc = normalizeLocation(location, defaultLocation);
208 return {
209 parent: `projects/${project}/locations/${loc}/connections/${connection}`,
210 location: loc,
211 project,
212 connectionId: connection,
213 };
214 };
215
216 const parentKey = (
217 connection: string | undefined,
218 location: string | undefined,
219 defaultLocation: string,
220 ) => {
221 if (connection === undefined || connection === "") return undefined;
222 const parsed = resolveParent("", connection, location, defaultLocation);
223 return `${parsed.location}/${parsed.connectionId}`;
224 };
225
226 const resourceName = (parent: string, repositoryId: string) =>
227 `${parent}/repositories/${repositoryId}`;
228
229 const userAnnotations = (
230 annotations: Record<string, string | undefined> | null | undefined,
231 ): Record<string, string> => stripInternalLabels(tagRecord(annotations));
232
233 const toId = (id: string, repositoryId: string | undefined, existing?: string) =>
234 Effect.gen(function* () {
235 return (
236 repositoryId ??
237 existing ??
238 (yield* createPhysicalName({
239 id,
240 maxLength: MAX_NAME_LENGTH,
241 lowercase: true,
242 }))
243 );
244 });
245
246 const annotationsEqual = (left: Record<string, string>, right: Record<string, string>) => {
247 const leftKeys = Object.keys(left).sort();
248 const rightKeys = Object.keys(right).sort();
249 return (
250 leftKeys.length === rightKeys.length &&
251 leftKeys.every((key, index) => key === rightKeys[index] && left[key] === right[key])
252 );
253 };
254
255 const toAttrs = (repository: cloudbuild.Repository, project: string) => {
256 const name = repository.name ?? "";
257 const parsed = parseName(name);
258 return {
259 name,
260 repositoryId: parsed.repositoryId,
261 connection: parsed.connection,
262 connectionId: parsed.connectionId,
263 project: parsed.project || project,
264 location: parsed.location,
265 remoteUri: repository.remoteUri ?? "",
266 annotations: userAnnotations(repository.annotations),
267 webhookId: repository.webhookId,
268 etag: repository.etag,
269 createTime: repository.createTime,
270 updateTime: repository.updateTime,
271 };
272 };
273
274 const getByName = (name: string) =>
275 cloudbuild
276 .getProjectsLocationsConnectionsRepositories({ name })
277 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
278
279 /**
280 * Wait on a Cloud Build repositories long-running operation (seconds to a minute).
281 * `ALREADY_EXISTS` (a create race) counts as success; so does `NOT_FOUND`
282 * when `notFoundOk` (deletes). Returns the final operation.
283 */
284 const waitForOperation = (operation: cloudbuild.Operation, options?: { notFoundOk?: boolean }) =>
285 Effect.suspend(() => {
286 let latest = operation;
287 return waitForLongRunningOperation(
288 operation,
289 (name) => {
290 const get = cloudbuild.getProjectsLocationsOperations({ name }).pipe(
291 Effect.tap((current) =>
292 Effect.sync(() => {
293 latest = current;
294 }),
295 ),
296 );
297 const observe: Effect.Effect<
298 cloudbuild.Operation,
299 cloudbuild.GetProjectsLocationsOperationsError,
300 GcpOpContext
301 > =
302 options?.notFoundOk === true
303 ? get.pipe(
304 Effect.catchTag("NotFound", () =>
305 Effect.succeed<cloudbuild.Operation>({ name, done: true }),
306 ),
307 )
308 : get.pipe(
309 // A just-returned operation can briefly 404 on read.
310 Effect.retry({
311 while: (error) => error._tag === "NotFound",
312 times: 5,
313 schedule: Schedule.exponential("250 millis"),
314 }),
315 );
316 return observe;
317 },
318 { budget: "5 minutes" },
319 ).pipe(
320 Effect.map(() => latest),
321 // google.rpc.Code ALREADY_EXISTS = 6, NOT_FOUND = 5.
322 Effect.catchTag("GCP.OperationFailed", (error) =>
323 error.code === 6 || (options?.notFoundOk === true && error.code === 5)
324 ? Effect.succeed(latest)
325 : Effect.fail(error),
326 ),
327 );
328 });
329
330 const waitUntilExists = (name: string) =>
331 getByName(name).pipe(
332 Effect.flatMap((repository) =>
333 repository
334 ? Effect.succeed(repository)
335 : Effect.fail(new RepositoryNotResolved({ resourceName: name })),
336 ),
337 Effect.retry({
338 while: (error) => error._tag === "GCP.CloudBuild.RepositoryNotResolved",
339 times: 8,
340 schedule: Schedule.spaced("1 second"),
341 }),
342 );
343
344 const waitUntilGone = (name: string) =>
345 getByName(name).pipe(
346 Effect.flatMap((repository) =>
347 repository === undefined
348 ? Effect.void
349 : Effect.fail(new RepositoryStillExists({ resourceName: name })),
350 ),
351 Effect.retry({
352 while: (error) => error._tag === "GCP.CloudBuild.RepositoryStillExists",
353 times: 10,
354 schedule: Schedule.spaced("1 second"),
355 }),
356 );
357
358 const listRepositoriesAt = (parent: string, project: string) =>
359 cloudbuild.listProjectsLocationsConnectionsRepositories
360 .pages({
361 parent,
362 pageSize: 1000,
363 returnPartialSuccess: true,
364 })
365 .pipe(
366 Stream.flatMap((page) => Stream.fromIterable(page.repositories ?? [])),
367 Stream.filter((repository) =>
368 Object.keys(repository.annotations ?? {}).some((key) => key.startsWith("alchemy-")),
369 ),
370 Stream.map((repository) => toAttrs(repository, project)),
371 Stream.runCollect,
372 Effect.map((chunk) => Array.from(chunk)),
373 Effect.catchTag("NotFound", () => Effect.succeed([])),
374 );
375
376 const listOwnedRepositories = (project: string) =>
377 Effect.gen(function* () {
378 const connections = yield* cloudbuild.listProjectsLocationsConnections
379 .pages({
380 parent: `projects/${project}/locations/-`,
381 pageSize: 1000,
382 returnPartialSuccess: true,
383 })
384 .pipe(
385 Stream.flatMap((page) => Stream.fromIterable(page.connections ?? [])),
386 Stream.runCollect,
387 Effect.map((chunk) => Array.from(chunk)),
388 Effect.catchTag("NotFound", () => Effect.succeed([] as cloudbuild.Connection[])),
389 );
390
391 const pages = yield* Effect.forEach(
392 connections,
393 (connection) =>
394 connection.name
395 ? listRepositoriesAt(connection.name, project)
396 : Effect.succeed([] as ReturnType<typeof toAttrs>[]),
397 { concurrency: 4 },
398 );
399 return pages.flat();
400 });
401
402 export const RepositoryProvider = () =>
403 Provider.succeed(Repository, {
404 stables: [
405 "name",
406 "repositoryId",
407 "connection",
408 "connectionId",
409 "project",
410 "location",
411 "remoteUri",
412 "webhookId",
413 "createTime",
414 ],
415
416 diff: Effect.fn(function* ({ news, olds, output }) {
417 const env = yield* GcpEnvironment.current;
418 if (!isResolved(news)) return undefined;
419 if (output === undefined && olds === undefined) return undefined;
420
421 const previousId = olds?.repositoryId ?? output?.repositoryId;
422 const nextId = news.repositoryId ?? previousId;
423 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
424 const nextLocation = normalizeLocation(
425 news.location ?? olds?.location ?? output?.location,
426 env.region,
427 );
428 const previousParent = parentKey(
429 olds?.connection ?? output?.connection,
430 olds?.location ?? output?.location,
431 env.region,
432 );
433 const nextParent =
434 news.connection !== undefined
435 ? parentKey(
436 news.connection,
437 news.location ?? olds?.location ?? output?.location,
438 env.region,
439 )
440 : previousParent;
441 const previousUri = olds?.remoteUri ?? output?.remoteUri ?? "";
442 const nextUri = news.remoteUri ?? previousUri;
443 const previousAnnotations = toLabels(olds?.annotations ?? output?.annotations);
444 const nextAnnotations = toLabels(news.annotations);
445
446 const replace =
447 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
448 previousLocation !== nextLocation ||
449 (previousParent !== undefined &&
450 nextParent !== undefined &&
451 previousParent !== nextParent) ||
452 (previousUri !== "" && nextUri !== previousUri) ||
453 !annotationsEqual(previousAnnotations, nextAnnotations);
454
455 if (!replace) return undefined;
456 return {
457 action: "replace" as const,
458 deleteFirst:
459 previousParent === nextParent &&
460 previousLocation === nextLocation &&
461 previousId !== undefined &&
462 nextId === previousId,
463 };
464 }),
465
466 read: Effect.fn(function* ({ id, olds, output }) {
467 const env = yield* GcpEnvironment.current;
468 let name = output?.name;
469 if (name === undefined || name.length === 0) {
470 const connectionRef = olds?.connection ?? output?.connection;
471 if (typeof connectionRef !== "string" || connectionRef.length === 0) {
472 return undefined;
473 }
474 const repositoryId = yield* toId(id, olds?.repositoryId, output?.repositoryId);
475 const parent = resolveParent(
476 env.project,
477 connectionRef,
478 olds?.location ?? output?.location,
479 env.region,
480 );
481 name = resourceName(parent.parent, repositoryId);
482 }
483 const existing = yield* getByName(name);
484 if (existing === undefined) return undefined;
485 const attrs = toAttrs(existing, env.project);
486 return (yield* hasAlchemyLabels(id, tagRecord(existing.annotations)))
487 ? attrs
488 : Unowned(attrs);
489 }),
490
491 list: () =>
492 Effect.gen(function* () {
493 const env = yield* GcpEnvironment.current;
494 return yield* listOwnedRepositories(env.project);
495 }),
496
497 reconcile: Effect.fn(function* ({ id, news, output }) {
498 const env = yield* GcpEnvironment.current;
499 const repositoryId = yield* toId(id, news.repositoryId, output?.repositoryId);
500 const parent = resolveParent(
501 env.project,
502 news.connection ?? output?.connection ?? "",
503 news.location ?? output?.location,
504 env.region,
505 );
506 const name = resourceName(parent.parent, repositoryId);
507 const desiredAnnotations = {
508 ...toLabels(news.annotations),
509 ...(yield* createInternalLabels(id)),
510 };
511
512 let current = yield* getByName(name);
513
514 if (current === undefined) {
515 const created = yield* cloudbuild
516 .createProjectsLocationsConnectionsRepositories({
517 parent: parent.parent,
518 repositoryId,
519 body: {
520 remoteUri: news.remoteUri,
521 annotations: desiredAnnotations,
522 },
523 })
524 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
525 if (created !== undefined) {
526 yield* waitForOperation(created);
527 }
528 current = yield* waitUntilExists(name);
529 }
530
531 if (current === undefined) {
532 return yield* new RepositoryNotResolved({ resourceName: name });
533 }
534
535 return toAttrs(current, env.project);
536 }),
537
538 delete: Effect.fn(function* ({ output }) {
539 const name = output.name;
540 if (name === undefined || name.length === 0) {
541 return;
542 }
543 const operation = yield* cloudbuild
544 .deleteProjectsLocationsConnectionsRepositories({ name })
545 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
546 if (operation !== undefined) {
547 yield* waitForOperation(operation, { notFoundOk: true });
548 }
549 yield* waitUntilGone(name);
550 }),
551 });