The best way to control Claude Code, Codex and more from any computer or phone
| 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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | expandParent, |
| 15 | hasAlchemyLabelMap, |
| 16 | listReleaseConfigs, |
| 17 | listRepositories, |
| 18 | listWorkflowConfigs, |
| 19 | listWorkflowInvocations, |
| 20 | listWorkspaces, |
| 21 | locationParent, |
| 22 | normalizeLocation, |
| 23 | parseResourceName, |
| 24 | replaceOnIdentity, |
| 25 | retryTransient, |
| 26 | sameJson, |
| 27 | sameText, |
| 28 | toPhysicalId, |
| 29 | updateMaskOf, |
| 30 | userLabels, |
| 31 | waitForOperation, |
| 32 | waitUntilExists, |
| 33 | waitUntilGone, |
| 34 | } from "./internal.ts"; |
| 35 | |
| 36 | export type GitRemoteSettings = { |
| 37 | /** Git remote URL. */ |
| 38 | url?: string; |
| 39 | /** Default branch name. Defaults to `main` when omitted. */ |
| 40 | defaultBranch?: string; |
| 41 | /** |
| 42 | * Secret Manager secret version used as a Git token |
| 43 | * (`projects/{project}/secrets/{secret}/versions/{version}`). |
| 44 | */ |
| 45 | authenticationTokenSecretVersion?: string; |
| 46 | /** |
| 47 | * Cloud Build `GitRepositoryLink` used for machine credentials |
| 48 | * (`projects/{project}/locations/{location}/connections/{connection}/gitRepositoryLinks/{link}`). |
| 49 | */ |
| 50 | gitRepositoryLink?: string; |
| 51 | /** SSH host public key. */ |
| 52 | hostPublicKey?: string; |
| 53 | /** |
| 54 | * Secret Manager secret version holding the SSH private key |
| 55 | * (`projects/{project}/secrets/{secret}/versions/{version}`). |
| 56 | */ |
| 57 | userPrivateKeySecretVersion?: string; |
| 58 | }; |
| 59 | |
| 60 | export type WorkspaceCompilationOverrides = { |
| 61 | /** Default database (Google Cloud project id). */ |
| 62 | defaultDatabase?: string; |
| 63 | /** Suffix appended to schema (BigQuery dataset) names. */ |
| 64 | schemaSuffix?: string; |
| 65 | /** Prefix prepended to table names. */ |
| 66 | tablePrefix?: string; |
| 67 | }; |
| 68 | |
| 69 | export type RepositoryProps = { |
| 70 | /** |
| 71 | * Repository id (the `{repository}` segment of |
| 72 | * `projects/{project}/locations/{location}/repositories/{repository}`). |
| 73 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 74 | * it replaces the repository. |
| 75 | */ |
| 76 | repositoryId?: string; |
| 77 | /** |
| 78 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 79 | * repository. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 80 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 81 | */ |
| 82 | location?: string; |
| 83 | /** |
| 84 | * User-friendly display name. Defaults to the repository id. |
| 85 | */ |
| 86 | displayName?: string; |
| 87 | /** |
| 88 | * User labels. Alchemy ownership labels are merged in automatically. |
| 89 | */ |
| 90 | labels?: Record<string, string>; |
| 91 | /** |
| 92 | * Parent folder or team folder |
| 93 | * (`projects/{project}/locations/{location}/folders/{folder}` or |
| 94 | * `.../teamFolders/{teamFolder}`). Changing it moves the repository. |
| 95 | */ |
| 96 | containingFolder?: string; |
| 97 | /** |
| 98 | * Git remote. Omit for a Dataform-managed (non-linked) repository. |
| 99 | */ |
| 100 | gitRemoteSettings?: GitRemoteSettings; |
| 101 | /** |
| 102 | * Secret Manager secret version interpolated into `.npmrc` |
| 103 | * (`projects/{project}/secrets/{secret}/versions/{version}`). |
| 104 | */ |
| 105 | npmrcEnvironmentVariablesSecretVersion?: string; |
| 106 | /** |
| 107 | * Service account used to run workflow invocations. |
| 108 | */ |
| 109 | serviceAccount?: string; |
| 110 | /** |
| 111 | * Workspace compilation overrides for `dataform.json` defaults. |
| 112 | */ |
| 113 | workspaceCompilationOverrides?: WorkspaceCompilationOverrides; |
| 114 | /** |
| 115 | * Customer-managed KMS key. Immutable after create. |
| 116 | */ |
| 117 | kmsKeyName?: string; |
| 118 | /** |
| 119 | * Grant `roles/dataform.admin` to the caller on create. |
| 120 | * @default true |
| 121 | */ |
| 122 | setAuthenticatedUserAdmin?: boolean; |
| 123 | }; |
| 124 | |
| 125 | export type Repository = Resource< |
| 126 | "GCP.Dataform.Repository", |
| 127 | RepositoryProps, |
| 128 | { |
| 129 | /** Full resource name `projects/{project}/locations/{location}/repositories/{repository}`. */ |
| 130 | name: string; |
| 131 | /** Repository id (last path segment). */ |
| 132 | repositoryId: string; |
| 133 | /** Project id. */ |
| 134 | project: string; |
| 135 | /** Location id. */ |
| 136 | location: string; |
| 137 | /** User-friendly display name. */ |
| 138 | displayName: string | undefined; |
| 139 | /** User labels (Alchemy ownership labels stripped). */ |
| 140 | labels: Record<string, string>; |
| 141 | /** Parent folder or team folder, if any. */ |
| 142 | containingFolder: string | undefined; |
| 143 | /** Associated team folder, if any. */ |
| 144 | teamFolderName: string | undefined; |
| 145 | /** Git remote URL, if linked. */ |
| 146 | gitRemoteUrl: string | undefined; |
| 147 | /** Effective default Git branch. */ |
| 148 | gitDefaultBranch: string | undefined; |
| 149 | /** Service account for workflow invocations. */ |
| 150 | serviceAccount: string | undefined; |
| 151 | /** Customer-managed KMS key, if set. */ |
| 152 | kmsKeyName: string | undefined; |
| 153 | /** RFC3339 creation timestamp. */ |
| 154 | createTime: string | undefined; |
| 155 | }, |
| 156 | never, |
| 157 | Providers |
| 158 | >; |
| 159 | |
| 160 | /** |
| 161 | * A Dataform Git repository — the container for workspaces, release |
| 162 | * configs, workflow configs, and workflow invocations. |
| 163 | * |
| 164 | * Changing `repositoryId`, `location`, or `kmsKeyName` replaces the |
| 165 | * repository. Display name, labels, Git remote, service account, npmrc |
| 166 | * secret, and compilation overrides update in place. `containingFolder` |
| 167 | * is applied with MoveRepository. |
| 168 | * |
| 169 | * ### Creating a Repository |
| 170 | * **Example:** Generated name |
| 171 | * ```typescript |
| 172 | * const repo = yield* GCP.Dataform.Repository("Analytics", { |
| 173 | * labels: { env: "test" }, |
| 174 | * }); |
| 175 | * ``` |
| 176 | * |
| 177 | * **Example:** Named repository |
| 178 | * ```typescript |
| 179 | * const repo = yield* GCP.Dataform.Repository("Analytics", { |
| 180 | * repositoryId: "analytics", |
| 181 | * location: "us-central1", |
| 182 | * displayName: "analytics", |
| 183 | * }); |
| 184 | * ``` |
| 185 | * |
| 186 | * @resource |
| 187 | * @category Dataform |
| 188 | */ |
| 189 | export const Repository = Resource<Repository>("GCP.Dataform.Repository"); |
| 190 | |
| 191 | export class RepositoryNotResolved extends Data.TaggedError("GCP.Dataform.RepositoryNotResolved")<{ |
| 192 | resourceName: string; |
| 193 | }> {} |
| 194 | |
| 195 | const resourceName = (project: string, location: string, repositoryId: string) => |
| 196 | `${locationParent(project, location)}/repositories/${repositoryId}`; |
| 197 | |
| 198 | const gitOf = (settings: GitRemoteSettings | undefined): dataform.GitRemoteSettings | undefined => { |
| 199 | if (settings === undefined) return undefined; |
| 200 | const ssh = |
| 201 | settings.hostPublicKey !== undefined || settings.userPrivateKeySecretVersion !== undefined |
| 202 | ? { |
| 203 | hostPublicKey: settings.hostPublicKey, |
| 204 | userPrivateKeySecretVersion: settings.userPrivateKeySecretVersion, |
| 205 | } |
| 206 | : undefined; |
| 207 | return { |
| 208 | url: settings.url, |
| 209 | defaultBranch: settings.defaultBranch, |
| 210 | authenticationTokenSecretVersion: settings.authenticationTokenSecretVersion, |
| 211 | gitRepositoryLink: settings.gitRepositoryLink, |
| 212 | sshAuthenticationConfig: ssh, |
| 213 | }; |
| 214 | }; |
| 215 | |
| 216 | const toAttrs = (repo: dataform.Repository, project: string, region: string) => { |
| 217 | const name = repo.name ?? ""; |
| 218 | const parsed = parseResourceName(name, "repositories", region); |
| 219 | return { |
| 220 | name, |
| 221 | repositoryId: parsed.id, |
| 222 | project: parsed.project || project, |
| 223 | location: parsed.location, |
| 224 | displayName: repo.displayName, |
| 225 | labels: userLabels(repo.labels), |
| 226 | containingFolder: repo.containingFolder, |
| 227 | teamFolderName: repo.teamFolderName, |
| 228 | gitRemoteUrl: repo.gitRemoteSettings?.url, |
| 229 | gitDefaultBranch: |
| 230 | repo.gitRemoteSettings?.effectiveDefaultBranch ?? repo.gitRemoteSettings?.defaultBranch, |
| 231 | serviceAccount: repo.serviceAccount, |
| 232 | kmsKeyName: repo.kmsKeyName, |
| 233 | createTime: repo.createTime, |
| 234 | }; |
| 235 | }; |
| 236 | |
| 237 | const getByName = (name: string) => |
| 238 | name.length === 0 || name.includes("//") |
| 239 | ? Effect.undefined |
| 240 | : dataform |
| 241 | .getProjectsLocationsRepositories({ name }) |
| 242 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 243 | |
| 244 | export const RepositoryProvider = () => |
| 245 | Provider.succeed(Repository, { |
| 246 | stables: ["name", "repositoryId", "project", "location", "kmsKeyName", "createTime"], |
| 247 | |
| 248 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 249 | if (!isResolved(news)) return undefined; |
| 250 | const previousKey = olds?.kmsKeyName ?? output?.kmsKeyName; |
| 251 | return replaceOnIdentity({ |
| 252 | previousId: olds?.repositoryId ?? output?.repositoryId, |
| 253 | nextId: news.repositoryId ?? olds?.repositoryId ?? output?.repositoryId, |
| 254 | previousLocation: olds?.location ?? output?.location, |
| 255 | nextLocation: news.location ?? olds?.location ?? output?.location, |
| 256 | extra: |
| 257 | previousKey !== undefined && |
| 258 | news.kmsKeyName !== undefined && |
| 259 | news.kmsKeyName !== previousKey, |
| 260 | }); |
| 261 | }), |
| 262 | |
| 263 | read: Effect.fn(function* ({ id, olds, output }) { |
| 264 | const env = yield* GcpEnvironment.current; |
| 265 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 266 | const repositoryId = yield* toPhysicalId(id, olds?.repositoryId, output?.repositoryId); |
| 267 | const name = output?.name ?? resourceName(env.project, location, repositoryId); |
| 268 | const existing = yield* getByName(name); |
| 269 | if (existing === undefined) return undefined; |
| 270 | const attrs = toAttrs(existing, env.project, env.region); |
| 271 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 272 | }), |
| 273 | |
| 274 | list: () => |
| 275 | Effect.gen(function* () { |
| 276 | const env = yield* GcpEnvironment.current; |
| 277 | const repos = yield* listRepositories(env.project, env.region); |
| 278 | return repos |
| 279 | .filter((repo) => hasAlchemyLabelMap(repo.labels)) |
| 280 | .map((repo) => toAttrs(repo, env.project, env.region)); |
| 281 | }), |
| 282 | |
| 283 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 284 | const env = yield* GcpEnvironment.current; |
| 285 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 286 | const repositoryId = yield* toPhysicalId(id, news.repositoryId, output?.repositoryId); |
| 287 | const name = resourceName(env.project, location, repositoryId); |
| 288 | const desiredLabels = { |
| 289 | ...toLabels(news.labels), |
| 290 | ...(yield* createInternalLabels(id)), |
| 291 | }; |
| 292 | const displayName = news.displayName ?? repositoryId; |
| 293 | const gitRemoteSettings = gitOf(news.gitRemoteSettings); |
| 294 | const containingFolder = |
| 295 | news.containingFolder !== undefined |
| 296 | ? news.containingFolder.includes("/") |
| 297 | ? news.containingFolder |
| 298 | : expandParent(news.containingFolder, env.project, location, "folders") |
| 299 | : undefined; |
| 300 | |
| 301 | let current = yield* getByName(output?.name ?? name); |
| 302 | |
| 303 | if (current === undefined) { |
| 304 | const created = yield* retryTransient( |
| 305 | dataform.createProjectsLocationsRepositories({ |
| 306 | parent: locationParent(env.project, location), |
| 307 | repositoryId, |
| 308 | body: { |
| 309 | displayName, |
| 310 | labels: desiredLabels, |
| 311 | containingFolder, |
| 312 | gitRemoteSettings, |
| 313 | npmrcEnvironmentVariablesSecretVersion: news.npmrcEnvironmentVariablesSecretVersion, |
| 314 | serviceAccount: news.serviceAccount, |
| 315 | workspaceCompilationOverrides: news.workspaceCompilationOverrides, |
| 316 | kmsKeyName: news.kmsKeyName, |
| 317 | setAuthenticatedUserAdmin: news.setAuthenticatedUserAdmin ?? true, |
| 318 | }, |
| 319 | }), |
| 320 | ).pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 321 | current = created ?? undefined; |
| 322 | if (current === undefined) { |
| 323 | current = yield* waitUntilExists(getByName(name), name); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (current === undefined) { |
| 328 | return yield* new RepositoryNotResolved({ resourceName: name }); |
| 329 | } |
| 330 | |
| 331 | const currentName = current.name ?? name; |
| 332 | const observedLabels = tagRecord(current.labels); |
| 333 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 334 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 335 | const displayChanged = !sameText(current.displayName, displayName); |
| 336 | const gitChanged = !sameJson( |
| 337 | current.gitRemoteSettings |
| 338 | ? { |
| 339 | url: current.gitRemoteSettings.url, |
| 340 | defaultBranch: current.gitRemoteSettings.defaultBranch, |
| 341 | authenticationTokenSecretVersion: |
| 342 | current.gitRemoteSettings.authenticationTokenSecretVersion, |
| 343 | gitRepositoryLink: current.gitRemoteSettings.gitRepositoryLink, |
| 344 | sshAuthenticationConfig: current.gitRemoteSettings.sshAuthenticationConfig, |
| 345 | } |
| 346 | : undefined, |
| 347 | gitRemoteSettings, |
| 348 | ); |
| 349 | const npmrcChanged = !sameText( |
| 350 | current.npmrcEnvironmentVariablesSecretVersion, |
| 351 | news.npmrcEnvironmentVariablesSecretVersion, |
| 352 | ); |
| 353 | const serviceAccountChanged = !sameText(current.serviceAccount, news.serviceAccount); |
| 354 | const overridesChanged = !sameJson( |
| 355 | current.workspaceCompilationOverrides, |
| 356 | news.workspaceCompilationOverrides, |
| 357 | ); |
| 358 | |
| 359 | if ( |
| 360 | labelsChanged || |
| 361 | displayChanged || |
| 362 | gitChanged || |
| 363 | npmrcChanged || |
| 364 | serviceAccountChanged || |
| 365 | overridesChanged |
| 366 | ) { |
| 367 | current = yield* retryTransient( |
| 368 | dataform.patchProjectsLocationsRepositories({ |
| 369 | name: currentName, |
| 370 | updateMask: updateMaskOf( |
| 371 | labelsChanged ? "labels" : undefined, |
| 372 | displayChanged ? "displayName" : undefined, |
| 373 | gitChanged ? "gitRemoteSettings" : undefined, |
| 374 | npmrcChanged ? "npmrcEnvironmentVariablesSecretVersion" : undefined, |
| 375 | serviceAccountChanged ? "serviceAccount" : undefined, |
| 376 | overridesChanged ? "workspaceCompilationOverrides" : undefined, |
| 377 | ), |
| 378 | body: { |
| 379 | displayName, |
| 380 | labels: desiredLabels, |
| 381 | gitRemoteSettings, |
| 382 | npmrcEnvironmentVariablesSecretVersion: news.npmrcEnvironmentVariablesSecretVersion, |
| 383 | serviceAccount: news.serviceAccount, |
| 384 | workspaceCompilationOverrides: news.workspaceCompilationOverrides, |
| 385 | }, |
| 386 | }), |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | const observedFolder = current.containingFolder ?? ""; |
| 391 | const desiredFolder = containingFolder ?? ""; |
| 392 | if (desiredFolder !== observedFolder) { |
| 393 | const moved = yield* retryTransient( |
| 394 | dataform.moveProjectsLocationsRepositories({ |
| 395 | name: currentName, |
| 396 | body: { destinationContainingFolder: desiredFolder }, |
| 397 | }), |
| 398 | ); |
| 399 | yield* waitForOperation(moved); |
| 400 | current = (yield* getByName(currentName)) ?? current; |
| 401 | } |
| 402 | |
| 403 | return toAttrs(current, env.project, env.region); |
| 404 | }), |
| 405 | |
| 406 | delete: Effect.fn(function* ({ output }) { |
| 407 | if (!output.name) return; |
| 408 | const parent = output.name; |
| 409 | const children = [ |
| 410 | ...(yield* listWorkspaces(parent)), |
| 411 | ...(yield* listWorkflowInvocations(parent)), |
| 412 | ...(yield* listWorkflowConfigs(parent)), |
| 413 | ...(yield* listReleaseConfigs(parent)), |
| 414 | ]; |
| 415 | yield* Effect.forEach( |
| 416 | children, |
| 417 | (child) => { |
| 418 | const name = child.name ?? ""; |
| 419 | if (name.includes("/workspaces/")) { |
| 420 | return dataform |
| 421 | .deleteProjectsLocationsRepositoriesWorkspaces({ name }) |
| 422 | .pipe(Effect.catchTag(["NotFound", "BadRequest"], () => Effect.void)); |
| 423 | } |
| 424 | if (name.includes("/workflowInvocations/")) { |
| 425 | return dataform |
| 426 | .deleteProjectsLocationsRepositoriesWorkflowInvocations({ |
| 427 | name, |
| 428 | }) |
| 429 | .pipe(Effect.catchTag(["NotFound", "BadRequest"], () => Effect.void)); |
| 430 | } |
| 431 | if (name.includes("/workflowConfigs/")) { |
| 432 | return dataform |
| 433 | .deleteProjectsLocationsRepositoriesWorkflowConfigs({ name }) |
| 434 | .pipe(Effect.catchTag(["NotFound", "BadRequest"], () => Effect.void)); |
| 435 | } |
| 436 | if (name.includes("/releaseConfigs/")) { |
| 437 | return dataform |
| 438 | .deleteProjectsLocationsRepositoriesReleaseConfigs({ name }) |
| 439 | .pipe(Effect.catchTag(["NotFound", "BadRequest"], () => Effect.void)); |
| 440 | } |
| 441 | return Effect.void; |
| 442 | }, |
| 443 | { concurrency: 4 }, |
| 444 | ); |
| 445 | yield* retryTransient( |
| 446 | dataform.deleteProjectsLocationsRepositories({ |
| 447 | name: output.name, |
| 448 | force: true, |
| 449 | }), |
| 450 | ).pipe( |
| 451 | Effect.retry({ |
| 452 | while: (error) => error._tag === "Conflict", |
| 453 | times: 8, |
| 454 | schedule: Schedule.spaced("2 seconds"), |
| 455 | }), |
| 456 | Effect.catchTag("NotFound", () => Effect.void), |
| 457 | ); |
| 458 | yield* waitUntilGone(getByName(output.name)); |
| 459 | }), |
| 460 | }); |