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

main
ts 250 lines 8.85 KB
Raw
1 import * as containeranalysis from "@distilled.cloud/gcp/containeranalysis_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 * as Effect from "effect/Effect";
7 import { GcpEnvironment } from "../Environment.ts";
8 import { hasAlchemyLabels } from "../Labels.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 expandNoteName,
12 hasOwnershipMarker,
13 ignoreGone,
14 listLocationNotes,
15 locationParent,
16 missingGet,
17 noteAttrs,
18 noteKind,
19 normalizeLocation,
20 parseDescription,
21 reconcileNote,
22 replaceOnIdentity,
23 retryTransient,
24 toPhysicalId,
25 } from "./internal.ts";
26 import type { NoteProps } from "./Note.ts";
27
28 export type LocationsNoteProps = NoteProps & {
29 /**
30 * Location (`us-central1`, `us-east1`, …). Immutable — changing it
31 * replaces the note. `US-CENTRAL1` is accepted and normalized to
32 * `us-central1`.
33 * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`)
34 */
35 location?: string;
36 };
37
38 export type LocationsNote = Resource<
39 "GCP.ContainerAnalysis.LocationsNote",
40 LocationsNoteProps,
41 {
42 /** Full resource name `projects/{project}/locations/{location}/notes/{note}`. */
43 name: string;
44 /** Note id (last path segment). */
45 noteId: string;
46 /** Project id. */
47 project: string;
48 /** Location id. */
49 location: string;
50 /** One-sentence summary. */
51 shortDescription: string | undefined;
52 /** User description with the Alchemy ownership prefix stripped. */
53 longDescription: string | undefined;
54 /** Server-reported analysis kind. */
55 kind: string | undefined;
56 /** RFC3339 expiration, if any. */
57 expirationTime: string | undefined;
58 /** URLs associated with this note. */
59 relatedUrl: containeranalysis.RelatedUrlList;
60 /** Related note resource names. */
61 relatedNoteNames: string[];
62 /** Attestation authority metadata. */
63 attestation: containeranalysis.AttestationNote | undefined;
64 /** Build provenance metadata. */
65 build: containeranalysis.BuildNote | undefined;
66 /** Discovery metadata. */
67 discovery: containeranalysis.DiscoveryNote | undefined;
68 /** Base image metadata. */
69 image: containeranalysis.ImageNote | undefined;
70 /** Package metadata. */
71 package: containeranalysis.PackageNote | undefined;
72 /** Vulnerability metadata. */
73 vulnerability: containeranalysis.VulnerabilityNote | undefined;
74 /** Compliance metadata. */
75 compliance: containeranalysis.ComplianceNote | undefined;
76 /** Deployment metadata. */
77 deployment: containeranalysis.DeploymentNote | undefined;
78 /** Upgrade metadata. */
79 upgrade: containeranalysis.UpgradeNote | undefined;
80 /** Vulnerability assessment metadata. */
81 vulnerabilityAssessment: containeranalysis.VulnerabilityAssessmentNote | undefined;
82 /** Secret metadata. */
83 secret: containeranalysis.SecretNote | undefined;
84 /** DSSE attestation metadata. */
85 dsseAttestation: containeranalysis.DSSEAttestationNote | undefined;
86 /** SBOM reference metadata. */
87 sbomReference: containeranalysis.SBOMReferenceNote | undefined;
88 /** AI skill analysis metadata. */
89 aiSkillAnalysis: containeranalysis.AISkillAnalysisNote | undefined;
90 /** RFC3339 creation timestamp. */
91 createTime: string | undefined;
92 /** RFC3339 last-update timestamp. */
93 updateTime: string | undefined;
94 },
95 never,
96 Providers
97 >;
98
99 /**
100 * A location-scoped Container Analysis (Grafeas) note. Same shape as
101 * {@link Note}, stored at
102 * `projects/{project}/locations/{location}/notes/{note}`.
103 *
104 * Location and note id are identity. Alchemy stamps ownership into
105 * `longDescription` for `list` / nuke.
106 *
107 * ### Creating a Locations Note
108 * **Example:** Regional attestation authority
109 * ```typescript
110 * const note = yield* GCP.ContainerAnalysis.LocationsNote("Authority", {
111 * location: "us-central1",
112 * shortDescription: "qa attestor",
113 * attestation: { hint: { humanReadableName: "QA" } },
114 * });
115 * ```
116 *
117 * @resource
118 * @category ContainerAnalysis
119 */
120 export const LocationsNote = Resource<LocationsNote>("GCP.ContainerAnalysis.LocationsNote");
121
122 const resourceName = (project: string, location: string, noteId: string) =>
123 expandNoteName(noteId, project, location);
124
125 const getByName = missingGet(containeranalysis.getProjectsLocationsNotes);
126
127 const toPublicAttrs = (
128 note: containeranalysis.Note,
129 project: string,
130 region: string,
131 ): LocationsNote["Attributes"] => {
132 const attrs = noteAttrs(note, project);
133 return {
134 name: attrs.name,
135 noteId: attrs.noteId,
136 project: attrs.project,
137 location: attrs.location ?? region,
138 shortDescription: attrs.shortDescription,
139 longDescription: attrs.longDescription,
140 kind: attrs.kind,
141 expirationTime: attrs.expirationTime,
142 relatedUrl: attrs.relatedUrl,
143 relatedNoteNames: attrs.relatedNoteNames,
144 attestation: attrs.attestation,
145 build: attrs.build,
146 discovery: attrs.discovery,
147 image: attrs.image,
148 package: attrs.package,
149 vulnerability: attrs.vulnerability,
150 compliance: attrs.compliance,
151 deployment: attrs.deployment,
152 upgrade: attrs.upgrade,
153 vulnerabilityAssessment: attrs.vulnerabilityAssessment,
154 secret: attrs.secret,
155 dsseAttestation: attrs.dsseAttestation,
156 sbomReference: attrs.sbomReference,
157 aiSkillAnalysis: attrs.aiSkillAnalysis,
158 createTime: attrs.createTime,
159 updateTime: attrs.updateTime,
160 };
161 };
162
163 export const LocationsNoteProvider = () =>
164 Provider.succeed(LocationsNote, {
165 stables: ["name", "noteId", "project", "location", "kind", "createTime"],
166
167 diff: Effect.fn(function* ({ news, olds, output }) {
168 if (!isResolved(news)) return undefined;
169 const env = yield* GcpEnvironment.current;
170 const previousKind = noteKind(olds ?? {}) ?? output?.kind;
171 const nextKind = noteKind(news) ?? previousKind;
172 return replaceOnIdentity({
173 previousId: olds?.noteId ?? output?.noteId,
174 nextId: news.noteId,
175 previousParent: locationParent(
176 "x",
177 normalizeLocation(olds?.location ?? output?.location, env.region),
178 ),
179 nextParent: locationParent(
180 "x",
181 normalizeLocation(news.location ?? olds?.location ?? output?.location, env.region),
182 ),
183 extra: previousKind !== undefined && nextKind !== undefined && previousKind !== nextKind,
184 });
185 }),
186
187 read: Effect.fn(function* ({ id, olds, output }) {
188 const env = yield* GcpEnvironment.current;
189 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
190 const noteId = yield* toPhysicalId(id, olds?.noteId, output?.noteId);
191 const name = output?.name ?? resourceName(env.project, location, noteId);
192 const existing = yield* getByName(name);
193 if (existing === undefined) return undefined;
194 const attrs = toPublicAttrs(existing, env.project, env.region);
195 const { labels } = parseDescription(existing.longDescription);
196 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
197 }),
198
199 list: () =>
200 Effect.gen(function* () {
201 const env = yield* GcpEnvironment.current;
202 const items = yield* listLocationNotes(env.project, env.region);
203 return items
204 .filter((item) => hasOwnershipMarker(item.longDescription))
205 .map((item) => toPublicAttrs(item, env.project, env.region));
206 }),
207
208 reconcile: Effect.fn(function* ({ id, news, output }) {
209 const env = yield* GcpEnvironment.current;
210 const location = normalizeLocation(news.location ?? output?.location, env.region);
211 const noteId = yield* toPhysicalId(id, news.noteId, output?.noteId);
212 const parent = locationParent(env.project, location);
213 const name = resourceName(env.project, location, noteId);
214 const current = yield* reconcileNote({
215 id,
216 name: output?.name ?? name,
217 parent,
218 noteId,
219 news,
220 ops: {
221 get: getByName,
222 create: (request) =>
223 retryTransient(
224 containeranalysis.createProjectsLocationsNotes({
225 parent: request.parent,
226 noteId: request.noteId,
227 body: request.body,
228 }),
229 ).pipe(
230 Effect.catchTag("Conflict", () =>
231 getByName(`${request.parent}/notes/${request.noteId}`),
232 ),
233 ),
234 patch: (request) =>
235 retryTransient(
236 containeranalysis.patchProjectsLocationsNotes({
237 name: request.name,
238 updateMask: request.updateMask,
239 body: request.body,
240 }),
241 ),
242 },
243 });
244 return toPublicAttrs(current, env.project, env.region);
245 }),
246
247 delete: Effect.fn(function* ({ output }) {
248 yield* ignoreGone(containeranalysis.deleteProjectsLocationsNotes({ name: output.name }));
249 }),
250 });