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

The API refuses a read-only mirror with 409 and the same sentence as every door (#16434) #16450

Merged opened this

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

Merged

fix/api-mirror-words → main 0 commits

  • Merged. fix/api-mirror-words was merged into main as 8f706ff.

Review

Sign in to review this change.
3 files changed +19 −6
src/actions/branch_words.ts −1
@@ -49,7 +49,6 @@ export const DELETE_ERRORS: Record<string, string> = {
4949 "That branch moved while the page was open, so nothing was deleted. Reload and look again.",
5050 };
5151
52
5352 /**
5453 * The one sentence a refused branch create says on every door (page, Talk and MCP, the terminal):
5554 * on a mirror its person may only read, where to change it instead (`copyWords`, from
src/http/api.ts +9 −5
@@ -87,7 +87,7 @@ import {
8787 ORIGIN_ALREADY_CONNECTED,
8888 } from "../actions/origins.ts";
8989 import {
90 createBranch,
90 createBranchSaying,
9191 deleteBranch as deleteBranchAction,
9292 } from "../actions/branches.ts";
9393 import { editFiles, type EditFile } from "../actions/contents.ts";
@@ -753,7 +753,7 @@ export const NOTHING_TO_CHANGE = () =>
753753 * caller's own request being wrong.
754754 */
755755 function branchStatus(error: string): number {
756 if (error === "exists" || error === "branch_moved") return 409;
756 if (error === "exists" || error === "branch_moved" || error === "mirror_read_only") return 409;
757757 if (error === "default_branch") return 409;
758758 if (error === "unknown_branch") return 404;
759759 return 422;
@@ -1933,12 +1933,16 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo
19331933 const repo = loadRepo(ctx);
19341934 if (!repo) return notFound();
19351935 const input = await body(ctx.request);
1936 const created = await createBranch(ctx.app, ctx.auth.user, repo, {
1936 const created = await createBranchSaying(ctx.app, ctx.auth.user, repo, {
19371937 name: str(input.name),
19381938 from: str(input.from),
19391939 });
1940 if (!created.ok)
1941 return originAware(created.error, branchStatus(created.error));
1940 if (!created.ok) {
1941 const { code, words } = created.error;
1942 // The sentence every door says: where to change a read-only mirror, or the origin's reason.
1943 if (code === "mirror_read_only" || code === "origin_refused") return apiError(code, 409, words);
1944 return originAware(code, branchStatus(code));
1945 }
19421946 return json(created.value, 201);
19431947 });
19441948
test/mirror-write-copy.test.ts +10
@@ -168,6 +168,16 @@ describe("a change asked of a mirror", () => {
168168 expect(wrote.text).toContain(SAID);
169169 });
170170
171 test("the API answers 409 with the same sentence, for a branch and for the contents route", async () => {
172 const headers = { "content-type": "application/json", authorization: `Bearer ${token}` };
173 const branch = await fetch(`${homeServers.url}/api/v1/repos/gina/voxel/branches`, { method: "POST", headers, body: JSON.stringify({ name: "via-api", from: "main" }) });
174 expect(branch.status).toBe(409);
175 expect(await branch.json()).toMatchObject({ error: "mirror_read_only", message: SAID });
176 const contents = await fetch(`${homeServers.url}/api/v1/repos/gina/voxel/contents`, { method: "POST", headers, body: JSON.stringify({ branch: "main", message: "x", files: [{ path: "a.txt", content: "a\n" }] }) });
177 expect(contents.status).toBe(409);
178 expect(await contents.json()).toMatchObject({ error: "mirror_read_only", message: SAID });
179 });
180
171181 test("the actions refuse it for every door: the page's branch create says the same sentence", async () => {
172182 const mirror = getRepo(home.db, "gina", "voxel")!;
173183 expect(await createBranch(home, gina, mirror, { name: "via-action" })).toEqual({ ok: false, error: "mirror_read_only" });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs