Public: anyone can read this; the crew can change it.
forge.json declares jobs: steps, needs (macos, xcode, docker), on (pull, land) and secrets, read and validated at the boundary #18512
Merged 2h 28m ago, live in production
scout/forge:scout/18507-forge-json-declares-jobs-steps-needs-mac → main 0 commits fork 3 threads · 3 unresolved
11 files
changed +381
−5
docs/features/jobs-declared.md added +23
| @@ -0,0 +1,23 @@ | ||
| 1 | --- | |
| 2 | topic: Your apps | |
| 3 | row: Declared jobs: forge.json lists the work a pull or a landing asks for (nothing runs yet) | |
| 4 | --- | |
| 5 | # Declared jobs: forge.json lists the work a pull or a landing asks for | |
| 6 | ||
| 7 | A repository can declare jobs in the top level of its `forge.json`. Reading and validating is all that happens today: nothing runs yet. | |
| 8 | ||
| 9 | json | |
| 10 | "jobs": [ | |
| 11 | { "name": "ios", "steps": ["xcodebuild test"], "needs": ["macos", "xcode"], "on": ["pull"], "secrets": ["SIGNING_KEY"] } | |
| 12 | ] | |
| 13 | ||
| 14 | ||
| 15 | - `name`: 1–40 lowercase letters, digits, `-` or `_`; unique. | |
| 16 | - `steps`: 1–50 commands, each one line. | |
| 17 | - `needs`: any of `macos`, `xcode`, `docker`; none by default. | |
| 18 | - `on`: `pull`, `land` or both (the default). | |
| 19 | - `secrets`: names only, like `SIGNING_KEY`; a value is refused. | |
| 20 | ||
| 21 | A bad shape fails the check at the push with a sentence naming the file, the entry and the key, for example `forge.json: jobs[0] ("ios").needs names "gpu", which is not one of macos, xcode, docker`. An unknown key is refused too. | |
| 22 | ||
| 23 | The repository's page lists each declared job as one row: its name, then steps, `runs on` its triggers, needs and secret names, for example `1 step · runs on pull · needs macos, xcode · secrets SIGNING_KEY`. The reader is `src/apps/jobs_spec.ts`; the page is `src/web/pages/repo.ts` through `src/web/studio/declared_jobs.ts`. The terminal's door is `src/tui/screens/declared_jobs.ts`: open a repository, then pick `<owner>/<repo> — Jobs` in the palette for the same rows from the same read (`src/actions/declared_jobs.ts`), for anyone who may read the repository. | |
src/actions/declared_jobs.ts added +52
| @@ -0,0 +1,52 @@ | ||
| 1 | /** | |
| 2 | * The jobs a repository's default branch declares in `forge.json` (`apps/jobs_spec.ts`), read for | |
| 3 | * its page and its terminal screen. Nothing runs yet. One small git read per view (the default | |
| 4 | * branch's tip, then one file at it); nothing is kept in memory. A forge.json that is not JSON or | |
| 5 | * whose jobs are refused gives no rows here: the check names the refusal at the push. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { readFileAt } from "../apps/checkout.ts"; | |
| 10 | import { parseJobs, type JobSpec } from "../apps/jobs_spec.ts"; | |
| 11 | import type { Repo } from "../core/repos.ts"; | |
| 12 | import type { User } from "../core/users.ts"; | |
| 13 | import { resolveOid } from "../git/merge.ts"; | |
| 14 | import { repoDir } from "../git/repos.ts"; | |
| 15 | import { requireReader } from "./access.ts"; | |
| 16 | ||
| 17 | /** The facts of one job in one line, the same words on the page and in the terminal. */ | |
| 18 | export function jobFacts(j: JobSpec): string { | |
| 19 | return [ | |
| 20 | j.steps.length === 1 ? "1 step" : `${j.steps.length} steps`, | |
| 21 | `runs on ${j.on.join(" and ")}`, | |
| 22 | ...(j.needs.length === 0 ? [] : [`needs ${j.needs.join(", ")}`]), | |
| 23 | ...(j.secrets.length === 0 ? [] : [`secrets ${j.secrets.join(", ")}`]), | |
| 24 | ].join(" · "); | |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * For a viewer already known to read `repo` (the repository page has checked); others use | |
| 29 | * `jobsFor`. | |
| 30 | */ | |
| 31 | export async function declaredJobs(app: App, repo: Repo): Promise<readonly JobSpec[]> { | |
| 32 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 33 | const tip = await resolveOid(dir, `refs/heads/${repo.defaultBranch}`); | |
| 34 | if (tip === undefined) return []; | |
| 35 | const text = await readFileAt(dir, tip, "forge.json"); | |
| 36 | let jobs: readonly JobSpec[] = []; | |
| 37 | try { | |
| 38 | const doc: unknown = text === undefined ? undefined : JSON.parse(text); | |
| 39 | if (typeof doc === "object" && doc !== null && !Array.isArray(doc)) { | |
| 40 | const read = parseJobs((doc as Record<string, unknown>)["jobs"]); | |
| 41 | if (read.ok) jobs = read.value; | |
| 42 | } | |
| 43 | } catch { | |
| 44 | // not JSON: the check's to report | |
| 45 | } | |
| 46 | return jobs; | |
| 47 | } | |
| 48 | ||
| 49 | /** The declared jobs, for a viewer who may read the repository; none for anyone else. */ | |
| 50 | export async function jobsFor(app: App, actor: User | undefined, repo: Repo): Promise<readonly JobSpec[]> { | |
| 51 | return requireReader(app, actor, repo).ok ? declaredJobs(app, repo) : []; | |
| 52 | } |
src/apps/jobs_spec.ts added +84
| @@ -0,0 +1,84 @@ | ||
| 1 | /** | |
| 2 | * `jobs` (top level of `forge.json`): the work a repository asks the forge to do on a pull or a | |
| 3 | * landing, declared and nothing more. Nothing runs yet; this reads and validates the shape. | |
| 4 | * | |
| 5 | * "jobs": [{ "name": "ios", "steps": ["xcodebuild test"], "needs": ["macos", "xcode"], | |
| 6 | * "on": ["pull"], "secrets": ["SIGNING_KEY"] }] | |
| 7 | * | |
| 8 | * `steps` is a list of commands (at least one). `needs` is a subset of `macos`, `xcode`, `docker` | |
| 9 | * (none by default). `on` is `pull` and/or `land` (both by default). `secrets` are names only, | |
| 10 | * never values (`NAME` like an environment variable). Pure: plain data in, plain data or one | |
| 11 | * sentence naming the file, the key and the entry out. Unknown keys are refused so a misspelt | |
| 12 | * `need` fails the manifest instead of meaning nothing. | |
| 13 | */ | |
| 14 | ||
| 15 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 16 | ||
| 17 | export const JOB_NEEDS = ["macos", "xcode", "docker"] as const; | |
| 18 | export const JOB_TRIGGERS = ["pull", "land"] as const; | |
| 19 | ||
| 20 | export interface JobSpec { | |
| 21 | readonly name: string; | |
| 22 | readonly steps: readonly string[]; | |
| 23 | readonly needs: readonly (typeof JOB_NEEDS)[number][]; | |
| 24 | readonly on: readonly (typeof JOB_TRIGGERS)[number][]; | |
| 25 | readonly secrets: readonly string[]; | |
| 26 | } | |
| 27 | ||
| 28 | export const JOBS_MAX = 20; | |
| 29 | export const STEPS_MAX = 50; | |
| 30 | export const STEP_MAX = 1_000; | |
| 31 | export const SECRETS_MAX = 20; | |
| 32 | const KEYS = new Set(["name", "steps", "needs", "on", "secrets"]); | |
| 33 | const NAME = /^[a-z0-9][a-z0-9_-]{0,39}$/; | |
| 34 | const SECRET = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/; | |
| 35 | ||
| 36 | const show = (s: string): string => s.replace(/[^\x20-\x7e]/g, "?").slice(0, 30); | |
| 37 | ||
| 38 | /** A list of distinct strings drawn from `allowed`, or the sentence that says why not. */ | |
| 39 | function pick<T extends string>(raw: unknown, allowed: readonly T[], at: string, fallback: readonly T[]): Result<readonly T[], string> { | |
| 40 | if (raw === undefined) return ok(fallback); | |
| 41 | if (!Array.isArray(raw)) return err(`${at} must be a list of ${allowed.join(", ")}`); | |
| 42 | const out: T[] = []; | |
| 43 | for (const item of raw) { | |
| 44 | const hit = allowed.find((a) => a === item); | |
| 45 | if (hit === undefined) return err(`${at} names ${typeof item === "string" ? `"${show(item)}"` : "something that is not text"}, which is not one of ${allowed.join(", ")}`); | |
| 46 | if (out.includes(hit)) return err(`${at} names ${hit} twice`); | |
| 47 | out.push(hit); | |
| 48 | } | |
| 49 | return ok(out); | |
| 50 | } | |
| 51 | ||
| 52 | export function parseJobs(value: unknown): Result<readonly JobSpec[], string> { | |
| 53 | if (value === undefined) return ok([]); | |
| 54 | if (!Array.isArray(value)) return err("forge.json: jobs must be a list"); | |
| 55 | if (value.length > JOBS_MAX) return err(`forge.json: jobs holds at most ${JOBS_MAX}`); | |
| 56 | const out: JobSpec[] = []; | |
| 57 | for (const [i, raw] of value.entries()) { | |
| 58 | let at = `forge.json: jobs[${i}]`; | |
| 59 | if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return err(`${at} must be an object`); | |
| 60 | const o = raw as Record<string, unknown>; | |
| 61 | if (typeof o["name"] === "string" && NAME.test(o["name"])) at += ` ("${o["name"]}")`; | |
| 62 | const unknown = Object.keys(o).find((k) => !KEYS.has(k)); | |
| 63 | if (unknown !== undefined) return err(`${at}: unknown key "${show(unknown)}"`); | |
| 64 | if (typeof o["name"] !== "string" || !NAME.test(o["name"])) return err(`${at}.name must be 1-40 lowercase letters, digits, - or _`); | |
| 65 | if (out.some((j) => j.name === o["name"])) return err(`${at}.name "${o["name"]}" is used twice`); | |
| 66 | const steps = o["steps"]; | |
| 67 | if (!Array.isArray(steps) || steps.length === 0 || steps.length > STEPS_MAX) return err(`${at}.steps must be a list of 1-${STEPS_MAX} commands`); | |
| 68 | for (const [s, step] of steps.entries()) | |
| 69 | if (typeof step !== "string" || step.trim() === "" || step.length > STEP_MAX || /[\x00-\x08\x0a-\x1f\x7f]/.test(step)) | |
| 70 | return err(`${at}.steps[${s}] must be one command of 1-${STEP_MAX} characters on one line`); | |
| 71 | const needs = pick(o["needs"], JOB_NEEDS, `${at}.needs`, []); | |
| 72 | if (!needs.ok) return needs; | |
| 73 | const on = pick(o["on"], JOB_TRIGGERS, `${at}.on`, JOB_TRIGGERS); | |
| 74 | if (!on.ok) return on; | |
| 75 | if (on.value.length === 0) return err(`${at}.on must name pull, land or both`); | |
| 76 | const secrets = o["secrets"] ?? []; | |
| 77 | if (!Array.isArray(secrets) || secrets.length > SECRETS_MAX) return err(`${at}.secrets must be a list of at most ${SECRETS_MAX} names`); | |
| 78 | for (const [s, name] of secrets.entries()) | |
| 79 | if (typeof name !== "string" || !SECRET.test(name)) return err(`${at}.secrets[${s}] must be a name like SIGNING_KEY, not a value`); | |
| 80 | if (new Set(secrets).size !== secrets.length) return err(`${at}.secrets names one twice`); | |
| 81 | out.push({ name: o["name"], steps: (steps as string[]).map((s) => s.trim()), needs: needs.value, on: on.value, secrets: secrets as string[] }); | |
| 82 | } | |
| 83 | return ok(out); | |
| 84 | } |
src/apps/manifest.ts +10
| @@ -21,6 +21,7 @@ import { parseAdapter, type AdapterSpec } from "./adapter.ts"; | ||
| 21 | 21 | import { parseToolchain, type ToolchainPin } from "./toolchain.ts"; |
| 22 | 22 | import { parseSchedules, type ScheduleSpec } from "./schedule_spec.ts"; |
| 23 | 23 | import { parseBeat, type BeatSpec } from "./beat_spec.ts"; |
| 24 | import { parseJobs, type JobSpec } from "./jobs_spec.ts"; | |
| 24 | 25 | import { parseFeature, scheduleSpecs } from "./feature.ts"; |
| 25 | 26 | import { PREVIEW_MODES } from "./prepared.ts"; |
| 26 | 27 | import { firstAskProblem } from "./first_ask.ts"; |
| @@ -123,6 +124,11 @@ export interface AppManifest { | ||
| 123 | 124 | * (`apps/beat_spec.ts`). Unset is none. |
| 124 | 125 | */ |
| 125 | 126 | readonly beat?: BeatSpec | undefined; |
| 127 | /** | |
| 128 | * `jobs` (top level of `forge.json`): work declared for a pull or a landing | |
| 129 | * (`apps/jobs_spec.ts`). Nothing runs yet. Unset is none. | |
| 130 | */ | |
| 131 | readonly jobs?: readonly JobSpec[] | undefined; | |
| 126 | 132 | } |
| 127 | 133 | |
| 128 | 134 | export interface DeclaredDeploy { |
| @@ -198,6 +204,9 @@ export function parseManifest( | ||
| 198 | 204 | if (!parsed.ok) return parsed; |
| 199 | 205 | const askProblem = firstAskProblem(parsed.value["firstAsk"]); |
| 200 | 206 | if (askProblem !== undefined) return invalid(askProblem); |
| 207 | // Read before the app is: a repository that is no app still has its jobs refused at the push. | |
| 208 | const jobs = parseJobs(parsed.value["jobs"]); | |
| 209 | if (!jobs.ok) return invalid(jobs.error); | |
| 201 | 210 | const app = parsed.value["app"]; |
| 202 | 211 | if (app === undefined) return err({ kind: "not_an_app" }); |
| 203 | 212 | if (!isObject(app)) return invalid("forge.json: app must be an object"); |
| @@ -328,6 +337,7 @@ export function parseManifest( | ||
| 328 | 337 | ...(toolchain.value === undefined ? {} : { toolchain: toolchain.value }), |
| 329 | 338 | ...(allSchedules.length === 0 ? {} : { schedules: allSchedules }), |
| 330 | 339 | ...(beat.value === undefined ? {} : { beat: beat.value }), |
| 340 | ...(jobs.value.length === 0 ? {} : { jobs: jobs.value }), | |
| 331 | 341 | }); |
| 332 | 342 | } |
| 333 | 343 |
src/tui/screens/declared_jobs.ts added +57
| @@ -0,0 +1,57 @@ | ||
| 1 | /** | |
| 2 | * The jobs a repository declares in `forge.json`, in the terminal: the page's list | |
| 3 | * (`web/studio/declared_jobs.ts`) from the same read (`actions/declared_jobs.ts`), gated the same | |
| 4 | * way, so a reader of the repository sees them and anyone else sees none. Read-only: nothing runs. | |
| 5 | */ | |
| 6 | ||
| 7 | import { jobFacts, jobsFor } from "../../actions/declared_jobs.ts"; | |
| 8 | import { requireReader } from "../../actions/access.ts"; | |
| 9 | import type { Repo } from "../../core/repos.ts"; | |
| 10 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 11 | import type { Command } from "../app/palette.ts"; | |
| 12 | import { binding } from "../app/keys.ts"; | |
| 13 | import { style } from "../term/style.ts"; | |
| 14 | import { truncate } from "../term/width.ts"; | |
| 15 | import type { Session } from "../session.ts"; | |
| 16 | import { fill, len, split } from "../ui/layout.ts"; | |
| 17 | import { ListState, drawList, type ListRow } from "./common.ts"; | |
| 18 | ||
| 19 | /** One line per declared job: its name, then its facts as the page words them. */ | |
| 20 | export async function jobLines(f: Session, repo: Repo): Promise<ListRow[]> { | |
| 21 | return (await jobsFor(f.app, f.user, repo)).map((j) => ({ title: j.name, meta: jobFacts(j) })); | |
| 22 | } | |
| 23 | ||
| 24 | export function declaredJobsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | |
| 25 | const state = new ListState(); | |
| 26 | let rows: ListRow[] = []; | |
| 27 | return { | |
| 28 | title: `${repo.ownerName}/${repo.name} — Jobs`, | |
| 29 | keys: [binding(["j", "k"], "move cursor")], | |
| 30 | async load() { | |
| 31 | rows = await jobLines(f, repo); | |
| 32 | ctx.invalidate(); | |
| 33 | }, | |
| 34 | render(area, s) { | |
| 35 | const c = ctx.theme.c; | |
| 36 | const [head, list] = split(area, "v", [len(1), fill(1)]); | |
| 37 | s.text(head!.x, head!.y, truncate(rows.length === 0 ? "This repository declares no jobs." : "Declared in forge.json; nothing runs yet.", head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 38 | drawList(s, list!, c, state, rows); | |
| 39 | }, | |
| 40 | key(e) { | |
| 41 | return state.key(e, rows.length); | |
| 42 | }, | |
| 43 | }; | |
| 44 | } | |
| 45 | ||
| 46 | /** The palette's command while a repository its viewer may read is open. */ | |
| 47 | export function declaredJobsCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] { | |
| 48 | if (repo === undefined || !requireReader(f.app, f.user, repo).ok) return []; | |
| 49 | return [ | |
| 50 | { | |
| 51 | id: "declared-jobs", | |
| 52 | title: `${repo.ownerName}/${repo.name} — Jobs`, | |
| 53 | hint: "the jobs forge.json declares: steps, triggers, needs, secrets", | |
| 54 | run: () => ctx.push(declaredJobsScreen(f, ctx, repo)), | |
| 55 | }, | |
| 56 | ]; | |
| 57 | } |
src/tui/screens/workspace.ts +2
| @@ -176,6 +176,7 @@ import { settingHistoryCommands } from "./settinghistory.ts"; | ||
| 176 | 176 | import { templateCommands } from "./template.ts"; |
| 177 | 177 | import { buildCacheCommands } from "./buildcache.ts"; |
| 178 | 178 | import { schedulesCommands } from "./schedules.ts"; |
| 179 | import { declaredJobsCommands } from "./declared_jobs.ts"; | |
| 179 | 180 | import { dependenciesCommands } from "./dependencies.ts"; |
| 180 | 181 | import { demoCommands } from "./demo.ts"; |
| 181 | 182 | import { appReleasesCommands } from "./appreleases.ts"; |
| @@ -1415,6 +1416,7 @@ export function workspaceScreen( | ||
| 1415 | 1416 | ...autonomyCommands(f, ctx, repo), |
| 1416 | 1417 | ...trafficCommands(f, ctx, repo), |
| 1417 | 1418 | ...schedulesCommands(f, ctx, repo), |
| 1419 | ...declaredJobsCommands(f, ctx, repo), | |
| 1418 | 1420 | ...dependenciesCommands(f, ctx, repo), |
| 1419 | 1421 | ...demoCommands(f, ctx, repo), |
| 1420 | 1422 | ...appReleasesCommands(f, ctx, repo), |
src/web/pages/repo.ts +4 −1
| @@ -109,6 +109,8 @@ import { growthBody, isAdmin } from "./growth_panel.ts"; | ||
| 109 | 109 | import { studioApp } from "../studio/app.ts"; |
| 110 | 110 | import { crashCard } from "../studio/crash_card.ts"; |
| 111 | 111 | import { declaresFeature } from "../../actions/features.ts"; |
| 112 | import { declaredJobs } from "../../actions/declared_jobs.ts"; | |
| 113 | import { declaredJobsCard } from "../studio/declared_jobs.ts"; | |
| 112 | 114 | import { getAppByRepo } from "../../core/apps.ts"; |
| 113 | 115 | import { field, readForm } from "../form.ts"; |
| 114 | 116 | import { hexdump, HEX_BYTES } from "../render/hexdump.ts"; |
| @@ -1665,6 +1667,7 @@ async function codeHome(ctx: Ctx, repo: Repo, appOf: { readonly isApp: boolean; | ||
| 1665 | 1667 | ? null |
| 1666 | 1668 | : upstreamOffer(ctx.app, ctx.auth.user, repo, parent, drift?.ahead); |
| 1667 | 1669 | const declares = isApp && (await timePhaseAsync(ctx.request, "feature", () => declaresFeature(ctx.app, ctx.auth.user, repo))); |
| 1670 | const jobs = await timePhaseAsync(ctx.request, "jobs", () => declaredJobs(ctx.app, repo)); | |
| 1668 | 1671 | const credit = await timePhaseAsync(ctx.request, "credit", () => |
| 1669 | 1672 | creditLine(ctx, repo, scope.source, ref), |
| 1670 | 1673 | ); |
| @@ -1685,7 +1688,7 @@ async function codeHome(ctx: Ctx, repo: Repo, appOf: { readonly isApp: boolean; | ||
| 1685 | 1688 | : html`<p class="muted latest-wave">Latest: |
| 1686 | 1689 | <a href="${repoBase(repo)}/waves/${encodeURIComponent(latest.version)}">${latest.version}</a> |
| 1687 | 1690 | · ${latest.headline} · ${latest.date}</p>`} |
| 1688 | ${reviewCard(ctx, repo)}${frozenBlock(ctx, repo)} ${appStrip(ctx, repo, declares)} | |
| 1691 | ${reviewCard(ctx, repo)}${frozenBlock(ctx, repo)} ${appStrip(ctx, repo, declares)}${declaredJobsCard(jobs)} | |
| 1689 | 1692 | ${isApp ? null : crashCard(ctx, repo)} |
| 1690 | 1693 | ${ctx.auth.user?.id === repo.ownerId && isContributionFork(ctx.app.db, repo.id) |
| 1691 | 1694 | ? contributionBar(repo) |
src/web/studio/declared_jobs.ts added +16
| @@ -0,0 +1,16 @@ | ||
| 1 | /** | |
| 2 | * The repository page's list of declared jobs (`forge.json` `jobs`): one `dl` row each, the name | |
| 3 | * and its facts (`jobFacts`, shared with the terminal). Declared only; nothing runs yet. Every | |
| 4 | * value is the repository's own text and goes through `html`, which escapes it. | |
| 5 | */ | |
| 6 | ||
| 7 | import { jobFacts } from "../../actions/declared_jobs.ts"; | |
| 8 | import type { JobSpec } from "../../apps/jobs_spec.ts"; | |
| 9 | import { html, type Html } from "../html.ts"; | |
| 10 | ||
| 11 | export function declaredJobsCard(jobs: readonly JobSpec[]): Html | null { | |
| 12 | if (jobs.length === 0) return null; | |
| 13 | return html`<section class="stack declared-jobs" id="declared-jobs"><h2>Jobs</h2> | |
| 14 | <dl>${jobs.map((j) => html`<dt><code>${j.name}</code></dt><dd class="muted">${jobFacts(j)}</dd>`)}</dl> | |
| 15 | </section>`; | |
| 16 | } |
test/apps-rollout.test.ts +8 −2
| @@ -95,6 +95,12 @@ async function bootForge(dataDir: string): Promise<Forge> { | ||
| 95 | 95 | return { app, servers, runtime, presence }; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | /** What the boot's production runs read, so a start that settled early says how. */ | |
| 99 | const startedWords = (forge: Forge, repoId: string) => | |
| 100 | `deploy runs: ${JSON.stringify( | |
| 101 | listRuns(forge.app.db, repoId, { kind: "deploy", limit: 3 }).map((r) => [r.state, r.summary]), | |
| 102 | )}`; | |
| 103 | ||
| 98 | 104 | const get = (forge: Forge, path: string) => |
| 99 | 105 | fetch(`${forge.servers.url}${path}`, { headers: { host: HOST }, redirect: "manual" }); |
| 100 | 106 | |
| @@ -251,7 +257,7 @@ describe("apps: through a rollout", () => { | ||
| 251 | 257 | // The new forge comes up on the same data, as the surge pod does. |
| 252 | 258 | b = await bootForge(dataDir); |
| 253 | 259 | const next = b; |
| 254 | expect(next.runtime.startingCount()).toBe(1); | |
| 260 | expect(next.runtime.startingCount(), startedWords(next, repo.id)).toBe(1); | |
| 255 | 261 | const notYet = await fetch(`${next.servers.url}/readyz`); |
| 256 | 262 | expect(notYet.status).toBe(503); |
| 257 | 263 | expect(await notYet.text()).toBe("starting 1 app"); |
| @@ -303,7 +309,7 @@ describe("apps: through a rollout", () => { | ||
| 303 | 309 | b = undefined; |
| 304 | 310 | |
| 305 | 311 | c = await bootForge(dataDir); |
| 306 | expect(c.runtime.startingCount()).toBe(1); | |
| 312 | expect(c.runtime.startingCount(), startedWords(c, repo.id)).toBe(1); | |
| 307 | 313 | const ready = await fetch(`${c.servers.url}/readyz`); |
| 308 | 314 | expect(ready.status).toBe(200); |
| 309 | 315 | expect(await ready.text()).toBe("ok"); |
test/jobs-spec.test.ts added +123
| @@ -0,0 +1,123 @@ | ||
| 1 | /** | |
| 2 | * `jobs` in forge.json (#18507): the shape, each refusal named by file and key, and the repository | |
| 3 | * page's one line per job. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { declaredJobs } from "../src/actions/declared_jobs.ts"; | |
| 10 | import { promote } from "./helpers/members.ts"; | |
| 11 | import { declaredJobsCommands, jobLines } from "../src/tui/screens/declared_jobs.ts"; | |
| 12 | import type { Ctx } from "../src/tui/app/app.ts"; | |
| 13 | import type { Session } from "../src/tui/session.ts"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import { parseJobs } from "../src/apps/jobs_spec.ts"; | |
| 16 | import { parseManifest } from "../src/apps/manifest.ts"; | |
| 17 | import { createUser } from "../src/core/users.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | import { seededRepo } from "./helpers/seeded_repo.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | const good = { name: "ios", steps: ["xcodebuild test"], needs: ["macos", "xcode"], on: ["pull"], secrets: ["SIGNING_KEY"] }; | |
| 25 | ||
| 26 | function refusal(list: unknown): string { | |
| 27 | const r = parseJobs(list); | |
| 28 | if (r.ok) throw new Error("expected a refusal"); | |
| 29 | return r.error; | |
| 30 | } | |
| 31 | ||
| 32 | test("a job parses into plain data, and what is left out takes its default", () => { | |
| 33 | const r = parseJobs([good, { name: "lint", steps: ["bun run lint"] }]); | |
| 34 | expect(r.ok && r.value).toEqual([ | |
| 35 | { name: "ios", steps: ["xcodebuild test"], needs: ["macos", "xcode"], on: ["pull"], secrets: ["SIGNING_KEY"] }, | |
| 36 | { name: "lint", steps: ["bun run lint"], needs: [], on: ["pull", "land"], secrets: [] }, | |
| 37 | ]); | |
| 38 | expect(parseJobs(undefined)).toEqual({ ok: true, value: [] }); | |
| 39 | }); | |
| 40 | ||
| 41 | test("an unknown need is refused naming the entry and the key", () => { | |
| 42 | expect(refusal([{ ...good, needs: ["windows"] }])).toBe( | |
| 43 | 'forge.json: jobs[0] ("ios").needs names "windows", which is not one of macos, xcode, docker', | |
| 44 | ); | |
| 45 | }); | |
| 46 | ||
| 47 | test("a bad trigger is refused, and so is none", () => { | |
| 48 | expect(refusal([{ ...good, on: ["push"] }])).toStartWith('forge.json: jobs[0] ("ios").on names "push"'); | |
| 49 | expect(refusal([{ ...good, on: [] }])).toBe('forge.json: jobs[0] ("ios").on must name pull, land or both'); | |
| 50 | }); | |
| 51 | ||
| 52 | test("steps must be one to fifty single-line commands", () => { | |
| 53 | expect(refusal([{ name: "a" }])).toBe("forge.json: jobs[0] (\"a\").steps must be a list of 1-50 commands"); | |
| 54 | expect(refusal([{ name: "a", steps: [] }])).toStartWith('forge.json: jobs[0] ("a").steps'); | |
| 55 | expect(refusal([{ name: "a", steps: ["ok", 3] }])).toStartWith('forge.json: jobs[0] ("a").steps[1]'); | |
| 56 | expect(refusal([{ name: "a", steps: ["a\nb"] }])).toStartWith('forge.json: jobs[0] ("a").steps[0]'); | |
| 57 | }); | |
| 58 | ||
| 59 | test("secrets are names, never values", () => { | |
| 60 | expect(refusal([{ ...good, secrets: ["sk live=abc"] }])).toStartWith('forge.json: jobs[0] ("ios").secrets[0]'); | |
| 61 | expect(refusal([{ ...good, secrets: ["A", "A"] }])).toBe('forge.json: jobs[0] ("ios").secrets names one twice'); | |
| 62 | }); | |
| 63 | ||
| 64 | test("shape errors: not a list, not an object, a bad or repeated name, an unknown key", () => { | |
| 65 | expect(refusal({})).toBe("forge.json: jobs must be a list"); | |
| 66 | expect(refusal(["x"])).toBe("forge.json: jobs[0] must be an object"); | |
| 67 | expect(refusal([{ ...good, name: "Bad Name" }])).toBe("forge.json: jobs[0].name must be 1-40 lowercase letters, digits, - or _"); | |
| 68 | expect(refusal([good, good])).toBe('forge.json: jobs[1] ("ios").name "ios" is used twice'); | |
| 69 | expect(refusal([{ ...good, need: [] }])).toBe('forge.json: jobs[0] ("ios"): unknown key "need"'); | |
| 70 | }); | |
| 71 | ||
| 72 | test("the manifest refuses a bad jobs block at the push, with or without an app", () => { | |
| 73 | const bad = { jobs: [{ ...good, needs: ["gpu"] }] }; | |
| 74 | for (const doc of [bad, { app: { run: "bun x.ts" }, ...bad }]) { | |
| 75 | const r = parseManifest(JSON.stringify(doc), undefined); | |
| 76 | expect(!r.ok && r.error.kind === "invalid" && r.error.message).toStartWith('forge.json: jobs[0] ("ios").needs'); | |
| 77 | } | |
| 78 | const ok = parseManifest(JSON.stringify({ app: { run: "bun x.ts" }, jobs: [good] }), undefined); | |
| 79 | expect(ok.ok && ok.value.jobs?.[0]?.name).toBe("ios"); | |
| 80 | }); | |
| 81 | ||
| 82 | let root = ""; | |
| 83 | let app: App; | |
| 84 | let servers: Servers; | |
| 85 | ||
| 86 | beforeAll(async () => { | |
| 87 | root = mkdtempSync(join(tmpdir(), "forge-jobs-spec-")); | |
| 88 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 89 | servers = await serve(app); | |
| 90 | }); | |
| 91 | afterAll(() => { | |
| 92 | servers.stop(); | |
| 93 | app.close(); | |
| 94 | rmSync(root, { recursive: true, force: true }); | |
| 95 | }); | |
| 96 | ||
| 97 | test("the repository page shows one line per declared job", async () => { | |
| 98 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 99 | if (!u.ok) throw new Error(u.error); | |
| 100 | const forge = JSON.stringify({ jobs: [good, { name: "docs", steps: ["bun run docs"], on: ["land"], secrets: ["TOKEN"] }] }); | |
| 101 | const repo = await seededRepo(app, u.value, "ci", "public", [{ message: "jobs", files: [{ path: "forge.json", content: forge }] }]); | |
| 102 | expect((await declaredJobs(app, repo)).map((j) => j.name)).toEqual(["ios", "docs"]); | |
| 103 | const plain = await seededRepo(app, u.value, "plain", "public", [{ message: "x", files: [{ path: "a.txt", content: "a" }] }]); | |
| 104 | const html = await (await fetch(`${servers.url}/alice/ci`)).text(); | |
| 105 | expect(html).toContain("ios"); | |
| 106 | expect(html).toContain("1 step · runs on pull · needs macos, xcode · secrets SIGNING_KEY"); | |
| 107 | expect(html).toContain("<code>docs</code>"); | |
| 108 | expect(await declaredJobs(app, plain)).toEqual([]); | |
| 109 | expect(await (await fetch(`${servers.url}/alice/plain`)).text()).not.toContain('id="declared-jobs"'); | |
| 110 | }); | |
| 111 | ||
| 112 | test("the terminal lists the same jobs, and a private repository's to its readers only", async () => { | |
| 113 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 114 | if (!u.ok) throw new Error(u.error); | |
| 115 | promote(app.db, u.value); | |
| 116 | const forge = JSON.stringify({ jobs: [good] }); | |
| 117 | const repo = await seededRepo(app, u.value, "tci", "private", [{ message: "jobs", files: [{ path: "forge.json", content: forge }] }]); | |
| 118 | const as = (user: typeof u.value | undefined) => ({ app, user, db: app.db }) as unknown as Session; | |
| 119 | expect(await jobLines(as(u.value), repo)).toEqual([{ title: "ios", meta: "1 step · runs on pull · needs macos, xcode · secrets SIGNING_KEY" }]); | |
| 120 | expect(await jobLines(as(undefined), repo)).toEqual([]); | |
| 121 | expect(declaredJobsCommands(as(u.value), {} as Ctx, repo)).toHaveLength(1); | |
| 122 | expect(declaredJobsCommands(as(undefined), {} as Ctx, repo)).toEqual([]); | |
| 123 | }); |
test/repo-timing.test.ts +2 −2
| @@ -52,10 +52,10 @@ afterAll(async () => { | ||
| 52 | 52 | await rm(dataDir, { recursive: true, force: true }); |
| 53 | 53 | }); |
| 54 | 54 | |
| 55 | test("a repository home names session, appof, refs, tree, readme, following, wave, credit, behind and render in order", async () => { | |
| 55 | test("a repository home names session, appof, refs, tree, readme, following, wave, jobs, credit, behind and render in order", async () => { | |
| 56 | 56 | const res = await fetch(`${servers.url}/alice/r`, { headers: { cookie } }); |
| 57 | 57 | expect(res.status).toBe(200); |
| 58 | 58 | await res.arrayBuffer(); |
| 59 | 59 | const names = requestTiming(res).split(", ").map((p) => p.split(";")[0]!); |
| 60 | expect(names).toEqual(["app", "session", "appof", "refs", "tree", "readme", "following", "wave", "credit", "behind", "render"]); | |
| 60 | expect(names).toEqual(["app", "session", "appof", "refs", "tree", "readme", "following", "wave", "jobs", "credit", "behind", "render"]); | |
| 61 | 61 | }); |
new above
offline
Both doors: the terminal has no screen for declared jobs. Add a read-only screen under src/tui/, and replace this sentence with its entry.