83 lines · 4.1 KBRaw
| 1 | import type { CiEnv } from "./ci-env.ts"; |
| 2 | import { approvalEvent } from "./run-id.ts"; |
| 3 | import { startRun, type StartResult } from "./start-run.ts"; |
| 4 | |
| 5 | export interface CiRepo { |
| 6 | readonly repoId: string; |
| 7 | readonly namespaceId: string; |
| 8 | /** The Artifacts repository name. */ |
| 9 | readonly repo: string; |
| 10 | } |
| 11 | |
| 12 | /** One accepted push, as the push queue saw it. */ |
| 13 | export interface CiPush extends CiRepo { |
| 14 | readonly pusher: "human" | "agent" | "system"; |
| 15 | readonly actorId: string | null; |
| 16 | readonly updates: readonly { readonly ref: string; readonly to: string | null }[]; |
| 17 | } |
| 18 | |
| 19 | export interface CiPull extends CiRepo { |
| 20 | readonly number: number; |
| 21 | readonly base: string; |
| 22 | readonly head: string; |
| 23 | readonly sha: string; |
| 24 | readonly author: "human" | "agent"; |
| 25 | readonly actorId: string | null; |
| 26 | } |
| 27 | |
| 28 | export type PushOutcome = { readonly ref: string; readonly pull: number | null } & StartResult; |
| 29 | |
| 30 | /** |
| 31 | * Starts the runs a push asks for: a push run for each pushed branch tip, and a pull request run for each open pull |
| 32 | * request whose head is that branch. Deleted refs and tags start nothing. |
| 33 | */ |
| 34 | export async function onPush(env: CiEnv, push: CiPush): Promise<PushOutcome[]> { |
| 35 | const outcomes: PushOutcome[] = []; |
| 36 | for (const update of push.updates) { |
| 37 | if (!update.to || !update.ref.startsWith("refs/heads/")) continue; |
| 38 | const branch = update.ref.slice("refs/heads/".length); |
| 39 | const result = await startRun(env, { ...repoOf(push), sha: update.to, ref: update.ref, source: { kind: "push", pusher: push.pusher, branch }, actorId: push.actorId }); |
| 40 | outcomes.push({ ref: update.ref, pull: null, ...result }); |
| 41 | const { results } = await env.DB.prepare( |
| 42 | "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'", |
| 43 | ) |
| 44 | .bind(push.repoId, branch) |
| 45 | .all<{ number: number; base_ref: string; by_agent: number }>(); |
| 46 | for (const pull of results) { |
| 47 | const author = pull.by_agent ? "agent" : push.pusher; |
| 48 | const started = await startPullRun(env, { ...repoOf(push), number: pull.number, base: pull.base_ref, head: branch, sha: update.to, author, actorId: push.actorId }); |
| 49 | outcomes.push({ ref: update.ref, pull: pull.number, ...started }); |
| 50 | } |
| 51 | } |
| 52 | return outcomes; |
| 53 | } |
| 54 | |
| 55 | export function onPullOpened(env: CiEnv, pull: CiPull): Promise<StartResult> { |
| 56 | return startPullRun(env, pull); |
| 57 | } |
| 58 | |
| 59 | function startPullRun(env: CiEnv, pull: Omit<CiPull, "author"> & { readonly author: "human" | "agent" | "system" }): Promise<StartResult> { |
| 60 | 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 }); |
| 61 | } |
| 62 | |
| 63 | /** A run started by hand on the tip of a branch, by someone with write access. An agent's run is untrusted. */ |
| 64 | export async function startManualRun(env: CiEnv, repo: CiRepo, branch: string, actorId: string, by: "human" | "agent"): Promise<StartResult | { readonly kind: "no_branch" }> { |
| 65 | using handle = await env.ARTIFACTS.get(repo.repo); |
| 66 | const [commit] = await handle.log({ ref: branch, limit: 1 }); |
| 67 | if (!commit) return { kind: "no_branch" }; |
| 68 | return startRun(env, { ...repoOf(repo), sha: commit.hash, ref: `refs/heads/${branch}`, source: { kind: "manual", by, branch }, actorId }); |
| 69 | } |
| 70 | |
| 71 | /** A person's answer to a job that waits for approval. Returns false when the job does not wait. */ |
| 72 | export async function decideJob(env: CiEnv, runId: string, job: string, decision: "approved" | "rejected", by: { readonly id: string; readonly name: string }): Promise<boolean> { |
| 73 | const hub = env.RUN_HUB.getByName(runId); |
| 74 | if (!(await hub.awaitingApproval()).includes(job)) return false; |
| 75 | await hub.decide(job, decision, by); |
| 76 | const instance = await env.RUNS.get(runId); |
| 77 | await instance.sendEvent({ type: approvalEvent(job), payload: { decision } }); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | function repoOf(repo: CiRepo): CiRepo { |
| 82 | return { repoId: repo.repoId, namespaceId: repo.namespaceId, repo: repo.repo }; |
| 83 | } |