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

Saving one repository setting never reverts another setting changed at the same time #15245

Merged opened this

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

Merged

scout/forge:scout/15035-saving-one-repository-setting-never-reve → main 1 commit fork

  • Merged. scout/15035-saving-one-repository-setting-never-reve was merged into main as b2d5e11.

Review

Sign in to review this change.
2 files changed +93 −30
src/core/repos.ts +36 −30
@@ -1549,39 +1549,45 @@ export function updateRepoSettings(
15491549 repo: Repo,
15501550 settings: Omit<RepoSettings, "archived">,
15511551 ): Repo {
1552 db.query(
1553 `UPDATE repo SET visibility = ?1, description = ?2, protect_default = ?3,
1554 default_branch = ?4, require_review = ?5, require_checks = ?7,
1555 autonomy = ?8, autonomy_direction = ?9,
1556 autonomy_slots = ?10, autonomy_by = ?11, crew_outsiders = ?12, decisions_by = ?13,
1557 plans_need_nod = ?14, planner_slots = ?15, reviewer_slots = ?16, polish_share = ?17,
1558 landing = ?18, is_template = ?19
1559 WHERE id = ?6`,
1560 ).run(
1561 settings.visibility ?? repo.visibility,
1562 settings.description ?? repo.description,
1563 (settings.protectDefault ?? repo.protectDefault) ? 1 : 0,
1564 settings.defaultBranch ?? repo.defaultBranch,
1565 (settings.requireReview ?? repo.requireReview) ? 1 : 0,
1566 repo.id,
1567 (settings.requireChecks ?? repo.requireChecks) ? 1 : 0,
1568 settings.autonomy ?? repo.autonomy,
1569 settings.autonomyDirection ?? repo.autonomyDirection,
1570 settings.autonomySlots ?? repo.autonomySlots,
1571 settings.autonomyBy === undefined ? repo.autonomyBy : settings.autonomyBy,
1572 (settings.crewOutsiders ?? repo.crewOutsiders) ? 1 : 0,
1573 settings.decisionsBy ?? repo.decisionsBy,
1574 (settings.plansNeedNod ?? repo.plansNeedNod) ? 1 : 0,
1575 settings.plannerSlots ?? repo.plannerSlots,
1576 settings.reviewerSlots ?? repo.reviewerSlots,
1577 settings.polishShare ?? repo.polishShare,
1578 settings.landing ?? repo.landing,
1579 (settings.template ?? repo.template) ? 1 : 0,
1580 );
1581 forgetListings(db);
1552 // Only the columns given are written: a snapshot `repo` read before
1553 // another save must never put that save's setting back (#15035).
1554 const sets: string[] = [];
1555 const values: (string | number | null)[] = [repo.id];
1556 for (const [key, column] of SETTING_COLUMNS) {
1557 const value = settings[key];
1558 if (value === undefined) continue;
1559 values.push(typeof value === "boolean" ? (value ? 1 : 0) : value);
1560 sets.push(`${column} = ?${values.length}`);
1561 }
1562 if (sets.length > 0) {
1563 db.query(`UPDATE repo SET ${sets.join(", ")} WHERE id = ?1`).run(...values);
1564 forgetListings(db);
1565 }
15821566 return getRepoById(db, repo.id)!;
15831567 }
15841568
1569 /** Each setting `updateRepoSettings` writes, and the column it lives in. */
1570 const SETTING_COLUMNS: readonly (readonly [keyof Omit<RepoSettings, "archived">, string])[] = [
1571 ["visibility", "visibility"],
1572 ["description", "description"],
1573 ["protectDefault", "protect_default"],
1574 ["defaultBranch", "default_branch"],
1575 ["requireReview", "require_review"],
1576 ["requireChecks", "require_checks"],
1577 ["autonomy", "autonomy"],
1578 ["autonomyDirection", "autonomy_direction"],
1579 ["autonomySlots", "autonomy_slots"],
1580 ["autonomyBy", "autonomy_by"],
1581 ["crewOutsiders", "crew_outsiders"],
1582 ["decisionsBy", "decisions_by"],
1583 ["plansNeedNod", "plans_need_nod"],
1584 ["plannerSlots", "planner_slots"],
1585 ["reviewerSlots", "reviewer_slots"],
1586 ["polishShare", "polish_share"],
1587 ["landing", "landing"],
1588 ["template", "is_template"],
1589 ];
1590
15851591 /**
15861592 * Archive (true) or wake (false) in one conditional write. True only when
15871593 * this call moved the repository: of two at once, one moves it and the
test/repo-settings-columns.test.ts added +57
@@ -0,0 +1,57 @@
1 /**
2 * Saving one repository setting writes only that setting: two saves from the
3 * same stale view, each of a different setting, both stay (#15035).
4 */
5
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createRepo } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { getRepoById, updateRepoSettings, type Repo } from "../src/core/repos.ts";
13 import { createUser } from "../src/core/users.ts";
14 import { quietLogs } from "./helpers/globals.ts";
15
16 quietLogs();
17
18 let dir = "";
19 let app: App;
20 let repo: Repo;
21
22 beforeAll(async () => {
23 dir = await mkdtemp(join(tmpdir(), "forge-settings-"));
24 app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 });
25 const u = await createUser(app.db, {
26 origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-settings-passphrase", isAdmin: true,
27 });
28 if (!u.ok) throw new Error("user");
29 const r = await createRepo(app, u.value, { name: "notes", visibility: "public" });
30 if (!r.ok) throw new Error("repo");
31 repo = r.value;
32 }, 60_000);
33
34 afterAll(async () => {
35 app.db.close();
36 await rm(dir, { recursive: true, force: true });
37 });
38
39 test("two saves of different settings from one stale snapshot both stay", () => {
40 const stale = getRepoById(app.db, repo.id)!;
41 expect(stale.requireReview).toBe(false);
42 updateRepoSettings(app.db, stale, { description: "kept" });
43 updateRepoSettings(app.db, stale, { requireReview: true });
44 updateRepoSettings(app.db, stale, { polishShare: stale.polishShare === 15 ? 20 : 15 });
45 const now = getRepoById(app.db, repo.id)!;
46 expect(now.description).toBe("kept");
47 expect(now.requireReview).toBe(true);
48 expect(now.polishShare).toBe(stale.polishShare === 15 ? 20 : 15);
49 expect(now.visibility).toBe("public");
50 });
51
52 test("a null autonomyBy is written, and an empty save changes nothing", () => {
53 updateRepoSettings(app.db, repo, { autonomyBy: null });
54 const before = getRepoById(app.db, repo.id)!;
55 expect(before.autonomyBy).toBeNull();
56 expect(updateRepoSettings(app.db, repo, {})).toEqual(before);
57 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs