import { Context, Effect, Layer, Schema } from "effect"; import { GitHubClient } from "./GitHubClient.js"; import type { GitHubError } from "./GitHubError.js"; import { unstubbed } from "./internal/unstubbed.js"; import { Repo } from "./Repo.js"; /** * The api-version this surface pins. * * @remarks * The legacy shape, which inlines the whole bundle in the listing, is deprecated * with a stated sunset. Pinning the version means the response is on a contract * the generated types do not describe — which is why this module is the one that * uses `requestDecoded` with owned schemas rather than the route table. * * Sent as a per-call header, which wins over the client's own * `apiVersion`, so these reads stay on this contract even when a consumer * pins the client to another version. */ const API_VERSION = "2026-03-10"; /** * A stored attestation. * * @public */ export class AttestationRecord extends Schema.Class("AttestationRecord")({ /** GitHub's id for it, when the response carried one. */ id: Schema.optionalKey(Schema.Int), /** Where a human can look at it. */ url: Schema.String, }) {} /** * One entry from an attestation listing. * * @public */ export class AttestationListEntry extends Schema.Class("AttestationListEntry")({ /** Where the bundle lives. */ url: Schema.String, /** The in-toto predicate type, when the listing reported one. */ predicateType: Schema.optionalKey(Schema.String), }) {} const UploadResponse = Schema.Struct({ id: Schema.optionalKey(Schema.Int) }); const ListResponse = Schema.Struct({ attestations: Schema.optionalKey( Schema.Array( Schema.Struct({ id: Schema.optionalKey(Schema.Int), bundle_url: Schema.optionalKey(Schema.String), predicate_type: Schema.optionalKey(Schema.String), }), ), ), }); /** * The attestation REST surface. * * @remarks * Upload and listing only. Building a statement, signing it and producing the * bundle belong to `@effected/sbom`; assembling those into a pipeline belongs to * the consumer. * * @public */ export interface AttestationShape { /** Store a signed bundle against the repository. */ readonly upload: (bundle: unknown) => Effect.Effect; /** * Everything attested about a subject digest. * * @remarks * **404 and 422 both mean "none"**, not "broken" — GitHub answers a digest it * has never seen either way depending on the path, and a caller asking "is * this attested?" wants an empty list for both. */ readonly listForSubject: ( sha256: string, options?: { readonly predicateType?: string | undefined }, ) => Effect.Effect, GitHubError, Repo>; } /** * Uploads signed attestation bundles to a repository and lists what is attested * about a subject digest. * * @remarks * Provide it with {@link Attestation.layer}, which needs a `GitHubClient`; each * method also needs a `Repo` in `R`. * * @public */ export class Attestation extends Context.Service()("@effected/github/Attestation") { /** The live service, built over a `GitHubClient`. */ static readonly layer: Layer.Layer = Layer.effect( this, Effect.map(GitHubClient, (client) => make(client)), ); /** An in-memory double; an unstubbed member dies when run, naming itself. */ static readonly makeTest = (overrides: Partial = {}): AttestationShape => ({ upload: overrides.upload ?? (() => dies("upload")), listForSubject: overrides.listForSubject ?? (() => dies("listForSubject")), }); /** {@link Attestation.makeTest} behind a `Layer`. */ static readonly layerTest = (overrides: Partial = {}): Layer.Layer => Layer.succeed(Attestation, Attestation.makeTest(overrides)); } const dies = unstubbed("Attestation.makeTest"); const make = (client: GitHubClient["Service"]): AttestationShape => ({ upload: Effect.fn("Attestation.upload")(function* (bundle: unknown) { const { owner, repo } = yield* Repo; yield* Effect.annotateCurrentSpan({ owner, repo }); const stored = yield* client.requestDecoded( "POST /repos/{owner}/{repo}/attestations", { owner, repo, bundle, headers: { "x-github-api-version": API_VERSION } }, UploadResponse, ); return AttestationRecord.make({ ...(stored.id !== undefined ? { id: stored.id } : {}), url: `https://github.com/${owner}/${repo}/attestations/${stored.id ?? ""}`, }); }), listForSubject: Effect.fn("Attestation.listForSubject")(function* ( sha256: string, options?: { readonly predicateType?: string | undefined }, ) { const { owner, repo } = yield* Repo; const digest = sha256.startsWith("sha256:") ? sha256 : `sha256:${sha256}`; yield* Effect.annotateCurrentSpan({ owner, repo, digest }); const listed = yield* client .requestDecoded( "GET /repos/{owner}/{repo}/attestations/{subject_digest}", { owner, repo, subject_digest: digest, ...(options?.predicateType !== undefined ? { predicate_type: options.predicateType } : {}), headers: { "x-github-api-version": API_VERSION }, }, ListResponse, ) .pipe( Effect.catchIf( (error) => error.kind === "notFound" || (error.status === 422 && error.kind === "rejected"), () => Effect.succeed({ attestations: [] as ReadonlyArray }), ), ); return (listed.attestations ?? []).flatMap((entry) => { const url = entry.bundle_url ?? (entry.id !== undefined ? `https://github.com/${owner}/${repo}/attestations/${entry.id}` : undefined); if (url === undefined) return []; return [ AttestationListEntry.make({ url, ...(entry.predicate_type !== undefined ? { predicateType: entry.predicate_type } : {}), }), ]; }); }), });