The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as CodePipeline from "@alchemy.run/aws/CodePipeline"; |
| 2 | import * as IAM from "@alchemy.run/aws/IAM"; |
| 3 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 4 | import * as S3 from "@alchemy.run/aws/S3"; |
| 5 | import * as Output from "alchemy/Output"; |
| 6 | import * as Duration from "effect/Duration"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 9 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import path from "pathe"; |
| 13 | |
| 14 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 15 | |
| 16 | export const FIXTURE_PIPELINE_NAME = process.env.ALCHEMY_TEST_STAGE |
| 17 | ? `alchemy-test-codepipeline-bindings-${process.env.ALCHEMY_TEST_STAGE}` |
| 18 | : "alchemy-test-codepipeline-bindings"; |
| 19 | export const SOURCE_OBJECT_KEY = "source.zip"; |
| 20 | export const SOURCE_STAGE = "Source"; |
| 21 | export const SOURCE_ACTION = "S3Source"; |
| 22 | export const APPROVAL_STAGE = "Approve"; |
| 23 | export const APPROVAL_ACTION = "ManualApproval"; |
| 24 | |
| 25 | /** A minimal valid (empty) zip archive: the end-of-central-directory record. */ |
| 26 | const EMPTY_ZIP = new Uint8Array([ |
| 27 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 28 | ]); |
| 29 | |
| 30 | export class CodePipelineTestFunction extends Lambda.Function<Lambda.Function>()( |
| 31 | "CodePipelineTestFunction", |
| 32 | ) {} |
| 33 | |
| 34 | /** |
| 35 | * Every route answers `{ …fields }` on success or `{ errorTag }` when the |
| 36 | * operation fails with a TYPED error — the test asserts the tag is a typed, |
| 37 | * non-authorization tag, which proves both the binding wiring and the IAM |
| 38 | * grant. An untyped error crashes into a 500 instead. |
| 39 | */ |
| 40 | const errorTagged = <A, E extends { _tag: string; message?: string }, R>( |
| 41 | effect: Effect.Effect<A, E, R>, |
| 42 | ): Effect.Effect<A | { errorTag: string; errorMessage?: string }, never, R> => |
| 43 | effect.pipe( |
| 44 | Effect.map((a): A | { errorTag: string; errorMessage?: string } => a), |
| 45 | Effect.catch((e) => Effect.succeed({ errorTag: e._tag, errorMessage: e.message })), |
| 46 | ); |
| 47 | |
| 48 | export default CodePipelineTestFunction.make( |
| 49 | { |
| 50 | main, |
| 51 | functionUrl: true, |
| 52 | // Pipeline state reads fan out SDK calls — AWS's 3s default |
| 53 | // intermittently times out under cold starts. |
| 54 | timeout: Duration.seconds(30), |
| 55 | }, |
| 56 | Effect.gen(function* () { |
| 57 | const source = yield* S3.Bucket("PipelineBindingsSource", { |
| 58 | versioning: "Enabled", |
| 59 | forceDestroy: true, |
| 60 | }); |
| 61 | const artifacts = yield* S3.Bucket("PipelineBindingsArtifacts", { |
| 62 | forceDestroy: true, |
| 63 | }); |
| 64 | |
| 65 | const role = yield* IAM.Role("PipelineBindingsRole", { |
| 66 | assumeRolePolicyDocument: { |
| 67 | Version: "2012-10-17", |
| 68 | Statement: [ |
| 69 | { |
| 70 | Effect: "Allow", |
| 71 | Principal: { Service: "codepipeline.amazonaws.com" }, |
| 72 | Action: ["sts:AssumeRole"], |
| 73 | }, |
| 74 | ], |
| 75 | }, |
| 76 | inlinePolicies: { |
| 77 | Artifacts: { |
| 78 | Version: "2012-10-17", |
| 79 | Statement: [ |
| 80 | { |
| 81 | Effect: "Allow", |
| 82 | Action: [ |
| 83 | "s3:GetObject", |
| 84 | "s3:GetObjectVersion", |
| 85 | "s3:PutObject", |
| 86 | "s3:GetBucketVersioning", |
| 87 | "s3:ListBucket", |
| 88 | ], |
| 89 | Resource: [ |
| 90 | source.bucketArn, |
| 91 | Output.interpolate`${source.bucketArn}/*` as any, |
| 92 | artifacts.bucketArn, |
| 93 | Output.interpolate`${artifacts.bucketArn}/*` as any, |
| 94 | ], |
| 95 | }, |
| 96 | ], |
| 97 | }, |
| 98 | }, |
| 99 | }); |
| 100 | |
| 101 | const pipeline = yield* CodePipeline.Pipeline("BindingsPipeline", { |
| 102 | pipelineName: FIXTURE_PIPELINE_NAME, |
| 103 | roleArn: role.roleArn, |
| 104 | artifactStore: { type: "S3", location: artifacts.bucketName }, |
| 105 | stages: [ |
| 106 | { |
| 107 | name: SOURCE_STAGE, |
| 108 | actions: [ |
| 109 | { |
| 110 | name: SOURCE_ACTION, |
| 111 | category: "Source", |
| 112 | owner: "AWS", |
| 113 | provider: "S3", |
| 114 | outputArtifacts: ["SourceOutput"], |
| 115 | configuration: { |
| 116 | S3Bucket: source.bucketName, |
| 117 | S3ObjectKey: SOURCE_OBJECT_KEY, |
| 118 | PollForSourceChanges: "false", |
| 119 | }, |
| 120 | }, |
| 121 | ], |
| 122 | }, |
| 123 | { |
| 124 | name: APPROVAL_STAGE, |
| 125 | actions: [ |
| 126 | { |
| 127 | name: APPROVAL_ACTION, |
| 128 | category: "Approval", |
| 129 | owner: "AWS", |
| 130 | provider: "Manual", |
| 131 | }, |
| 132 | ], |
| 133 | }, |
| 134 | ], |
| 135 | }); |
| 136 | |
| 137 | // Source plane (cross-service: seed the source object from the Lambda). |
| 138 | const putObject = yield* S3.PutObject(source); |
| 139 | // Execution plane |
| 140 | const startExecution = yield* CodePipeline.StartPipelineExecution(pipeline); |
| 141 | const stopExecution = yield* CodePipeline.StopPipelineExecution(pipeline); |
| 142 | const getState = yield* CodePipeline.GetPipelineState(pipeline); |
| 143 | const getExecution = yield* CodePipeline.GetPipelineExecution(pipeline); |
| 144 | const listExecutions = yield* CodePipeline.ListPipelineExecutions(pipeline); |
| 145 | const listActions = yield* CodePipeline.ListActionExecutions(pipeline); |
| 146 | const listRules = yield* CodePipeline.ListRuleExecutions(pipeline); |
| 147 | const listDeployTargets = yield* CodePipeline.ListDeployActionExecutionTargets(pipeline); |
| 148 | // Stage plane |
| 149 | const retryStage = yield* CodePipeline.RetryStageExecution(pipeline); |
| 150 | const rollbackStage = yield* CodePipeline.RollbackStage(pipeline); |
| 151 | const enableTransition = yield* CodePipeline.EnableStageTransition(pipeline); |
| 152 | const disableTransition = yield* CodePipeline.DisableStageTransition(pipeline); |
| 153 | const overrideCondition = yield* CodePipeline.OverrideStageCondition(pipeline); |
| 154 | // Approval + source-revision plane |
| 155 | const putApproval = yield* CodePipeline.PutApprovalResult(pipeline); |
| 156 | const putActionRevision = yield* CodePipeline.PutActionRevision(pipeline); |
| 157 | // Job-worker plane (account-scoped — no resource anchor) |
| 158 | const getJobDetails = yield* CodePipeline.GetJobDetails(); |
| 159 | const putJobSuccess = yield* CodePipeline.PutJobSuccessResult(); |
| 160 | const putJobFailure = yield* CodePipeline.PutJobFailureResult(); |
| 161 | const pollForJobs = yield* CodePipeline.PollForJobs(); |
| 162 | const acknowledgeJob = yield* CodePipeline.AcknowledgeJob(); |
| 163 | |
| 164 | // Deploy-time: creates the EventBridge rule (default bus, source |
| 165 | // aws.codepipeline) targeting this Function. Runtime firing rides on the |
| 166 | // real executions the suite starts; the test verifies the rule deploys. |
| 167 | yield* CodePipeline.consumePipelineEvents( |
| 168 | { kinds: ["execution"], pipelineNames: [FIXTURE_PIPELINE_NAME] }, |
| 169 | (events) => |
| 170 | Stream.runForEach(events, (event) => |
| 171 | Effect.log(`codepipeline event: ${event.detail.pipeline} -> ${event.detail.state}`), |
| 172 | ), |
| 173 | ); |
| 174 | |
| 175 | return { |
| 176 | fetch: Effect.gen(function* () { |
| 177 | const request = yield* HttpServerRequest; |
| 178 | const url = new URL(request.originalUrl); |
| 179 | const pathname = url.pathname; |
| 180 | const route = `${request.method} ${pathname}`; |
| 181 | const param = (name: string) => url.searchParams.get(name)!; |
| 182 | |
| 183 | switch (route) { |
| 184 | // ---- source plane ---- |
| 185 | case "POST /source/upload": { |
| 186 | const result = yield* errorTagged( |
| 187 | putObject({ Key: SOURCE_OBJECT_KEY, Body: EMPTY_ZIP }), |
| 188 | ); |
| 189 | return yield* HttpServerResponse.json( |
| 190 | "errorTag" in result ? result : { versionId: result.VersionId }, |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | // ---- execution plane ---- |
| 195 | case "POST /execution/start": { |
| 196 | const result = yield* errorTagged(startExecution()); |
| 197 | return yield* HttpServerResponse.json( |
| 198 | "errorTag" in result ? result : { executionId: result.pipelineExecutionId }, |
| 199 | ); |
| 200 | } |
| 201 | case "POST /execution/stop": { |
| 202 | const body = (yield* request.json) as unknown as { |
| 203 | id: string; |
| 204 | abandon?: boolean; |
| 205 | }; |
| 206 | const result = yield* errorTagged( |
| 207 | stopExecution({ |
| 208 | pipelineExecutionId: body.id, |
| 209 | abandon: body.abandon, |
| 210 | reason: "stopped by bindings test", |
| 211 | }), |
| 212 | ); |
| 213 | return yield* HttpServerResponse.json( |
| 214 | "errorTag" in result ? result : { executionId: result.pipelineExecutionId }, |
| 215 | ); |
| 216 | } |
| 217 | case "GET /state": { |
| 218 | const result = yield* errorTagged(getState()); |
| 219 | if ("errorTag" in result) { |
| 220 | return yield* HttpServerResponse.json(result); |
| 221 | } |
| 222 | const approvalState = result.stageStates |
| 223 | ?.find((s) => s.stageName === APPROVAL_STAGE) |
| 224 | ?.actionStates?.find((a) => a.actionName === APPROVAL_ACTION); |
| 225 | const approveStage = result.stageStates?.find((s) => s.stageName === APPROVAL_STAGE); |
| 226 | return yield* HttpServerResponse.json({ |
| 227 | pipelineName: result.pipelineName, |
| 228 | stageNames: (result.stageStates ?? []).map((s) => s.stageName), |
| 229 | approvalToken: approvalState?.latestExecution?.token, |
| 230 | approvalStatus: approvalState?.latestExecution?.status, |
| 231 | inboundTransitionEnabled: approveStage?.inboundTransitionState?.enabled, |
| 232 | }); |
| 233 | } |
| 234 | case "GET /execution/get": { |
| 235 | const result = yield* errorTagged(getExecution({ pipelineExecutionId: param("id") })); |
| 236 | return yield* HttpServerResponse.json( |
| 237 | "errorTag" in result ? result : { status: result.pipelineExecution?.status }, |
| 238 | ); |
| 239 | } |
| 240 | case "GET /executions": { |
| 241 | const result = yield* errorTagged(listExecutions()); |
| 242 | return yield* HttpServerResponse.json( |
| 243 | "errorTag" in result |
| 244 | ? result |
| 245 | : { |
| 246 | ids: (result.pipelineExecutionSummaries ?? []).map( |
| 247 | (e) => e.pipelineExecutionId, |
| 248 | ), |
| 249 | }, |
| 250 | ); |
| 251 | } |
| 252 | case "GET /actions": { |
| 253 | const executionId = url.searchParams.get("executionId"); |
| 254 | const result = yield* errorTagged( |
| 255 | listActions( |
| 256 | executionId !== null ? { filter: { pipelineExecutionId: executionId } } : undefined, |
| 257 | ), |
| 258 | ); |
| 259 | return yield* HttpServerResponse.json( |
| 260 | "errorTag" in result |
| 261 | ? result |
| 262 | : { |
| 263 | actions: (result.actionExecutionDetails ?? []).map((a) => a.actionName), |
| 264 | }, |
| 265 | ); |
| 266 | } |
| 267 | case "GET /rules": { |
| 268 | const result = yield* errorTagged(listRules()); |
| 269 | return yield* HttpServerResponse.json( |
| 270 | "errorTag" in result ? result : { count: (result.ruleExecutionDetails ?? []).length }, |
| 271 | ); |
| 272 | } |
| 273 | case "GET /deploy-targets": { |
| 274 | const result = yield* errorTagged( |
| 275 | listDeployTargets({ |
| 276 | actionExecutionId: param("actionExecutionId"), |
| 277 | }), |
| 278 | ); |
| 279 | return yield* HttpServerResponse.json( |
| 280 | "errorTag" in result ? result : { count: (result.targets ?? []).length }, |
| 281 | ); |
| 282 | } |
| 283 | |
| 284 | // ---- stage plane ---- |
| 285 | case "POST /stage/retry": { |
| 286 | const body = (yield* request.json) as unknown as { |
| 287 | stageName: string; |
| 288 | executionId: string; |
| 289 | }; |
| 290 | const result = yield* errorTagged( |
| 291 | retryStage({ |
| 292 | stageName: body.stageName, |
| 293 | pipelineExecutionId: body.executionId, |
| 294 | retryMode: "FAILED_ACTIONS", |
| 295 | }), |
| 296 | ); |
| 297 | return yield* HttpServerResponse.json( |
| 298 | "errorTag" in result ? result : { executionId: result.pipelineExecutionId }, |
| 299 | ); |
| 300 | } |
| 301 | case "POST /stage/rollback": { |
| 302 | const body = (yield* request.json) as unknown as { |
| 303 | stageName: string; |
| 304 | targetExecutionId: string; |
| 305 | }; |
| 306 | const result = yield* errorTagged( |
| 307 | rollbackStage({ |
| 308 | stageName: body.stageName, |
| 309 | targetPipelineExecutionId: body.targetExecutionId, |
| 310 | }), |
| 311 | ); |
| 312 | return yield* HttpServerResponse.json( |
| 313 | "errorTag" in result ? result : { executionId: result.pipelineExecutionId }, |
| 314 | ); |
| 315 | } |
| 316 | case "POST /transition/disable": { |
| 317 | const body = (yield* request.json) as unknown as { |
| 318 | stageName: string; |
| 319 | }; |
| 320 | const result = yield* errorTagged( |
| 321 | disableTransition({ |
| 322 | stageName: body.stageName, |
| 323 | transitionType: "Inbound", |
| 324 | reason: "bindings test freeze", |
| 325 | }), |
| 326 | ); |
| 327 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 328 | } |
| 329 | case "POST /transition/enable": { |
| 330 | const body = (yield* request.json) as unknown as { |
| 331 | stageName: string; |
| 332 | }; |
| 333 | const result = yield* errorTagged( |
| 334 | enableTransition({ |
| 335 | stageName: body.stageName, |
| 336 | transitionType: "Inbound", |
| 337 | }), |
| 338 | ); |
| 339 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 340 | } |
| 341 | case "POST /condition/override": { |
| 342 | const body = (yield* request.json) as unknown as { |
| 343 | stageName: string; |
| 344 | executionId: string; |
| 345 | }; |
| 346 | const result = yield* errorTagged( |
| 347 | overrideCondition({ |
| 348 | stageName: body.stageName, |
| 349 | pipelineExecutionId: body.executionId, |
| 350 | conditionType: "BEFORE_ENTRY", |
| 351 | }), |
| 352 | ); |
| 353 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 354 | } |
| 355 | |
| 356 | // ---- approval + source-revision plane ---- |
| 357 | case "POST /approval": { |
| 358 | const body = (yield* request.json) as unknown as { |
| 359 | token: string; |
| 360 | status: "Approved" | "Rejected"; |
| 361 | }; |
| 362 | const result = yield* errorTagged( |
| 363 | putApproval({ |
| 364 | stageName: APPROVAL_STAGE, |
| 365 | actionName: APPROVAL_ACTION, |
| 366 | token: body.token, |
| 367 | result: { |
| 368 | status: body.status, |
| 369 | summary: "answered by bindings test", |
| 370 | }, |
| 371 | }), |
| 372 | ); |
| 373 | return yield* HttpServerResponse.json( |
| 374 | "errorTag" in result ? result : { approvedAt: result.approvedAt }, |
| 375 | ); |
| 376 | } |
| 377 | case "POST /action/revision": { |
| 378 | const body = (yield* request.json) as unknown as { |
| 379 | actionName: string; |
| 380 | revisionId: string; |
| 381 | }; |
| 382 | const result = yield* errorTagged( |
| 383 | putActionRevision({ |
| 384 | stageName: SOURCE_STAGE, |
| 385 | actionName: body.actionName, |
| 386 | actionRevision: { |
| 387 | revisionId: body.revisionId, |
| 388 | revisionChangeId: body.revisionId, |
| 389 | created: new Date(), |
| 390 | }, |
| 391 | }), |
| 392 | ); |
| 393 | return yield* HttpServerResponse.json( |
| 394 | "errorTag" in result |
| 395 | ? result |
| 396 | : { |
| 397 | newRevision: result.newRevision, |
| 398 | executionId: result.pipelineExecutionId, |
| 399 | }, |
| 400 | ); |
| 401 | } |
| 402 | |
| 403 | // ---- job-worker plane ---- |
| 404 | case "GET /job/get": { |
| 405 | const result = yield* errorTagged(getJobDetails({ jobId: param("id") })); |
| 406 | return yield* HttpServerResponse.json( |
| 407 | "errorTag" in result ? result : { jobId: result.jobDetails?.id }, |
| 408 | ); |
| 409 | } |
| 410 | case "POST /job/success": { |
| 411 | const body = (yield* request.json) as unknown as { |
| 412 | jobId: string; |
| 413 | }; |
| 414 | const result = yield* errorTagged(putJobSuccess({ jobId: body.jobId })); |
| 415 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 416 | } |
| 417 | case "POST /job/failure": { |
| 418 | const body = (yield* request.json) as unknown as { |
| 419 | jobId: string; |
| 420 | }; |
| 421 | const result = yield* errorTagged( |
| 422 | putJobFailure({ |
| 423 | jobId: body.jobId, |
| 424 | failureDetails: { |
| 425 | type: "JobFailed", |
| 426 | message: "failed by bindings test", |
| 427 | }, |
| 428 | }), |
| 429 | ); |
| 430 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 431 | } |
| 432 | |
| 433 | case "POST /job/poll": { |
| 434 | // The fixture declares no custom action type — a typed |
| 435 | // ActionTypeNotFoundException proves wiring + grant. |
| 436 | const result = yield* errorTagged( |
| 437 | pollForJobs({ |
| 438 | actionTypeId: { |
| 439 | category: "Build", |
| 440 | owner: "Custom", |
| 441 | provider: "AlchemyBindingsTest", |
| 442 | version: "1", |
| 443 | }, |
| 444 | maxBatchSize: 1, |
| 445 | }), |
| 446 | ); |
| 447 | return yield* HttpServerResponse.json( |
| 448 | "errorTag" in result ? result : { count: (result.jobs ?? []).length }, |
| 449 | ); |
| 450 | } |
| 451 | case "POST /job/ack": { |
| 452 | const body = (yield* request.json) as unknown as { |
| 453 | jobId: string; |
| 454 | nonce: string; |
| 455 | }; |
| 456 | const result = yield* errorTagged( |
| 457 | acknowledgeJob({ jobId: body.jobId, nonce: body.nonce }), |
| 458 | ); |
| 459 | return yield* HttpServerResponse.json( |
| 460 | "errorTag" in result ? result : { status: result.status }, |
| 461 | ); |
| 462 | } |
| 463 | |
| 464 | default: |
| 465 | return yield* HttpServerResponse.json({ error: "Not found", route }, { status: 404 }); |
| 466 | } |
| 467 | }).pipe(Effect.orDie), |
| 468 | }; |
| 469 | }).pipe( |
| 470 | Effect.provide( |
| 471 | Layer.mergeAll( |
| 472 | Lambda.EventSource, |
| 473 | S3.PutObjectHttp, |
| 474 | CodePipeline.StartPipelineExecutionHttp, |
| 475 | CodePipeline.StopPipelineExecutionHttp, |
| 476 | CodePipeline.GetPipelineStateHttp, |
| 477 | CodePipeline.GetPipelineExecutionHttp, |
| 478 | CodePipeline.ListPipelineExecutionsHttp, |
| 479 | CodePipeline.ListActionExecutionsHttp, |
| 480 | CodePipeline.ListRuleExecutionsHttp, |
| 481 | CodePipeline.ListDeployActionExecutionTargetsHttp, |
| 482 | CodePipeline.RetryStageExecutionHttp, |
| 483 | CodePipeline.RollbackStageHttp, |
| 484 | CodePipeline.EnableStageTransitionHttp, |
| 485 | CodePipeline.DisableStageTransitionHttp, |
| 486 | CodePipeline.OverrideStageConditionHttp, |
| 487 | CodePipeline.PutApprovalResultHttp, |
| 488 | CodePipeline.PutActionRevisionHttp, |
| 489 | CodePipeline.GetJobDetailsHttp, |
| 490 | CodePipeline.PutJobSuccessResultHttp, |
| 491 | CodePipeline.PutJobFailureResultHttp, |
| 492 | CodePipeline.PollForJobsHttp, |
| 493 | CodePipeline.AcknowledgeJobHttp, |
| 494 | ), |
| 495 | ), |
| 496 | ), |
| 497 | ); |