import { 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; hooks: DurableObjectStub; claims: Claims; repo: string; body: Record; } 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 { 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 { 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 { return ref === undefined || ref === "" ? await defaultBranch(knot, repo) : branchFromRef(ref); } export async function defaultBranch(knot: string, repo: string): Promise { 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 { 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 { 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 { 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 & { 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 { 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 { 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> { const names = new Set(); 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 { 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 = 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([ ["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 & { trigger: Record }, workflows: Workflow[], repoName: string, stored?: Set, ): Promise { 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): 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): 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): Promise { // 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): Promise { 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): Promise { 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): Promise { 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 { 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, repo: string, rkey: unknown, ): Promise { 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 { 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 { 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 { 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 { 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 { 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 { 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"); } }