Public: anyone can read this; the crew can change it.
A push that changes an app's version is recorded as a release #13728
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13707-a-push-that-changes-an-app-s-version-is → main 8 commits fork 2 threads · 2 unresolved
20 files
changed +765
−4
docs/features/changelog.md +26
| @@ -55,3 +55,29 @@ Chapter-grouped views (see Chapters) carry no month headings, so `/changelog#202 | ||
| 55 | 55 | `/changelog` reads the newest 30 live releases, so the first page stays small however many have shipped (under 40 KB with 500 pulls; the first page answered in about 11 ms median in `test/changelog-window.test.ts`). Below them, inside the live region, a plain link reads "Older releases, before 12 Mar" (the UTC day of the last release shown): it goes to `?before=owner/name#number`, the last pull shown, and that page holds the next 30 older ones. Follow it until it is gone; the last page has no link. Nothing is on its way past the first page. |
| 56 | 56 | |
| 57 | 57 | The repository and bet filters apply while the page is read, so `?repo=owner/name` or `?bet=name` gives 30 matching releases a page, and the link is there only when more matching ones remain; a filter matching a few shows them on one page with no link. The filters ride along in the link. A cursor that names no public pull is the first page, never an error. The page's `ETag` holds the cursor. The feeds, `/api/v1/changelog` and MCP `releases` are not paged. |
| 58 | ||
| 59 | ## App releases (#13707) | |
| 60 | Say where the app tells its version, under `app` in `forge.json`: | |
| 61 | ||
| 62 | json | |
| 63 | { "app": { "run": "bun src/main.ts", "version": "/healthz" } } | |
| 64 | ||
| 65 | ||
| 66 | `version` is a path the running app answers (`/healthz`, JSON with a `version` string, or one line of | |
| 67 | text) or a file in the commit (`VERSION`, `package.json`). When production, deployed from the | |
| 68 | default branch, first serves a version the repository has not had, the forge records a release: | |
| 69 | the version, the commit, the commit's subject and body as the note, the tests' verdict (`pending`, | |
| 70 | `passed`, `failed`, `none`) and the time from the push to serving. A redeploy of the same version | |
| 71 | records nothing; an app with no `version` records nothing. | |
| 72 | ||
| 73 | Read them: | |
| 74 | ||
| 75 | - `/{owner}/{name}/releases`: the page, for whoever may read the repository. | |
| 76 | - `GET /api/v1/repos/{owner}/{name}/releases`: `{ items: [...] }`, newest first; `limit`, and `before` (the `cursor` of the last item seen, so releases sharing a time are not skipped). | |
| 77 | - `GET /api/v1/changelog`: a `releases` list beside the landed pulls, public repositories only; | |
| 78 | `?repo=owner/name` narrows it. | |
| 79 | - The terminal: the palette's `<owner>/<name> — Releases` while that repository is open. | |
| 80 | ||
| 81 | Releases are recorded from the day the forge carries this; earlier deploys are not backfilled. | |
| 82 | ||
| 83 | A release still `pending` when the forge restarts is settled to `none` on boot: its verdict went with the process. Reads are capped: the app's answer at 4 KB (the stream is cancelled), a version file at 64 KB. |
src/apps/checkout.ts +6
| @@ -22,8 +22,14 @@ export async function readFileAt( | ||
| 22 | 22 | dir: string, |
| 23 | 23 | oid: string, |
| 24 | 24 | path: string, |
| 25 | maxBytes = Infinity, | |
| 25 | 26 | ): Promise<string | undefined> { |
| 26 | 27 | if (!isSafeOid(oid)) return undefined; |
| 28 | // A blob over the cap costs a stat and a verdict, not one byte off the pipe. | |
| 29 | if (maxBytes !== Infinity) { | |
| 30 | const stat = await readerFor(dir).stat(`${oid}:${path}`); | |
| 31 | if (!stat.ok || stat.value.type !== "blob" || stat.value.size > maxBytes) return undefined; | |
| 32 | } | |
| 27 | 33 | const object = await readerFor(dir).object(`${oid}:${path}`); |
| 28 | 34 | if (!object.ok || object.value.type !== "blob") return undefined; |
| 29 | 35 | return new TextDecoder().decode(object.value.body); |
src/apps/manifest.ts +23
| @@ -44,6 +44,12 @@ export interface AppManifest { | ||
| 44 | 44 | * before this field existed. |
| 45 | 45 | */ |
| 46 | 46 | readonly looks: readonly string[]; |
| 47 | /** | |
| 48 | * `app.version`: where the app says which version it is, a path the running app answers | |
| 49 | * (`/healthz`) or a file in the commit (`VERSION`, `package.json`). A deploy of the default | |
| 50 | * branch that serves a new one is recorded as a release (`core/app_releases.ts`). Unset is none. | |
| 51 | */ | |
| 52 | readonly version?: string | undefined; | |
| 47 | 53 | /** |
| 48 | 54 | * `app.looksAsOwner`: the pages of `looks` whose phone and desktop |
| 49 | 55 | * pictures are taken signed in as the owner, even of a public app — |
| @@ -179,6 +185,8 @@ export function parseManifest( | ||
| 179 | 185 | return invalid( |
| 180 | 186 | `forge.json: app.ready must be a whole number of seconds, 1–${READY_MAX}`, |
| 181 | 187 | ); |
| 188 | const version = versionField(app); | |
| 189 | if (!version.ok) return version; | |
| 182 | 190 | const looks = looksField(app); |
| 183 | 191 | if (!looks.ok) return looks; |
| 184 | 192 | const looksAsOwner = looksAsOwnerField(app, looks.value); |
| @@ -238,6 +246,7 @@ export function parseManifest( | ||
| 238 | 246 | ready: ready ?? READY_DEFAULT, |
| 239 | 247 | tty: tty.value || null, |
| 240 | 248 | looks: looks.value, |
| 249 | ...(version.value === undefined ? {} : { version: version.value }), | |
| 241 | 250 | looksAsOwner: looksAsOwner.value, |
| 242 | 251 | pages: pages.value, |
| 243 | 252 | ...(launch === undefined ? {} : { launch }), |
| @@ -253,6 +262,20 @@ export function parseManifest( | ||
| 253 | 262 | }); |
| 254 | 263 | } |
| 255 | 264 | |
| 265 | const VERSION_SOURCE = /^[A-Za-z0-9][A-Za-z0-9._\-/]{0,119}$|^\/[A-Za-z0-9._\-/]{0,119}$/; | |
| 266 | ||
| 267 | /** | |
| 268 | * `app.version`: a path starting with `/` (asked of the running app) or a plain file path in the | |
| 269 | * tree. | |
| 270 | */ | |
| 271 | function versionField(app: Json): Result<string | undefined, ManifestError> { | |
| 272 | const value = app["version"]; | |
| 273 | if (value === undefined) return ok(undefined); | |
| 274 | if (typeof value !== "string" || !VERSION_SOURCE.test(value) || value.split("/").includes("..")) | |
| 275 | return invalid("forge.json: app.version must be a path such as /healthz or a file such as VERSION"); | |
| 276 | return ok(value); | |
| 277 | } | |
| 278 | ||
| 256 | 279 | const EGRESS_MAX = 32; |
| 257 | 280 | const HOST_RE = |
| 258 | 281 | /^(\*\.)?([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)*[a-z0-9]([a-z0-9-]*[a-z0-9])?(:[1-9]\d{0,4})?$/; |
src/apps/release_record.ts added +107
| @@ -0,0 +1,107 @@ | ||
| 1 | /** | |
| 2 | * Recording a release when production serves a new version (#13707). Reads the version the app | |
| 3 | * declares in `app.version`, the commit's subject and body for the note, and writes one | |
| 4 | * `app_release` row the first time a version is served. Never throws: a deploy must not fail | |
| 5 | * because its release could not be noted. | |
| 6 | */ | |
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { readFileAt } from "./checkout.ts"; | |
| 9 | import { commitLog } from "../git/read.ts"; | |
| 10 | import { parseVersion, recordAppRelease, setReleaseTests, type AppRelease } from "../core/app_releases.ts"; | |
| 11 | ||
| 12 | const ASK_MS = 2_000; | |
| 13 | const ANSWER_MAX = 4_096; | |
| 14 | const FILE_MAX = 65_536; | |
| 15 | ||
| 16 | /** At most `max` bytes of the body as text; the stream is cancelled once the cap is reached. */ | |
| 17 | async function readCapped(response: Response, max: number): Promise<string> { | |
| 18 | if (response.body === null) return ""; | |
| 19 | const reader = response.body.getReader(); | |
| 20 | const chunks: Uint8Array[] = []; | |
| 21 | let have = 0; | |
| 22 | while (have < max) { | |
outdated
MEDIUM: response.text() reads the whole body before the slice. Stream at most 4 KB and cancel the rest, so an app cannot push unbounded bytes into the forge. |
||
| 23 | const { done, value } = await reader.read(); | |
| 24 | if (done) break; | |
| 25 | chunks.push(value.subarray(0, max - have)); | |
| 26 | have += chunks[chunks.length - 1]!.length; | |
| 27 | } | |
| 28 | await reader.cancel().catch(() => undefined); | |
| 29 | const bytes = new Uint8Array(have); | |
| 30 | let at = 0; | |
| 31 | for (const c of chunks) { | |
| 32 | bytes.set(c, at); | |
| 33 | at += c.length; | |
| 34 | } | |
| 35 | return new TextDecoder().decode(bytes); | |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * The version `source` names for this deployment: a path the app answers, or a file in the commit. | |
| 40 | */ | |
| 41 | export async function readAppVersion(source: string, port: number, dir: string, oid: string): Promise<string | undefined> { | |
| 42 | try { | |
| 43 | if (source.startsWith("/")) { | |
| 44 | const response = await fetch(`http://127.0.0.1:${port}${source}`, { signal: AbortSignal.timeout(ASK_MS), redirect: "manual" }); | |
| 45 | if (!response.ok) return undefined; | |
| 46 | return parseVersion(await readCapped(response, ANSWER_MAX)); | |
| 47 | } | |
| 48 | const text = await readFileAt(dir, oid, source, FILE_MAX); | |
| 49 | return text === undefined ? undefined : parseVersion(text); | |
| 50 | } catch { | |
| 51 | return undefined; | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | /** Milliseconds from `from` to `to`, or null when either is not a time. */ | |
| 56 | function between(from: string | undefined, to: string): number | null { | |
| 57 | if (from === undefined) return null; | |
| 58 | const ms = Date.parse(to) - Date.parse(from); | |
| 59 | return Number.isFinite(ms) && ms >= 0 ? ms : null; | |
| 60 | } | |
| 61 | ||
| 62 | export async function noteRelease( | |
| 63 | db: Database, | |
| 64 | input: { | |
| 65 | repoId: string; | |
| 66 | repoDir: string; | |
| 67 | source: string; | |
| 68 | port: number; | |
| 69 | oid: string; | |
| 70 | servingAt: string; | |
| 71 | pushedAt: string | undefined; | |
| 72 | startedAt: string | undefined; | |
| 73 | tested: boolean; | |
| 74 | }, | |
| 75 | ): Promise<AppRelease | undefined> { | |
| 76 | const version = await readAppVersion(input.source, input.port, input.repoDir, input.oid); | |
| 77 | if (version === undefined) return undefined; | |
| 78 | const log = await commitLog(input.repoDir, input.oid, { limit: 1 }); | |
| 79 | const commit = log.ok ? log.value[0] : undefined; | |
| 80 | return recordAppRelease(db, { | |
| 81 | repoId: input.repoId, | |
| 82 | version, | |
| 83 | commit: input.oid, | |
| 84 | subject: commit?.subject ?? "", | |
| 85 | body: commit?.body ?? "", | |
| 86 | tests: input.tested ? "pending" : "none", | |
| 87 | servingMs: between(input.pushedAt ?? input.startedAt, input.servingAt), | |
| 88 | servedAt: input.servingAt, | |
| 89 | }); | |
| 90 | } | |
| 91 | ||
| 92 | /** Only production, with a declared `app.version`, records releases. */ | |
| 93 | export function recordsReleases(env: string, manifest: { version?: string | undefined }): manifest is { version: string } { | |
| 94 | return env === "production" && manifest.version !== undefined; | |
| 95 | } | |
| 96 | ||
| 97 | /** Settle a release's tests once the deploy's tests have run; none declared leaves it `none`. */ | |
| 98 | export function noteReleaseTests( | |
| 99 | db: Database, | |
| 100 | env: string, | |
| 101 | manifest: { version?: string | undefined; test: unknown }, | |
| 102 | repoId: string, | |
| 103 | oid: string, | |
| 104 | passed: boolean, | |
| 105 | ): void { | |
| 106 | if (recordsReleases(env, manifest) && manifest.test !== null) setReleaseTests(db, repoId, oid, passed ? "passed" : "failed"); | |
| 107 | } |
src/apps/runtime.ts +19
| @@ -21,6 +21,8 @@ import { dropCheckouts } from "./dormant_checkout.ts"; | ||
| 21 | 21 | import { listEgressGrants } from "../core/egress_grants.ts"; |
| 22 | 22 | import { recordCarried } from "../core/carried.ts"; |
| 23 | 23 | import { commitLog } from "../git/read.ts"; |
| 24 | import { noteRelease, noteReleaseTests, recordsReleases } from "./release_record.ts"; | |
| 25 | import { settleStaleReleases } from "../core/app_releases.ts"; | |
| 24 | 26 | import { forgetLanded, noteLanded } from "./landed.ts"; |
| 25 | 27 | import { releaseOfCommit } from "../core/release_subject.ts"; |
| 26 | 28 | import { pullDir, runDir } from "../core/pulls.ts"; |
| @@ -786,6 +788,8 @@ export class AppsRuntime { | ||
| 786 | 788 | const noun = cut === 1 ? "check" : "checks"; |
| 787 | 789 | log.info(`boot after an unclean stop: ${cut} ${noun} cut by it, queued again`, { cut }); |
| 788 | 790 | } |
| 791 | // A release whose tests were running when the last forge stopped will never hear their verdict. | |
| 792 | settleStaleReleases(this.app.db); | |
| 789 | 793 | // An awake interval the last forge left open ends at its last heartbeat. |
| 790 | 794 | const orphans = this.metered(() => closeOrphans(this.app.db)); |
| 791 | 795 | if (orphans) log.info("closed open meter intervals", { count: orphans }); |
| @@ -1500,6 +1504,20 @@ export class AppsRuntime { | ||
| 1500 | 1504 | log.error("could not publish app.serving", { error: String(error) }); |
| 1501 | 1505 | } |
| 1502 | 1506 | |
| 1507 | // A new version served by production is a release (#13707); never fails the deploy. | |
| 1508 | if (recordsReleases(state.env, live.manifest)) | |
| 1509 | await noteRelease(this.app.db, { | |
| 1510 | repoId: repo.id, | |
| 1511 | repoDir: repoDir(this.app.reposRoot, repo.ownerName, repo.name), | |
| 1512 | source: live.manifest.version, | |
| 1513 | port: live.handle.port, | |
| 1514 | oid: live.commitOid, | |
| 1515 | servingAt, | |
| 1516 | pushedAt: createdAt, | |
| 1517 | startedAt: deployStartedAt ?? undefined, | |
| 1518 | tested: live.manifest.test !== null, | |
| 1519 | }).catch((error: unknown) => log.error("could not record a release", { error: String(error) })); | |
| 1520 | ||
| 1503 | 1521 | // A preview is held to the website specification once it is live, beside |
| 1504 | 1522 | // its tests and after the swap-in (#2955). Report-only and unawaited: the |
| 1505 | 1523 | // run neither waits for it nor can it fail. |
| @@ -1514,6 +1532,7 @@ export class AppsRuntime { | ||
| 1514 | 1532 | const tested = await outcome.tested; |
| 1515 | 1533 | state.deploying = undefined; |
| 1516 | 1534 | lease(); |
| 1535 | noteReleaseTests(this.app.db, state.env, live.manifest, repo.id, live.commitOid, tested.ok); | |
| 1517 | 1536 | if (tested.ok) { |
| 1518 | 1537 | const done = finishRun(this.app.db, run.id, { |
| 1519 | 1538 | state: "ok", |
src/contract/openapi/routes/getApiV1Changelog.ts +1 −1
| @@ -3,7 +3,7 @@ import type { Route } from "../shared.ts"; | ||
| 3 | 3 | export const route: Route = { |
| 4 | 4 | method: "get", |
| 5 | 5 | path: "/api/v1/changelog", |
| 6 | summary: "What went live and what is on its way, as /changelog shows it, for anyone: version, commit, live and onItsWay rows (repo, number, title, at, and for the forge's own landings by: who built it, with: the models and people its commit credits) of public repositories; ?repo=all|owner/name, this forge's own by default (all when it has none); carries a weak ETag of the live version, the chosen repo and the rows, and a repeat sending If-None-Match gets 304 with no body and the same ETag and cache-control", | |
| 6 | summary: "What went live and what is on its way, as /changelog shows it, for anyone: version, commit, releases (each version a public app's production deploy first served: repo, version, commit, subject, note, tests, servingMs, servedAt; ?repo narrows them), live and onItsWay rows (repo, number, title, at, and for the forge's own landings by: who built it, with: the models and people its commit credits) of public repositories; ?repo=all|owner/name, this forge's own by default (all when it has none); carries a weak ETag of the live version, the chosen repo and the rows, and a repeat sending If-None-Match gets 304 with no body and the same ETag and cache-control", | |
| 7 | 7 | alsoStatus: [304], |
| 8 | 8 | auth: false, |
| 9 | 9 | role: "anonymous", |
src/contract/openapi/routes/getApiV1ReposOwnerNameReleases.ts added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | import { REPO, type Route, limitTo } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "get", | |
| 5 | path: `${REPO}/releases`, | |
| 6 | summary: | |
| 7 | "List a repository's releases, newest first: each time the default branch's production deploy first served a version the app declares in forge.json app.version; answers { items: [{ version, commit, subject, note, tests, servingMs, servedAt }] }; tests is pending, passed, failed or none; servingMs is the time from the push to serving; limit 1 to 200 (default 50), before is the servedAt of the last item seen; a private repository needs a token", | |
| 8 | auth: false, | |
| 9 | role: "anonymous", | |
| 10 | query: { limit: limitTo(200), before: "string" }, | |
| 11 | refusals: { | |
| 12 | invalid_limit: "limit is not a whole number from 1 to 200 (422)", | |
| 13 | }, | |
| 14 | }; |
src/contract/openapi/routes/index.ts +1
| @@ -192,6 +192,7 @@ export { route as getApiV1ReposOwnerNamePullsNumberReviews } from "./getApiV1Rep | ||
| 192 | 192 | export { route as getApiV1ReposOwnerNamePullsNumberThreads } from "./getApiV1ReposOwnerNamePullsNumberThreads.ts"; |
| 193 | 193 | export { route as getApiV1ReposOwnerNameQueue } from "./getApiV1ReposOwnerNameQueue.ts"; |
| 194 | 194 | export { route as getApiV1ReposOwnerNameQueueGates } from "./getApiV1ReposOwnerNameQueueGates.ts"; |
| 195 | export { route as getApiV1ReposOwnerNameReleases } from "./getApiV1ReposOwnerNameReleases.ts"; | |
| 195 | 196 | export { route as getApiV1ReposOwnerNameReports } from "./getApiV1ReposOwnerNameReports.ts"; |
| 196 | 197 | export { route as getApiV1ReposOwnerNameRoadmap } from "./getApiV1ReposOwnerNameRoadmap.ts"; |
| 197 | 198 | export { route as getApiV1ReposOwnerNameRuns } from "./getApiV1ReposOwnerNameRuns.ts"; |
src/core/app_releases.ts added +162
| @@ -0,0 +1,162 @@ | ||
| 1 | /** | |
| 2 | * An app's releases (#13707): a deploy of the default branch that serves a version the app has | |
| 3 | * not served before. The version comes from the app itself (`app.version` in forge.json: a path | |
| 4 | * it answers, or a file in the commit); the note is the commit's subject and body. Plain data in | |
| 5 | * and out, no authz: callers gate on `canRead`. | |
| 6 | */ | |
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { id } from "../kernel/id.ts"; | |
| 9 | ||
| 10 | export type TestsResult = "pending" | "passed" | "failed" | "none"; | |
| 11 | ||
| 12 | export interface AppRelease { | |
| 13 | readonly version: string; | |
| 14 | readonly commit: string; | |
| 15 | readonly subject: string; | |
| 16 | readonly note: string; | |
| 17 | readonly tests: TestsResult; | |
| 18 | /** Milliseconds from the push (or the deploy's start) to serving; null when unknown. */ | |
| 19 | readonly servingMs: number | null; | |
| 20 | readonly servedAt: string; | |
| 21 | /** | |
| 22 | * Where the next page starts: pass it as `before` (`servedAt~id`, so equal times are not | |
| 23 | * skipped). | |
| 24 | */ | |
| 25 | readonly cursor: string; | |
| 26 | } | |
| 27 | ||
| 28 | interface Row { | |
| 29 | id: string; | |
| 30 | version: string; | |
| 31 | commit_oid: string; | |
| 32 | subject: string; | |
| 33 | note: string; | |
| 34 | tests: TestsResult; | |
| 35 | serving_ms: number | null; | |
| 36 | served_at: string; | |
| 37 | } | |
| 38 | ||
| 39 | const VERSION = /^[A-Za-z0-9][A-Za-z0-9._+\-]{0,63}$/; | |
| 40 | const NOTE_MAX = 8_000; | |
| 41 | ||
| 42 | /** | |
| 43 | * The version an answer or file holds: JSON with a string `version`, else its first line; else | |
| 44 | * none. | |
| 45 | */ | |
| 46 | export function parseVersion(text: string): string | undefined { | |
| 47 | const trimmed = text.trim(); | |
| 48 | let candidate: unknown = trimmed.split("\n", 1)[0]?.trim(); | |
| 49 | if (trimmed.startsWith("{")) { | |
| 50 | try { | |
| 51 | candidate = (JSON.parse(trimmed) as { version?: unknown }).version; | |
| 52 | } catch { | |
| 53 | return undefined; | |
| 54 | } | |
| 55 | } | |
| 56 | const v = typeof candidate === "string" ? candidate.replace(/^v(?=\d)/, "") : undefined; | |
| 57 | return v !== undefined && VERSION.test(v) ? v : undefined; | |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Record `version` as a release unless the repository has already had it. Returns the release, | |
| 62 | * or undefined when the version was served before (a redeploy of the same version is no release). | |
| 63 | */ | |
| 64 | export function recordAppRelease( | |
| 65 | db: Database, | |
| 66 | input: { | |
| 67 | repoId: string; | |
| 68 | version: string; | |
| 69 | commit: string; | |
| 70 | subject: string; | |
| 71 | body: string; | |
| 72 | tests: TestsResult; | |
| 73 | servingMs: number | null; | |
| 74 | servedAt: string; | |
| 75 | }, | |
| 76 | ): AppRelease | undefined { | |
| 77 | if (!VERSION.test(input.version)) return undefined; | |
| 78 | const rowId = id(); | |
| 79 | const note = input.body.trim().slice(0, NOTE_MAX); | |
| 80 | const done = db | |
| 81 | .query( | |
| 82 | `INSERT OR IGNORE INTO app_release (id, repo_id, version, commit_oid, subject, note, tests, serving_ms, served_at) | |
| 83 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)`, | |
| 84 | ) | |
| 85 | .run(rowId, input.repoId, input.version, input.commit, input.subject.slice(0, 500), note, input.tests, input.servingMs, input.servedAt); | |
| 86 | if (done.changes === 0) return undefined; | |
| 87 | return { | |
| 88 | version: input.version, | |
| 89 | commit: input.commit, | |
| 90 | subject: input.subject.slice(0, 500), | |
| 91 | note, | |
| 92 | tests: input.tests, | |
| 93 | servingMs: input.servingMs, | |
| 94 | servedAt: input.servedAt, | |
| 95 | cursor: `${input.servedAt}~${rowId}`, | |
| 96 | }; | |
| 97 | } | |
| 98 | ||
| 99 | /** Releases still `pending` when the forge starts lost their tests with the last process: none. */ | |
| 100 | export function settleStaleReleases(db: Database): number { | |
| 101 | return db.query("UPDATE app_release SET tests = 'none' WHERE tests = 'pending'").run().changes; | |
| 102 | } | |
| 103 | ||
| 104 | /** The tests' verdict for the release a commit made, once they have run. */ | |
| 105 | export function setReleaseTests(db: Database, repoId: string, commit: string, tests: TestsResult): void { | |
| 106 | db.query("UPDATE app_release SET tests = ?3 WHERE repo_id = ?1 AND commit_oid = ?2").run(repoId, commit, tests); | |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * A repository's releases, newest first; `before` is the `cursor` of the last one already seen (a | |
| 111 | * bare `servedAt` takes everything older than that time). | |
| 112 | */ | |
| 113 | export function listAppReleases(db: Database, repoId: string, limit = 50, before?: string): AppRelease[] { | |
| 114 | const [at = "9999", rest = "\uffff"] = before === undefined ? [] : before.split("~", 2); | |
| 115 | return db | |
| 116 | .query<Row, [string, string, number, string]>( | |
| 117 | `SELECT id, version, commit_oid, subject, note, tests, serving_ms, served_at FROM app_release | |
| 118 | WHERE repo_id = ?1 AND (served_at < ?2 OR (served_at = ?2 AND id < ?4)) ORDER BY served_at DESC, id DESC LIMIT ?3`, | |
| 119 | ) | |
| 120 | .all(repoId, at, Math.min(Math.max(limit, 1), 200), rest) | |
| 121 | .map((r) => ({ | |
| 122 | version: r.version, | |
| 123 | commit: r.commit_oid, | |
| 124 | subject: r.subject, | |
| 125 | note: r.note, | |
| 126 | tests: r.tests, | |
| 127 | servingMs: r.serving_ms, | |
| 128 | servedAt: r.served_at, | |
| 129 | cursor: `${r.served_at}~${r.id}`, | |
| 130 | })); | |
| 131 | } | |
| 132 | ||
| 133 | /** A release of a public repository, with the repository it belongs to. */ | |
| 134 | export interface PublicRelease extends AppRelease { | |
| 135 | readonly repo: string; | |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * Releases of public repositories, newest first: what /api/v1/changelog adds to the landed pulls. | |
| 140 | */ | |
| 141 | export function listPublicReleases(db: Database, limit = 50, repo?: string): PublicRelease[] { | |
| 142 | const [owner = "", name = ""] = repo === undefined ? [] : repo.split("/"); | |
| 143 | return db | |
| 144 | .query<Row & { repo: string }, [number, string | null, string | null]>( | |
| 145 | `SELECT a.id, u.username || '/' || r.name AS repo, a.version, a.commit_oid, a.subject, a.note, a.tests, a.serving_ms, a.served_at | |
| 146 | FROM app_release a JOIN repo r ON r.id = a.repo_id JOIN user u ON u.id = r.owner_id | |
| 147 | WHERE r.visibility = 'public' AND (?2 IS NULL OR (u.username = ?2 AND r.name = ?3)) | |
| 148 | ORDER BY a.served_at DESC LIMIT ?1`, | |
| 149 | ) | |
| 150 | .all(Math.min(Math.max(limit, 1), 200), repo === undefined ? null : owner, repo === undefined ? null : name) | |
| 151 | .map((r) => ({ | |
| 152 | repo: r.repo, | |
| 153 | version: r.version, | |
| 154 | commit: r.commit_oid, | |
| 155 | subject: r.subject, | |
| 156 | note: r.note, | |
| 157 | tests: r.tests, | |
| 158 | servingMs: r.serving_ms, | |
| 159 | servedAt: r.served_at, | |
| 160 | cursor: `${r.served_at}~${r.id}`, | |
| 161 | })); | |
| 162 | } |
src/core/migration-pins/20261115150000-app-release.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | a1a36852846af9945d850e608fbcda865a848c709a07cdc740aff7cbcb4a0190 |
src/core/migrations/20261115150000-app-release.ts added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | // A push that changes an app's version is a release (#13707): one row per version the default | |
| 2 | // branch's production deploy first served. Additive; read by repository and newest first. | |
| 3 | export default ` | |
| 4 | CREATE TABLE IF NOT EXISTS app_release ( | |
| 5 | id TEXT PRIMARY KEY, | |
| 6 | repo_id TEXT NOT NULL REFERENCES repo(id) ON DELETE CASCADE, | |
| 7 | version TEXT NOT NULL, | |
| 8 | commit_oid TEXT NOT NULL, | |
| 9 | subject TEXT NOT NULL, | |
| 10 | note TEXT NOT NULL, | |
| 11 | tests TEXT NOT NULL, | |
| 12 | serving_ms INTEGER, | |
| 13 | served_at TEXT NOT NULL, | |
| 14 | UNIQUE (repo_id, version) | |
| 15 | ); | |
| 16 | CREATE INDEX IF NOT EXISTS app_release_by_repo ON app_release(repo_id, served_at DESC); | |
| 17 | CREATE INDEX IF NOT EXISTS app_release_by_time ON app_release(served_at DESC); | |
| 18 | `; |
src/http/api.ts +10
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * back to continue); errors are `{ error, message }`. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { listAppReleases } from "../core/app_releases.ts"; | |
| 11 | 12 | import { openapiHandler } from "../contract/served.ts"; |
| 12 | 13 | import { CHECK_ASKED } from "../actions/pull_ask_words.ts"; |
| 13 | 14 | import { liveOf } from "../perf/live.ts"; |
| @@ -1863,6 +1864,15 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1863 | 1864 | // ── git reads. The agent arena showed the write path was complete but the |
| 1864 | 1865 | // read path was missing, so automation couldn't inspect before acting. |
| 1865 | 1866 | |
| 1867 | router.add("GET", "/api/v1/repos/:owner/:name/releases", (ctx) => { | |
| 1868 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 1869 | const repo = loadRepo(ctx); | |
| 1870 | if (!repo) return notFound(); | |
| 1871 | const limit = Number(ctx.url.searchParams.get("limit") ?? 50); | |
| 1872 | if (!Number.isInteger(limit) || limit < 1 || limit > 200) return apiError("invalid_limit", 422, "limit is a whole number from 1 to 200"); | |
| 1873 | const before = ctx.url.searchParams.get("before") ?? undefined; | |
| 1874 | return json({ items: listAppReleases(ctx.app.db, repo.id, limit, before === undefined || before.length > 80 ? undefined : before) }); | |
| 1875 | }); | |
| 1866 | 1876 | // Branches carry their divergence from the default and whether they are |
| 1867 | 1877 | // protected — the three facts a branch list is asked for. `ahead`/`behind` |
| 1868 | 1878 | // are null when the repo has more branches than the comparison cap. |
src/http/server.ts +2
| @@ -191,6 +191,7 @@ import { | ||
| 191 | 191 | } from "../web/pages/passkeys.ts"; |
| 192 | 192 | import { openSearch } from "../web/opensearch.ts"; |
| 193 | 193 | import { confirmPage, searchPage } from "../web/pages/search.ts"; |
| 194 | import { appReleasesPage } from "../web/pages/app_releases.ts"; | |
| 194 | 195 | import { chainPage, checkSubmit, modeSubmit, proposeParentSubmit, severSubmit, takeSubmit } from "../web/pages/chain.ts"; |
| 195 | 196 | import { followingSubmit } from "../web/pages/following.ts"; |
| 196 | 197 | import { reconnectSubmit } from "../web/pages/frozen.ts"; |
| @@ -1697,6 +1698,7 @@ export function buildRouter(): Router<Handler> { | ||
| 1697 | 1698 | // A fork takes what it follows: git's part by itself, the rest by the crew. |
| 1698 | 1699 | router.add("POST", "/:owner/:name/following", followingSubmit); |
| 1699 | 1700 | router.add("GET", "/:owner/:name/following", chainPage); |
| 1701 | router.add("GET", "/:owner/:name/releases", appReleasesPage); | |
| 1700 | 1702 | router.add("POST", "/:owner/:name/following/sever", severSubmit); |
| 1701 | 1703 | router.add("POST", "/:owner/:name/following/mode", modeSubmit); |
| 1702 | 1704 | router.add("POST", "/:owner/:name/following/mode/:mode", modeSubmit); |
src/release/answer.ts +14 −2
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import type { App } from "../app.ts"; |
| 8 | import { listPublicReleases } from "../core/app_releases.ts"; | |
| 8 | 9 | import { getRepoById } from "../core/repos.ts"; |
| 9 | 10 | import type { User } from "../core/users.ts"; |
| 10 | 11 | import { running } from "../kernel/version.ts"; |
| @@ -13,7 +14,9 @@ import { chosenRepo, readLandings, type Landing, type Landings } from "./landing | ||
| 13 | 14 | import { keeperReleaseOfSha, type Stage } from "../keeper/record.ts"; |
| 14 | 15 | import { listReleases, releaseName } from "./record.ts"; |
| 15 | 16 | |
| 16 | /** What the forge can say of a kept release's soundness: the keeper's verdict, else the preflight's. */ | |
| 17 | /** | |
| 18 | * What the forge can say of a kept release's soundness: the keeper's verdict, else the preflight's. | |
| 19 | */ | |
| 17 | 20 | export type Proof = "proven" | "unproven" | "rolled back"; |
| 18 | 21 | |
| 19 | 22 | export const RELEASES_REFUSAL = "the release history is for the forge's administrators and its repository's readers"; |
| @@ -47,7 +50,16 @@ export async function shippedAnswer(app: App, asked: string | null = null) { | ||
| 47 | 50 | const read = await readOnce(app); |
| 48 | 51 | const choice = chosenRepo(asked, [...read.onItsWay, ...read.live], read.own); |
| 49 | 52 | const keep = (l: Landing) => choice === "all" || l.repo === choice; |
| 50 | return { version: read.version, commit: read.commit, live: read.live.filter(keep), onItsWay: read.onItsWay.filter(keep) }; | |
| 53 | // The versions public apps' production first served (#13707); `owner/name` narrows them, anything | |
| 54 | // else is all. | |
| 55 | const named = asked !== null && /^[^/\s]{1,80}\/[^/\s]{1,80}$/.test(asked) ? asked : undefined; | |
| 56 | return { | |
| 57 | version: read.version, | |
| 58 | commit: read.commit, | |
| 59 | live: read.live.filter(keep), | |
| 60 | onItsWay: read.onItsWay.filter(keep), | |
| 61 | releases: listPublicReleases(app.db, 50, named), | |
| 62 | }; | |
| 51 | 63 | } |
| 52 | 64 | |
| 53 | 65 | /** |
src/release/app_words.ts added +22
| @@ -0,0 +1,22 @@ | ||
| 1 | /** The words an app's release is told in, shared by its page and its terminal screen (#13707). */ | |
| 2 | import type { AppRelease } from "../core/app_releases.ts"; | |
| 3 | ||
| 4 | const TESTS: Record<AppRelease["tests"], string> = { | |
| 5 | pending: "tests running", | |
| 6 | passed: "tests passed", | |
| 7 | failed: "tests failed", | |
| 8 | none: "no tests", | |
| 9 | }; | |
| 10 | ||
| 11 | /** `1.2s` / `340 ms` / `2 min 5 s`: time from push to serving. */ | |
| 12 | export function servingWords(ms: number | null): string { | |
| 13 | if (ms === null) return "time to serving unknown"; | |
| 14 | if (ms < 1_000) return `serving in ${ms} ms`; | |
| 15 | if (ms < 60_000) return `serving in ${(ms / 1_000).toFixed(1)} s`; | |
| 16 | return `serving in ${Math.floor(ms / 60_000)} min ${Math.round((ms % 60_000) / 1_000)} s`; | |
| 17 | } | |
| 18 | ||
| 19 | /** The facts of one release on one line: tests, time to serving, commit. */ | |
| 20 | export function releaseFacts(r: AppRelease): string { | |
| 21 | return `${TESTS[r.tests]} · ${servingWords(r.servingMs)} · ${r.commit.slice(0, 7)}`; | |
| 22 | } |
src/tui/screens/appreleases.ts added +49
| @@ -0,0 +1,49 @@ | ||
| 1 | /** | |
| 2 | * An app's releases in the terminal (#13707): the /:owner/:name/releases page's rows, from the | |
| 3 | * same read (`core/app_releases.ts`) and the same words (`release/app_words.ts`). The palette | |
| 4 | * offers it while an open repository has one; whoever may read the repository sees it. | |
| 5 | */ | |
| 6 | import { listAppReleases } from "../../core/app_releases.ts"; | |
| 7 | import { canRead, type Repo } from "../../core/repos.ts"; | |
| 8 | import { releaseFacts } from "../../release/app_words.ts"; | |
| 9 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 10 | import type { Command } from "../app/palette.ts"; | |
| 11 | import { binding } from "../app/keys.ts"; | |
| 12 | import type { Session } from "../session.ts"; | |
| 13 | import { ListState, drawList, type ListRow } from "./common.ts"; | |
| 14 | ||
| 15 | function rowsFor(f: Session, repo: Repo): ListRow[] { | |
| 16 | if (!canRead(f.app.db, repo, f.user)) return []; | |
| 17 | return listAppReleases(f.app.db, repo.id, 100).map((r) => ({ | |
| 18 | title: `${r.version} · ${r.subject}`, | |
| 19 | meta: releaseFacts(r), | |
| 20 | ...(r.note === "" ? {} : { note: r.note.split("\n", 1)[0]! }), | |
| 21 | })); | |
| 22 | } | |
| 23 | ||
| 24 | export function appReleasesScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | |
| 25 | const state = new ListState(); | |
| 26 | return { | |
| 27 | title: `${repo.ownerName}/${repo.name} — Releases`, | |
| 28 | keys: [binding(["j", "k"], "move cursor")], | |
| 29 | render(area, s) { | |
| 30 | drawList(s, area, ctx.theme.c, state, rowsFor(f, repo)); | |
| 31 | }, | |
| 32 | key(e) { | |
| 33 | return state.key(e, rowsFor(f, repo).length); | |
| 34 | }, | |
| 35 | }; | |
| 36 | } | |
| 37 | ||
| 38 | /** The palette's command while a repository with a release is open. */ | |
| 39 | export function appReleasesCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] { | |
| 40 | if (repo === undefined || rowsFor(f, repo).length === 0) return []; | |
| 41 | return [ | |
| 42 | { | |
| 43 | id: "releases-app", | |
| 44 | title: `${repo.ownerName}/${repo.name} — Releases`, | |
| 45 | hint: "the versions this app's production has served", | |
| 46 | run: () => ctx.push(appReleasesScreen(f, ctx, repo)), | |
| 47 | }, | |
| 48 | ]; | |
| 49 | } |
src/tui/screens/workspace.ts +2
| @@ -190,6 +190,7 @@ import { settingHistoryCommands } from "./settinghistory.ts"; | ||
| 190 | 190 | import { templateCommands } from "./template.ts"; |
| 191 | 191 | import { buildCacheCommands } from "./buildcache.ts"; |
| 192 | 192 | import { schedulesCommands } from "./schedules.ts"; |
| 193 | import { appReleasesCommands } from "./appreleases.ts"; | |
| 193 | 194 | import { migrationCommands } from "./migration.ts"; |
| 194 | 195 | import { dropCommands } from "./drop.ts"; |
| 195 | 196 | import { recheckWindowCommands } from "./recheckwindow.ts"; |
| @@ -2192,6 +2193,7 @@ export function workspaceScreen( | ||
| 2192 | 2193 | ...autonomyCommands(f, ctx, repo), |
| 2193 | 2194 | ...trafficCommands(f, ctx, repo), |
| 2194 | 2195 | ...schedulesCommands(f, ctx, repo), |
| 2196 | ...appReleasesCommands(f, ctx, repo), | |
| 2195 | 2197 | ...dropCommands(f, ctx, repo), |
| 2196 | 2198 | ...migrationCommands(f, ctx, repo), |
| 2197 | 2199 | ...recheckWindowCommands(f, ctx, repo, () => screen), |
src/web/pages/app_releases.ts added +40
| @@ -0,0 +1,40 @@ | ||
| 1 | /** | |
| 2 | * `/:owner/:name/releases`: the versions this app's production has served, newest first, each | |
| 3 | * with the commit's note, the tests' verdict and the time from push to serving (#13707). | |
| 4 | * Server-rendered; readable by whoever may read the repository. | |
| 5 | */ | |
| 6 | import { listAppReleases } from "../../core/app_releases.ts"; | |
| 7 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 8 | import type { Ctx } from "../../http/context.ts"; | |
| 9 | import { releaseFacts } from "../../release/app_words.ts"; | |
| 10 | import { loadRepo, notFoundPage, pageHead, repoBase } from "../chrome.ts"; | |
| 11 | import { html } from "../html.ts"; | |
| 12 | import { page } from "../layout.ts"; | |
| 13 | import { empty, row, sha, time } from "../ui.ts"; | |
| 14 | ||
| 15 | export function appReleasesPage(ctx: Ctx): Response { | |
| 16 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 17 | const repo = timePhase(ctx.request, "repo", () => loadRepo(ctx)); | |
| 18 | if (!repo) return notFoundPage(ctx); | |
| 19 | const releases = timePhase(ctx.request, "list", () => listAppReleases(ctx.app.db, repo.id, 100)); | |
| 20 | return timePhase(ctx.request, "render", () => page({ | |
| 21 | ctx, | |
| 22 | title: "Releases", | |
| 23 | body: html`<div class="narrow-wide"> | |
| 24 | ${pageHead("Releases", { sub: `Each version ${repo.ownerName}/${repo.name} first served in production.` })} | |
| 25 | ${releases.length === 0 | |
| 26 | ? empty({ | |
| 27 | title: "No release yet", | |
| 28 | note: "A release is recorded when production first serves a new version.", | |
| 29 | }) | |
| 30 | : html`<ul class="list" aria-label="Releases">${releases.map((r) => | |
| 31 | row({ | |
| 32 | title: html`${r.version} · ${r.subject}`, | |
| 33 | meta: html`${releaseFacts(r)} · ${sha(r.commit, `${repoBase(repo)}/commit/${r.commit}`)}`, | |
| 34 | ...(r.note === "" ? {} : { facts: [{ label: "Note", value: r.note }] }), | |
| 35 | trail: time(r.servedAt), | |
| 36 | }), | |
| 37 | )}</ul>`} | |
| 38 | </div>`, | |
| 39 | })); | |
| 40 | } |
src/web/pages/changelog.ts +2 −1
| @@ -414,7 +414,8 @@ export async function changelogJson(ctx: Ctx): Promise<Response> { | ||
| 414 | 414 | const answer = await shippedAnswer(ctx.app, asked); |
| 415 | 415 | // The answer is the same for every viewer (public repositories only), so a shared cache may keep |
| 416 | 416 | // it. |
| 417 | const etag = changelogTag(answer.version, [...answer.live, ...answer.onItsWay], [asked ?? "", answer.live.length]); | |
| 417 | const { releases } = answer; | |
| 418 | const etag = changelogTag(answer.version, [...answer.live, ...answer.onItsWay], [asked ?? "", answer.live.length, releases.length, releases[0]?.servedAt ?? "", releases.map((r) => r.tests[0]).join("")]); | |
| 418 | 419 | const headers = { etag, "cache-control": "public, max-age=5" }; |
| 419 | 420 | if (holds(ctx.request.headers.get("if-none-match"), etag)) return new Response(null, { status: 304, headers }); |
| 420 | 421 | return json(answer, 200, headers); |
test/app-releases.test.ts added +246
| @@ -0,0 +1,246 @@ | ||
| 1 | /** | |
| 2 | * A push that changes an app's version is a release (#13707): the manifest field, the record, the | |
| 3 | * page, the API and the changelog. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createToken } from "../src/core/tokens.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { listAppReleases, parseVersion, recordAppRelease, setReleaseTests } from "../src/core/app_releases.ts"; | |
| 14 | import { parseManifest } from "../src/apps/manifest.ts"; | |
| 15 | import { readAppVersion } from "../src/apps/release_record.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | ||
| 18 | describe("app.version in forge.json", () => { | |
| 19 | const manifest = (version: unknown) => parseManifest(JSON.stringify({ app: { run: "x", version } }), undefined); | |
| 20 | test("a path or a file is read; anything else is refused", () => { | |
| 21 | const ok = manifest("/healthz"); | |
| 22 | expect(ok.ok && ok.value.version).toBe("/healthz"); | |
| 23 | const file = manifest("VERSION"); | |
| 24 | expect(file.ok && file.value.version).toBe("VERSION"); | |
| 25 | for (const bad of ["../x", "/a/../b", "a b", 3, "", "-x;"]) expect(manifest(bad).ok).toBe(false); | |
| 26 | }); | |
| 27 | test("unset is none", () => { | |
| 28 | const m = parseManifest(JSON.stringify({ app: { run: "x" } }), undefined); | |
| 29 | expect(m.ok && m.value.version).toBeUndefined(); | |
| 30 | }); | |
| 31 | }); | |
| 32 | ||
| 33 | describe("parseVersion", () => { | |
| 34 | test("JSON, plain text and nonsense", () => { | |
| 35 | expect(parseVersion('{"ok":true,"version":"1.2.3"}')).toBe("1.2.3"); | |
| 36 | expect(parseVersion("v0.7.1\nmore")).toBe("0.7.1"); | |
| 37 | expect(parseVersion("<html>")).toBeUndefined(); | |
| 38 | expect(parseVersion('{"version":5}')).toBeUndefined(); | |
| 39 | }); | |
| 40 | }); | |
| 41 | ||
| 42 | describe("readAppVersion", () => { | |
| 43 | test("asks the running app's path", async () => { | |
| 44 | const server = Bun.serve({ port: 0, fetch: () => Response.json({ version: "4.5.6" }) }); | |
| 45 | try { | |
| 46 | expect(await readAppVersion("/healthz", server.port!, "/nowhere", "0".repeat(40))).toBe("4.5.6"); | |
| 47 | } finally { | |
| 48 | void server.stop(true); | |
| 49 | } | |
| 50 | }); | |
| 51 | }); | |
| 52 | ||
| 53 | describe("releases", () => { | |
| 54 | let dataDir: string; | |
| 55 | let app: App; | |
| 56 | let servers: Servers; | |
| 57 | let token: string; | |
| 58 | let pub: string; | |
| 59 | let priv: string; | |
| 60 | beforeAll(async () => { | |
| 61 | dataDir = await mkdtemp(join(tmpdir(), "forge-app-releases-")); | |
| 62 | app = createApp({ dataDir, port: 0 }); | |
| 63 | servers = await serve(app); | |
| 64 | const u = await createUser(app.db, { | |
| 65 | origin: "seed", trusted: true, username: "pop", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true, | |
| 66 | }); | |
| 67 | if (!u.ok) throw new Error(u.error); | |
| 68 | token = createToken(app.db, u.value.id, "a", "admin").token; | |
| 69 | const a = await createRepo(app, u.value, { name: "pop", visibility: "public" }); | |
| 70 | const b = await createRepo(app, u.value, { name: "hidden", visibility: "private" }); | |
| 71 | if (!a.ok || !b.ok) throw new Error("repo"); | |
| 72 | pub = a.value.id; | |
| 73 | priv = b.value.id; | |
| 74 | for (let i = 1; i <= 3; i++) | |
| 75 | recordAppRelease(app.db, { | |
| 76 | repoId: pub, version: `1.0.${i}`, commit: `${i}`.repeat(40), subject: `Ship 1.0.${i}`, body: `<b>note ${i}</b>`, | |
| 77 | tests: "pending", servingMs: 1500, servedAt: `2026-11-1${i}T10:00:00.000Z`, | |
| 78 | }); | |
| 79 | recordAppRelease(app.db, { | |
| 80 | repoId: priv, version: "9.9.9", commit: "9".repeat(40), subject: "Secret", body: "", tests: "none", servingMs: null, servedAt: "2026-11-14T10:00:00.000Z", | |
| 81 | }); | |
| 82 | }, 30_000); | |
| 83 | afterAll(async () => { | |
| 84 | void servers.stop?.(); | |
| 85 | await rm(dataDir, { recursive: true, force: true }); | |
| 86 | }); | |
| 87 | ||
| 88 | test("a version is a release once", () => { | |
| 89 | const again = recordAppRelease(app.db, { | |
| 90 | repoId: pub, version: "1.0.3", commit: "f".repeat(40), subject: "Redeploy", body: "", tests: "none", servingMs: 1, servedAt: "2026-11-15T10:00:00.000Z", | |
| 91 | }); | |
| 92 | expect(again).toBeUndefined(); | |
| 93 | expect(listAppReleases(app.db, pub).map((r) => r.version)).toEqual(["1.0.3", "1.0.2", "1.0.1"]); | |
| 94 | }); | |
| 95 | test("the tests' verdict is attached to the commit's release", () => { | |
| 96 | setReleaseTests(app.db, pub, "3".repeat(40), "passed"); | |
| 97 | expect(listAppReleases(app.db, pub)[0]!.tests).toBe("passed"); | |
| 98 | }); | |
| 99 | test("the API lists them newest first and pages by before", async () => { | |
| 100 | const res = await fetch(`${servers.url}/api/v1/repos/pop/pop/releases?limit=2`); | |
| 101 | const body: any = await res.json(); | |
| 102 | expect(body.items.map((r: any) => r.version)).toEqual(["1.0.3", "1.0.2"]); | |
| 103 | expect(body.items[0]).toMatchObject({ commit: "3".repeat(40), subject: "Ship 1.0.3", servingMs: 1500 }); | |
| 104 | const next: any = await (await fetch(`${servers.url}/api/v1/repos/pop/pop/releases?before=${encodeURIComponent(body.items[1].cursor)}`)).json(); | |
| 105 | expect(next.items.map((r: any) => r.version)).toEqual(["1.0.1"]); | |
| 106 | expect((await fetch(`${servers.url}/api/v1/repos/pop/pop/releases?limit=0`)).status).toBe(422); | |
| 107 | }); | |
| 108 | test("a private repository's releases are not found without a token", async () => { | |
| 109 | expect((await fetch(`${servers.url}/api/v1/repos/pop/hidden/releases`)).status).toBe(404); | |
| 110 | expect((await fetch(`${servers.url}/pop/hidden/releases`)).status).toBe(404); | |
| 111 | const withToken: any = await (await fetch(`${servers.url}/api/v1/repos/pop/hidden/releases`, { headers: { authorization: `Bearer ${token}` } })).json(); | |
| 112 | expect(withToken.items[0].version).toBe("9.9.9"); | |
| 113 | }); | |
| 114 | test("the page shows them, escaped", async () => { | |
| 115 | const text = await (await fetch(`${servers.url}/pop/pop/releases`)).text(); | |
| 116 | expect(text).toContain("1.0.3"); | |
| 117 | expect(text).toContain("<b>note 3</b>"); | |
| 118 | expect(text).not.toContain("<b>note 3</b>"); | |
| 119 | }); | |
| 120 | test("the changelog lists public releases only", async () => { | |
| 121 | const body: any = await (await fetch(`${servers.url}/api/v1/changelog`)).json(); | |
| 122 | expect(body.releases.map((r: any) => `${r.repo}@${r.version}`)).toEqual(["pop/[email protected]", "pop/[email protected]", "pop/[email protected]"]); | |
| 123 | }); | |
| 124 | }); | |
| 125 | ||
| 126 | import { execSync } from "node:child_process"; | |
| 127 | import { mkdirSync, writeFileSync } from "node:fs"; | |
| 128 | import { noteRelease, noteReleaseTests, recordsReleases } from "../src/apps/release_record.ts"; | |
| 129 | import { settleStaleReleases, listPublicReleases } from "../src/core/app_releases.ts"; | |
| 130 | import { appReleasesCommands } from "../src/tui/screens/appreleases.ts"; | |
| 131 | ||
| 132 | describe("reading is capped", () => { | |
| 133 | test("an endless answer is cut and cancelled", async () => { | |
| 134 | let cancelled: () => void = () => {}; | |
| 135 | const wasCancelled = new Promise<void>((r) => (cancelled = r)); | |
| 136 | const server = Bun.serve({ | |
| 137 | port: 0, | |
| 138 | fetch: () => | |
| 139 | new Response( | |
| 140 | new ReadableStream({ | |
| 141 | pull(c) { | |
| 142 | c.enqueue(new TextEncoder().encode("x".repeat(1024))); | |
| 143 | }, | |
| 144 | cancel() { | |
| 145 | cancelled(); | |
| 146 | }, | |
| 147 | }), | |
| 148 | ), | |
| 149 | }); | |
| 150 | try { | |
| 151 | expect(await readAppVersion("/v", server.port!, "/nowhere", "0".repeat(40))).toBeUndefined(); | |
| 152 | await wasCancelled; | |
| 153 | } finally { | |
| 154 | void server.stop(true); | |
| 155 | } | |
| 156 | }); | |
| 157 | }); | |
| 158 | ||
| 159 | describe("noteRelease and the file source", () => { | |
| 160 | let dir: string; | |
| 161 | let oid: string; | |
| 162 | let db: App["db"]; | |
| 163 | let app: App; | |
| 164 | let repoId: string; | |
| 165 | let work: string; | |
| 166 | beforeAll(async () => { | |
| 167 | work = await mkdtemp(join(tmpdir(), "forge-noterelease-")); | |
| 168 | app = createApp({ dataDir: work, port: 0 }); | |
| 169 | db = app.db; | |
| 170 | const u = await createUser(db, { origin: "seed", trusted: true, username: "ann", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 171 | if (!u.ok) throw new Error(u.error); | |
| 172 | const r = await createRepo(app, u.value, { name: "x", visibility: "public" }); | |
| 173 | if (!r.ok) throw new Error("repo"); | |
| 174 | repoId = r.value.id; | |
| 175 | dir = join(work, "bare.git"); | |
| 176 | const tree = join(work, "tree"); | |
| 177 | mkdirSync(tree); | |
| 178 | writeFileSync(join(tree, "VERSION"), "v2.0.0\n"); | |
| 179 | writeFileSync(join(tree, "BIG"), "1".repeat(70_000)); | |
| 180 | const env = { ...process.env, GIT_AUTHOR_NAME: "a", GIT_AUTHOR_EMAIL: "a@a", GIT_COMMITTER_NAME: "a", GIT_COMMITTER_EMAIL: "a@a" }; | |
| 181 | execSync(`git init -q . && git add . && git commit -q -m "Ship two" -m "the body"`, { cwd: tree, env }); | |
| 182 | oid = execSync("git rev-parse HEAD", { cwd: tree }).toString().trim(); | |
| 183 | execSync(`git clone -q --bare ${tree} ${dir}`); | |
| 184 | }, 30_000); | |
| 185 | afterAll(() => rm(work, { recursive: true, force: true })); | |
| 186 | ||
| 187 | const input = (source: string) => ({ | |
| 188 | repoId, repoDir: dir, source, port: 1, oid, servingAt: "2026-11-15T10:00:02.000Z", | |
| 189 | pushedAt: "2026-11-15T10:00:00.000Z", startedAt: undefined, tested: true, | |
| 190 | }); | |
| 191 | test("a file in the commit is the version; a file over the cap is none", async () => { | |
| 192 | expect(await readAppVersion("VERSION", 1, dir, oid)).toBe("2.0.0"); | |
| 193 | expect(await readAppVersion("BIG", 1, dir, oid)).toBeUndefined(); | |
| 194 | expect(await readAppVersion("MISSING", 1, dir, oid)).toBeUndefined(); | |
| 195 | }); | |
| 196 | test("noteRelease records pending with the commit's words, once; the verdict then settles it", async () => { | |
| 197 | const r = await noteRelease(db, input("VERSION")); | |
| 198 | expect(r).toMatchObject({ version: "2.0.0", subject: "Ship two", note: "the body", tests: "pending", servingMs: 2000 }); | |
| 199 | expect(await noteRelease(db, input("VERSION"))).toBeUndefined(); | |
| 200 | noteReleaseTests(db, "production", { version: "VERSION", test: "bun test" }, repoId, oid, true); | |
| 201 | expect(listAppReleases(db, repoId)[0]!.tests).toBe("passed"); | |
| 202 | noteReleaseTests(db, "production", { version: "VERSION", test: "bun test" }, repoId, oid, false); | |
| 203 | expect(listAppReleases(db, repoId)[0]!.tests).toBe("failed"); | |
| 204 | }); | |
| 205 | test("only production with a version records; no test command leaves the verdict alone", () => { | |
| 206 | expect(recordsReleases("production", { version: "/h" })).toBe(true); | |
| 207 | expect(recordsReleases("production", {})).toBe(false); | |
| 208 | expect(recordsReleases("preview", { version: "/h" })).toBe(false); | |
| 209 | noteReleaseTests(db, "production", { version: "VERSION", test: null }, repoId, oid, true); | |
| 210 | noteReleaseTests(db, "preview", { version: "VERSION", test: "t" }, repoId, oid, true); | |
| 211 | expect(listAppReleases(db, repoId)[0]!.tests).toBe("failed"); | |
| 212 | }); | |
| 213 | test("a restart settles pending to none", () => { | |
| 214 | recordAppRelease(db, { repoId, version: "3.0.0", commit: "c".repeat(40), subject: "s", body: "", tests: "pending", servingMs: null, servedAt: "2026-11-16T00:00:00.000Z" }); | |
| 215 | expect(settleStaleReleases(db)).toBe(1); | |
| 216 | expect(listAppReleases(db, repoId)[0]!.tests).toBe("none"); | |
| 217 | }); | |
| 218 | test("releases sharing a time are all paged, by cursor", () => { | |
| 219 | for (const v of ["5.0.0", "5.0.1", "5.0.2"]) | |
| 220 | recordAppRelease(db, { repoId, version: v, commit: "d".repeat(40), subject: v, body: "", tests: "none", servingMs: null, servedAt: "2026-12-01T00:00:00.000Z" }); | |
| 221 | const seen: string[] = []; | |
| 222 | let before: string | undefined; | |
| 223 | for (let i = 0; i < 6; i++) { | |
| 224 | const page = listAppReleases(db, repoId, 1, before); | |
| 225 | if (page.length === 0) break; | |
| 226 | seen.push(page[0]!.version); | |
| 227 | before = page[0]!.cursor; | |
| 228 | } | |
| 229 | expect(seen.slice(0, 3).sort()).toEqual(["5.0.0", "5.0.1", "5.0.2"]); | |
| 230 | expect(seen).toHaveLength(new Set(seen).size); | |
| 231 | expect(seen).toContain("2.0.0"); | |
| 232 | }); | |
| 233 | test("?repo narrows the public list", () => { | |
| 234 | expect(listPublicReleases(db, 50, "ann/x").length).toBeGreaterThan(0); | |
| 235 | expect(listPublicReleases(db, 50, "ann/other")).toEqual([]); | |
| 236 | }); | |
| 237 | test("the terminal command is absent with no releases and for a guest on a private repo", () => { | |
| 238 | const f: any = { app, user: undefined }; | |
| 239 | const ctx: any = { push() {} }; | |
| 240 | const repo: any = { id: "nope", ownerName: "ann", name: "x", visibility: "public" }; | |
| 241 | expect(appReleasesCommands(f, ctx, repo)).toEqual([]); | |
| 242 | expect(appReleasesCommands(f, ctx, { ...repo, id: repoId })).toHaveLength(1); | |
| 243 | app.db.query("UPDATE repo SET visibility = 'private' WHERE id = ?").run(repoId); | |
| 244 | expect(appReleasesCommands(f, ctx, { ...repo, id: repoId, visibility: "private" })).toEqual([]); | |
| 245 | }); | |
| 246 | }); |
new above
offline
The how-to for a new feature belongs in its own docs/features/app-releases.md, not a section of the changelog doc. Move it and link it from here.