@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 187 lines 6.08 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7 } from "../EventBridge/EventSource.ts";
8
9 /**
10 * The `detail` payload ECR delivers to EventBridge on every completed image
11 * push or delete (`ECR Image Action`).
12 */
13 export interface ImageActionDetail {
14 /** Whether the action was a `PUSH` or a `DELETE`. */
15 "action-type": "PUSH" | "DELETE";
16 /** Outcome of the action (`SUCCESS` or `FAILURE`). */
17 result: "SUCCESS" | "FAILURE";
18 /** Name of the repository the image was pushed to / deleted from. */
19 "repository-name": string;
20 /** Manifest digest of the affected image, e.g. `sha256:…`. */
21 "image-digest": string;
22 /** Tag of the affected image, when the action targeted a tag. */
23 "image-tag"?: string;
24 /** Media type of the manifest, e.g. `application/vnd.docker.distribution.manifest.v2+json`. */
25 "manifest-media-type"?: string;
26 /** Artifact media type, for OCI artifacts. */
27 "artifact-media-type"?: string;
28 /** Additional event fields (the schema grows over time). */
29 [key: string]: unknown;
30 }
31
32 /** An ECR image action EventBridge event delivered to the handler. */
33 export type ImageActionEvent = EventRecord<ImageActionDetail>;
34
35 /**
36 * The `detail` payload ECR delivers to EventBridge when an image scan
37 * completes (`ECR Image Scan`).
38 */
39 export interface ImageScanDetail {
40 /** Status of the completed scan, e.g. `COMPLETE`. */
41 "scan-status": string;
42 /** Name of the repository containing the scanned image. */
43 "repository-name": string;
44 /** Manifest digest of the scanned image. */
45 "image-digest": string;
46 /** Tags of the scanned image. */
47 "image-tags"?: string[];
48 /** Vulnerability counts keyed by severity, e.g. `{ HIGH: 2 }`. */
49 "finding-severity-counts"?: Record<string, number>;
50 /** Additional event fields (the schema grows over time). */
51 [key: string]: unknown;
52 }
53
54 /** An ECR image scan EventBridge event delivered to the handler. */
55 export type ImageScanEvent = EventRecord<ImageScanDetail>;
56
57 export interface ImageActionsProps extends EventRouteProps {
58 /**
59 * Logical id for the backing EventBridge rule.
60 * @default "EcrImageActions"
61 */
62 id?: string;
63 /**
64 * Only deliver events for these repositories (by name).
65 * @default all repositories
66 */
67 repositories?: string[];
68 /**
69 * Only deliver `PUSH` or `DELETE` actions.
70 * @default both
71 */
72 actionTypes?: ("PUSH" | "DELETE")[];
73 /**
74 * Only deliver actions with these results (e.g. `["SUCCESS"]`).
75 * @default both
76 */
77 results?: ("SUCCESS" | "FAILURE")[];
78 }
79
80 /**
81 * Event source connecting ECR image pushes and deletes to the hosting
82 * compute. ECR publishes an event to the account's default EventBridge bus
83 * (source `aws.ecr`, detail-type `ECR Image Action`) every time an image
84 * push or delete completes; this subscribes the host Function to those
85 * events so it can react without polling.
86 *
87 * ECR publishes to EventBridge automatically — no additional resource is
88 * created besides the EventBridge rule targeting the host. Provide the
89 * host-specific implementation layer (e.g. `AWS.Lambda.EventSource`) on the
90 * Function effect.
91 *
92 * ### Consuming Image Events
93 * **Example:** React to Successful Pushes
94 * ```typescript
95 * import * as AWS from "@alchemy.run/aws";
96 *
97 * export default DeployBot.make(
98 * { main: import.meta.url },
99 * Effect.gen(function* () {
100 * yield* AWS.ECR.consumeImageActions(
101 * { actionTypes: ["PUSH"], results: ["SUCCESS"] },
102 * (events) =>
103 * Stream.runForEach(events, (event) =>
104 * Effect.log(
105 * `${event.detail["repository-name"]}:${event.detail["image-tag"]} pushed`,
106 * ),
107 * ),
108 * );
109 * return {};
110 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
111 * );
112 * ```
113 */
114 export const consumeImageActions = <StreamReq = never, Req = never>(
115 props: ImageActionsProps,
116 process: (
117 events: Stream.Stream<ImageActionEvent, never, StreamReq>,
118 ) => Effect.Effect<void, never, Req>,
119 ) =>
120 consumeBusEvents(
121 props.id ?? "EcrImageActions",
122 {
123 source: ["aws.ecr"],
124 "detail-type": ["ECR Image Action"],
125 ...(props.repositories || props.actionTypes || props.results
126 ? {
127 detail: {
128 ...(props.repositories ? { "repository-name": [...props.repositories] } : {}),
129 ...(props.actionTypes ? { "action-type": [...props.actionTypes] } : {}),
130 ...(props.results ? { result: [...props.results] } : {}),
131 },
132 }
133 : {}),
134 },
135 { description: props.description, state: props.state },
136 process,
137 );
138
139 export interface ImageScansProps extends EventRouteProps {
140 /**
141 * Logical id for the backing EventBridge rule.
142 * @default "EcrImageScans"
143 */
144 id?: string;
145 /**
146 * Only deliver events for these repositories (by name).
147 * @default all repositories
148 */
149 repositories?: string[];
150 }
151
152 /**
153 * Event source connecting completed ECR image scans to the hosting compute.
154 * ECR publishes an event to the account's default EventBridge bus (source
155 * `aws.ecr`, detail-type `ECR Image Scan`) when a vulnerability scan
156 * finishes — including scan-on-push scans; this subscribes the host Function
157 * to those events so it can react to new findings without polling.
158 *
159 * ### Consuming Image Events
160 * **Example:** Alert on High-Severity Findings
161 * ```typescript
162 * yield* AWS.ECR.consumeImageScans({}, (events) =>
163 * Stream.runForEach(events, (event) =>
164 * Effect.log(
165 * event.detail["repository-name"],
166 * event.detail["finding-severity-counts"],
167 * ),
168 * ),
169 * );
170 * ```
171 */
172 export const consumeImageScans = <StreamReq = never, Req = never>(
173 props: ImageScansProps,
174 process: (
175 events: Stream.Stream<ImageScanEvent, never, StreamReq>,
176 ) => Effect.Effect<void, never, Req>,
177 ) =>
178 consumeBusEvents(
179 props.id ?? "EcrImageScans",
180 {
181 source: ["aws.ecr"],
182 "detail-type": ["ECR Image Scan"],
183 ...(props.repositories ? { detail: { "repository-name": [...props.repositories] } } : {}),
184 },
185 { description: props.description, state: props.state },
186 process,
187 );