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

main
ts 153 lines 5.67 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Pipeline } from "./Pipeline.ts";
5
6 /**
7 * Shared scaffolding for AWS CodePipeline HTTP bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
11 * below. Everything except the operation, the IAM action, and the injected
12 * pipeline-name key is boilerplate.
13 *
14 * CodePipeline addresses the pipeline as `name` on some operations
15 * (StartPipelineExecution, GetPipelineState) and `pipelineName` on the rest.
16 * Stage- and action-addressed operations authorize against sub-resource ARNs
17 * (`{pipelineArn}/{stage}` / `{pipelineArn}/{stage}/{action}`) — set
18 * `subScoped` to additionally grant on `{pipelineArn}/*`.
19 */
20
21 const BindPipelinePolicy = Effect.gen(function* () {
22 const bind = yield* AWSBindings.Bind;
23 return Effect.fn(function* (
24 tag: string,
25 pipeline: Pipeline,
26 actions: readonly string[],
27 subScoped: boolean | undefined,
28 ) {
29 if (!globalThis.__ALCHEMY_RUNTIME__) {
30 yield* bind`${tag}(${pipeline})`({
31 policyStatements: [
32 {
33 Effect: "Allow",
34 Action: [...actions],
35 Resource: [
36 Output.interpolate`${pipeline.pipelineArn}`,
37 ...(subScoped ? [Output.interpolate`${pipeline.pipelineArn}/*`] : []),
38 ],
39 },
40 ],
41 });
42 }
43 });
44 });
45
46 /**
47 * Build the impl Effect for an operation that addresses the pipeline via a
48 * `name` field (StartPipelineExecution, GetPipelineState): the runtime
49 * callable injects the bound {@link Pipeline}'s name and the deploy-time
50 * half grants `actions` on the pipeline ARN.
51 */
52 export const makeCodePipelineNameHttpBinding = <I extends { name?: string }, A, E, R>(options: {
53 /** Fully-qualified binding tag, e.g. `AWS.CodePipeline.GetPipelineState`. */
54 tag: string;
55 /** The distilled operation; `name` is injected from the pipeline. */
56 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
57 /** IAM actions granted on the pipeline ARN. */
58 actions: readonly string[];
59 }) =>
60 Effect.gen(function* () {
61 const bindPipelinePolicy = yield* BindPipelinePolicy;
62 const op = yield* AWSBindings.withCredentials(options.operation);
63
64 return Effect.fn(function* <P extends Pipeline>(pipeline: P) {
65 // Outputs yield a DEFERRED effect — resolve again per invocation below.
66 const PipelineName = yield* pipeline.pipelineName;
67 yield* bindPipelinePolicy(options.tag, pipeline, options.actions, false);
68 return Effect.fn(`${options.tag}(${pipeline.LogicalId})`)(function* (
69 request?: Omit<I, "name">,
70 ) {
71 return yield* op({
72 ...request,
73 name: yield* PipelineName,
74 } as I);
75 });
76 });
77 });
78
79 /**
80 * Build the impl Effect for an operation that addresses the pipeline via a
81 * `pipelineName` field (the majority): the runtime callable injects the
82 * bound {@link Pipeline}'s name and the deploy-time half grants `actions`
83 * on the pipeline ARN (plus `{pipelineArn}/*` when `subScoped` — stage- and
84 * action-addressed operations authorize against sub-resource ARNs).
85 */
86 export const makeCodePipelinePipelineNameHttpBinding = <
87 I extends { pipelineName?: string },
88 A,
89 E,
90 R,
91 >(options: {
92 /** Fully-qualified binding tag, e.g. `AWS.CodePipeline.RollbackStage`. */
93 tag: string;
94 /** The distilled operation; `pipelineName` is injected from the pipeline. */
95 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
96 /** IAM actions granted on the pipeline ARN. */
97 actions: readonly string[];
98 /** Also grant on `{pipelineArn}/*` (stage-/action-addressed actions). */
99 subScoped?: boolean;
100 }) =>
101 Effect.gen(function* () {
102 const bindPipelinePolicy = yield* BindPipelinePolicy;
103 const op = yield* AWSBindings.withCredentials(options.operation);
104
105 return Effect.fn(function* <P extends Pipeline>(pipeline: P) {
106 const PipelineName = yield* pipeline.pipelineName;
107 yield* bindPipelinePolicy(options.tag, pipeline, options.actions, options.subScoped);
108 return Effect.fn(`${options.tag}(${pipeline.LogicalId})`)(function* (
109 request?: Omit<I, "pipelineName">,
110 ) {
111 return yield* op({
112 ...request,
113 pipelineName: yield* PipelineName,
114 } as I);
115 });
116 });
117 });
118
119 /**
120 * Build the impl Effect for a job-worker operation (GetJobDetails,
121 * PutJobSuccessResult, PutJobFailureResult). CodePipeline job actions do
122 * not support resource-level permissions, so the deploy-time half grants
123 * `actions` on `*`; the runtime callable passes the request through as-is.
124 */
125 export const makeCodePipelineJobHttpBinding = <I, A, E, R>(options: {
126 /** Fully-qualified binding tag, e.g. `AWS.CodePipeline.PutJobSuccessResult`. */
127 tag: string;
128 /** The distilled operation, invoked with the caller's request as-is. */
129 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
130 /** IAM actions granted on `*` (no resource-level permission support). */
131 actions: readonly string[];
132 }) =>
133 Effect.gen(function* () {
134 const bind = yield* AWSBindings.Bind;
135 const op = yield* AWSBindings.withCredentials(options.operation);
136
137 return Effect.fn(function* () {
138 if (!globalThis.__ALCHEMY_RUNTIME__) {
139 yield* bind`${options.tag}()`({
140 policyStatements: [
141 {
142 Effect: "Allow",
143 Action: [...options.actions],
144 Resource: ["*"],
145 },
146 ],
147 });
148 }
149 return Effect.fn(options.tag)(function* (request: I) {
150 return yield* op(request);
151 });
152 });
153 });