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, 06:00 UTC
| 1 | import * as artifactregistry from "@distilled.cloud/gcp/artifactregistry_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels, hasAlchemyLabels, stripInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | ResourceNotResolved, |
| 13 | expandRepository, |
| 14 | hasAlchemyLabelMap, |
| 15 | listAlchemyRepositories, |
| 16 | listAttachments, |
| 17 | listChildResources, |
| 18 | locationFromRepository, |
| 19 | missingGet, |
| 20 | normalizeLocation, |
| 21 | parseName, |
| 22 | replaceOnIdentity, |
| 23 | sameJson, |
| 24 | sameText, |
| 25 | sortedStrings, |
| 26 | toPhysicalId, |
| 27 | waitForDeleteOperation, |
| 28 | waitForOperation, |
| 29 | waitUntilExists, |
| 30 | waitUntilGone, |
| 31 | } from "./internal.ts"; |
| 32 | |
| 33 | const DEFAULT_TYPE = "application/json"; |
| 34 | |
| 35 | export type RepositoriesAttachmentProps = { |
| 36 | /** |
| 37 | * Parent repository. Full name |
| 38 | * `projects/{project}/locations/{location}/repositories/{repository}` |
| 39 | * or the repository id (combined with `location`). Immutable — |
| 40 | * changing it replaces the attachment. |
| 41 | */ |
| 42 | repository: string; |
| 43 | /** |
| 44 | * Region used when `repository` is a bare id. |
| 45 | * @default the repository's location, else the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 46 | */ |
| 47 | location?: string; |
| 48 | /** |
| 49 | * Attachment id (the `{attachment}` segment of |
| 50 | * `.../repositories/{repository}/attachments/{attachment}`). If omitted, |
| 51 | * a unique name is generated. Immutable — changing it replaces the |
| 52 | * attachment. |
| 53 | */ |
| 54 | attachmentId?: string; |
| 55 | /** |
| 56 | * Target the attachment is for — a Version, Package, or Repository |
| 57 | * resource name. Bare ids are resolved under `repository`. Immutable — |
| 58 | * changing it replaces the attachment. |
| 59 | */ |
| 60 | target: string; |
| 61 | /** |
| 62 | * Files that belong to this attachment, as File resource names or file |
| 63 | * ids under `repository`. Immutable — changing them replaces the |
| 64 | * attachment. |
| 65 | */ |
| 66 | files: string[]; |
| 67 | /** |
| 68 | * Attachment type, for example `application/vnd.spdx+json`. |
| 69 | * @default "application/json" |
| 70 | */ |
| 71 | type?: string; |
| 72 | /** |
| 73 | * Namespace this attachment belongs to, for example |
| 74 | * `artifactanalysis.googleapis.com`. |
| 75 | */ |
| 76 | attachmentNamespace?: string; |
| 77 | /** |
| 78 | * User annotations. Alchemy ownership keys (`alchemy-stack`, |
| 79 | * `alchemy-stage`, `alchemy-id`) are merged in automatically. |
| 80 | * Attachments have no labels field and no update API — changing |
| 81 | * annotations replaces the attachment. |
| 82 | */ |
| 83 | annotations?: Record<string, string>; |
| 84 | }; |
| 85 | |
| 86 | export type RepositoriesAttachment = Resource< |
| 87 | "GCP.ArtifactRegistry.RepositoriesAttachment", |
| 88 | RepositoriesAttachmentProps, |
| 89 | { |
| 90 | /** Full resource name `projects/{project}/locations/{location}/repositories/{repository}/attachments/{attachment}`. */ |
| 91 | name: string; |
| 92 | /** Attachment id (last path segment). */ |
| 93 | attachmentId: string; |
| 94 | /** Parent repository resource name. */ |
| 95 | repository: string; |
| 96 | /** Project id. */ |
| 97 | project: string; |
| 98 | /** Location id (`us-central1`). */ |
| 99 | location: string; |
| 100 | /** Target resource name. */ |
| 101 | target: string | undefined; |
| 102 | /** File resource names on this attachment. */ |
| 103 | files: string[]; |
| 104 | /** Attachment type. */ |
| 105 | type: string | undefined; |
| 106 | /** Attachment namespace. */ |
| 107 | attachmentNamespace: string | undefined; |
| 108 | /** User annotations (Alchemy ownership keys stripped). */ |
| 109 | annotations: Record<string, string>; |
| 110 | /** OCI version created for Docker attachments, if any. */ |
| 111 | ociVersionName: string | undefined; |
| 112 | /** RFC3339 creation timestamp. */ |
| 113 | createTime: string | undefined; |
| 114 | /** RFC3339 last-update timestamp. */ |
| 115 | updateTime: string | undefined; |
| 116 | }, |
| 117 | never, |
| 118 | Providers |
| 119 | >; |
| 120 | |
| 121 | /** |
| 122 | * Artifact Registry metadata attached to a repository, package, or |
| 123 | * version. An attachment points at one or more files already stored in |
| 124 | * the repository. |
| 125 | * |
| 126 | * Attachments have no update API — any change replaces the resource. |
| 127 | * Alchemy stamps ownership into `annotations` so `list` / nuke can find |
| 128 | * them. |
| 129 | * |
| 130 | * ### Creating a RepositoriesAttachment |
| 131 | * **Example:** Attach an SBOM file to a repository |
| 132 | * ```typescript |
| 133 | * const attachment = yield* GCP.ArtifactRegistry.RepositoriesAttachment( |
| 134 | * "Sbom", |
| 135 | * { |
| 136 | * repository: artifacts.name, |
| 137 | * target: artifacts.name, |
| 138 | * type: "application/spdx+json", |
| 139 | * files: [sbomFileName], |
| 140 | * annotations: { env: "prod" }, |
| 141 | * }, |
| 142 | * ); |
| 143 | * ``` |
| 144 | * |
| 145 | * @resource |
| 146 | * @category ArtifactRegistry |
| 147 | */ |
| 148 | export const RepositoriesAttachment = Resource<RepositoriesAttachment>( |
| 149 | "GCP.ArtifactRegistry.RepositoriesAttachment", |
| 150 | ); |
| 151 | |
| 152 | const expandTarget = (repository: string, target: string) => { |
| 153 | const next = target.replace(/\/+$/, ""); |
| 154 | if (next.includes("/")) return next; |
| 155 | return `${repository}/packages/${next}`; |
| 156 | }; |
| 157 | |
| 158 | const expandFile = (repository: string, file: string) => { |
| 159 | const next = file.replace(/\/+$/, ""); |
| 160 | if (next.includes("/files/")) return next; |
| 161 | return `${repository}/files/${next}`; |
| 162 | }; |
| 163 | |
| 164 | const resourceNameOf = (repository: string, attachmentId: string) => |
| 165 | `${repository}/attachments/${attachmentId}`; |
| 166 | |
| 167 | const userAnnotations = ( |
| 168 | annotations: Record<string, string | undefined> | null | undefined, |
| 169 | ): Record<string, string> => stripInternalLabels(tagRecord(annotations)); |
| 170 | |
| 171 | const toAttrs = (attachment: artifactregistry.Attachment, project: string, region: string) => { |
| 172 | const name = attachment.name ?? ""; |
| 173 | const parsed = parseName(name, "attachments", region); |
| 174 | return { |
| 175 | name, |
| 176 | attachmentId: parsed.id, |
| 177 | repository: parsed.parent, |
| 178 | project: parsed.project || project, |
| 179 | location: parsed.location, |
| 180 | target: attachment.target, |
| 181 | files: [...(attachment.files ?? [])], |
| 182 | type: attachment.type, |
| 183 | attachmentNamespace: attachment.attachmentNamespace, |
| 184 | annotations: userAnnotations(attachment.annotations), |
| 185 | ociVersionName: attachment.ociVersionName, |
| 186 | createTime: attachment.createTime, |
| 187 | updateTime: attachment.updateTime, |
| 188 | }; |
| 189 | }; |
| 190 | |
| 191 | const getByName = missingGet(artifactregistry.getProjectsLocationsRepositoriesAttachments); |
| 192 | |
| 193 | export const RepositoriesAttachmentProvider = () => |
| 194 | Provider.succeed(RepositoriesAttachment, { |
| 195 | stables: ["name", "attachmentId", "repository", "project", "location", "createTime"], |
| 196 | |
| 197 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 198 | if (!isResolved(news)) return undefined; |
| 199 | const env = yield* GcpEnvironment.current; |
| 200 | const previousId = olds?.attachmentId ?? output?.attachmentId; |
| 201 | const nextId = news.attachmentId ?? previousId; |
| 202 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 203 | const nextLocation = normalizeLocation( |
| 204 | news.location ?? locationFromRepository(news.repository, previousLocation), |
| 205 | env.region, |
| 206 | ); |
| 207 | const extra = |
| 208 | !sameText(olds?.target ?? output?.target, news.target) || |
| 209 | JSON.stringify(sortedStrings(olds?.files ?? output?.files)) !== |
| 210 | JSON.stringify(sortedStrings(news.files)) || |
| 211 | !sameText(olds?.type ?? output?.type, news.type ?? DEFAULT_TYPE) || |
| 212 | !sameText( |
| 213 | olds?.attachmentNamespace ?? output?.attachmentNamespace, |
| 214 | news.attachmentNamespace, |
| 215 | ) || |
| 216 | !sameJson(olds?.annotations ?? output?.annotations ?? {}, news.annotations ?? {}); |
| 217 | return replaceOnIdentity({ |
| 218 | previousId, |
| 219 | nextId, |
| 220 | previousLocation, |
| 221 | nextLocation, |
| 222 | previousParent: olds?.repository ?? output?.repository, |
| 223 | nextParent: news.repository, |
| 224 | extra, |
| 225 | }); |
| 226 | }), |
| 227 | |
| 228 | read: Effect.fn(function* ({ id, olds, output }) { |
| 229 | const env = yield* GcpEnvironment.current; |
| 230 | const location = normalizeLocation( |
| 231 | olds?.location ?? |
| 232 | output?.location ?? |
| 233 | locationFromRepository(olds?.repository ?? output?.repository, env.region), |
| 234 | env.region, |
| 235 | ); |
| 236 | const repository = expandRepository( |
| 237 | olds?.repository ?? output?.repository ?? "", |
| 238 | env.project, |
| 239 | location, |
| 240 | ); |
| 241 | const attachmentId = yield* toPhysicalId( |
| 242 | id, |
| 243 | olds?.attachmentId, |
| 244 | output?.attachmentId, |
| 245 | "attachment", |
| 246 | ); |
| 247 | const name = output?.name ?? resourceNameOf(repository, attachmentId); |
| 248 | const existing = yield* getByName(name); |
| 249 | if (existing === undefined) return undefined; |
| 250 | const attrs = toAttrs(existing, env.project, env.region); |
| 251 | return (yield* hasAlchemyLabels(id, tagRecord(existing.annotations))) |
| 252 | ? attrs |
| 253 | : Unowned(attrs); |
| 254 | }), |
| 255 | |
| 256 | list: () => |
| 257 | Effect.gen(function* () { |
| 258 | const env = yield* GcpEnvironment.current; |
| 259 | const repos = yield* listAlchemyRepositories(env.project); |
| 260 | const attachments = yield* listChildResources(repos, listAttachments); |
| 261 | return attachments |
| 262 | .filter((attachment) => hasAlchemyLabelMap(attachment.annotations)) |
| 263 | .map((attachment) => toAttrs(attachment, env.project, env.region)); |
| 264 | }), |
| 265 | |
| 266 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 267 | const env = yield* GcpEnvironment.current; |
| 268 | const location = normalizeLocation( |
| 269 | news.location ?? output?.location ?? locationFromRepository(news.repository, env.region), |
| 270 | env.region, |
| 271 | ); |
| 272 | const repository = expandRepository(news.repository, env.project, location); |
| 273 | const attachmentId = yield* toPhysicalId( |
| 274 | id, |
| 275 | news.attachmentId, |
| 276 | output?.attachmentId, |
| 277 | "attachment", |
| 278 | ); |
| 279 | const name = resourceNameOf(repository, attachmentId); |
| 280 | const target = expandTarget(repository, news.target); |
| 281 | const files = news.files.map((file) => expandFile(repository, file)); |
| 282 | const type = news.type ?? DEFAULT_TYPE; |
| 283 | const attachmentNamespace = news.attachmentNamespace; |
| 284 | const annotations = { |
| 285 | ...tagRecord(news.annotations), |
| 286 | ...(yield* createInternalLabels(id)), |
| 287 | }; |
| 288 | |
| 289 | let current = yield* getByName(name); |
| 290 | |
| 291 | if (current === undefined) { |
| 292 | const created = yield* artifactregistry |
| 293 | .createProjectsLocationsRepositoriesAttachments({ |
| 294 | parent: repository, |
| 295 | attachmentId, |
| 296 | body: { |
| 297 | target, |
| 298 | files, |
| 299 | type, |
| 300 | attachmentNamespace, |
| 301 | annotations, |
| 302 | }, |
| 303 | }) |
| 304 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 305 | if (created !== undefined) { |
| 306 | yield* waitForOperation(created); |
| 307 | } |
| 308 | current = yield* waitUntilExists(getByName(name), name); |
| 309 | } |
| 310 | |
| 311 | if (current === undefined) { |
| 312 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 313 | } |
| 314 | |
| 315 | return toAttrs(current, env.project, env.region); |
| 316 | }), |
| 317 | |
| 318 | delete: Effect.fn(function* ({ output }) { |
| 319 | const operation = yield* artifactregistry |
| 320 | .deleteProjectsLocationsRepositoriesAttachments({ |
| 321 | name: output.name, |
| 322 | }) |
| 323 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 324 | if (operation !== undefined) { |
| 325 | yield* waitForDeleteOperation(operation); |
| 326 | } |
| 327 | yield* waitUntilGone(getByName(output.name), output.name); |
| 328 | }), |
| 329 | }); |