@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 119 lines 3.66 KB
Raw
1 import type * as amplify from "@distilled.cloud/aws/amplify";
2 import * as Effect from "effect/Effect";
3 import type * as Stream from "effect/Stream";
4 import {
5 consumeBusEvents,
6 type EventRecord,
7 type EventRouteProps,
8 } from "../EventBridge/EventSource.ts";
9
10 /**
11 * The `detail` payload Amplify Hosting delivers to EventBridge when a
12 * deployment (build job) changes status.
13 */
14 export interface DeploymentStatusChangeDetail {
15 /**
16 * ID of the Amplify app the job belongs to.
17 */
18 appId: string;
19 /**
20 * Name of the branch the job ran on.
21 */
22 branchName: string;
23 /**
24 * ID of the build job whose status changed.
25 */
26 jobId: string;
27 /**
28 * New status of the job (`STARTED`, `SUCCEED`, or `FAILED`).
29 */
30 jobStatus: amplify.JobStatus;
31 }
32
33 /** A deployment-status-change EventBridge event delivered to the handler. */
34 export type DeploymentStatusChangeEvent = EventRecord<DeploymentStatusChangeDetail>;
35
36 export interface DeploymentStatusChangeEventSourceProps extends EventRouteProps {
37 /**
38 * Logical id prefix for the backing EventBridge rule.
39 * @default "Amplify"
40 */
41 id?: string;
42 /**
43 * Only deliver events whose `jobStatus` is one of these values. Omit to
44 * receive every status transition (`STARTED`, `SUCCEED`, `FAILED`).
45 */
46 jobStatus?: amplify.JobStatus[];
47 }
48
49 /**
50 * Deliver Amplify Hosting deployment status changes (`aws.amplify` /
51 * `"Amplify Deployment Status Change"` EventBridge events) to the host
52 * Function — e.g. to notify a chat channel on failed builds or kick off
53 * cache invalidation when a deploy succeeds.
54 *
55 * The EventBridge pattern matches every Amplify app in the account and
56 * region (the pattern must be literal — `Output` values such as `app.appId`
57 * do not resolve inside the deployed bundle); inspect `event.detail.appId` /
58 * `event.detail.branchName` in the handler if multiple apps share the
59 * Function. Provide `AWS.Lambda.EventSource` on the Function effect to
60 * implement the subscription.
61 *
62 * ### Reacting To Deployments
63 * **Example:** Alert on Failed Builds
64 * ```typescript
65 * yield* AWS.Amplify.consumeDeploymentStatusChanges(
66 * { jobStatus: ["FAILED"] },
67 * (events) =>
68 * Stream.runForEach(events, (event) =>
69 * Effect.log(
70 * `build ${event.detail.jobId} failed on ` +
71 * `${event.detail.appId}/${event.detail.branchName}`,
72 * ),
73 * ),
74 * );
75 * ```
76 *
77 * **Example:** Register the Event Source inside a Lambda Function
78 * ```typescript
79 * import * as AWS from "@alchemy.run/aws";
80 *
81 * export class DeployHooks extends AWS.Lambda.Function<AWS.Lambda.Function>()(
82 * "DeployHooks",
83 * ) {}
84 *
85 * export default DeployHooks.make(
86 * { main: import.meta.url },
87 * Effect.gen(function* () {
88 * yield* AWS.Amplify.consumeDeploymentStatusChanges(
89 * { jobStatus: ["SUCCEED"] },
90 * (events) =>
91 * Stream.runForEach(events, (event) =>
92 * Effect.log(`deployed ${event.detail.branchName}`),
93 * ),
94 * );
95 * return {};
96 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
97 * );
98 * ```
99 */
100 export const consumeDeploymentStatusChanges = <StreamReq = never, Req = never>(
101 props: DeploymentStatusChangeEventSourceProps,
102 process: (
103 events: Stream.Stream<DeploymentStatusChangeEvent, never, StreamReq>,
104 ) => Effect.Effect<void, never, Req>,
105 ) =>
106 Effect.gen(function* () {
107 const { id, jobStatus, ...routeProps } = props;
108
109 yield* consumeBusEvents(
110 `${id ?? "Amplify"}-DeploymentStatusChanges`,
111 {
112 source: ["aws.amplify"],
113 "detail-type": ["Amplify Deployment Status Change"],
114 ...(jobStatus && jobStatus.length > 0 ? { detail: { jobStatus } } : {}),
115 },
116 routeProps,
117 process,
118 );
119 });