Public: anyone can read this; the crew can change it.
Production's releases tool lists the releases cut from main, so the v1 probe's two release lines can pass #15407
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15404-production-s-releases-tool-lists-the-rel → main 0 commits fork 1 thread · 1 unresolved
5 files
changed +211
−2
src/release/answer.ts +6 −1
| @@ -15,6 +15,7 @@ import { chapterEntries, groupByChapter, wavesOf } from "./chapter_groups.ts"; | ||
| 15 | 15 | import { chosenRepo, readLandings, type Landing, type Landings } from "./landings.ts"; |
| 16 | 16 | import { keeperReleaseOfSha, type Stage } from "../keeper/record.ts"; |
| 17 | 17 | import { listReleases, releaseName } from "./record.ts"; |
| 18 | import { mergeReleases, publicTagReleases } from "./repo_tags.ts"; | |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * What the forge can say of a kept release's soundness: the keeper's verdict, else the preflight's. |
| @@ -56,6 +57,10 @@ export async function shippedAnswer(app: App, asked: string | null = null) { | ||
| 56 | 57 | // else is all. |
| 57 | 58 | const named = asked !== null && /^[^/\s]{1,80}\/[^/\s]{1,80}$/.test(asked) ? asked : undefined; |
| 58 | 59 | const live = read.live.filter(keep); |
| 60 | // A repository's releases are also its `v*` tags: the forge's own is never an app here, so | |
| 61 | // `app_release` alone listed none of them (#15404). Absent names the forge's own. | |
| 62 | const tagged = named ?? (asked === null ? read.own : undefined); | |
| 63 | const tags = tagged === undefined ? [] : await publicTagReleases(app, tagged); | |
| 59 | 64 | const waves = wavesOf(groupByChapter(live, await withWaves(app, chapterEntries(app.db, live, undefined)))); |
| 60 | 65 | return { |
| 61 | 66 | // The release number, or null for a build that is not one: never the word "dev". |
| @@ -66,7 +71,7 @@ export async function shippedAnswer(app: App, asked: string | null = null) { | ||
| 66 | 71 | live, |
| 67 | 72 | waves, |
| 68 | 73 | onItsWay: read.onItsWay.filter(keep), |
| 69 | releases: listPublicReleases(app.db, 50, named), | |
| 74 | releases: mergeReleases(listPublicReleases(app.db, 50, named), tags), | |
| 70 | 75 | }; |
| 71 | 76 | } |
| 72 | 77 |
src/release/repo_tags.ts added +101
| @@ -0,0 +1,101 @@ | ||
| 1 | /** | |
| 2 | * A public repository's `v*` tags as releases (#15404): what the token-free | |
| 3 | * `releases` tool and /api/v1/changelog list beside the versions apps served. | |
| 4 | * A repository that is never deployed as an app here — the forge's own, cut | |
| 5 | * by `scripts/cut-release.ts` — has its releases only as tags, so a read of | |
| 6 | * `app_release` alone answered `releases: []` while the repository held | |
| 7 | * hundreds. Read from the git store, never the network, and kept a short | |
| 8 | * while so a burst of anonymous reads costs git once. | |
| 9 | */ | |
| 10 | ||
| 11 | import type { Database } from "bun:sqlite"; | |
| 12 | import type { PublicRelease } from "../core/app_releases.ts"; | |
| 13 | import { getRepo } from "../core/repos.ts"; | |
| 14 | import { runGit } from "../git/env.ts"; | |
| 15 | import { repoDir } from "../git/repos.ts"; | |
| 16 | ||
| 17 | const TAG = /^v(\d+(?:\.\d+){0,3}(?:[-+][0-9A-Za-z.\-]{1,40})?)$/; | |
| 18 | const OID = /^[0-9a-f]{40}$/; | |
| 19 | /** How long one read answers before git is asked again. */ | |
| 20 | const KEEP_MS = 30_000; | |
| 21 | ||
| 22 | type Kept = Map<string, { at: number; read: Promise<PublicRelease[]> }>; | |
| 23 | /** What each forge read, keyed by its database so two forges in one process never share it. */ | |
| 24 | const keptOf = new WeakMap<Database, Kept>(); | |
| 25 | ||
| 26 | function keptFor(db: Database): Kept { | |
| 27 | let kept = keptOf.get(db); | |
| 28 | if (kept === undefined) keptOf.set(db, (kept = new Map())); | |
| 29 | return kept; | |
| 30 | } | |
| 31 | ||
| 32 | /** Parses `for-each-ref` output (fields split by NUL, records by SOH) into releases of `repo`. */ | |
| 33 | export function parseTagLines(out: string, repo: string): PublicRelease[] { | |
| 34 | const releases: PublicRelease[] = []; | |
| 35 | for (const entry of out.split("\x01")) { | |
| 36 | const [name = "", peeled = "", own = "", secs = "", subject = ""] = entry.replace(/^\n/, "").split("\x00"); | |
| 37 | const version = TAG.exec(name)?.[1]; | |
| 38 | const commit = OID.test(peeled) ? peeled : own; | |
| 39 | const at = Number(secs) * 1000; | |
| 40 | if (version === undefined || !OID.test(commit) || !Number.isFinite(at) || at <= 0) continue; | |
| 41 | const servedAt = new Date(at).toISOString(); | |
| 42 | releases.push({ | |
| 43 | repo, | |
| 44 | version, | |
| 45 | commit, | |
| 46 | // A cut's message is its JSON record, no words for a reader. | |
| 47 | subject: subject.startsWith("{") ? `v${version}` : subject.slice(0, 500), | |
| 48 | note: "", | |
| 49 | tests: "none", | |
| 50 | servingMs: null, | |
| 51 | servedAt, | |
| 52 | cursor: `${servedAt}~${name}`, | |
| 53 | }); | |
| 54 | } | |
| 55 | return releases; | |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * The newest `limit` `v*` tags of the public repository `named` (`owner/name`); [] for a private, | |
| 60 | * absent or unreadable one. | |
| 61 | */ | |
| 62 | export async function publicTagReleases( | |
| 63 | app: { readonly db: Database; readonly reposRoot: string }, | |
| 64 | named: string, | |
| 65 | limit = 50, | |
| 66 | now = Date.now(), | |
| 67 | ): Promise<PublicRelease[]> { | |
| 68 | const [owner = "", name = ""] = named.split("/"); | |
| 69 | const repo = getRepo(app.db, owner, name); | |
| 70 | if (repo === undefined || repo.visibility !== "public") return []; | |
| 71 | const kept = keptFor(app.db); | |
| 72 | const key = `${app.reposRoot}\0${repo.id}\0${limit}`; | |
| 73 | const hit = kept.get(key); | |
| 74 | if (hit !== undefined && now - hit.at < KEEP_MS) return hit.read; | |
| 75 | const full = `${repo.ownerName}/${repo.name}`; | |
| 76 | const read = runGit( | |
| 77 | [ | |
| 78 | "for-each-ref", | |
| 79 | `--count=${Math.min(Math.max(limit, 1), 200)}`, | |
| 80 | "--sort=-creatordate", | |
| 81 | "--format=%(refname:short)%00%(*objectname)%00%(objectname)%00%(creatordate:unix)%00%(contents:subject)%01", | |
| 82 | "refs/tags/v*", | |
| 83 | ], | |
| 84 | { cwd: repoDir(app.reposRoot, repo.ownerName, repo.name) }, | |
| 85 | ) | |
| 86 | .then((out) => (out.ok ? parseTagLines(out.value, full) : [])) | |
| 87 | .catch(() => []); | |
| 88 | kept.set(key, { at: now, read }); | |
| 89 | return read; | |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * App releases and tag releases as one list newest first; a version both name is listed once, as | |
| 94 | * the app served it. | |
| 95 | */ | |
| 96 | export function mergeReleases(served: readonly PublicRelease[], tagged: readonly PublicRelease[], limit = 50): PublicRelease[] { | |
| 97 | const seen = new Set(served.map((r) => `${r.repo}\0${r.version}`)); | |
| 98 | return [...served, ...tagged.filter((r) => !seen.has(`${r.repo}\0${r.version}`))] | |
| 99 | .sort((a, b) => (a.servedAt < b.servedAt ? 1 : a.servedAt > b.servedAt ? -1 : 0)) | |
| 100 | .slice(0, limit); | |
| 101 | } |
src/web/feeds.ts +1 −1
| @@ -303,7 +303,7 @@ export async function changelogAtom(ctx: Ctx): Promise<Response> { | ||
| 303 | 303 | <updated>${xml(stamp(updated))}</updated> |
| 304 | 304 | <link rel="self" type="application/atom+xml" href="${xml(origin)}/changelog.atom"/> |
| 305 | 305 | <link rel="alternate" type="text/html" href="${xml(origin)}/changelog"/> |
| 306 | <generator>forge</generator> | |
| 306 | <generator${answer.version === null ? "" : ` version="${xml(answer.version)}"`}>forge</generator> | |
| 307 | 307 | ${entries.join("\n")} |
| 308 | 308 | </feed> |
| 309 | 309 | `; |
test/helpers/plain-git.ts +11
| @@ -8,6 +8,17 @@ export function git(cwd: string, ...args: string[]): string { | ||
| 8 | 8 | return p.stdout.toString().trim(); |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | /** As `git`, with author and committer dated `date` and `stdin` fed in; throws when git fails. */ | |
| 12 | export function gitAt(cwd: string, args: readonly string[], date: string, stdin?: string): string { | |
| 13 | const p = Bun.spawnSync(["git", ...args], { | |
| 14 | cwd, | |
| 15 | stdin: stdin === undefined ? "ignore" : new TextEncoder().encode(stdin), | |
| 16 | env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t", GIT_AUTHOR_DATE: date, GIT_COMMITTER_DATE: date }, | |
| 17 | }); | |
| 18 | if (p.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${p.stderr.toString()}`); | |
| 19 | return p.stdout.toString().trim(); | |
| 20 | } | |
| 21 | ||
| 11 | 22 | /** Insert a closed, merged pull numbered `n` whose merge commit is `oid`. */ |
| 12 | 23 | export function mergedPull(db: import("bun:sqlite").Database, p: { repoId: string; branch: string; authorId: string; n: number; oid: string; at: string; title: string }): void { |
| 13 | 24 | const issueId = crypto.randomUUID(); |
test/release-repo-tags.test.ts added +92
| @@ -0,0 +1,92 @@ | ||
| 1 | /** | |
| 2 | * The token-free `releases` answer lists a public repository's `v*` tags with | |
| 3 | * version and commit (#15404): the forge's own repository is never deployed as | |
| 4 | * an app on itself, so `app_release` alone answered `releases: []` while it | |
| 5 | * held hundreds of tags, and the v1 probe's two release lines failed. | |
| 6 | */ | |
| 7 | ||
| 8 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { repoDir } from "../src/git/repos.ts"; | |
| 16 | import { shippedAnswer } from "../src/release/answer.ts"; | |
| 17 | import { serve } from "../src/http/server.ts"; | |
| 18 | import { mergeReleases, parseTagLines } from "../src/release/repo_tags.ts"; | |
| 19 | import { newestRelease } from "../scripts/v1-probe.ts"; | |
| 20 | import { gitAt } from "./helpers/plain-git.ts"; | |
| 21 | ||
| 22 | let data: string; | |
| 23 | let app: App; | |
| 24 | let work: string; | |
| 25 | const tips: string[] = []; | |
| 26 | const run = (cwd: string, args: string[], date = "2026-11-20T10:00:00Z", stdin?: string): string => gitAt(cwd, args, date, stdin); | |
| 27 | ||
| 28 | beforeAll(async () => { | |
| 29 | data = await mkdtemp(join(tmpdir(), "forge-repo-tags-")); | |
| 30 | app = createApp({ dataDir: data, port: 0 }); | |
| 31 | const owner = await createUser(app.db, { origin: "seed", trusted: true, username: "trevato", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 32 | if (!owner.ok) throw new Error(owner.error); | |
| 33 | for (const [name, visibility] of [["forge", "public"], ["secret", "private"]] as const) { | |
| 34 | const repo = await createRepo(app, owner.value, { name, visibility }); | |
| 35 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 36 | } | |
| 37 | work = join(data, "work"); | |
| 38 | await Bun.$`mkdir -p ${work}`.quiet(); | |
| 39 | await run(work, ["init", "-q", "-b", "main"]); | |
| 40 | for (const [i, v] of ["0.7.628", "0.7.629"].entries()) { | |
| 41 | const date = `2026-11-2${i}T10:00:00Z`; | |
| 42 | await run(work, ["commit", "-q", "--allow-empty", "-m", `cut ${v}`], date); | |
| 43 | tips.push(await run(work, ["rev-parse", "HEAD"])); | |
| 44 | // As scripts/cut-release.ts cuts them: annotated, the message the JSON record. | |
| 45 | await run(work, ["tag", "-a", "-F", "-", `v${v}`], date, JSON.stringify({ version: v, migrations: 3 })); | |
| 46 | } | |
| 47 | await run(work, ["tag", "not-a-release"]); | |
| 48 | for (const name of ["forge", "secret"]) await run(repoDir(app.reposRoot, "trevato", name), ["fetch", "-q", work, "main:main", "+refs/tags/*:refs/tags/*"]); | |
| 49 | }, 60_000); | |
| 50 | ||
| 51 | afterAll(async () => { | |
| 52 | app.close(); | |
| 53 | await rm(data, { recursive: true, force: true }); | |
| 54 | }); | |
| 55 | ||
| 56 | test("a public repository's tags are its releases, newest first, with version and the commit they tag", async () => { | |
| 57 | const answer = await shippedAnswer(app, "trevato/forge"); | |
| 58 | expect(answer.releases.map((r) => [r.repo, r.version, r.commit])).toEqual([ | |
| 59 | ["trevato/forge", "0.7.629", tips[1]!], | |
| 60 | ["trevato/forge", "0.7.628", tips[0]!], | |
| 61 | ]); | |
| 62 | expect(answer.releases[0]!.subject).toBe("v0.7.629"); | |
| 63 | // What the v1 probe reads from the tool's JSON. | |
| 64 | const rpc = { result: { content: [{ type: "text", text: JSON.stringify(answer) }] } }; | |
| 65 | expect(newestRelease(rpc, "trevato/forge")).toEqual({ release: { name: "v0.7.629", version: "0.7.629", commit: tips[1]! } }); | |
| 66 | }); | |
| 67 | ||
| 68 | test("a private repository's tags are never read for a stranger", async () => { | |
| 69 | expect((await shippedAnswer(app, "trevato/secret")).releases).toEqual([]); | |
| 70 | expect((await shippedAnswer(app, "nobody/here")).releases).toEqual([]); | |
| 71 | }); | |
| 72 | ||
| 73 | test("a version an app served is listed once, as served, among the tags", () => { | |
| 74 | const tag = parseTagLines(`v1.2.0\x00${"a".repeat(40)}\x00${"b".repeat(40)}\x001700000000\x00{"version":"1.2.0"}\x01\nlatest\x00\x00${"c".repeat(40)}\x001700000000\x00x\x01`, "o/r"); | |
| 75 | expect(tag.map((r) => [r.version, r.commit])).toEqual([["1.2.0", "a".repeat(40)]]); | |
| 76 | const served = { ...tag[0]!, subject: "the served one", tests: "passed" as const }; | |
| 77 | expect(mergeReleases([served], tag)).toEqual([served]); | |
| 78 | }); | |
| 79 | ||
| 80 | test("/changelog.atom names the running release even when no landing is live, as the probe's `version agrees` reads it", async () => { | |
| 81 | const was = process.env["FORGE_VERSION"]; | |
| 82 | process.env["FORGE_VERSION"] = "v0.7.629"; | |
| 83 | const servers = await serve(app); | |
| 84 | try { | |
| 85 | const body = await (await fetch(`${servers.url}/changelog.atom`)).text(); | |
| 86 | expect(body).toContain('<generator version="0.7.629">forge</generator>'); | |
| 87 | } finally { | |
| 88 | servers.stop(); | |
| 89 | if (was === undefined) delete process.env["FORGE_VERSION"]; | |
| 90 | else process.env["FORGE_VERSION"] = was; | |
| 91 | } | |
| 92 | }); |
new above
offline
Nit: a failed git read is cached as [] for 30s. Fine today; consider not caching failures so a transient error is not sticky.