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 * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | expandRepository, |
| 12 | forEachOwnedRepository, |
| 13 | hasAlchemyLabelMap, |
| 14 | listWorkflowConfigs, |
| 15 | normalizeLocation, |
| 16 | parseResourceName, |
| 17 | replaceOnIdentity, |
| 18 | retryTransient, |
| 19 | sameJson, |
| 20 | sameText, |
| 21 | toPhysicalId, |
| 22 | updateMaskOf, |
| 23 | waitUntilExists, |
| 24 | waitUntilGone, |
| 25 | } from "./internal.ts"; |
| 26 | |
| 27 | export type InvocationTarget = { |
| 28 | /** Action name within `database` and `schema`. */ |
| 29 | name?: string; |
| 30 | /** Database (Google Cloud project id). */ |
| 31 | database?: string; |
| 32 | /** Schema (BigQuery dataset id). */ |
| 33 | schema?: string; |
| 34 | }; |
| 35 | |
| 36 | export type InvocationConfig = { |
| 37 | /** Include transitive dependents of included actions. */ |
| 38 | transitiveDependentsIncluded?: boolean; |
| 39 | /** Service account used to run the invocation. */ |
| 40 | serviceAccount?: string; |
| 41 | /** |
| 42 | * Action tags to include. Alchemy ownership tags (`alchemy-stack`, |
| 43 | * `alchemy-stage`, `alchemy-id`) are merged in because workflow |
| 44 | * configs have no labels field. |
| 45 | */ |
| 46 | includedTags?: string[]; |
| 47 | /** Fully refresh incremental tables. */ |
| 48 | fullyRefreshIncrementalTablesEnabled?: boolean; |
| 49 | /** BigQuery query priority (`INTERACTIVE` or `BATCH`). */ |
| 50 | queryPriority?: dataform.InvocationConfigQueryPriorityEnum | (string & {}); |
| 51 | /** Include transitive dependencies of included actions. */ |
| 52 | transitiveDependenciesIncluded?: boolean; |
| 53 | /** Action identifiers to include. */ |
| 54 | includedTargets?: InvocationTarget[]; |
| 55 | }; |
| 56 | |
| 57 | export type RepositoriesWorkflowConfigProps = { |
| 58 | /** |
| 59 | * Parent repository. Full name |
| 60 | * `projects/{project}/locations/{location}/repositories/{repository}` |
| 61 | * or the repository id (combined with `location`). Immutable — |
| 62 | * changing it replaces the workflow config. |
| 63 | */ |
| 64 | repository: string; |
| 65 | /** |
| 66 | * Region used when `repository` is a bare id. |
| 67 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 68 | */ |
| 69 | location?: string; |
| 70 | /** |
| 71 | * Workflow config id. If omitted, a unique RFC1035 name is generated. |
| 72 | * Immutable — changing it replaces the workflow config. |
| 73 | */ |
| 74 | workflowConfigId?: string; |
| 75 | /** |
| 76 | * Release config whose `releaseCompilationResult` is executed. Full |
| 77 | * name `.../repositories/{repository}/releaseConfigs/{releaseConfig}` |
| 78 | * or the release config id. |
| 79 | */ |
| 80 | releaseConfig: string; |
| 81 | /** |
| 82 | * Cron schedule for automatic workflow invocations. |
| 83 | */ |
| 84 | cronSchedule?: string; |
| 85 | /** |
| 86 | * IANA time zone used when interpreting `cronSchedule`. |
| 87 | * @default "UTC" |
| 88 | */ |
| 89 | timeZone?: string; |
| 90 | /** |
| 91 | * Disable automatic workflow invocations. |
| 92 | * @default false |
| 93 | */ |
| 94 | disabled?: boolean; |
| 95 | /** |
| 96 | * Invocation options. |
| 97 | */ |
| 98 | invocationConfig?: InvocationConfig; |
| 99 | }; |
| 100 | |
| 101 | export type RepositoriesWorkflowConfig = Resource< |
| 102 | "GCP.Dataform.RepositoriesWorkflowConfig", |
| 103 | RepositoriesWorkflowConfigProps, |
| 104 | { |
| 105 | /** Full resource name `.../repositories/{repository}/workflowConfigs/{workflowConfig}`. */ |
| 106 | name: string; |
| 107 | /** Workflow config id (last path segment). */ |
| 108 | workflowConfigId: string; |
| 109 | /** Parent repository resource name. */ |
| 110 | repository: string; |
| 111 | /** Project id. */ |
| 112 | project: string; |
| 113 | /** Location id. */ |
| 114 | location: string; |
| 115 | /** Release config resource name. */ |
| 116 | releaseConfig: string | undefined; |
| 117 | /** Cron schedule, if set. */ |
| 118 | cronSchedule: string | undefined; |
| 119 | /** Time zone. */ |
| 120 | timeZone: string | undefined; |
| 121 | /** Whether automatic invocations are disabled. */ |
| 122 | disabled: boolean; |
| 123 | /** User invocation tags (Alchemy ownership tags stripped). */ |
| 124 | includedTags: string[] | undefined; |
| 125 | /** RFC3339 creation timestamp. */ |
| 126 | createTime: string | undefined; |
| 127 | /** RFC3339 last-update timestamp. */ |
| 128 | updateTime: string | undefined; |
| 129 | }, |
| 130 | never, |
| 131 | Providers |
| 132 | >; |
| 133 | |
| 134 | /** |
| 135 | * A Dataform workflow configuration — executes a release config's |
| 136 | * compilation result on a schedule (or on demand). |
| 137 | * |
| 138 | * Workflow configs have no labels field, so ownership is the parent |
| 139 | * repository's Alchemy labels. Changing `workflowConfigId`, |
| 140 | * `repository`, or `location` replaces the config. Release config, |
| 141 | * schedule, disabled flag, and invocation options update in place. |
| 142 | * |
| 143 | * ### Creating a Workflow Config |
| 144 | * **Example:** Run a release |
| 145 | * ```typescript |
| 146 | * const workflow = yield* GCP.Dataform.RepositoriesWorkflowConfig("Hourly", { |
| 147 | * repository: repo.name, |
| 148 | * releaseConfig: release.name, |
| 149 | * }); |
| 150 | * ``` |
| 151 | * |
| 152 | * **Example:** Disabled schedule |
| 153 | * ```typescript |
| 154 | * const workflow = yield* GCP.Dataform.RepositoriesWorkflowConfig("Hourly", { |
| 155 | * repository: repo.name, |
| 156 | * releaseConfig: release.name, |
| 157 | * cronSchedule: "0 * * * *", |
| 158 | * disabled: true, |
| 159 | * }); |
| 160 | * ``` |
| 161 | * |
| 162 | * @resource |
| 163 | * @category Dataform |
| 164 | */ |
| 165 | export const RepositoriesWorkflowConfig = Resource<RepositoriesWorkflowConfig>( |
| 166 | "GCP.Dataform.RepositoriesWorkflowConfig", |
| 167 | ); |
| 168 | |
| 169 | export class RepositoriesWorkflowConfigNotResolved extends Data.TaggedError( |
| 170 | "GCP.Dataform.RepositoriesWorkflowConfigNotResolved", |
| 171 | )<{ |
| 172 | resourceName: string; |
| 173 | }> {} |
| 174 | |
| 175 | const resourceName = (repository: string, workflowConfigId: string) => |
| 176 | `${repository}/workflowConfigs/${workflowConfigId}`; |
| 177 | |
| 178 | const invocationOf = ( |
| 179 | config: InvocationConfig | undefined, |
| 180 | ): dataform.InvocationConfig | undefined => { |
| 181 | if (config === undefined) return undefined; |
| 182 | return { |
| 183 | transitiveDependentsIncluded: config.transitiveDependentsIncluded, |
| 184 | serviceAccount: config.serviceAccount, |
| 185 | includedTags: config.includedTags, |
| 186 | fullyRefreshIncrementalTablesEnabled: config.fullyRefreshIncrementalTablesEnabled, |
| 187 | queryPriority: config.queryPriority, |
| 188 | transitiveDependenciesIncluded: config.transitiveDependenciesIncluded, |
| 189 | includedTargets: config.includedTargets, |
| 190 | }; |
| 191 | }; |
| 192 | |
| 193 | const toAttrs = (config: dataform.WorkflowConfig, project: string, region: string) => { |
| 194 | const name = config.name ?? ""; |
| 195 | const parsed = parseResourceName(name, "workflowConfigs", region); |
| 196 | return { |
| 197 | name, |
| 198 | workflowConfigId: parsed.id, |
| 199 | repository: parsed.parent, |
| 200 | project: parsed.project || project, |
| 201 | location: parsed.location, |
| 202 | releaseConfig: config.releaseConfig, |
| 203 | cronSchedule: config.cronSchedule, |
| 204 | timeZone: config.timeZone, |
| 205 | disabled: config.disabled === true, |
| 206 | includedTags: config.invocationConfig?.includedTags, |
| 207 | createTime: config.createTime, |
| 208 | updateTime: config.updateTime, |
| 209 | }; |
| 210 | }; |
| 211 | |
| 212 | const getByName = (name: string) => |
| 213 | name.length === 0 || name.includes("//") |
| 214 | ? Effect.undefined |
| 215 | : dataform |
| 216 | .getProjectsLocationsRepositoriesWorkflowConfigs({ name }) |
| 217 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 218 | |
| 219 | export const RepositoriesWorkflowConfigProvider = () => |
| 220 | Provider.succeed(RepositoriesWorkflowConfig, { |
| 221 | stables: ["name", "workflowConfigId", "repository", "project", "location", "createTime"], |
| 222 | |
| 223 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 224 | if (!isResolved(news)) return undefined; |
| 225 | const env = yield* GcpEnvironment.current; |
| 226 | const location = normalizeLocation( |
| 227 | news.location ?? olds?.location ?? output?.location, |
| 228 | env.region, |
| 229 | ); |
| 230 | return replaceOnIdentity({ |
| 231 | previousId: olds?.workflowConfigId ?? output?.workflowConfigId, |
| 232 | nextId: news.workflowConfigId ?? olds?.workflowConfigId ?? output?.workflowConfigId, |
| 233 | previousLocation: olds?.location ?? output?.location, |
| 234 | nextLocation: news.location ?? olds?.location ?? output?.location, |
| 235 | previousParent: olds?.repository ?? output?.repository, |
| 236 | nextParent: expandRepository(news.repository, env.project, location), |
| 237 | extra: |
| 238 | news.invocationConfig !== undefined && |
| 239 | olds?.invocationConfig !== undefined && |
| 240 | !sameJson(olds.invocationConfig, news.invocationConfig), |
| 241 | }); |
| 242 | }), |
| 243 | |
| 244 | read: Effect.fn(function* ({ id, olds, output }) { |
| 245 | const env = yield* GcpEnvironment.current; |
| 246 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 247 | const repository = expandRepository( |
| 248 | olds?.repository ?? |
| 249 | output?.repository ?? |
| 250 | parseResourceName(output?.name ?? "", "workflowConfigs", env.region).parent, |
| 251 | env.project, |
| 252 | location, |
| 253 | ); |
| 254 | const workflowConfigId = yield* toPhysicalId( |
| 255 | id, |
| 256 | olds?.workflowConfigId, |
| 257 | output?.workflowConfigId, |
| 258 | ); |
| 259 | const name = output?.name ?? resourceName(repository, workflowConfigId); |
| 260 | const existing = yield* getByName(name); |
| 261 | if (existing === undefined) return undefined; |
| 262 | const attrs = toAttrs(existing, env.project, env.region); |
| 263 | const parent = yield* dataform |
| 264 | .getProjectsLocationsRepositories({ name: attrs.repository }) |
| 265 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 266 | if (parent === undefined) return Unowned(attrs); |
| 267 | return hasAlchemyLabelMap(parent.labels) ? attrs : Unowned(attrs); |
| 268 | }), |
| 269 | |
| 270 | list: () => |
| 271 | Effect.gen(function* () { |
| 272 | const env = yield* GcpEnvironment.current; |
| 273 | const configs = yield* forEachOwnedRepository(env.project, env.region, (repo) => |
| 274 | listWorkflowConfigs(repo.name ?? ""), |
| 275 | ); |
| 276 | return configs.map((item) => toAttrs(item, env.project, env.region)); |
| 277 | }), |
| 278 | |
| 279 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 280 | const env = yield* GcpEnvironment.current; |
| 281 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 282 | const repository = expandRepository(news.repository, env.project, location); |
| 283 | const workflowConfigId = yield* toPhysicalId( |
| 284 | id, |
| 285 | news.workflowConfigId, |
| 286 | output?.workflowConfigId, |
| 287 | ); |
| 288 | const name = resourceName(repository, workflowConfigId); |
| 289 | const resolvedRelease = news.releaseConfig.includes("/") |
| 290 | ? news.releaseConfig |
| 291 | : `${repository}/releaseConfigs/${news.releaseConfig}`; |
| 292 | const disabled = news.disabled === true; |
| 293 | const invocationConfig = invocationOf(news.invocationConfig); |
| 294 | |
| 295 | let current = yield* getByName(output?.name ?? name); |
| 296 | |
| 297 | if (current === undefined) { |
| 298 | const created = yield* retryTransient( |
| 299 | dataform.createProjectsLocationsRepositoriesWorkflowConfigs({ |
| 300 | parent: repository, |
| 301 | workflowConfigId, |
| 302 | body: { |
| 303 | releaseConfig: resolvedRelease, |
| 304 | cronSchedule: news.cronSchedule, |
| 305 | timeZone: news.timeZone, |
| 306 | disabled, |
| 307 | invocationConfig, |
| 308 | }, |
| 309 | }), |
| 310 | ).pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 311 | current = created ?? undefined; |
| 312 | if (current === undefined) { |
| 313 | current = yield* waitUntilExists(getByName(name), name); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (current === undefined) { |
| 318 | return yield* new RepositoriesWorkflowConfigNotResolved({ resourceName: name }); |
| 319 | } |
| 320 | |
| 321 | const currentName = current.name ?? name; |
| 322 | const releaseChanged = !sameText(current.releaseConfig, resolvedRelease); |
| 323 | const cronChanged = !sameText(current.cronSchedule, news.cronSchedule); |
| 324 | const zoneChanged = !sameText(current.timeZone, news.timeZone); |
| 325 | const disabledChanged = (current.disabled === true) !== disabled; |
| 326 | |
| 327 | if (releaseChanged || cronChanged || zoneChanged || disabledChanged) { |
| 328 | current = yield* retryTransient( |
| 329 | dataform.patchProjectsLocationsRepositoriesWorkflowConfigs({ |
| 330 | name: currentName, |
| 331 | updateMask: updateMaskOf( |
| 332 | releaseChanged ? "releaseConfig" : undefined, |
| 333 | cronChanged ? "cronSchedule" : undefined, |
| 334 | zoneChanged ? "timeZone" : undefined, |
| 335 | disabledChanged ? "disabled" : undefined, |
| 336 | ), |
| 337 | body: { |
| 338 | releaseConfig: resolvedRelease, |
| 339 | cronSchedule: news.cronSchedule, |
| 340 | timeZone: news.timeZone, |
| 341 | disabled, |
| 342 | }, |
| 343 | }), |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | return toAttrs(current, env.project, env.region); |
| 348 | }), |
| 349 | |
| 350 | delete: Effect.fn(function* ({ output }) { |
| 351 | if (!output.name) return; |
| 352 | yield* retryTransient( |
| 353 | dataform.deleteProjectsLocationsRepositoriesWorkflowConfigs({ |
| 354 | name: output.name, |
| 355 | }), |
| 356 | ).pipe( |
| 357 | Effect.catchTags({ |
| 358 | NotFound: () => Effect.void, |
| 359 | BadRequest: () => Effect.void, |
| 360 | }), |
| 361 | ); |
| 362 | yield* waitUntilGone(getByName(output.name)); |
| 363 | }), |
| 364 | }); |