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
| 1 | import * as codebuild from "@distilled.cloud/aws/codebuild"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import { toWireMinutes } from "alchemy/Util/Duration"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import type * as Duration from "effect/Duration"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import { |
| 15 | normalizePolicyDocument, |
| 16 | stringifyPolicyDocument, |
| 17 | type PolicyDocument, |
| 18 | } from "../IAM/Policy.ts"; |
| 19 | import type { Providers } from "../Providers.ts"; |
| 20 | |
| 21 | export class ProjectUnexpectedResponse extends Data.TaggedError("ProjectUnexpectedResponse")<{ |
| 22 | readonly message: string; |
| 23 | readonly cause?: unknown; |
| 24 | }> {} |
| 25 | |
| 26 | /** |
| 27 | * Where CodeBuild fetches the source to build. |
| 28 | */ |
| 29 | export interface ProjectSourceConfig { |
| 30 | /** |
| 31 | * Source provider type. `NO_SOURCE` builds run only the inline `buildspec` |
| 32 | * (nothing is fetched); `S3` pulls an object; the git types |
| 33 | * (`CODECOMMIT`/`GITHUB`/`GITLAB`/`BITBUCKET`/…) require a matching |
| 34 | * source credential or connection. |
| 35 | */ |
| 36 | type: |
| 37 | | "NO_SOURCE" |
| 38 | | "S3" |
| 39 | | "CODECOMMIT" |
| 40 | | "CODEPIPELINE" |
| 41 | | "GITHUB" |
| 42 | | "GITHUB_ENTERPRISE" |
| 43 | | "GITLAB" |
| 44 | | "GITLAB_SELF_MANAGED" |
| 45 | | "BITBUCKET"; |
| 46 | /** |
| 47 | * Source location. Required for every type except `NO_SOURCE` and |
| 48 | * `CODEPIPELINE`. For `S3` this is `bucket/path/to/object.zip`; for git |
| 49 | * types it is the clone URL. |
| 50 | */ |
| 51 | location?: string; |
| 52 | /** |
| 53 | * Inline build spec (YAML/JSON). Required when `type` is `NO_SOURCE`; |
| 54 | * otherwise overrides the `buildspec.yml` in the source root. May also be |
| 55 | * an `arn:aws:s3:::` path to a build spec object. |
| 56 | */ |
| 57 | buildspec?: string; |
| 58 | /** |
| 59 | * Depth of history to fetch for git sources. `0` fetches full history. |
| 60 | */ |
| 61 | gitCloneDepth?: number; |
| 62 | /** |
| 63 | * Report the build's start and completion status back to the source |
| 64 | * provider (git sources only). |
| 65 | */ |
| 66 | reportBuildStatus?: boolean; |
| 67 | /** |
| 68 | * Ignore TLS errors when connecting to the source (git sources only). |
| 69 | */ |
| 70 | insecureSsl?: boolean; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Where CodeBuild writes build output artifacts. |
| 75 | */ |
| 76 | export interface ProjectArtifactsConfig { |
| 77 | /** |
| 78 | * Artifact destination. `NO_ARTIFACTS` discards output; `S3` uploads to a |
| 79 | * bucket; `CODEPIPELINE` hands artifacts back to a pipeline stage. |
| 80 | * @default "NO_ARTIFACTS" |
| 81 | */ |
| 82 | type: "NO_ARTIFACTS" | "S3" | "CODEPIPELINE"; |
| 83 | /** |
| 84 | * Output bucket name (for `type: "S3"`). |
| 85 | */ |
| 86 | location?: string; |
| 87 | /** |
| 88 | * Path inside the bucket to write to. |
| 89 | */ |
| 90 | path?: string; |
| 91 | /** |
| 92 | * Whether to prefix the artifact path with the build ID. |
| 93 | * @default "NONE" |
| 94 | */ |
| 95 | namespaceType?: "NONE" | "BUILD_ID"; |
| 96 | /** |
| 97 | * Name of the artifact object/folder. |
| 98 | */ |
| 99 | name?: string; |
| 100 | /** |
| 101 | * Whether the artifact is zipped. |
| 102 | * @default "NONE" |
| 103 | */ |
| 104 | packaging?: "NONE" | "ZIP"; |
| 105 | /** |
| 106 | * Use the artifact `name` verbatim instead of appending it to the path. |
| 107 | */ |
| 108 | overrideArtifactName?: boolean; |
| 109 | /** |
| 110 | * Disable default artifact encryption. |
| 111 | */ |
| 112 | encryptionDisabled?: boolean; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * An environment variable exposed to the build container. |
| 117 | */ |
| 118 | export interface ProjectEnvironmentVariable { |
| 119 | /** Variable name. */ |
| 120 | name: string; |
| 121 | /** |
| 122 | * Variable value. For `PLAINTEXT` this is the literal value; for |
| 123 | * `PARAMETER_STORE`/`SECRETS_MANAGER` it is the parameter name / secret |
| 124 | * ARN to resolve at build time. |
| 125 | */ |
| 126 | value: string; |
| 127 | /** |
| 128 | * How to interpret `value`. |
| 129 | * @default "PLAINTEXT" |
| 130 | */ |
| 131 | type?: "PLAINTEXT" | "PARAMETER_STORE" | "SECRETS_MANAGER"; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * The build container: image, compute size, and runtime settings. |
| 136 | */ |
| 137 | export interface ProjectEnvironmentConfig { |
| 138 | /** |
| 139 | * Container environment type. |
| 140 | * @default "LINUX_CONTAINER" |
| 141 | */ |
| 142 | type?: |
| 143 | | "LINUX_CONTAINER" |
| 144 | | "LINUX_GPU_CONTAINER" |
| 145 | | "ARM_CONTAINER" |
| 146 | | "WINDOWS_SERVER_2019_CONTAINER" |
| 147 | | "WINDOWS_SERVER_2022_CONTAINER" |
| 148 | | "LINUX_LAMBDA_CONTAINER" |
| 149 | | "ARM_LAMBDA_CONTAINER" |
| 150 | | "MAC_ARM"; |
| 151 | /** |
| 152 | * Docker image to run the build in, e.g. |
| 153 | * `aws/codebuild/amazonlinux2-x86_64-standard:5.0`. |
| 154 | */ |
| 155 | image: string; |
| 156 | /** |
| 157 | * Compute size for the build fleet. |
| 158 | * @default "BUILD_GENERAL1_SMALL" |
| 159 | */ |
| 160 | computeType?: |
| 161 | | "BUILD_GENERAL1_SMALL" |
| 162 | | "BUILD_GENERAL1_MEDIUM" |
| 163 | | "BUILD_GENERAL1_LARGE" |
| 164 | | "BUILD_GENERAL1_XLARGE" |
| 165 | | "BUILD_GENERAL1_2XLARGE" |
| 166 | | "BUILD_LAMBDA_1GB" |
| 167 | | "BUILD_LAMBDA_2GB" |
| 168 | | "BUILD_LAMBDA_4GB" |
| 169 | | "BUILD_LAMBDA_8GB" |
| 170 | | "BUILD_LAMBDA_10GB"; |
| 171 | /** |
| 172 | * Environment variables available to every build. |
| 173 | */ |
| 174 | environmentVariables?: ProjectEnvironmentVariable[]; |
| 175 | /** |
| 176 | * Run the build container in privileged mode (required to build Docker |
| 177 | * images inside the build). |
| 178 | * @default false |
| 179 | */ |
| 180 | privilegedMode?: boolean; |
| 181 | /** |
| 182 | * ARN of an S3 object holding a PEM-encoded certificate to install. |
| 183 | */ |
| 184 | certificate?: string; |
| 185 | /** |
| 186 | * Credentials used to pull the build image. |
| 187 | * @default "CODEBUILD" |
| 188 | */ |
| 189 | imagePullCredentialsType?: "CODEBUILD" | "SERVICE_ROLE"; |
| 190 | } |
| 191 | |
| 192 | /** Build log destinations for a CodeBuild project. */ |
| 193 | export interface ProjectLogsConfig { |
| 194 | /** CloudWatch Logs delivery configuration. */ |
| 195 | cloudWatchLogs?: { |
| 196 | /** Whether CloudWatch Logs delivery is enabled. */ |
| 197 | status: "ENABLED" | "DISABLED"; |
| 198 | /** Optional CloudWatch log group name. */ |
| 199 | groupName?: string; |
| 200 | /** Optional CloudWatch log stream name. */ |
| 201 | streamName?: string; |
| 202 | }; |
| 203 | /** S3 log delivery configuration. */ |
| 204 | s3Logs?: { |
| 205 | /** Whether S3 log delivery is enabled. */ |
| 206 | status: "ENABLED" | "DISABLED"; |
| 207 | /** S3 bucket and prefix for build logs. */ |
| 208 | location?: string; |
| 209 | /** Disable default encryption for S3 build logs. */ |
| 210 | encryptionDisabled?: boolean; |
| 211 | /** Access granted to the destination bucket owner. */ |
| 212 | bucketOwnerAccess?: "NONE" | "READ_ONLY" | "FULL"; |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | export interface ProjectProps { |
| 217 | /** |
| 218 | * Name of the build project (2-255 chars). If omitted a deterministic |
| 219 | * physical name is generated. Changing the name replaces the project. |
| 220 | */ |
| 221 | projectName?: string; |
| 222 | /** |
| 223 | * Description of the project. |
| 224 | */ |
| 225 | description?: string; |
| 226 | /** |
| 227 | * Source configuration. |
| 228 | */ |
| 229 | source: ProjectSourceConfig; |
| 230 | /** |
| 231 | * Artifact configuration. |
| 232 | * @default { type: "NO_ARTIFACTS" } |
| 233 | */ |
| 234 | artifacts?: ProjectArtifactsConfig; |
| 235 | /** |
| 236 | * Build container configuration. |
| 237 | */ |
| 238 | environment: ProjectEnvironmentConfig; |
| 239 | /** |
| 240 | * ARN of the IAM role CodeBuild assumes to run builds. Must trust |
| 241 | * `codebuild.amazonaws.com`. |
| 242 | */ |
| 243 | serviceRole: string; |
| 244 | /** |
| 245 | * How long a build may run before CodeBuild stops it, e.g. `"1 hour"` |
| 246 | * (5 minutes to 36 hours). Rounded to whole minutes on the wire. |
| 247 | * @default 60 minutes |
| 248 | */ |
| 249 | timeout?: Duration.Input; |
| 250 | /** |
| 251 | * How long a build may sit queued before it is failed, e.g. |
| 252 | * `"30 minutes"` (5 minutes to 8 hours). Rounded to whole minutes on |
| 253 | * the wire. |
| 254 | */ |
| 255 | queuedTimeout?: Duration.Input; |
| 256 | /** |
| 257 | * Maximum number of builds allowed to run concurrently for this project. |
| 258 | */ |
| 259 | concurrentBuildLimit?: number; |
| 260 | /** |
| 261 | * KMS key ARN/alias used to encrypt build output. Defaults to the |
| 262 | * account's default S3 CMK. |
| 263 | */ |
| 264 | encryptionKey?: string; |
| 265 | /** |
| 266 | * Enable a publicly visible build badge. |
| 267 | * @default false |
| 268 | */ |
| 269 | badgeEnabled?: boolean; |
| 270 | /** |
| 271 | * Build log destinations. Set both destinations to `DISABLED` for builds |
| 272 | * that do not need persisted logs. |
| 273 | */ |
| 274 | logsConfig?: ProjectLogsConfig; |
| 275 | /** |
| 276 | * Resource policy attached to the project — shares the project with other |
| 277 | * AWS accounts by granting them read actions such as |
| 278 | * `codebuild:BatchGetProjects`. Accepts a typed {@link PolicyDocument} or a |
| 279 | * raw JSON string (escape hatch / adoption of an existing policy). Omit to |
| 280 | * remove any existing policy. |
| 281 | */ |
| 282 | resourcePolicy?: PolicyDocument | string; |
| 283 | /** |
| 284 | * User-defined tags. |
| 285 | */ |
| 286 | tags?: Record<string, string>; |
| 287 | } |
| 288 | |
| 289 | export interface Project extends Resource< |
| 290 | "AWS.CodeBuild.Project", |
| 291 | ProjectProps, |
| 292 | { |
| 293 | /** Physical name of the build project. */ |
| 294 | projectName: string; |
| 295 | /** ARN of the build project. */ |
| 296 | projectArn: string; |
| 297 | }, |
| 298 | never, |
| 299 | Providers |
| 300 | > {} |
| 301 | |
| 302 | /** |
| 303 | * An AWS CodeBuild build project — a reusable definition of how to run a |
| 304 | * build: the source, the build container, the compute size, the IAM role, |
| 305 | * and where artifacts land. |
| 306 | * |
| 307 | * The project is a definition only; creating it is instant and free. |
| 308 | * Running a build (`StartBuild`) provisions compute and is billed per |
| 309 | * build-minute. |
| 310 | * ### Creating a Project |
| 311 | * **Example:** NO_SOURCE Project with an Inline Buildspec |
| 312 | * ```typescript |
| 313 | * const project = yield* CodeBuild.Project("Hello", { |
| 314 | * serviceRole: role.roleArn, |
| 315 | * source: { |
| 316 | * type: "NO_SOURCE", |
| 317 | * buildspec: [ |
| 318 | * "version: 0.2", |
| 319 | * "phases:", |
| 320 | * " build:", |
| 321 | * " commands:", |
| 322 | * " - echo Hello from CodeBuild", |
| 323 | * ].join("\n"), |
| 324 | * }, |
| 325 | * environment: { |
| 326 | * image: "aws/codebuild/amazonlinux2-x86_64-standard:5.0", |
| 327 | * computeType: "BUILD_GENERAL1_SMALL", |
| 328 | * }, |
| 329 | * }); |
| 330 | * ``` |
| 331 | * |
| 332 | * **Example:** S3-Source Project with S3 Artifacts |
| 333 | * ```typescript |
| 334 | * const project = yield* CodeBuild.Project("Packager", { |
| 335 | * serviceRole: role.roleArn, |
| 336 | * source: { type: "S3", location: `${bucket.bucketName}/source.zip` }, |
| 337 | * artifacts: { type: "S3", location: bucket.bucketName, name: "out.zip", packaging: "ZIP" }, |
| 338 | * environment: { |
| 339 | * image: "aws/codebuild/amazonlinux2-x86_64-standard:5.0", |
| 340 | * environmentVariables: [{ name: "STAGE", value: "prod" }], |
| 341 | * }, |
| 342 | * }); |
| 343 | * ``` |
| 344 | * |
| 345 | * @resource |
| 346 | */ |
| 347 | export const Project = Resource<Project>("AWS.CodeBuild.Project"); |
| 348 | |
| 349 | /** Convert a CodeBuild wire tag list into a plain record. */ |
| 350 | const toTagRecord = ( |
| 351 | tags: ReadonlyArray<{ key?: string; value?: string }> | undefined, |
| 352 | ): Record<string, string> => |
| 353 | Object.fromEntries( |
| 354 | (tags ?? []) |
| 355 | .filter( |
| 356 | (tag): tag is { key: string; value: string } => |
| 357 | typeof tag.key === "string" && typeof tag.value === "string", |
| 358 | ) |
| 359 | .map((tag) => [tag.key, tag.value]), |
| 360 | ); |
| 361 | |
| 362 | const toWireTags = (tags: Record<string, string>): codebuild.Tag[] => |
| 363 | Object.entries(tags).map(([key, value]) => ({ key, value })); |
| 364 | |
| 365 | const toWireSource = (source: ProjectSourceConfig): codebuild.ProjectSource => ({ |
| 366 | type: source.type, |
| 367 | location: source.location, |
| 368 | buildspec: source.buildspec, |
| 369 | gitCloneDepth: source.gitCloneDepth, |
| 370 | reportBuildStatus: source.reportBuildStatus, |
| 371 | insecureSsl: source.insecureSsl, |
| 372 | }); |
| 373 | |
| 374 | const toWireArtifacts = ( |
| 375 | artifacts: ProjectArtifactsConfig | undefined, |
| 376 | ): codebuild.ProjectArtifacts => ({ |
| 377 | type: artifacts?.type ?? "NO_ARTIFACTS", |
| 378 | location: artifacts?.location, |
| 379 | path: artifacts?.path, |
| 380 | namespaceType: artifacts?.namespaceType, |
| 381 | name: artifacts?.name, |
| 382 | packaging: artifacts?.packaging, |
| 383 | overrideArtifactName: artifacts?.overrideArtifactName, |
| 384 | encryptionDisabled: artifacts?.encryptionDisabled, |
| 385 | }); |
| 386 | |
| 387 | const toWireEnvironment = ( |
| 388 | environment: ProjectEnvironmentConfig, |
| 389 | ): codebuild.ProjectEnvironment => ({ |
| 390 | type: environment.type ?? "LINUX_CONTAINER", |
| 391 | image: environment.image, |
| 392 | computeType: environment.computeType ?? "BUILD_GENERAL1_SMALL", |
| 393 | environmentVariables: environment.environmentVariables?.map((v) => ({ |
| 394 | name: v.name, |
| 395 | value: v.value, |
| 396 | type: v.type, |
| 397 | })), |
| 398 | privilegedMode: environment.privilegedMode, |
| 399 | certificate: environment.certificate, |
| 400 | imagePullCredentialsType: environment.imagePullCredentialsType, |
| 401 | }); |
| 402 | |
| 403 | /** |
| 404 | * CodeBuild validates the service role at create/update time; a freshly |
| 405 | * created IAM role is not yet assumable, surfacing as an |
| 406 | * `InvalidInputException` whose message mentions the role is not authorized. |
| 407 | * Retry (bounded) through IAM propagation. The explicit return annotation |
| 408 | * keeps the retry's conditional type out of declaration emit (which would |
| 409 | * otherwise widen the provider layer — see PATTERNS §7). |
| 410 | */ |
| 411 | const retryIamPropagation = <A, E extends { readonly _tag: string; readonly message?: string }, R>( |
| 412 | effect: Effect.Effect<A, E, R>, |
| 413 | ): Effect.Effect<A, E, R> => |
| 414 | effect.pipe( |
| 415 | Effect.retry({ |
| 416 | while: (e) => |
| 417 | e._tag === "InvalidInputException" && |
| 418 | (e.message ?? "").toLowerCase().includes("not authorized"), |
| 419 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 420 | }), |
| 421 | ); |
| 422 | |
| 423 | export const ProjectProvider = () => |
| 424 | Provider.effect( |
| 425 | Project, |
| 426 | Effect.gen(function* () { |
| 427 | const toName = (id: string, props: Partial<ProjectProps>) => |
| 428 | props.projectName |
| 429 | ? Effect.succeed(props.projectName) |
| 430 | : createPhysicalName({ id, maxLength: 128 }); |
| 431 | |
| 432 | /** Read a project by name; a missing project reads as absent. */ |
| 433 | const getProject = Effect.fn(function* (name: string) { |
| 434 | const response = yield* codebuild.batchGetProjects({ names: [name] }); |
| 435 | return response.projects?.[0]; |
| 436 | }); |
| 437 | |
| 438 | return { |
| 439 | stables: ["projectName", "projectArn"], |
| 440 | |
| 441 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 442 | if (!isResolved(news)) return undefined; |
| 443 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 444 | return { action: "replace" } as const; |
| 445 | } |
| 446 | }), |
| 447 | |
| 448 | read: Effect.fn(function* ({ id, olds, output }) { |
| 449 | const name = output?.projectName ?? (yield* toName(id, olds ?? {})); |
| 450 | const project = yield* getProject(name); |
| 451 | if (project === undefined || project.arn === undefined) { |
| 452 | return undefined; |
| 453 | } |
| 454 | const attrs = { |
| 455 | projectName: project.name ?? name, |
| 456 | projectArn: project.arn, |
| 457 | }; |
| 458 | const tags = toTagRecord(project.tags); |
| 459 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 460 | }), |
| 461 | |
| 462 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 463 | const name = output?.projectName ?? (yield* toName(id, news)); |
| 464 | const internalTags = yield* createInternalTags(id); |
| 465 | const desiredTags = { ...internalTags, ...news.tags }; |
| 466 | |
| 467 | // 1. Observe — cloud state is authoritative. |
| 468 | let observed = yield* getProject(name); |
| 469 | |
| 470 | const spec = { |
| 471 | description: news.description, |
| 472 | source: toWireSource(news.source), |
| 473 | artifacts: toWireArtifacts(news.artifacts), |
| 474 | environment: toWireEnvironment(news.environment), |
| 475 | serviceRole: news.serviceRole, |
| 476 | // The CodeBuild wire unit for build/queue timeouts is whole minutes. |
| 477 | timeoutInMinutes: toWireMinutes(news.timeout), |
| 478 | queuedTimeoutInMinutes: toWireMinutes(news.queuedTimeout), |
| 479 | concurrentBuildLimit: news.concurrentBuildLimit, |
| 480 | encryptionKey: news.encryptionKey, |
| 481 | badgeEnabled: news.badgeEnabled, |
| 482 | logsConfig: news.logsConfig, |
| 483 | tags: toWireTags(desiredTags), |
| 484 | }; |
| 485 | |
| 486 | // 3. Sync — updateProject is a full upsert of the definition and |
| 487 | // tags; apply the desired spec (project updates are instant). A |
| 488 | // delete→redeploy race can leave batchGetProjects returning the |
| 489 | // just-deleted project; updateProject then reports the truth with |
| 490 | // a typed ResourceNotFoundException — treat it as missing. |
| 491 | if (observed !== undefined) { |
| 492 | observed = yield* retryIamPropagation(codebuild.updateProject({ name, ...spec })).pipe( |
| 493 | Effect.map((updated) => updated.project), |
| 494 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 495 | ); |
| 496 | } |
| 497 | |
| 498 | // 2. Ensure — create if missing; tolerate the create/create race. |
| 499 | if (observed === undefined) { |
| 500 | const created = yield* retryIamPropagation( |
| 501 | codebuild.createProject({ name, ...spec }), |
| 502 | ).pipe( |
| 503 | Effect.catchTag("ResourceAlreadyExistsException", () => |
| 504 | getProject(name).pipe(Effect.map((p) => ({ project: p }))), |
| 505 | ), |
| 506 | ); |
| 507 | observed = created.project; |
| 508 | } |
| 509 | |
| 510 | if (observed === undefined || observed.arn === undefined) { |
| 511 | return yield* new ProjectUnexpectedResponse({ |
| 512 | message: `CodeBuild project '${name}' disappeared while reconciling`, |
| 513 | }); |
| 514 | } |
| 515 | |
| 516 | // 3b. Sync — resource policy. Compare observed against desired in |
| 517 | // normalized form (keys sorted, no whitespace) so a re-deploy of an |
| 518 | // equivalent document — regardless of key order or string vs typed |
| 519 | // object — is a no-op that skips the API entirely. |
| 520 | const desiredPolicy = |
| 521 | news.resourcePolicy === undefined |
| 522 | ? undefined |
| 523 | : typeof news.resourcePolicy === "string" |
| 524 | ? news.resourcePolicy |
| 525 | : stringifyPolicyDocument(news.resourcePolicy); |
| 526 | const observedPolicy = yield* codebuild |
| 527 | .getResourcePolicy({ resourceArn: observed.arn }) |
| 528 | .pipe( |
| 529 | Effect.map((res) => res.policy), |
| 530 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 531 | ); |
| 532 | if (desiredPolicy === undefined) { |
| 533 | if (observedPolicy !== undefined && observedPolicy !== "") { |
| 534 | yield* codebuild.deleteResourcePolicy({ |
| 535 | resourceArn: observed.arn, |
| 536 | }); |
| 537 | } |
| 538 | } else if ( |
| 539 | observedPolicy === undefined || |
| 540 | normalizePolicyDocument(observedPolicy) !== normalizePolicyDocument(desiredPolicy) |
| 541 | ) { |
| 542 | yield* codebuild.putResourcePolicy({ |
| 543 | resourceArn: observed.arn, |
| 544 | policy: desiredPolicy, |
| 545 | }); |
| 546 | } |
| 547 | |
| 548 | // 4. Return fresh attributes. |
| 549 | yield* session.note(name); |
| 550 | return { |
| 551 | projectName: observed.name ?? name, |
| 552 | projectArn: observed.arn, |
| 553 | }; |
| 554 | }), |
| 555 | |
| 556 | delete: Effect.fn(function* ({ output }) { |
| 557 | // deleteProject is idempotent — a missing project returns success. |
| 558 | yield* codebuild.deleteProject({ name: output.projectName }); |
| 559 | }), |
| 560 | |
| 561 | list: () => |
| 562 | codebuild.listProjects.pages({}).pipe( |
| 563 | Stream.runCollect, |
| 564 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.projects ?? [])), |
| 565 | Effect.flatMap((names) => |
| 566 | names.length === 0 |
| 567 | ? Effect.succeed([] as { projectName: string; projectArn: string }[]) |
| 568 | : Effect.forEach( |
| 569 | // batchGetProjects accepts up to 100 names per call. |
| 570 | chunkNames(names, 100), |
| 571 | (batch) => |
| 572 | codebuild |
| 573 | .batchGetProjects({ names: batch }) |
| 574 | .pipe(Effect.map((res) => res.projects ?? [])), |
| 575 | { concurrency: 2 }, |
| 576 | ).pipe( |
| 577 | Effect.map((results) => |
| 578 | results |
| 579 | .flat() |
| 580 | .flatMap((p) => |
| 581 | p.name !== undefined && p.arn !== undefined |
| 582 | ? [{ projectName: p.name, projectArn: p.arn }] |
| 583 | : [], |
| 584 | ), |
| 585 | ), |
| 586 | ), |
| 587 | ), |
| 588 | ), |
| 589 | }; |
| 590 | }), |
| 591 | ); |
| 592 | |
| 593 | /** Split a list into fixed-size chunks. */ |
| 594 | const chunkNames = (names: string[], size: number): string[][] => { |
| 595 | const chunks: string[][] = []; |
| 596 | for (let i = 0; i < names.length; i += size) { |
| 597 | chunks.push(names.slice(i, i + size)); |
| 598 | } |
| 599 | return chunks; |
| 600 | }; |