Public: anyone can read this; the crew can change it.
GET a repository's commits answers `{ items, next_cursor }`, and every test that reads it is updated #16929
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/5083-get-a-repository-s-commits-answers-items → main 0 commits fork 1 thread · 1 unresolved
13 files
changed +66
−25
- docs/features/list-limit.md
- scripts/home-eval.ts
- src/contract/openapi/routes/getApiV1ReposOwnerNameCommits.ts
- src/http/api.ts
- test/api-commit-timing.test.ts
- test/api-commits-timing.test.ts
- test/api-list-shape.test.ts
- test/commits-etag.test.ts
- test/origin-pull-state.test.ts
- test/parity.test.ts
- test/read-api.test.ts
- test/view-source.test.ts
- test/web-browse.test.ts
docs/features/list-limit.md +1 −1
| @@ -43,7 +43,7 @@ Every GET route the contract gives a `limit`, read by `parseLimit` (directly, or | ||
| 43 | 43 | - [x] `/api/v1/repos` (max 200, cursor) |
| 44 | 44 | - [x] `/api/v1/repos/{owner}/{name}/app/pulls` (max 100) |
| 45 | 45 | - [x] `/api/v1/repos/{owner}/{name}/branches` (max 200, cursor) |
| 46 | - [x] `/api/v1/repos/{owner}/{name}/commits` (max 100) | |
| 46 | - [x] `/api/v1/repos/{owner}/{name}/commits` (max 100, cursor) | |
| 47 | 47 | - [x] `/api/v1/repos/{owner}/{name}/decisions` (max 100, cursor) |
| 48 | 48 | - [x] `/api/v1/repos/{owner}/{name}/events` (max 200, cursor) |
| 49 | 49 | - [x] `/api/v1/repos/{owner}/{name}/family` (max 200) |
scripts/home-eval.ts +3 −2
| @@ -40,8 +40,9 @@ async function truth(): Promise<Truth> { | ||
| 40 | 40 | for (let i = 0; i < live.length; i += 8) { |
| 41 | 41 | await Promise.all( |
| 42 | 42 | live.slice(i, i + 8).map(async (full) => { |
| 43 | const log: { date: string }[] = await get(`/api/v1/repos/${full}/commits?limit=1`); | |
| 44 | if (log[0] !== undefined && Date.now() - Date.parse(log[0].date) > 3 * DAY) quiet.push(full); | |
| 43 | const log: { items: { date: string }[] } = await get(`/api/v1/repos/${full}/commits?limit=1`); | |
| 44 | const last = log.items[0]; | |
| 45 | if (last !== undefined && Date.now() - Date.parse(last.date) > 3 * DAY) quiet.push(full); | |
| 45 | 46 | }), |
| 46 | 47 | ); |
| 47 | 48 | } |
src/contract/openapi/routes/getApiV1ReposOwnerNameCommits.ts +1 −1
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | import { REPO, type Route, limitTo } from "../shared.ts"; |
| 2 | 2 | |
| 3 | export const route: Route = { method: "get", path: `${REPO}/commits`, summary: "List commits newest first from `ref` as a bare array", detail: "List commits newest first from `ref` (default: the default branch) as a bare array of { oid, parents, author, email, date, subject, body }; limit 1 to 100 (default 30); an unsafe ref is a 422 bad_ref; a private repository needs a token; the 200 carries a weak etag, cache-control private, no-cache and vary authorization, cookie, and a repeat sending If-None-Match gets 304 with no body until the ref moves", auth: false, role: "anonymous", alsoStatus: [304], query: { ref: "string", limit: limitTo(100) }, refuses: [404] }; | |
| 3 | export const route: Route = { method: "get", path: `${REPO}/commits`, summary: "List commits newest first from `ref` as `{ items, next_cursor }`", detail: "List commits newest first from `ref` (default: the default branch) as `{ items, next_cursor }`, items of { oid, parents, author, email, date, subject, body }; limit 1 to 100 (default 30); a cursor (the next_cursor of the page before, 40 to 64 hex) continues the log after that commit and `ref` is then ignored, so the cursor's own history is paged (a malformed cursor or one naming no commit is a 422 invalid_cursor); an unsafe ref is a 422 bad_ref; a private repository needs a token; the 200 carries a weak etag, cache-control private, no-cache and vary authorization, cookie, and a repeat sending If-None-Match gets 304 with no body until the ref moves", auth: false, role: "anonymous", alsoStatus: [304], query: { ref: "string", cursor: "string", limit: limitTo(100) }, refuses: [404] }; |
src/http/api.ts +14 −3
| @@ -2026,14 +2026,25 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 2026 | 2026 | if (!isSafeRef(ref)) return json({ error: "bad_ref" }, 422); |
| 2027 | 2027 | const limit = parseLimit(ctx.url.searchParams.get("limit"), 100, 30); |
| 2028 | 2028 | if (limit === undefined) return limitRefusal(100); |
| 2029 | // The cursor is the last oid of the page before: the log goes on after it. | |
| 2030 | const cursor = ctx.url.searchParams.get("cursor"); | |
| 2031 | if (cursor !== null && !/^[0-9a-f]{40,64}$/.test(cursor)) | |
| 2032 | return cursorRefusal("cursor is the next_cursor of a page before"); | |
| 2029 | 2033 | const log = await timePhaseAsync(ctx.request, "git", () => |
| 2030 | commitLog(dirOf(ctx, repo), ref, { limit }), | |
| 2034 | commitLog(dirOf(ctx, repo), cursor ?? ref, { limit: limit + 1, skip: cursor === null ? 0 : 1 }), | |
| 2031 | 2035 | ); |
| 2036 | if (cursor !== null && !log.ok) | |
| 2037 | return cursorRefusal("cursor names a commit that is gone: start again without it"); | |
| 2032 | 2038 | // The tag is of the exact bytes sent plus the ref and limit asked, so a moved tip |
| 2033 | 2039 | // misses, a push to another branch does not, and two queries never share a tag. |
| 2034 | 2040 | return timePhase(ctx.request, "render", () => { |
| 2035 | const text = JSON.stringify(log.ok ? log.value : []); | |
| 2036 | const etag = weakEtag(`${ref}\n${limit}\n${text}`); | |
| 2041 | const all = log.ok ? log.value : []; | |
| 2042 | const items = all.slice(0, limit); | |
| 2043 | const text = JSON.stringify({ | |
| 2044 | items, | |
| 2045 | next_cursor: all.length > limit ? items[items.length - 1]!.oid : null, | |
| 2046 | }); | |
| 2047 | const etag = weakEtag(`${ref}\n${limit}\n${cursor ?? ""}\n${text}`); | |
| 2037 | 2048 | const headers = { etag, "cache-control": "private, no-cache", vary: "authorization, cookie" }; |
| 2038 | 2049 | if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers }); |
| 2039 | 2050 | return new Response(text, { headers: { "content-type": "application/json", ...headers } }); |
test/api-commit-timing.test.ts +1 −1
| @@ -44,7 +44,7 @@ beforeAll(async () => { | ||
| 44 | 44 | }); |
| 45 | 45 | if (!done.ok) throw new Error(JSON.stringify(done.error)); |
| 46 | 46 | const res = await fetch(`${servers.url}/api/v1/repos/alice/r/commits?ref=main`, { headers: { cookie } }); |
| 47 | oid = ((await res.json()) as { oid: string }[])[0]!.oid; | |
| 47 | oid = ((await res.json()) as { items: { oid: string }[] }).items[0]!.oid; | |
| 48 | 48 | }); |
| 49 | 49 | |
| 50 | 50 | afterAll(async () => { |
test/api-commits-timing.test.ts +3 −3
| @@ -63,14 +63,14 @@ test("the commits list names session, git, render in order", async () => { | ||
| 63 | 63 | const got = await phases("/api/v1/repos/alice/r/commits"); |
| 64 | 64 | expect(got.status).toBe(200); |
| 65 | 65 | expect(got.names).toEqual(["session", "git", "render"]); |
| 66 | expect(JSON.parse(got.body)[0].subject).toBe("change"); | |
| 66 | expect(JSON.parse(got.body).items[0].subject).toBe("change"); | |
| 67 | 67 | }); |
| 68 | 68 | |
| 69 | test("an unknown ref still names the three phases and answers []", async () => { | |
| 69 | test("an unknown ref still names the three phases and answers an empty page", async () => { | |
| 70 | 70 | const got = await phases("/api/v1/repos/alice/r/commits?ref=nope"); |
| 71 | 71 | expect(got.status).toBe(200); |
| 72 | 72 | expect(got.names).toEqual(["session", "git", "render"]); |
| 73 | expect(JSON.parse(got.body)).toEqual([]); | |
| 73 | expect(JSON.parse(got.body)).toEqual({ items: [], next_cursor: null }); | |
| 74 | 74 | }); |
| 75 | 75 | |
| 76 | 76 | test("a bad ref is a 422 bad_ref", async () => { |
test/api-list-shape.test.ts −1
| @@ -19,7 +19,6 @@ import { seedAdmin } from "./helpers/admin.ts"; | ||
| 19 | 19 | |
| 20 | 20 | /** Routes that answer a bare array today → the open issue that changes them. */ |
| 21 | 21 | const BARE: Record<string, string> = { |
| 22 | "/api/v1/repos/:owner/:name/commits": "#5083", | |
| 23 | 22 | "/api/v1/user/origins": "#4841", |
| 24 | 23 | "/api/v1/user/origin-apps": "#4841", |
| 25 | 24 | // The Gitea dialect a connecting forge reads is a bare array by definition (#8900). |
test/commits-etag.test.ts +28 −2
| @@ -61,13 +61,15 @@ afterAll(async () => { | ||
| 61 | 61 | await rm(dataDir, { recursive: true, force: true }); |
| 62 | 62 | }); |
| 63 | 63 | |
| 64 | test("the 200 carries a weak tag, private no-cache and vary, and the body stays a bare array", async () => { | |
| 64 | test("the 200 carries a weak tag, private no-cache and vary, and the body is { items, next_cursor }", async () => { | |
| 65 | 65 | const res = await get(); |
| 66 | 66 | expect(res.status).toBe(200); |
| 67 | 67 | expect(res.headers.get("etag")).toMatch(/^W\/"/); |
| 68 | 68 | expect(res.headers.get("cache-control")).toBe("private, no-cache"); |
| 69 | 69 | expect(res.headers.get("vary")).toContain("authorization, cookie"); |
| 70 | expect(Array.isArray(await res.json())).toBe(true); | |
| 70 | const body = (await res.json()) as { items: unknown[]; next_cursor: string | null }; | |
| 71 | expect(Array.isArray(body.items)).toBe(true); | |
| 72 | expect(body.next_cursor).toBeNull(); | |
| 71 | 73 | }); |
| 72 | 74 | |
| 73 | 75 | test("a matching If-None-Match is a bodiless 304 with the same headers and the security floor", async () => { |
| @@ -110,3 +112,27 @@ test("bad_ref 422 and 404 carry no etag", async () => { | ||
| 110 | 112 | expect(missing.status).toBe(404); |
| 111 | 113 | expect(missing.headers.get("etag")).toBeNull(); |
| 112 | 114 | }); |
| 115 | ||
| 116 | test("a cursor continues the log after the last oid of the page before", async () => { | |
| 117 | await commit("three"); | |
| 118 | type Page = { items: { oid: string }[]; next_cursor: string | null }; | |
| 119 | const first = (await (await get(`${URL_}?limit=1`)).json()) as Page; | |
| 120 | expect(first.items).toHaveLength(1); | |
| 121 | expect(first.next_cursor).toBe(first.items[0]!.oid); | |
| 122 | const second = (await (await get(`${URL_}?limit=1&cursor=${first.next_cursor}`)).json()) as Page; | |
| 123 | expect(second.items).toHaveLength(1); | |
| 124 | expect(second.items[0]!.oid).not.toBe(first.items[0]!.oid); | |
| 125 | const all = (await (await get(`${URL_}?limit=100`)).json()) as Page; | |
| 126 | expect(all.items[1]!.oid).toBe(second.items[0]!.oid); | |
| 127 | expect(all.next_cursor).toBeNull(); | |
| 128 | expect((await get(`${URL_}?cursor=zz`)).status).toBe(422); | |
| 129 | expect((await get(`${URL_}?cursor=${"0".repeat(40)}`)).status).toBe(422); | |
| 130 | }); | |
| 131 | ||
| 132 | test("with a cursor, ref is ignored: the cursor's own history is paged", async () => { | |
| 133 | type Page = { items: { oid: string }[]; next_cursor: string | null }; | |
| 134 | const first = (await (await get(`${URL_}?limit=1`)).json()) as Page; | |
| 135 | const plain = (await (await get(`${URL_}?limit=1&cursor=${first.next_cursor}`)).json()) as Page; | |
| 136 | const withRef = (await (await get(`${URL_}?ref=side&limit=1&cursor=${first.next_cursor}`)).json()) as Page; | |
| 137 | expect(withRef).toEqual(plain); | |
| 138 | }); |
test/origin-pull-state.test.ts +1 −1
| @@ -152,7 +152,7 @@ describe("a pull proposed to an origin forge", () => { | ||
| 152 | 152 | expect(getPull(mine.db, mirror().id, number)?.originUrl).toContain(`/bob/app/pulls/${number}`); |
| 153 | 153 | |
| 154 | 154 | // The commit that arrived keeps its author. |
| 155 | const commits = (await (await bobApi("/api/v1/repos/bob/app/commits?ref=alice/main&limit=1")).json()) as { author: string; subject: string }[]; | |
| 155 | const commits = ((await (await bobApi("/api/v1/repos/bob/app/commits?ref=alice/main&limit=1")).json()) as { items: { author: string; subject: string }[] }).items; | |
| 156 | 156 | expect(commits[0]).toMatchObject({ author: "alice", subject: "my change" }); |
| 157 | 157 | |
| 158 | 158 | // Asking for the hook is off the request path: it lands shortly after. |
test/parity.test.ts +1 −1
| @@ -1101,7 +1101,7 @@ describe("parity: full control through actions and the API", () => { | ||
| 1101 | 1101 | expect((await call("GET", "/api/v1/repos/alice/pub/tags?cursor=a..b")).status).toBe(422); |
| 1102 | 1102 | |
| 1103 | 1103 | const commits = await call("GET", "/api/v1/repos/alice/pub/commits"); |
| 1104 | const oid = commits.list[0]!.oid as string; | |
| 1104 | const oid = (commits.json.items as Json[])[0]!.oid as string; | |
| 1105 | 1105 | const one = await call("GET", `/api/v1/repos/alice/pub/commits/${oid}`); |
| 1106 | 1106 | expect(one.status).toBe(200); |
| 1107 | 1107 | expect(one.json.oid).toBe(oid); |
test/read-api.test.ts +3 −3
| @@ -80,11 +80,11 @@ describe("read-back API (contents, branches, commits)", () => { | ||
| 80 | 80 | }); |
| 81 | 81 | |
| 82 | 82 | test("commits walks history at a ref", async () => { |
| 83 | const commits = (await ( | |
| 83 | const commits = ((await ( | |
| 84 | 84 | await api("/api/v1/repos/dev/lib/commits?ref=main") |
| 85 | 85 | ).json()) as { |
| 86 | subject: string; | |
| 87 | }[]; | |
| 86 | items: { subject: string }[]; | |
| 87 | }).items; | |
| 88 | 88 | expect(commits[0]?.subject).toBe("init"); |
| 89 | 89 | }); |
| 90 | 90 |
test/view-source.test.ts +5 −3
| @@ -142,9 +142,11 @@ describe("RepoSource: one repository, two places to read it from", () => { | ||
| 142 | 142 | { path: "src/index.ts", content: SOURCE_FILE }, |
| 143 | 143 | ], |
| 144 | 144 | }); |
| 145 | const upstreamCommits = (await ( | |
| 146 | await fetch(`${originServers.url}/api/v1/repos/octo/widgets/commits`) | |
| 147 | ).json()) as { oid: string; subject: string }[]; | |
| 145 | const upstreamCommits = ( | |
| 146 | (await ( | |
| 147 | await fetch(`${originServers.url}/api/v1/repos/octo/widgets/commits`) | |
| 148 | ).json()) as { items: { oid: string; subject: string }[] } | |
| 149 | ).items; | |
| 148 | 150 | |
| 149 | 151 | // The fake Forgejo API in front of it, describing the same repository — |
| 150 | 152 | // the same files, the same log, so "equivalent" is a claim with teeth. |
test/web-browse.test.ts +5 −3
| @@ -130,9 +130,11 @@ describe("browsing an origin repository on the web", () => { | ||
| 130 | 130 | { path: "src/index.ts", content: SOURCE_FILE }, |
| 131 | 131 | ], |
| 132 | 132 | }); |
| 133 | const upstream = (await ( | |
| 134 | await fetch(`${originServers.url}/api/v1/repos/octo/widgets/commits`) | |
| 135 | ).json()) as { oid: string }[]; | |
| 133 | const upstream = ( | |
| 134 | (await ( | |
| 135 | await fetch(`${originServers.url}/api/v1/repos/octo/widgets/commits`) | |
| 136 | ).json()) as { items: { oid: string }[] } | |
| 137 | ).items; | |
| 136 | 138 | const tip = upstream[0]!.oid; |
| 137 | 139 | |
| 138 | 140 | const state = newFakeState("forgejo", ORIGIN_LOGIN, ORIGIN_TOKEN); |
new above
offline
The cursor replaces
refas the git start point, so?ref=x&cursor=<oid on y>pages y while the etag is keyed on x. Either refuse an oid that is not an ancestor ofref, or document in the route detail thatrefis ignored once a cursor is given.