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 * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { AWSEnvironment } from "../Environment.ts"; |
| 12 | |
| 13 | export class ReportGroupUnexpectedResponse extends Data.TaggedError( |
| 14 | "ReportGroupUnexpectedResponse", |
| 15 | )<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | /** |
| 21 | * Where CodeBuild exports the raw report data. |
| 22 | */ |
| 23 | export interface ReportGroupExportConfig { |
| 24 | /** |
| 25 | * `NO_EXPORT` keeps report data only in CodeBuild (30-day retention); |
| 26 | * `S3` additionally exports the raw data to a bucket. |
| 27 | * @default "NO_EXPORT" |
| 28 | */ |
| 29 | exportConfigType?: "NO_EXPORT" | "S3"; |
| 30 | /** |
| 31 | * S3 destination for exported report data (for `exportConfigType: "S3"`). |
| 32 | */ |
| 33 | s3Destination?: { |
| 34 | /** Name of the destination bucket. */ |
| 35 | bucket?: string; |
| 36 | /** AWS account that owns the bucket, when not the report's account. */ |
| 37 | bucketOwner?: string; |
| 38 | /** Path prefix inside the bucket. */ |
| 39 | path?: string; |
| 40 | /** |
| 41 | * Whether the exported data is zipped. |
| 42 | * @default "NONE" |
| 43 | */ |
| 44 | packaging?: "ZIP" | "NONE"; |
| 45 | /** KMS key used to encrypt the exported data. */ |
| 46 | encryptionKey?: string; |
| 47 | /** Disable encryption of the exported data. */ |
| 48 | encryptionDisabled?: boolean; |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | export interface ReportGroupProps { |
| 53 | /** |
| 54 | * Name of the report group (2-128 chars). Builds address it from the |
| 55 | * buildspec `reports:` section as `<project-name>-<group-name>` or by this |
| 56 | * exact name. If omitted a deterministic physical name is generated. |
| 57 | * Changing the name replaces the report group. |
| 58 | */ |
| 59 | reportGroupName?: string; |
| 60 | /** |
| 61 | * What the group's reports contain: test results or code coverage. |
| 62 | * Changing the type replaces the report group. |
| 63 | */ |
| 64 | type: "TEST" | "CODE_COVERAGE"; |
| 65 | /** |
| 66 | * Where raw report data is exported. |
| 67 | * @default { exportConfigType: "NO_EXPORT" } |
| 68 | */ |
| 69 | exportConfig?: ReportGroupExportConfig; |
| 70 | /** |
| 71 | * Also delete any reports in the group when the group is destroyed. |
| 72 | * @default true |
| 73 | */ |
| 74 | deleteReports?: boolean; |
| 75 | /** |
| 76 | * User-defined tags. |
| 77 | */ |
| 78 | tags?: Record<string, string>; |
| 79 | } |
| 80 | |
| 81 | export interface ReportGroup extends Resource< |
| 82 | "AWS.CodeBuild.ReportGroup", |
| 83 | ReportGroupProps, |
| 84 | { |
| 85 | /** Physical name of the report group. */ |
| 86 | reportGroupName: string; |
| 87 | /** ARN of the report group. */ |
| 88 | reportGroupArn: string; |
| 89 | } |
| 90 | > {} |
| 91 | |
| 92 | /** |
| 93 | * An AWS CodeBuild report group — a named collection of test or |
| 94 | * code-coverage reports produced by builds. A build's buildspec `reports:` |
| 95 | * section uploads its test results into a report group, and the report-read |
| 96 | * bindings ({@link BatchGetReports}, {@link DescribeTestCases}, …) let |
| 97 | * runtime code query them. |
| 98 | * |
| 99 | * ### Creating a Report Group |
| 100 | * **Example:** Test Report Group |
| 101 | * ```typescript |
| 102 | * const reports = yield* CodeBuild.ReportGroup("UnitTests", { |
| 103 | * type: "TEST", |
| 104 | * }); |
| 105 | * ``` |
| 106 | * |
| 107 | * **Example:** Coverage Group Exported to S3 |
| 108 | * ```typescript |
| 109 | * const coverage = yield* CodeBuild.ReportGroup("Coverage", { |
| 110 | * type: "CODE_COVERAGE", |
| 111 | * exportConfig: { |
| 112 | * exportConfigType: "S3", |
| 113 | * s3Destination: { bucket: bucket.bucketName, packaging: "ZIP" }, |
| 114 | * }, |
| 115 | * }); |
| 116 | * ``` |
| 117 | * |
| 118 | * @resource |
| 119 | */ |
| 120 | export const ReportGroup = Resource<ReportGroup>("AWS.CodeBuild.ReportGroup"); |
| 121 | |
| 122 | /** Convert a CodeBuild wire tag list into a plain record. */ |
| 123 | const toTagRecord = ( |
| 124 | tags: ReadonlyArray<{ key?: string; value?: string }> | undefined, |
| 125 | ): Record<string, string> => |
| 126 | Object.fromEntries( |
| 127 | (tags ?? []) |
| 128 | .filter( |
| 129 | (tag): tag is { key: string; value: string } => |
| 130 | typeof tag.key === "string" && typeof tag.value === "string", |
| 131 | ) |
| 132 | .map((tag) => [tag.key, tag.value]), |
| 133 | ); |
| 134 | |
| 135 | const toWireTags = (tags: Record<string, string>): codebuild.Tag[] => |
| 136 | Object.entries(tags).map(([key, value]) => ({ key, value })); |
| 137 | |
| 138 | const toWireExportConfig = ( |
| 139 | exportConfig: ReportGroupExportConfig | undefined, |
| 140 | ): codebuild.ReportExportConfig => ({ |
| 141 | exportConfigType: exportConfig?.exportConfigType ?? "NO_EXPORT", |
| 142 | s3Destination: exportConfig?.s3Destination, |
| 143 | }); |
| 144 | |
| 145 | /** Builds a report group's ARN from its name in the ambient account/region. */ |
| 146 | const reportGroupArn = Effect.fn(function* (name: string) { |
| 147 | const { accountId, region } = yield* AWSEnvironment.current; |
| 148 | return `arn:aws:codebuild:${region}:${accountId}:report-group/${name}`; |
| 149 | }); |
| 150 | |
| 151 | /** Structural comparison for the export-config sync no-op check. */ |
| 152 | const sameExportConfig = ( |
| 153 | observed: codebuild.ReportExportConfig | undefined, |
| 154 | desired: codebuild.ReportExportConfig, |
| 155 | ): boolean => |
| 156 | (observed?.exportConfigType ?? "NO_EXPORT") === desired.exportConfigType && |
| 157 | JSON.stringify(observed?.s3Destination ?? null) === JSON.stringify(desired.s3Destination ?? null); |
| 158 | |
| 159 | const sameTags = (observed: Record<string, string>, desired: Record<string, string>): boolean => |
| 160 | Object.keys(observed).length === Object.keys(desired).length && |
| 161 | Object.entries(desired).every(([k, v]) => observed[k] === v); |
| 162 | |
| 163 | export const ReportGroupProvider = () => |
| 164 | Provider.effect( |
| 165 | ReportGroup, |
| 166 | Effect.gen(function* () { |
| 167 | const toName = (id: string, props: Partial<ReportGroupProps>) => |
| 168 | props.reportGroupName |
| 169 | ? Effect.succeed(props.reportGroupName) |
| 170 | : createPhysicalName({ id, maxLength: 128 }); |
| 171 | |
| 172 | /** Read a report group by ARN; missing or DELETING reads as absent. */ |
| 173 | const getReportGroup = Effect.fn(function* (arn: string) { |
| 174 | const response = yield* codebuild.batchGetReportGroups({ |
| 175 | reportGroupArns: [arn], |
| 176 | }); |
| 177 | const group = response.reportGroups?.[0]; |
| 178 | return group?.status === "DELETING" ? undefined : group; |
| 179 | }); |
| 180 | |
| 181 | return { |
| 182 | stables: ["reportGroupName", "reportGroupArn"], |
| 183 | |
| 184 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 185 | if (!isResolved(news)) return undefined; |
| 186 | if ( |
| 187 | (yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {})) || |
| 188 | olds?.type !== news.type |
| 189 | ) { |
| 190 | return { action: "replace" } as const; |
| 191 | } |
| 192 | }), |
| 193 | |
| 194 | read: Effect.fn(function* ({ id, olds, output }) { |
| 195 | const name = output?.reportGroupName ?? (yield* toName(id, olds ?? {})); |
| 196 | const arn = output?.reportGroupArn ?? (yield* reportGroupArn(name)); |
| 197 | const group = yield* getReportGroup(arn); |
| 198 | if (group === undefined || group.arn === undefined) { |
| 199 | return undefined; |
| 200 | } |
| 201 | const attrs = { |
| 202 | reportGroupName: group.name ?? name, |
| 203 | reportGroupArn: group.arn, |
| 204 | }; |
| 205 | const tags = toTagRecord(group.tags); |
| 206 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 207 | }), |
| 208 | |
| 209 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 210 | const name = output?.reportGroupName ?? (yield* toName(id, news)); |
| 211 | const arn = output?.reportGroupArn ?? (yield* reportGroupArn(name)); |
| 212 | const internalTags = yield* createInternalTags(id); |
| 213 | const desiredTags = { ...internalTags, ...news.tags }; |
| 214 | const desiredExport = toWireExportConfig(news.exportConfig); |
| 215 | |
| 216 | // 1. Observe — cloud state is authoritative. |
| 217 | let observed = yield* getReportGroup(arn); |
| 218 | |
| 219 | // 3. Sync — diff observed export config + tags against desired |
| 220 | // and skip the API entirely on a no-op. updateReportGroup is a |
| 221 | // full upsert of both mutable aspects. A delete→redeploy race can |
| 222 | // leave batchGetReportGroups returning the just-deleted group; |
| 223 | // updateReportGroup then reports the truth with a typed |
| 224 | // ResourceNotFoundException — treat it as missing. |
| 225 | if ( |
| 226 | observed !== undefined && |
| 227 | (!sameExportConfig(observed.exportConfig, desiredExport) || |
| 228 | !sameTags(toTagRecord(observed.tags), desiredTags)) |
| 229 | ) { |
| 230 | observed = yield* codebuild |
| 231 | .updateReportGroup({ |
| 232 | arn, |
| 233 | exportConfig: desiredExport, |
| 234 | tags: toWireTags(desiredTags), |
| 235 | }) |
| 236 | .pipe( |
| 237 | Effect.map((res) => res.reportGroup), |
| 238 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | // 2. Ensure — create if missing; tolerate the create/create race. |
| 243 | if (observed === undefined) { |
| 244 | observed = yield* codebuild |
| 245 | .createReportGroup({ |
| 246 | name, |
| 247 | type: news.type, |
| 248 | exportConfig: desiredExport, |
| 249 | tags: toWireTags(desiredTags), |
| 250 | }) |
| 251 | .pipe( |
| 252 | Effect.map((res) => res.reportGroup), |
| 253 | Effect.catchTag("ResourceAlreadyExistsException", () => getReportGroup(arn)), |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | if (observed === undefined || observed.arn === undefined) { |
| 258 | return yield* new ReportGroupUnexpectedResponse({ |
| 259 | message: `CodeBuild report group '${name}' disappeared while reconciling`, |
| 260 | }); |
| 261 | } |
| 262 | |
| 263 | yield* session.note(name); |
| 264 | return { |
| 265 | reportGroupName: observed.name ?? name, |
| 266 | reportGroupArn: observed.arn, |
| 267 | }; |
| 268 | }), |
| 269 | |
| 270 | delete: Effect.fn(function* ({ olds, output }) { |
| 271 | // deleteReportGroup is idempotent — deleting a missing group |
| 272 | // returns success. `deleteReports` clears contained reports first |
| 273 | // (a non-empty group cannot be deleted otherwise). |
| 274 | yield* codebuild.deleteReportGroup({ |
| 275 | arn: output.reportGroupArn, |
| 276 | deleteReports: olds.deleteReports ?? true, |
| 277 | }); |
| 278 | }), |
| 279 | |
| 280 | list: () => |
| 281 | codebuild.listReportGroups.pages({}).pipe( |
| 282 | Stream.runCollect, |
| 283 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.reportGroups ?? [])), |
| 284 | Effect.flatMap((arns) => |
| 285 | arns.length === 0 |
| 286 | ? Effect.succeed([] as { reportGroupName: string; reportGroupArn: string }[]) |
| 287 | : Effect.forEach( |
| 288 | // batchGetReportGroups accepts up to 100 ARNs per call. |
| 289 | chunkArns(arns, 100), |
| 290 | (batch) => |
| 291 | codebuild |
| 292 | .batchGetReportGroups({ reportGroupArns: batch }) |
| 293 | .pipe(Effect.map((res) => res.reportGroups ?? [])), |
| 294 | { concurrency: 2 }, |
| 295 | ).pipe( |
| 296 | Effect.map((results) => |
| 297 | results.flat().flatMap((g) => |
| 298 | g.name !== undefined && g.arn !== undefined |
| 299 | ? [ |
| 300 | { |
| 301 | reportGroupName: g.name, |
| 302 | reportGroupArn: g.arn, |
| 303 | }, |
| 304 | ] |
| 305 | : [], |
| 306 | ), |
| 307 | ), |
| 308 | ), |
| 309 | ), |
| 310 | ), |
| 311 | }; |
| 312 | }), |
| 313 | ); |
| 314 | |
| 315 | /** Split a list into fixed-size chunks. */ |
| 316 | const chunkArns = (arns: string[], size: number): string[][] => { |
| 317 | const chunks: string[][] = []; |
| 318 | for (let i = 0; i < arns.length; i += size) { |
| 319 | chunks.push(arns.slice(i, i + size)); |
| 320 | } |
| 321 | return chunks; |
| 322 | }; |