import type { CiEnv } from "./ci-env.ts"; import { approvalEvent } from "./run-id.ts"; import { startRun, type StartResult } from "./start-run.ts"; export interface CiRepo { readonly repoId: string; readonly namespaceId: string; /** The Artifacts repository name. */ readonly repo: string; } /** One accepted push, as the push queue saw it. */ export interface CiPush extends CiRepo { readonly pusher: "human" | "agent" | "system"; readonly actorId: string | null; readonly updates: readonly { readonly ref: string; readonly to: string | null }[]; } export interface CiPull extends CiRepo { readonly number: number; readonly base: string; readonly head: string; readonly sha: string; readonly author: "human" | "agent"; readonly actorId: string | null; } export type PushOutcome = { readonly ref: string; readonly pull: number | null } & StartResult; /** * Starts the runs a push asks for: a push run for each pushed branch tip, and a pull request run for each open pull * request whose head is that branch. Deleted refs and tags start nothing. */ export async function onPush(env: CiEnv, push: CiPush): Promise { const outcomes: PushOutcome[] = []; for (const update of push.updates) { if (!update.to || !update.ref.startsWith("refs/heads/")) continue; const branch = update.ref.slice("refs/heads/".length); const result = await startRun(env, { ...repoOf(push), sha: update.to, ref: update.ref, source: { kind: "push", pusher: push.pusher, branch }, actorId: push.actorId }); outcomes.push({ ref: update.ref, pull: null, ...result }); const { results } = await env.DB.prepare( "SELECT p.number, p.base_ref, EXISTS (SELECT 1 FROM events e WHERE e.stream = 'pull:' || p.id AND e.kind = 'pull.opened' AND e.actor_kind = 'agent') AS by_agent FROM pulls p WHERE p.repo_id = ? AND p.head_ref = ? AND p.status = 'open'", ) .bind(push.repoId, branch) .all<{ number: number; base_ref: string; by_agent: number }>(); for (const pull of results) { const author = pull.by_agent ? "agent" : push.pusher; const started = await startPullRun(env, { ...repoOf(push), number: pull.number, base: pull.base_ref, head: branch, sha: update.to, author, actorId: push.actorId }); outcomes.push({ ref: update.ref, pull: pull.number, ...started }); } } return outcomes; } export function onPullOpened(env: CiEnv, pull: CiPull): Promise { return startPullRun(env, pull); } function startPullRun(env: CiEnv, pull: Omit & { readonly author: "human" | "agent" | "system" }): Promise { return startRun(env, { ...repoOf(pull), sha: pull.sha, ref: `refs/heads/${pull.head}`, source: { kind: "pull_request", author: pull.author, fromFork: false, number: pull.number, base: pull.base, head: pull.head }, actorId: pull.actorId }); } /** A run started by hand on the tip of a branch, by someone with write access. An agent's run is untrusted. */ export async function startManualRun(env: CiEnv, repo: CiRepo, branch: string, actorId: string, by: "human" | "agent"): Promise { using handle = await env.ARTIFACTS.get(repo.repo); const [commit] = await handle.log({ ref: branch, limit: 1 }); if (!commit) return { kind: "no_branch" }; return startRun(env, { ...repoOf(repo), sha: commit.hash, ref: `refs/heads/${branch}`, source: { kind: "manual", by, branch }, actorId }); } /** A person's answer to a job that waits for approval. Returns false when the job does not wait. */ export async function decideJob(env: CiEnv, runId: string, job: string, decision: "approved" | "rejected", by: { readonly id: string; readonly name: string }): Promise { const hub = env.RUN_HUB.getByName(runId); if (!(await hub.awaitingApproval()).includes(job)) return false; await hub.decide(job, decision, by); const instance = await env.RUNS.get(runId); await instance.sendEvent({ type: approvalEvent(job), payload: { decision } }); return true; } function repoOf(repo: CiRepo): CiRepo { return { repoId: repo.repoId, namespaceId: repo.namespaceId, repo: repo.repo }; }