Public: anyone can read this; the crew can change it.
A pull that conflicts with its base says so first: `mergeable` and `conflicts` on the pull, and never a green mark #1321
scout/forge:scout/1177-a-pull-that-conflicts-with-its-base-says → main 3 commits fork
6 files
changed +210
−12
scripts/gate.ts +1
| @@ -86,6 +86,7 @@ export const GATE: readonly string[] = [ | ||
| 86 | 86 | "test/fork-pull-approval.test.ts", |
| 87 | 87 | "test/cross-repo-pull.test.ts", |
| 88 | 88 | "test/pulls.test.ts", |
| 89 | "test/pull-conflicts.test.ts", | |
| 89 | 90 | "test/api-reader-marks.test.ts", |
| 90 | 91 | "test/app-members.test.ts", |
| 91 | 92 | "test/apps.test.ts", |
src/core/pulls.ts +42 −11
| @@ -638,18 +638,49 @@ export async function pullStatus( | ||
| 638 | 638 | reviews, |
| 639 | 639 | }; |
| 640 | 640 | } |
| 641 | const verdict = await mergeVerdict(dir, baseOid, headOid); | |
| 642 | return { baseOid, headOid, ahead, ...verdict, reviews }; | |
| 643 | } | |
| 644 | ||
| 645 | /** A merge-tree verdict never changes for a pair of commits, so it is kept | |
| 646 | * by (base, head) oid and recomputed only when either one moves. */ | |
| 647 | const VERDICTS_KEPT = 512; | |
| 648 | const verdicts = new Map< | |
| 649 | string, | |
| 650 | { mergeable: boolean; conflicts: string[] } | |
| 651 | >(); | |
| 652 | ||
| 653 | async function mergeVerdict( | |
| 654 | dir: string, | |
| 655 | baseOid: string, | |
| 656 | headOid: string, | |
| 657 | ): Promise<{ mergeable: boolean; conflicts: string[] }> { | |
| 658 | const key = `${dir}\0${baseOid}\0${headOid}`; | |
| 659 | const kept = verdicts.get(key); | |
| 660 | if (kept !== undefined) return { ...kept, conflicts: [...kept.conflicts] }; | |
| 641 | 661 | const check = await mergeable(dir, baseOid, headOid); |
| 642 | if (check.ok) | |
| 643 | return { | |
| 644 | baseOid, | |
| 645 | headOid, | |
| 646 | ahead, | |
| 647 | mergeable: true, | |
| 648 | conflicts: [], | |
| 649 | reviews, | |
| 650 | }; | |
| 651 | const conflicts = check.error.kind === "conflict" ? check.error.files : []; | |
| 652 | return { baseOid, headOid, ahead, mergeable: false, conflicts, reviews }; | |
| 662 | const verdict = check.ok | |
| 663 | ? { mergeable: true, conflicts: [] } | |
| 664 | : { | |
| 665 | mergeable: false, | |
| 666 | conflicts: check.error.kind === "conflict" ? check.error.files : [], | |
| 667 | }; | |
| 668 | // A failed spawn (timeout, unsafe oid) is not a fact about the commits. | |
| 669 | if (check.ok || check.error.kind === "conflict") { | |
| 670 | if (verdicts.size >= VERDICTS_KEPT) | |
| 671 | verdicts.delete(verdicts.keys().next().value as string); | |
| 672 | verdicts.set(key, verdict); | |
| 673 | } | |
| 674 | return verdict; | |
| 675 | } | |
| 676 | ||
| 677 | /** The files this pull conflicts with its base's current head in; empty when | |
| 678 | * it merges cleanly or has nothing left to apply. */ | |
| 679 | export async function pullConflicts( | |
| 680 | dir: string, | |
| 681 | pull: Pull, | |
| 682 | ): Promise<string[]> { | |
| 683 | return (await pullStatus(dir, pull)).conflicts; | |
| 653 | 684 | } |
| 654 | 685 | |
| 655 | 686 | /** |
src/http/api.ts +2
| @@ -2751,6 +2751,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2751 | 2751 | const hold = getHold(ctx.app.db, found.pull.id); |
| 2752 | 2752 | return json({ |
| 2753 | 2753 | ...withLabels(ctx, found.pull), |
| 2754 | mergeable: status.mergeable, | |
| 2755 | conflicts: status.conflicts, | |
| 2754 | 2756 | status, |
| 2755 | 2757 | diff, |
| 2756 | 2758 | hold: hold === undefined ? null : holdJson(hold), |
src/web/pages/pull.ts +9 −1
| @@ -32,6 +32,7 @@ import { | ||
| 32 | 32 | type PullStatus, |
| 33 | 33 | } from "../../core/pulls.ts"; |
| 34 | 34 | import { pullMayRun } from "../../core/pull_trust.ts"; |
| 35 | import { conflictsPhrase } from "../studio/words.ts"; | |
| 35 | 36 | import { checkSummary, type CheckSummary } from "../../core/runs.ts"; |
| 36 | 37 | import { |
| 37 | 38 | anchorKey, |
| @@ -198,16 +199,22 @@ const WAITING_WORDS = | ||
| 198 | 199 | * a repository with neither has no checks, and should not grow furniture |
| 199 | 200 | * for them. A run is named so the reader can go and look at its log. |
| 200 | 201 | */ |
| 201 | function checksLine( | |
| 202 | export function checksLine( | |
| 202 | 203 | base: string, |
| 203 | 204 | check: CheckSummary, |
| 204 | 205 | required: boolean, |
| 205 | 206 | isApp: boolean, |
| 206 | 207 | /** The head is a reader's and no writer has approved it yet. */ |
| 207 | 208 | waiting = false, |
| 209 | /** Files the head conflicts with its base in: never a green mark then. */ | |
| 210 | conflicts = 0, | |
| 208 | 211 | /** `tierLine` for a repository whose check has a fast tier; else empty. */ |
| 209 | 212 | tier = "", |
| 210 | 213 | ): Html { |
| 214 | if (conflicts > 0) | |
| 215 | return html`<p class="checks-line"> | |
| 216 | <span class="bad">${icon("alert")}${conflictsPhrase(conflicts)}</span> | |
| 217 | </p>`; | |
| 211 | 218 | if (!isApp && !required && check.state === "none" && !waiting) return html``; |
| 212 | 219 | const run = check.run; |
| 213 | 220 | const link = |
| @@ -309,6 +316,7 @@ function mergeBox( | ||
| 309 | 316 | repo.requireChecks, |
| 310 | 317 | isApp, |
| 311 | 318 | !pullMayRun(ctx.app.db, repo, pull, status.headOid), |
| 319 | status.conflicts.length, | |
| 312 | 320 | tier, |
| 313 | 321 | ); |
| 314 | 322 | const reopen = html`<form method="post" action="${thread}/state"> |
src/web/studio/words.ts +4
| @@ -150,6 +150,10 @@ function detailNote(detail: CheckDetail): string { | ||
| 150 | 150 | return `${detail.passed} passed, ${detail.failed} failed: ${names}`; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | export function conflictsPhrase(count: number): string { | |
| 154 | return `Conflicts in ${count} file${count === 1 ? "" : "s"}`; | |
| 155 | } | |
| 156 | ||
| 153 | 157 | /** A single open pull's check, in the studio's own words — never dev's |
| 154 | 158 | * "Checks passed/failed": the same five words as the rest of the page. */ |
| 155 | 159 | function checkRunWord(check: CheckSummary): { word: Word; note: string } { |
test/pull-conflicts.test.ts added +152
| @@ -0,0 +1,152 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 7 | import { checksLine } from "../src/web/pages/pull.ts"; | |
| 8 | import { conflictsPhrase } from "../src/web/studio/words.ts"; | |
| 9 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 10 | ||
| 11 | const gitEnv = gitClientEnv(); | |
| 12 | ||
| 13 | describe("a pull that conflicts with its base says so first", () => { | |
| 14 | let dataDir: string; | |
| 15 | let work: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let base: string; | |
| 19 | let cookie = ""; | |
| 20 | let token = ""; | |
| 21 | let dir: string; | |
| 22 | ||
| 23 | async function git(args: string[]): Promise<void> { | |
| 24 | const p = Bun.spawn(["git", ...args], { | |
| 25 | cwd: dir, | |
| 26 | env: gitEnv, | |
| 27 | stdout: "ignore", | |
| 28 | stderr: "pipe", | |
| 29 | }); | |
| 30 | const [err, code] = await Promise.all([ | |
| 31 | new Response(p.stderr).text(), | |
| 32 | p.exited, | |
| 33 | ]); | |
| 34 | if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`); | |
| 35 | } | |
| 36 | const api = async (path: string) => | |
| 37 | (await ( | |
| 38 | await fetch(`${base}/api/v1/repos/dev/app${path}`, { | |
| 39 | headers: { authorization: `Bearer ${token}` }, | |
| 40 | }) | |
| 41 | ).json()) as { | |
| 42 | mergeable: boolean; | |
| 43 | conflicts: string[]; | |
| 44 | status: { mergeable: boolean }; | |
| 45 | }; | |
| 46 | ||
| 47 | beforeAll(async () => { | |
| 48 | dataDir = await mkdtemp(join(tmpdir(), "forge-pc-")); | |
| 49 | work = await mkdtemp(join(tmpdir(), "forge-pc-work-")); | |
| 50 | app = createApp({ dataDir, port: 0 }); | |
| 51 | servers = await serve(app); | |
| 52 | base = servers.url; | |
| 53 | const signup = await fetch(`${base}/signup`, { | |
| 54 | method: "POST", | |
| 55 | body: new URLSearchParams({ | |
| 56 | username: "dev", | |
| 57 | email: "[email protected]", | |
| 58 | password: "pull-request-pass", | |
| 59 | }), | |
| 60 | redirect: "manual", | |
| 61 | }); | |
| 62 | cookie = (signup.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 63 | token = await fetch(`${base}/api/v1/tokens`, { | |
| 64 | method: "POST", | |
| 65 | headers: { "content-type": "application/json" }, | |
| 66 | body: JSON.stringify({ | |
| 67 | adminConfirm: true, | |
| 68 | username: "dev", | |
| 69 | password: "pull-request-pass", | |
| 70 | name: "cli", | |
| 71 | scope: "rw", | |
| 72 | }), | |
| 73 | }) | |
| 74 | .then((r) => r.json()) | |
| 75 | .then((j) => (j as { token: string }).token); | |
| 76 | await fetch(`${base}/api/v1/repos`, { | |
| 77 | method: "POST", | |
| 78 | headers: { | |
| 79 | authorization: `Bearer ${token}`, | |
| 80 | "content-type": "application/json", | |
| 81 | }, | |
| 82 | body: JSON.stringify({ name: "app", visibility: "public" }), | |
| 83 | }); | |
| 84 | const remote = `${base.replace("http://", `http://dev:${token}@`)}/dev/app.git`; | |
| 85 | dir = work; | |
| 86 | await git(["clone", remote, "app"]); | |
| 87 | dir = join(work, "app"); | |
| 88 | await Bun.write(join(dir, "code.txt"), "alpha\nbeta\ngamma\n"); | |
| 89 | await git(["add", "."]); | |
| 90 | await git(["commit", "-m", "base"]); | |
| 91 | await git(["push", "origin", "main"]); | |
| 92 | }); | |
| 93 | ||
| 94 | afterAll(async () => { | |
| 95 | servers.stop(); | |
| 96 | app.close(); | |
| 97 | await rm(dataDir, { recursive: true, force: true }); | |
| 98 | await rm(work, { recursive: true, force: true }); | |
| 99 | }); | |
| 100 | ||
| 101 | test("conflicted in the API and on the page; resolving clears both", async () => { | |
| 102 | await git(["checkout", "-b", "side"]); | |
| 103 | await Bun.write(join(dir, "code.txt"), "alpha\nbeta\nGAMMA-side\n"); | |
| 104 | await git(["commit", "-am", "side"]); | |
| 105 | await git(["push", "origin", "side"]); | |
| 106 | await git(["checkout", "main"]); | |
| 107 | await Bun.write(join(dir, "code.txt"), "alpha\nbeta\nGAMMA-main\n"); | |
| 108 | await git(["commit", "-am", "main"]); | |
| 109 | await git(["push", "origin", "main"]); | |
| 110 | ||
| 111 | const created = await fetch(`${base}/dev/app/pulls/new`, { | |
| 112 | method: "POST", | |
| 113 | headers: { cookie }, | |
| 114 | body: new URLSearchParams({ | |
| 115 | base: "main", | |
| 116 | head: "side", | |
| 117 | title: "Side", | |
| 118 | body: "", | |
| 119 | }), | |
| 120 | redirect: "manual", | |
| 121 | }); | |
| 122 | const loc = created.headers.get("location")!; | |
| 123 | ||
| 124 | const sick = await api("/pulls/1"); | |
| 125 | expect(sick.mergeable).toBe(false); | |
| 126 | expect(sick.conflicts).toEqual(["code.txt"]); | |
| 127 | let page = await (await fetch(`${base}${loc}/files`)).text(); | |
| 128 | expect(page).toContain("Conflicts in 1 file"); | |
| 129 | expect(page).not.toContain("Checks passed"); | |
| 130 | ||
| 131 | // Resolve: merge main into the branch and push. | |
| 132 | await git(["checkout", "side"]); | |
| 133 | await git(["merge", "main", "-X", "theirs", "-m", "resolve"]); | |
| 134 | await git(["push", "origin", "side"]); | |
| 135 | const well = await api("/pulls/1"); | |
| 136 | expect(well.mergeable).toBe(true); | |
| 137 | expect(well.conflicts).toEqual([]); | |
| 138 | page = await (await fetch(`${base}${loc}/files`)).text(); | |
| 139 | expect(page).not.toContain("Conflicts in 1 file"); | |
| 140 | }); | |
| 141 | ||
| 142 | test("a conflict outranks a passing check on every line that shows one", () => { | |
| 143 | const check = { state: "ok", run: null } as never; | |
| 144 | const line = String(checksLine("/dev/app", check, true, true, false, 2)); | |
| 145 | expect(line).toContain("Conflicts in 2 files"); | |
| 146 | expect(line).not.toContain("Checks passed"); | |
| 147 | expect(String(checksLine("/dev/app", check, true, true))).toContain( | |
| 148 | "Checks passed", | |
| 149 | ); | |
| 150 | expect(conflictsPhrase(1)).toBe("Conflicts in 1 file"); | |
| 151 | }); | |
| 152 | }); |
new above
offline