Public: anyone can read this; the crew can change it.

The OpenAPI document describes the chapters routes with their fields #16538

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/16141-the-openapi-document-describes-the-chapt → main 0 commits fork 3 threads · 3 unresolved

  • Merged. torque/16141-the-openapi-document-describes-the-chapt was merged into main as 0179cc6.

Review

Sign in to review this change.
5 files changed +100 −3
src/contract/openapi/chapter_fields.ts added +50
@@ -0,0 +1,50 @@
1 /**
2 * The fields of a chapter and the words its refusals carry, as the chapters routes in
3 * `http/api/chapters.ts` answer them (`shape` there), said once for the three routes' descriptions.
4 */
5
6 /**
7 * One chapter as every chapters route answers it; `items` is the issues in it, each with number,
8 * title, status, doneAt, pull, builder and costUsd.
9 */
10 export const CHAPTER_FIELDS = {
11 name: "string",
12 means: "string",
13 state: "string",
14 patchOf: "string|null",
15 openedAt: "string|null",
16 closedAt: "string|null",
17 tag: "object|null",
18 notes: "string|null",
19 costUsd: "number|null",
20 progress: "object",
21 items: "array",
22 } as const;
23
24 /** What drafting refuses by stable `error` code (`draft` in `actions/chapters.ts`). */
25 export const CHAPTER_DRAFT_REFUSALS = {
26 forbidden: "403: only a person who administers the repository acts on chapters",
outdated

This one set serves draft and acts. Split it per route, and add the acts codes it lacks: not_draft, no_issue, unfinished, unproven, no_branch, tag_exists, git and too_late. Take the words from CHAPTER_WORDS.

27 exists: "409: a chapter with that name already exists",
28 invalid_name: "422: the name is not letters, digits, dots, dashes and underscores, at most 40",
29 means_too_long: "422: what done means is at most 300 characters",
30 } as const;
31
32 /**
33 * What the acts (`open`, `add`, `remove`, `ship`, `patch`) refuse by stable `error` code; the words
34 * are `CHAPTER_WORDS` in `actions/chapters.ts`, the statuses are the route's in
35 * `http/api/chapters.ts`. `ship` answers `unfinished`, `unproven`, `no_branch`, `tag_exists` and
36 * `git`.
37 */
38 export const CHAPTER_ACT_REFUSALS = {
39 forbidden: "403: only a person who administers the repository acts on chapters",
40 not_found: "404: no such chapter, or nothing to do",
41 no_issue: "404: no such issue here (add, remove)",
42 already_open: "409: another chapter is already open (open, patch)",
43 not_draft: "409: that chapter is not open for this (patch needs a closed chapter)",
44 closed: "409: a closed chapter cannot be changed or reopened; open a patch chapter instead",
45 unfinished: "409: items are still open (ship, which moves them to the next chapter, is the way to close)",
46 unproven: "409: v1.0.0 closes only when the proof passes on production, every probe (ship)",
47 no_branch: "409: the default branch has no commit to tag yet (ship)",
48 tag_exists: "409: a git tag with that name already exists (ship)",
49 git: "409: the tag could not be written (ship)",
50 } as const;
src/contract/openapi/routes/getApiV1ReposOwnerNameChapters.ts +1 −1
@@ -1,3 +1,3 @@
11 import { REPO, type Route } from "../shared.ts";
22
3 export const route: Route = { method: "get", path: `${REPO}/chapters`, summary: "The repository's chapters", detail: "The repository's chapters (a version with an end): the open one, the drafts waiting after it and the closed ones, each with its name, what done means, state, and its list of issues (done only when live and proven, building, left), progress and the date it finishes at the current pace; a closed chapter carries its tag, notes, cost and the day it closed", auth: false, role: "anonymous", refuses: [404] };
3 export const route: Route = { method: "get", path: `${REPO}/chapters`, summary: "The repository's chapters", detail: "The repository's chapters (a version with an end): the open one, the drafts waiting after it and the closed ones, each with its name, what done means, state, and its list of issues (done only when live and proven, building, left), progress and the date it finishes at the current pace; a closed chapter carries its tag, notes, cost and the day it closed", auth: false, role: "anonymous", reply: { chapters: "array" }, refusals: { not_found: "404: no such repository, or it is not readable" }, refuses: [404] };
src/contract/openapi/routes/postApiV1ReposOwnerNameChapters.ts +2 −1
@@ -1,3 +1,4 @@
11 import { REPO, type Route } from "../shared.ts";
2 import { CHAPTER_FIELDS, CHAPTER_DRAFT_REFUSALS } from "../chapter_fields.ts";
23
3 export const route: Route = { method: "post", path: `${REPO}/chapters`, summary: "Draft a chapter: `name` and `means`", detail: "Draft a chapter: `name` (the git tag it will carry, at most 40 characters) and `means`, one sentence of what done means. Only a person who administers the repository", auth: true, role: "member", body: { name: "string" }, refuses: [401, 404] };
4 export const route: Route = { method: "post", path: `${REPO}/chapters`, summary: "Draft a chapter: `name` and `means`", detail: "Draft a chapter: `name` (the git tag it will carry, at most 40 characters) and `means`, one sentence of what done means. Only a person who administers the repository", auth: true, role: "member", body: { name: "string", means: "string" }, status: 201, reply: CHAPTER_FIELDS, refusals: CHAPTER_DRAFT_REFUSALS, refuses: [401, 404] };
src/contract/openapi/routes/postApiV1ReposOwnerNameChaptersChapterVerb.ts +2 −1
@@ -1,3 +1,4 @@
11 import { REPO, type Route } from "../shared.ts";
2 import { CHAPTER_FIELDS, CHAPTER_ACT_REFUSALS } from "../chapter_fields.ts";
23
3 export const route: Route = { method: "post", path: `${REPO}/chapters/{chapter}/{verb}`, summary: "Open, add to, remove from, ship or patch a chapter", detail: "Act on a chapter. `verb` is `open` (the repository's one open chapter, from now), `add` or `remove` (`number`: an issue; the owner's act, recorded), `ship` (tag the default branch with the chapter's name, write its notes from what landed, move what is left to the next chapter and close it) or `patch` (open a patch chapter for a closed one). Only a person who administers the repository; a closed chapter cannot be reopened", auth: true, role: "member", refuses: [401, 404] };
4 export const route: Route = { method: "post", path: `${REPO}/chapters/{chapter}/{verb}`, summary: "Open, add to, remove from, ship or patch a chapter", detail: "Act on a chapter. `verb` is `open` (the repository's one open chapter, from now), `add` or `remove` (`number`: an issue, required for these two, read by no other verb, and the other verbs take no body; the owner's act, recorded), `ship` (tag the default branch with the chapter's name, write its notes from what landed, move what is left to the next chapter and close it) or `patch` (open a patch chapter for a closed one). `open`, `ship` and `patch` answer the chapter (every field present); `add` and `remove` answer `{ ok: true }` instead. Only a person who administers the repository; a closed chapter cannot be reopened", auth: true, role: "member", body: { number: "integer" }, reply: CHAPTER_FIELDS, refusals: CHAPTER_ACT_REFUSALS, refuses: [401, 404] };
outdated

The reply is { ok: true } when the act returns no value, so CHAPTER_FIELDS is not always the answer. number is read only for add and remove. Say both in the description.

test/openapi-chapters.test.ts added +45
@@ -0,0 +1,45 @@
1 /** The OpenAPI document describes every chapters route with its request and response fields. */
2 import { expect, test } from "bun:test";
3 import { readFileSync } from "node:fs";
4 import { openapi } from "../src/contract/openapi.ts";
5
6 const R = "/api/v1/repos/{owner}/{name}";
7 const doc = () => openapi("https://f.test", "1", "admin") as any;
8 const props = (op: any, code: string) => Object.keys(op.responses[code].content["application/json"].schema.properties);
9 const chapterKeys = ["name", "means", "state", "patchOf", "openedAt", "closedAt", "tag", "notes", "costUsd", "progress", "items"];
10
11 test("every chapters route the server registers is in the document", () => {
12 const src = readFileSync(new URL("../src/http/api/chapters.ts", import.meta.url), "utf8");
13 const routes = [...src.matchAll(/router\.add\("(GET|POST)", "([^"]+)"/g)].map((m) => [m[1]!.toLowerCase(), m[2]!.replace(/:(\w+)/g, "{$1}")] as const);
14 expect(routes.length).toBe(3);
15 for (const [method, path] of routes) expect(doc().paths[path]?.[method], `${method} ${path}`).toBeDefined();
16 });
17
18 test("the list answers { chapters }", () => {
19 expect(props(doc().paths[`${R}/chapters`].get, "200")).toEqual(["chapters"]);
20 });
21
22 test("drafting takes name and means and answers 201 with a chapter", () => {
23 const op = doc().paths[`${R}/chapters`].post;
24 expect(Object.keys(op.requestBody.content["application/json"].schema.properties)).toEqual(["name", "means"]);
25 expect(props(op, "201")).toEqual(chapterKeys);
26 const refused = op.responses.default.description;
27 for (const code of ["exists", "invalid_name", "means_too_long", "forbidden"]) expect(refused).toContain(`\`${code}\``);
28 for (const code of ["already_open", "not_found", "closed"]) expect(refused).not.toContain(`\`${code}\``);
29 });
30
31 test("an act takes number and answers a chapter", () => {
outdated

Assert the acts route's refusal codes here, such as unfinished and tag_exists. Without this the missing codes could not fail a test.

32 const op = doc().paths[`${R}/chapters/{chapter}/{verb}`].post;
33 expect(op.requestBody.content["application/json"].schema.properties.number.type).toBe("integer");
34 expect(props(op, "200")).toEqual(chapterKeys);
35 });
36
37 test("the acts list the refusals they answer, ship's included, and not drafting's", () => {
38 const op = doc().paths[`${R}/chapters/{chapter}/{verb}`].post;
39 const refused = op.responses.default.description;
40 for (const code of ["forbidden", "not_found", "no_issue", "already_open", "not_draft", "closed", "unfinished", "unproven", "no_branch", "tag_exists", "git"]) expect(refused, code).toContain(`\`${code}\``);
41 for (const code of ["exists", "invalid_name", "means_too_long"]) expect(refused, code).not.toContain(`\`${code}\``);
42 expect(op.description).toContain("{ ok: true }");
43 expect(op.requestBody.content["application/json"].schema.properties.number.type).toBe("integer");
44 expect(op.description).toContain("read by no other verb");
45 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs