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

main
ts 329 lines 10.5 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 listProjectNotes,
15 missingGet,
16 noteAttrs,
17 noteKind,
18 parseDescription,
19 projectParent,
20 reconcileNote,
21 replaceOnIdentity,
22 retryTransient,
23 toPhysicalId,
24 } from "./internal.ts";
25
26 export type NoteProps = {
27 /**
28 * Note id (the `{note}` segment of
29 * `projects/{project}/notes/{note}`). If omitted, a unique RFC1035 id is
30 * generated. Immutable — changing it replaces the note.
31 */
32 noteId?: string;
33 /**
34 * One-sentence summary of the analysis this note describes.
35 */
36 shortDescription?: string;
37 /**
38 * Detailed description. Alchemy stamps ownership into this field for
39 * `list` / nuke because notes have no labels API.
40 */
41 longDescription?: string;
42 /**
43 * RFC3339 expiration. Empty means the note does not expire.
44 */
45 expirationTime?: string;
46 /**
47 * URLs associated with this note.
48 */
49 relatedUrl?: containeranalysis.RelatedUrlList;
50 /**
51 * Other note resource names related to this note.
52 */
53 relatedNoteNames?: string[];
54 /**
55 * Attestation authority note. Used when no other kind is set.
56 */
57 attestation?: containeranalysis.AttestationNote;
58 /**
59 * Build provenance note.
60 */
61 build?: containeranalysis.BuildNote;
62 /**
63 * Discovery note.
64 */
65 discovery?: containeranalysis.DiscoveryNote;
66 /**
67 * Base image note.
68 */
69 image?: containeranalysis.ImageNote;
70 /**
71 * Package note.
72 */
73 package?: containeranalysis.PackageNote;
74 /**
75 * Vulnerability note.
76 */
77 vulnerability?: containeranalysis.VulnerabilityNote;
78 /**
79 * Compliance note.
80 */
81 compliance?: containeranalysis.ComplianceNote;
82 /**
83 * Deployment note.
84 */
85 deployment?: containeranalysis.DeploymentNote;
86 /**
87 * Upgrade note.
88 */
89 upgrade?: containeranalysis.UpgradeNote;
90 /**
91 * Vulnerability assessment note.
92 */
93 vulnerabilityAssessment?: containeranalysis.VulnerabilityAssessmentNote;
94 /**
95 * Secret note.
96 */
97 secret?: containeranalysis.SecretNote;
98 /**
99 * DSSE attestation note.
100 */
101 dsseAttestation?: containeranalysis.DSSEAttestationNote;
102 /**
103 * SBOM reference note.
104 */
105 sbomReference?: containeranalysis.SBOMReferenceNote;
106 /**
107 * AI skill analysis note.
108 */
109 aiSkillAnalysis?: containeranalysis.AISkillAnalysisNote;
110 };
111
112 export type Note = Resource<
113 "GCP.ContainerAnalysis.Note",
114 NoteProps,
115 {
116 /** Full resource name `projects/{project}/notes/{note}`. */
117 name: string;
118 /** Note id (last path segment). */
119 noteId: string;
120 /** Project id. */
121 project: string;
122 /** One-sentence summary. */
123 shortDescription: string | undefined;
124 /** User description with the Alchemy ownership prefix stripped. */
125 longDescription: string | undefined;
126 /** Server-reported analysis kind. */
127 kind: string | undefined;
128 /** RFC3339 expiration, if any. */
129 expirationTime: string | undefined;
130 /** URLs associated with this note. */
131 relatedUrl: containeranalysis.RelatedUrlList;
132 /** Related note resource names. */
133 relatedNoteNames: string[];
134 /** Attestation authority metadata. */
135 attestation: containeranalysis.AttestationNote | undefined;
136 /** Build provenance metadata. */
137 build: containeranalysis.BuildNote | undefined;
138 /** Discovery metadata. */
139 discovery: containeranalysis.DiscoveryNote | undefined;
140 /** Base image metadata. */
141 image: containeranalysis.ImageNote | undefined;
142 /** Package metadata. */
143 package: containeranalysis.PackageNote | undefined;
144 /** Vulnerability metadata. */
145 vulnerability: containeranalysis.VulnerabilityNote | undefined;
146 /** Compliance metadata. */
147 compliance: containeranalysis.ComplianceNote | undefined;
148 /** Deployment metadata. */
149 deployment: containeranalysis.DeploymentNote | undefined;
150 /** Upgrade metadata. */
151 upgrade: containeranalysis.UpgradeNote | undefined;
152 /** Vulnerability assessment metadata. */
153 vulnerabilityAssessment: containeranalysis.VulnerabilityAssessmentNote | undefined;
154 /** Secret metadata. */
155 secret: containeranalysis.SecretNote | undefined;
156 /** DSSE attestation metadata. */
157 dsseAttestation: containeranalysis.DSSEAttestationNote | undefined;
158 /** SBOM reference metadata. */
159 sbomReference: containeranalysis.SBOMReferenceNote | undefined;
160 /** AI skill analysis metadata. */
161 aiSkillAnalysis: containeranalysis.AISkillAnalysisNote | undefined;
162 /** RFC3339 creation timestamp. */
163 createTime: string | undefined;
164 /** RFC3339 last-update timestamp. */
165 updateTime: string | undefined;
166 },
167 never,
168 Providers
169 >;
170
171 /**
172 * A project-scoped Container Analysis (Grafeas) note. Notes describe a
173 * type of analysis; occurrences attach a note to a resource such as a
174 * container image.
175 *
176 * Notes have no labels field, so Alchemy stamps ownership into
177 * `longDescription` for `list` / nuke. `noteId` is identity — changing it
178 * replaces the note. Descriptions, related URLs, and kind payloads update
179 * in place. Switching the analysis kind replaces the note.
180 *
181 * ### Creating a Note
182 * **Example:** Attestation authority
183 * ```typescript
184 * const note = yield* GCP.ContainerAnalysis.Note("Authority", {
185 * shortDescription: "qa attestor",
186 * attestation: { hint: { humanReadableName: "QA" } },
187 * });
188 * ```
189 *
190 * **Example:** Explicit id and related URL
191 * ```typescript
192 * const note = yield* GCP.ContainerAnalysis.Note("Authority", {
193 * noteId: "qa-attestor",
194 * shortDescription: "qa attestor",
195 * longDescription: "signs production images",
196 * relatedUrl: [{ url: "https://example.com/policy", label: "policy" }],
197 * attestation: { hint: { humanReadableName: "QA" } },
198 * });
199 * ```
200 *
201 * ### Updating a Note
202 * Re-declare the same logical id with changed props; the engine keeps the
203 * physical resource and updates it in place.
204 *
205 * **Example:** Change the summary
206 * ```typescript
207 * const note = yield* GCP.ContainerAnalysis.Note("Authority", {
208 * shortDescription: "qa and staging attestor",
209 * attestation: { hint: { humanReadableName: "QA" } },
210 * });
211 * ```
212 *
213 * @resource
214 * @category ContainerAnalysis
215 */
216 export const Note = Resource<Note>("GCP.ContainerAnalysis.Note");
217
218 const resourceName = (project: string, noteId: string) => expandNoteName(noteId, project);
219
220 const getByName = missingGet(containeranalysis.getProjectsNotes);
221
222 const toPublicAttrs = (note: containeranalysis.Note, project: string): Note["Attributes"] => {
223 const attrs = noteAttrs(note, project);
224 return {
225 name: attrs.name,
226 noteId: attrs.noteId,
227 project: attrs.project,
228 shortDescription: attrs.shortDescription,
229 longDescription: attrs.longDescription,
230 kind: attrs.kind,
231 expirationTime: attrs.expirationTime,
232 relatedUrl: attrs.relatedUrl,
233 relatedNoteNames: attrs.relatedNoteNames,
234 attestation: attrs.attestation,
235 build: attrs.build,
236 discovery: attrs.discovery,
237 image: attrs.image,
238 package: attrs.package,
239 vulnerability: attrs.vulnerability,
240 compliance: attrs.compliance,
241 deployment: attrs.deployment,
242 upgrade: attrs.upgrade,
243 vulnerabilityAssessment: attrs.vulnerabilityAssessment,
244 secret: attrs.secret,
245 dsseAttestation: attrs.dsseAttestation,
246 sbomReference: attrs.sbomReference,
247 aiSkillAnalysis: attrs.aiSkillAnalysis,
248 createTime: attrs.createTime,
249 updateTime: attrs.updateTime,
250 };
251 };
252
253 export const NoteProvider = () =>
254 Provider.succeed(Note, {
255 stables: ["name", "noteId", "project", "kind", "createTime"],
256
257 diff: Effect.fn(function* ({ news, olds, output }) {
258 if (!isResolved(news)) return undefined;
259 const previousKind = noteKind(olds ?? {}) ?? output?.kind;
260 const nextKind = noteKind(news) ?? previousKind;
261 return replaceOnIdentity({
262 previousId: olds?.noteId ?? output?.noteId,
263 nextId: news.noteId,
264 extra: previousKind !== undefined && nextKind !== undefined && previousKind !== nextKind,
265 });
266 }),
267
268 read: Effect.fn(function* ({ id, olds, output }) {
269 const env = yield* GcpEnvironment.current;
270 const noteId = yield* toPhysicalId(id, olds?.noteId, output?.noteId);
271 const name = output?.name ?? resourceName(env.project, noteId);
272 const existing = yield* getByName(name);
273 if (existing === undefined) return undefined;
274 const attrs = toPublicAttrs(existing, env.project);
275 const { labels } = parseDescription(existing.longDescription);
276 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
277 }),
278
279 list: () =>
280 Effect.gen(function* () {
281 const env = yield* GcpEnvironment.current;
282 const items = yield* listProjectNotes(env.project);
283 return items
284 .filter((item) => hasOwnershipMarker(item.longDescription))
285 .map((item) => toPublicAttrs(item, env.project));
286 }),
287
288 reconcile: Effect.fn(function* ({ id, news, output }) {
289 const env = yield* GcpEnvironment.current;
290 const noteId = yield* toPhysicalId(id, news.noteId, output?.noteId);
291 const parent = projectParent(env.project);
292 const name = resourceName(env.project, noteId);
293 const current = yield* reconcileNote({
294 id,
295 name: output?.name ?? name,
296 parent,
297 noteId,
298 news,
299 ops: {
300 get: getByName,
301 create: (request) =>
302 retryTransient(
303 containeranalysis.createProjectsNotes({
304 parent: request.parent,
305 noteId: request.noteId,
306 body: request.body,
307 }),
308 ).pipe(
309 Effect.catchTag("Conflict", () =>
310 getByName(`${request.parent}/notes/${request.noteId}`),
311 ),
312 ),
313 patch: (request) =>
314 retryTransient(
315 containeranalysis.patchProjectsNotes({
316 name: request.name,
317 updateMask: request.updateMask,
318 body: request.body,
319 }),
320 ),
321 },
322 });
323 return toPublicAttrs(current, env.project);
324 }),
325
326 delete: Effect.fn(function* ({ output }) {
327 yield* ignoreGone(containeranalysis.deleteProjectsNotes({ name: output.name }));
328 }),
329 });