Something went wrong. Try again.
Tangled Spindle built on Cloudflare Workers
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import { findPipeline, headCommit, loadWorkflows, servedRepo } from "./notify";import type { PipelineRow, Store } from "./store";import { triggers } from "./workflow";import { invalidRequest, isDid, XrpcError } from "./xrpc";
export function repoParam(repo: string | null | undefined): string { if (!isDid(repo)) { throw invalidRequest("repo must be a DID"); } return repo;}
interface Definition { id: string; name: string; source: object; triggers: unknown;}
function definition( repo: string, commit: string, id: string, workflowTriggers: unknown,): Definition { return { id, name: id, source: { $type: "org.tangled.ci.defs#fileSource", repo, commit, path: `.tangled/workflows/${id}`, }, triggers: workflowTriggers, };}
function view(pipeline: PipelineRow): Record<string, unknown> { return { id: pipeline.rkey, repo: pipeline.repo, trigger: JSON.parse(pipeline.trigger) as unknown, commit: pipeline.commit_sha, createdAt: pipeline.created_at, workflows: pipeline.runs.map((run) => { const id = run.workflow; return { id, name: id, // A started build reads as pending until Buildkite reports on it. status: run.status === "started" ? "pending" : run.status, ...(run.started_at === null ? {} : { startedAt: run.started_at }), ...(run.finished_at === null ? {} : { finishedAt: run.finished_at }), ...(run.error === null ? {} : { error: run.error }), definition: definition( pipeline.repo, pipeline.commit_sha, id, JSON.parse(run.triggers) as unknown, ), }; }), };}
interface PipelineQuery { repo: string | null; limit?: number | undefined; cursor?: string | null | undefined; commits?: string[] | undefined; triggers?: string[] | undefined;}
export async function queryPipelines( args: PipelineQuery, store: DurableObjectStub<Store>,): Promise<Record<string, unknown>> { const repo = repoParam(args.repo); const { limit = 50, commits = [] } = args; if (!Number.isInteger(limit) || limit < 1 || limit > 250) { throw invalidRequest("limit must be an integer from 1 to 250"); } if (commits.some((commit) => commit.length > 64)) { throw invalidRequest("commits must be at most 64 characters"); } const result = await store.queryPipelines(repo, { commits, triggers: args.triggers ?? [], cursor: args.cursor ?? undefined, limit, }); const last = result.pipelines.at(-1); return { ...(result.pipelines.length === limit && last !== undefined ? { cursor: last.rkey } : {}), total: result.total, pipelines: result.pipelines.map(view), };}
export async function getPipeline( args: { repo: string | null; pipeline: string | null }, store: DurableObjectStub<Store>,): Promise<Record<string, unknown>> { return view(await findPipeline(store, repoParam(args.repo), args.pipeline));}
async function definitionsAt(env: Env, repo: string, ref: string): Promise<Definition[]> { const { knot, name } = await servedRepo({ env, repo }); const commit = await headCommit(knot, repo, ref); // A repo without commits has no HEAD. if (commit === undefined && ref === "HEAD") { return []; } if (commit === undefined) { throw new XrpcError(404, "RefNotFound", `${ref} does not exist`); } const workflows = await loadWorkflows(knot, repo, commit, name); return workflows.map((w) => definition(repo, commit, w.name, triggers(w)));}
export async function listWorkflowDefinitions( args: { repo: string | null; ref?: string | null | undefined }, env: Env,): Promise<Record<string, unknown>> { const repo = repoParam(args.repo); const ref = args.ref ?? ""; if (ref.length > 256) { throw invalidRequest("ref must be at most 256 characters"); } const workflows = await definitionsAt(env, repo, ref === "" ? "HEAD" : ref); return { workflows };}
export async function getWorkflowDefinition( args: { repo: string | null; workflow: string | null }, env: Env,): Promise<Record<string, unknown>> { const repo = repoParam(args.repo); const id = args.workflow ?? ""; if (id.length < 1 || id.length > 128) { throw invalidRequest("workflow must be 1 to 128 characters"); } const workflow = (await definitionsAt(env, repo, "HEAD")).find((w) => w.id === id); if (workflow === undefined) { throw new XrpcError(404, "WorkflowNotFound", `${id} is not a workflow at HEAD`); } return { workflow };}