Something went wrong. Try again.
Tangled Spindle built on Cloudflare Workers
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930import { authenticate } from "./auth";import type { Claims } from "./auth";import { authorization, branchFromRef, cancelBuild, pipelineUrl, readBuild } from "./buildkite";import type { Commit } from "./buildkite";import { didWeb, resolveDid, serviceOrigin } from "./identity";import type { NewPipeline, PipelineRow, RunRow, Store } from "./store";import { afterErrors, invalidWorkflow, isTrusted, matchesPush, parseWorkflow, pipelineSlug, resolveAfter, triggers, withPrerequisites,} from "./workflow";import type { BuildkiteTarget, Workflow } from "./workflow";import type { Repository, Webhooks } from "./webhooks";import { errorMessage, errorName, getRecord, invalidRequest, isDid, isObject, isPairList, string, upstream, upstreamError, XrpcError, xrpcUrl,} from "./xrpc";
const buildkiteApi = "https://api.buildkite.com/v2";const workflowDir = ".tangled/workflows";const maxTokenLifetimeSeconds = 300;const maxWorkflows = 50;const maxWorkflowBytes = 256 * 1024;
interface Context { env: Env; store: DurableObjectStub<Store>; hooks: DurableObjectStub<Webhooks>; claims: Claims; repo: string; body: Record<string, unknown>;}
export function isObjectId(sha: unknown): sha is string { return typeof sha === "string" && /^(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/.test(sha);}
function isStringList(value: unknown): value is string[] { return Array.isArray(value) && value.every((item) => typeof item === "string");}
export interface Manifest { name: string; createdAt: string; description: string; website?: string; fork: boolean;}
export function repository(env: Env, knot: string, manifest: Manifest): Repository { const { name, createdAt, description, website, fork } = manifest; const fullName = `${env.OWNER_DID}/${name.toLowerCase()}`; const host = new URL(knot).host; return { name, full_name: fullName, description, fork, html_url: `https://${host}/${fullName}`, clone_url: `https://${host}/${fullName}`, ssh_url: `ssh://git@${host}/${fullName}`, ...(website === undefined ? {} : { website }), created_at: createdAt, updated_at: createdAt, owner: { did: env.OWNER_DID }, };}
function output(rkey?: string): Response { return Response.json(rkey === undefined ? {} : { ci: { pipelines: [rkey] } });}
export async function resolveKnot(repo: string): Promise<string> { const knot = serviceOrigin(await resolveDid(repo), "tangled_knot", "TangledKnot"); if (knot === undefined) { throw new XrpcError(503, "RepoKnotUnavailable", `${repo} declares no knot`); } return knot;}
// Returns the repo manifest when the repo is served.// The knot writes the manifest, so the owner must confirm the repo from their own PDS.export async function admitRepo( env: Env, repo: string, knot: string,): Promise<Manifest | undefined> { const [manifest, ownerDoc] = await Promise.all([ getRecord(knot, repo, "org.tangled.repo.manifest", "self", 500), resolveDid(env.OWNER_DID), ]); const declaration = manifest?.["declaration"]; if (manifest?.["spindle"] !== `did:web:${env.HOSTNAME}` || !isObject(declaration)) { return undefined; } const { owner, name } = declaration; if (owner !== env.OWNER_DID || typeof name !== "string") { return undefined; } const pds = serviceOrigin(ownerDoc, "atproto_pds", "AtprotoPersonalDataServer"); if (pds === undefined) { throw new XrpcError(503, "UpstreamUnavailable", `${owner} has no PDS`); } const record = await getRecord( pds, owner, "org.tangled.repo.declaration", name.toLowerCase(), 503, ); if (record?.["repo"] !== repo) { return undefined; } const { createdAt, description, website, source } = manifest; return { name, createdAt: typeof createdAt === "string" && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|[+-]\d\d:\d\d)$/i.test(createdAt) && !Number.isNaN(Date.parse(createdAt)) ? createdAt : "", description: typeof description === "string" && [...new Intl.Segmenter().segment(description)].length <= 140 ? description : "", ...(typeof website === "string" && /^https?:$/.test(URL.parse(website)?.protocol ?? "") ? { website } : {}), fork: isObject(source), };}
async function refBranch(knot: string, repo: string, ref: string | undefined): Promise<string> { return ref === undefined || ref === "" ? await defaultBranch(knot, repo) : branchFromRef(ref);}
export async function defaultBranch(knot: string, repo: string): Promise<string> { const refs = await getRecord(knot, repo, "org.tangled.git.refs", "self", 500); const branch = refs?.["defaultBranch"]; return typeof branch === "string" && branch !== "" ? branchFromRef(branch) : "HEAD";}
function firstCommit(body: unknown): unknown { return isObject(body) && Array.isArray(body["commits"]) ? body["commits"][0] : undefined;}
// Returns undefined when the knot cannot name the commit, because the build runs without its message.export async function readCommit( knot: string, repo: string, sha: string,): Promise<Commit | undefined> { const url = xrpcUrl(knot, "org.tangled.git.listCommits", { repo, ref: sha, limit: "1" }); try { const response = await upstream(url, 500); const body: unknown = response.ok ? await response.json() : undefined; const commit = firstCommit(body); const message = isObject(commit) ? string(commit["message"])?.trimEnd() : undefined; if ( !isObject(commit) || string(commit["hash"])?.toLowerCase() !== sha.toLowerCase() || message === undefined || message === "" ) { console.warn("commit unavailable", { repo, sha, status: response.status }); return undefined; } const { author } = commit; return { message: message.slice(0, 10_000).toWellFormed(), ...(isObject(author) && typeof author["name"] === "string" && typeof author["email"] === "string" ? { author: { name: author["name"], email: author["email"] } } : {}), }; } catch (error) { console.warn("commit unavailable", { repo, sha, error: String(error) }); return undefined; }}
// Returns undefined when the ref does not exist.export async function headCommit( knot: string, repo: string, ref: string,): Promise<string | undefined> { const url = xrpcUrl(knot, "org.tangled.git.listCommits", { repo, ref, limit: "1" }); const response = await upstream(url, 500); if (!response.ok) { if ((await errorName(response)) === "RefNotFound") { return undefined; } throw upstreamError(url, 500, `HTTP ${response.status}`); } const commit = firstCommit(await response.json().catch(() => null)); const hash = isObject(commit) ? commit["hash"] : undefined; if (!isObjectId(hash)) { throw upstreamError(url, 500, "response names no commit"); } return hash;}
// Skips workflows that treadle cannot build.export async function loadWorkflows( knot: string, repo: string, ref: string, repoName: string,): Promise<Workflow[]> { const treeUrl = xrpcUrl(knot, "org.tangled.git.getTree", { repo, ref, path: workflowDir }); const tree = await upstream(treeUrl, 500); if (!tree.ok) { const name = await errorName(tree); if (name === "PathNotFound") { return []; } if (name === "RefNotFound" || name === "RepoNotFound") { throw new XrpcError(422, "PushRefused", `knot does not serve ${ref}: ${name}`); } throw upstreamError(treeUrl, 500, `HTTP ${tree.status}`); } const body: unknown = await tree.json().catch(() => null); if (!isObject(body) || !Array.isArray(body["entries"])) { throw upstreamError(treeUrl, 500, "response has no entries"); } const entries: unknown[] = body["entries"]; const files = entries.filter( (entry): entry is Record<string, unknown> & { name: string } => isObject(entry) && typeof entry["name"] === "string" && (entry["mode"] === "0100644" || entry["mode"] === "0100755") && /\.ya?ml$/.test(entry["name"]), ); const big = files.find( (entry) => typeof entry["size"] === "number" && entry["size"] > maxWorkflowBytes, ); if (files.length > maxWorkflows || big !== undefined) { const found = big === undefined ? `${files.length} workflow files` : `${big.name} is too large`; throw new XrpcError( 422, "WorkflowsTooLarge", `${found}: treadle reads at most ${maxWorkflows} workflow files of 256 KiB each`, ); } const names = files.map((entry) => entry.name); const workflows = await Promise.all( names.map(async (name) => { const url = xrpcUrl(knot, "org.tangled.git.getBlob", { repo, ref, path: `${workflowDir}/${name}`, }); const blob = await upstream(url, 500); if (blob.status >= 400 && blob.status < 500 && ![408, 429].includes(blob.status)) { console.warn("unreadable workflow", { repo, ref, name, status: blob.status }); return []; } if (!blob.ok) { throw upstreamError(url, 500, `HTTP ${blob.status}`); } const raw = await blob.text(); try { return [parseWorkflow(name, raw)]; } catch (e) { const error = errorMessage(e); console.warn("invalid workflow", { repo, ref, name, error }); return invalidWorkflow(name, raw, error) ?? []; } }), ); const supported = workflows.flat().filter((workflow) => { const { name, engine } = workflow; if (engine === "cloudflare" || workflow.error !== undefined) { return true; } if (engine !== "buildkite") { console.log("unsupported engine", { repo, ref, name, engine }); return false; } if ((workflow.buildkite.pipeline ?? pipelineSlug(repoName)) === "") { console.warn("no Buildkite pipeline", { repo, ref, name, repoName }); return false; } return true; }); return resolveAfter(supported);}
// Shows workflow files over the limits as one failed run.async function buildableWorkflows( knot: string, repo: string, ref: string, repoName: string,): Promise<Workflow[]> { try { return await loadWorkflows(knot, repo, ref, repoName); } catch (error) { if (!(error instanceof XrpcError) || error.error !== "WorkflowsTooLarge") { throw error; } return [ { name: workflowDir, raw: "", engine: undefined, when: [], buildkite: {}, cloudflare: undefined, error: error.message, }, ]; }}
type Event = "push" | "manual" | "pull_request";
// Returns undefined when the default branch has no such workflow file.async function defaultBranchFile( knot: string, repo: string, branch: string, name: string,): Promise<string | undefined> { const url = xrpcUrl(knot, "org.tangled.git.getBlob", { repo, ref: `refs/heads/${branch}`, path: `${workflowDir}/${name}`, }); const blob = await upstream(url, 500); if (!blob.ok) { if ((await errorName(blob)) === "PathNotFound") { return undefined; } throw upstreamError(url, 500, `HTTP ${blob.status}`); } return await blob.text();}
// Returns the names of the engine: cloudflare workflows whose run is trusted. The trusted branches// come from the default-branch workflow file, never from the built commit. A tag has no branch.// A knot failure leaves the run untrusted.export async function trustedWorkflows( knot: string, repo: string, event: Event, branch: string | null, commit: string, workflows: Workflow[],): Promise<Set<string>> { const names = new Set<string>(); const jobs = workflows.filter((workflow) => workflow.cloudflare !== undefined); if (event === "pull_request" || branch === null || jobs.length === 0) { return names; } try { // A manual run accepts any commit, and the knot cannot tell whether the branch holds it. if ( event === "manual" && (await headCommit(knot, repo, `refs/heads/${branch}`))?.toLowerCase() !== commit.toLowerCase() ) { return names; } const main = await defaultBranch(knot, repo); if (main === "HEAD") { return names; } for (const { name } of jobs) { // oxlint-disable-next-line no-await-in-loop const raw = await defaultBranchFile(knot, repo, main, name); if (isTrusted(event, raw, main, branch)) { names.add(name); } } } catch (error) { console.warn("trust unavailable", { repo, branch, error: String(error) }); } return names;}
function usesBuildkite(workflow: Workflow): boolean { return workflow.cloudflare === undefined && workflow.error === undefined;}
// A pull request must not pick the Buildkite pipeline that builds it.export async function defaultTargets( knot: string, repo: string, workflows: Workflow[],): Promise<Workflow[]> { if (!workflows.some(usesBuildkite)) { return workflows; } const main = await defaultBranch(knot, repo); return await Promise.all( workflows.map(async (workflow) => { if (!usesBuildkite(workflow)) { return workflow; } const raw = main === "HEAD" ? undefined : await defaultBranchFile(knot, repo, main, workflow.name); let target: BuildkiteTarget = {}; try { target = raw === undefined ? {} : parseWorkflow(workflow.name, raw).buildkite; } catch { // An invalid default-branch file gives the default pipeline. } const { cleanCheckout: _, ...fromMain } = target; const { cleanCheckout } = workflow.buildkite; return { ...workflow, buildkite: { ...fromMain, ...(cleanCheckout === undefined ? {} : { cleanCheckout }), }, }; }), );}
export function newRuns( env: Env, workflows: Workflow[], repoName: string, trusted: Set<string> = new Set(),): NewPipeline["runs"] { const errors = afterErrors(workflows); return workflows.map((workflow) => { const error = workflow.error ?? errors.get(workflow.name); return { // A Store from before failed runs starts the run, and the Runner then fails it. engine: workflow.cloudflare === undefined && error === undefined ? "buildkite" : "cloudflare", trusted: error === undefined && trusted.has(workflow.name), ...(error === undefined ? {} : { error }), ...(workflow.after === undefined || workflow.after.length === 0 ? {} : { after: workflow.after }), workflow: workflow.name, raw: workflow.raw, triggers: triggers(workflow), org: workflow.buildkite.org ?? env.BUILDKITE_ORG, pipeline: workflow.buildkite.pipeline ?? pipelineSlug(repoName), cleanCheckout: workflow.buildkite.cleanCheckout, }; });}
const events = new Map<unknown, Event>([ ["org.tangled.event.push", "push"], ["org.tangled.ci.trigger.manual", "manual"], ["org.tangled.event.pullRequest", "pull_request"],]);
async function dispatch( ctx: Context, knot: string, pipeline: Omit<NewPipeline, "runs" | "repo"> & { trigger: Record<string, unknown> }, workflows: Workflow[], repoName: string, stored?: Set<string>,): Promise<string> { const ref = string(pipeline.trigger["ref"]); const trusted = stored ?? (await trustedWorkflows( knot, ctx.repo, events.get(pipeline.trigger["$type"]) ?? "pull_request", ref?.startsWith("refs/") === true && !ref.startsWith("refs/heads/") ? null : pipeline.branch, pipeline.commit, workflows, )); const input = { ...pipeline, repo: ctx.repo, runs: newRuns(ctx.env, workflows, repoName, trusted), }; return await ctx.store.createPipeline(input);}
// Returns the knot and name of a repo that this spindle still serves.export async function servedRepo({ env, repo,}: Pick<Context, "env" | "repo">): Promise<{ knot: string; name: string }> { const knot = await resolveKnot(repo); const manifest = await admitRepo(env, repo, knot); if (manifest === undefined) { throw new XrpcError(404, "RepoNotFound", "repository is not served by this spindle"); } return { knot, name: manifest.name };}
// A repo token comes from the knot that hosts the repo.export function authorize({ claims, repo, env }: Pick<Context, "claims" | "repo" | "env">): void { if (claims.iss !== repo && claims.iss !== env.OWNER_DID) { throw new XrpcError(403, "AccessControl", `${claims.iss} cannot run CI for ${repo}`); }}
// Refuses an undeclared repo before its DID document or knot is fetched.async function declared({ store, repo }: Pick<Context, "store" | "repo">): Promise<boolean> { // A Store from before this check has no such method. return await store.declares(repo).catch(() => true);}
// A replayed event would start its builds again.export async function claimToken({ claims, store,}: Pick<Context, "claims" | "store">): Promise<void> { if (claims.jti === undefined || claims.exp - Date.now() / 1000 > maxTokenLifetimeSeconds) { throw new XrpcError(403, "Auth", "token has no jti or lives too long"); } if (!(await store.admitToken(claims.iss, claims.jti, claims.exp))) { throw new XrpcError(403, "Auth", "token was already used"); }}
async function push(ctx: Context, event: Record<string, unknown>): Promise<Response> { const { claims, repo, store, env } = ctx; const { ref, oldSha, newSha, actor, pushOptions } = event; if (event["repo"] !== repo) { throw invalidRequest("push event names a different repo"); } if (!isDid(actor)) { throw invalidRequest("push event names no actor DID"); } if (typeof ref !== "string" || typeof oldSha !== "string" || typeof newSha !== "string") { throw invalidRequest("push event needs ref, oldSha, and newSha"); } if (!(await declared(ctx))) { return output(); } const knot = await resolveKnot(repo); if (didWeb(knot) !== claims.iss) { throw new XrpcError(403, "Auth", `${claims.iss} does not host ${repo}`); } await claimToken(ctx);
if (!ref.startsWith("refs/heads/") && !ref.startsWith("refs/tags/")) { return output(); } if (!isObjectId(oldSha) || !isObjectId(newSha)) { throw new XrpcError( 422, "PushRefused", "push SHAs must be 40 or 64 character hex object IDs", ); } if (/^0+$/.test(newSha)) { return output(); } const pushKey = [repo, ref, oldSha, newSha].join(" "); const existing = await store.pushPipeline(pushKey); if (existing !== undefined) { return output(existing); } const manifest = await admitRepo(env, repo, knot); if (manifest === undefined) { return output(); } const { name } = manifest; if (await ctx.hooks.subscribed(["push"])) { const payload = { ref, before: oldSha, after: newSha, repository: repository(env, knot, manifest), pusher: { did: actor }, }; await ctx.hooks.firePush(pushKey, payload, `Tangled-Hook/${newSha.slice(0, 7)}`); } const skip = Array.isArray(pushOptions) && pushOptions.some((o) => o === "skip-ci" || o === "ci-skip"); if (skip) { return output(); } const [all, info] = await Promise.all([ buildableWorkflows(knot, repo, newSha, name), readCommit(knot, repo, newSha), ]); const workflows = all.filter((wf) => matchesPush(wf, ref)); if (workflows.length === 0) { return output(); } const trigger = { $type: "org.tangled.event.push", repo, ref, oldSha, newSha, actor }; return output( await dispatch( ctx, knot, { trigger, pushKey, commit: newSha, branch: branchFromRef(ref), ...info }, workflows, name, ), );}
async function manual(ctx: Context, event: Record<string, unknown>): Promise<Response> { const { repo } = ctx; const { commit, ref, workflows: only, inputs } = event; if (event["repo"] !== repo) { throw invalidRequest("manual trigger names a different repo"); } if (!isObjectId(commit)) { throw invalidRequest("commit must be a 40 or 64 character hex SHA"); } if ( inputs !== undefined && !(isPairList(inputs) && inputs.every(({ key }) => /^[A-Za-z_]\w*$/.test(key))) ) { throw invalidRequest("each input needs a name of letters, digits and underscores"); } authorize(ctx); if (!(await declared(ctx))) { throw new XrpcError(404, "RepoNotFound", "repository is not served by this spindle"); } await claimToken(ctx); const { knot, name } = await servedRepo(ctx); // The knot cannot tell whether the ref contains the commit, so only check that the ref exists. if ( typeof ref === "string" && ref !== "" && (await headCommit(knot, repo, ref)) === undefined ) { throw invalidRequest(`${ref} does not exist`); } const names = isStringList(only) ? only : undefined; const [all, info] = await Promise.all([ buildableWorkflows(knot, repo, commit, name), readCommit(knot, repo, commit), ]); const workflows = names === undefined || names.length === 0 ? all : withPrerequisites( all, (workflow) => workflow.name === workflowDir || names.includes(workflow.name), ); if (workflows.length === 0) { throw invalidRequest("no workflows matched"); } const trigger = { $type: "org.tangled.ci.trigger.manual", repo, commit, ...(typeof ref === "string" ? { ref } : {}), ...(names === undefined ? {} : { workflows: names }), ...(isPairList(inputs) ? { inputs: inputs.map(({ key, value }) => ({ key, value })) } : {}), }; const branch = await refBranch(knot, repo, string(ref)); return output( await dispatch( ctx, knot, { trigger, pushKey: null, commit, branch, ...info }, workflows, name, ), );}
async function context( request: Request, env: Env, exports: Cloudflare.Exports, method: string,): Promise<Context> { const claims = await authenticate(request, env, exports, method); const body: unknown = await request.json().catch(() => null); if (!isObject(body) || !isDid(body["repo"])) { throw invalidRequest("body needs a repo DID"); } const repo = body["repo"]; return { env, store: exports.Store.getByName("main"), hooks: exports.Webhooks.getByName(repo), claims, repo, body, };}
export async function findPipeline( store: DurableObjectStub<Store>, repo: string, rkey: unknown,): Promise<PipelineRow> { if (typeof rkey !== "string" || rkey === "") { throw invalidRequest("pipeline is required"); } const pipeline = await store.getPipeline(repo, rkey); if (pipeline === undefined) { throw new XrpcError(404, "PipelineNotFound", `${rkey} is not a pipeline of ${repo}`); } return pipeline;}
export function findRun(pipeline: PipelineRow, workflow: string): RunRow { const run = pipeline.runs.find((r) => r.workflow === workflow); if (run === undefined) { throw new XrpcError( 404, "WorkflowNotFound", `${workflow} is not a workflow of ${pipeline.rkey}`, ); } return run;}
async function storedPipeline(ctx: Context): Promise<PipelineRow> { return await findPipeline(ctx.store, ctx.repo, ctx.body["pipeline"]);}
// Returns the runs that the `workflows` parameter names, or every run when it names none.function wantedRuns(ctx: Context, stored: PipelineRow): RunRow[] { const only = ctx.body["workflows"] ?? []; if (!isStringList(only)) { throw invalidRequest("workflows must be a list of names"); } const names = new Set(stored.runs.map((run) => run.workflow)); const unknown = only.filter((name) => !names.has(name)); if (unknown.length > 0) { throw invalidRequest(`${stored.rkey} has no workflow ${unknown.join(", ")}`); } return only.length > 0 ? stored.runs.filter((run) => only.includes(run.workflow)) : stored.runs;}
export async function retryPipeline( request: Request, env: Env, exports: Cloudflare.Exports,): Promise<Response> { const ctx = await context(request, env, exports, "org.tangled.ci.retryPipeline"); const { claims, repo } = ctx; authorize(ctx); await claimToken(ctx); const stored = await storedPipeline(ctx); if (stored.runs.some((run) => run.status === "blocked")) { throw new XrpcError(400, "PipelineNotRetryable", `${stored.rkey} is blocked`); } const wanted = new Set(wantedRuns(ctx, stored).map((run) => run.workflow)); const { knot, name } = await servedRepo(ctx); const [all, info] = await Promise.all([ buildableWorkflows(knot, repo, stored.commit_sha, name), readCommit(knot, repo, stored.commit_sha), ]); let workflows = withPrerequisites( all, (workflow) => workflow.name === workflowDir || wanted.has(workflow.name), ); if (workflows.length === 0) { throw invalidRequest("no workflows matched"); } const trigger: unknown = JSON.parse(stored.trigger); if (!isObject(trigger)) { throw new XrpcError(400, "PipelineNotRetryable", `${stored.rkey} has no trigger`); } if (trigger["$type"] === "org.tangled.event.pullRequest") { workflows = await defaultTargets(knot, repo, workflows); } // A repo token names the user that the knot minted it for. trigger["actor"] = claims.iss === repo ? (claims.act ?? repo) : claims.iss; const branch = await refBranch(knot, repo, string(trigger["ref"])); const rkey = await dispatch( ctx, knot, { trigger, pushKey: null, commit: stored.commit_sha, branch, held: stored.unapproved === 1, ...info, }, workflows, name, new Set(stored.runs.filter((run) => run.trusted === 1).map((run) => run.workflow)), ); return Response.json({ pipeline: rkey });}
async function buildkite(env: Env, url: string, init: RequestInit = {}): Promise<Response> { const response = await upstream( url, 503, { ...init, headers: { Authorization: authorization(env.BUILDKITE_TOKEN), "Content-Type": "application/json", }, }, { read: readBuild }, ); if (!response.ok) { throw upstreamError(url, 503, `HTTP ${response.status}`); } return response;}
// Starts held runs, unblocks the block steps, and leaves the new status to the Buildkite webhooks.export async function approvePipeline( request: Request, env: Env, exports: Cloudflare.Exports,): Promise<Response> { const ctx = await context(request, env, exports, "org.tangled.ci.approvePipeline"); authorize(ctx); await claimToken(ctx); const stored = await storedPipeline(ctx); await servedRepo(ctx); const blocked = stored.runs.filter((run) => run.status === "blocked"); if (blocked.length === 0) { throw new XrpcError(400, "PipelineNotBlocked", `${stored.rkey} is not blocked`); } if ( blocked.some((run) => run.build_number === null) && (await ctx.store.releaseBlocked(ctx.repo, stored.rkey)) === 0 && blocked.every((run) => run.build_number === null) ) { throw new XrpcError(400, "PipelineNotBlocked", `${stored.rkey} is not blocked`); } for (const run of blocked.filter((r) => r.build_number !== null)) { const url = `${pipelineUrl(run.bk_org, run.bk_pipeline)}/builds/${run.build_number}`; // oxlint-disable-next-line no-await-in-loop const build: unknown = await (await buildkite(env, url)).json().catch(() => null); if (!isObject(build) || !Array.isArray(build["jobs"])) { throw upstreamError(url, 503, "response has no jobs"); } const jobs: unknown[] = build["jobs"]; for (const job of jobs) { if (isObject(job) && job["type"] === "manual" && job["unblockable"] === true) { const id = encodeURIComponent(String(job["id"])); // oxlint-disable-next-line no-await-in-loop await buildkite( env, `${buildkiteApi}/organizations/${encodeURIComponent(run.bk_org)}/jobs/${id}/unblock`, { method: "PUT", body: "{}" }, ); } } } return new Response(null);}
export async function cancelPipeline( request: Request, env: Env, exports: Cloudflare.Exports,): Promise<Response> { const ctx = await context(request, env, exports, "org.tangled.ci.cancelPipeline"); authorize(ctx); await claimToken(ctx); const stored = await storedPipeline(ctx); const workflows = wantedRuns(ctx, stored).map((run) => run.workflow); await servedRepo(ctx); const builds = await ctx.store.cancelRuns(ctx.repo, stored.rkey, workflows); const cancelled: string[] = []; try { for (const run of builds) { // oxlint-disable-next-line no-await-in-loop const ok = await cancelBuild( env.BUILDKITE_TOKEN, run.bk_org, run.bk_pipeline, run.build_number, ); // Buildkite refuses a finished build, and its webhook reports how it finished. // It also refuses a build at a block step, and no webhook follows. if (ok || run.status === "blocked") { cancelled.push(run.workflow); } } } finally { await ctx.store.markCancelled(ctx.repo, stored.rkey, cancelled); } return new Response(null);}
export async function notifyEvent( request: Request, env: Env, exports: Cloudflare.Exports,): Promise<Response> { const ctx = await context(request, env, exports, "org.tangled.event.notifyEvent"); const event = ctx.body["event"]; if (!isObject(event)) { throw invalidRequest("body needs a repo DID and an event"); } switch (event["$type"]) { case "org.tangled.event.push": return await push(ctx, event); case "org.tangled.ci.trigger.manual": return await manual(ctx, event); default: throw invalidRequest("event is not a push or manual trigger"); }}