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

A push to main alone rebuilds combined/N for every open kept-fork pull into it #10023

Parked opened this
Closed without merging

mason/forge:mason/9994-a-push-to-main-alone-rebuilds-combined-n → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +234 −4
src/core/recombine.ts added +74
@@ -0,0 +1,74 @@
1 /**
2 * Recombining after main moves (#9994): a kept fork's `combined/N` is main
3 * merged into the author's head, so a push to main alone makes every one
4 * stale. One pass at a time per repository rebuilds the branch of each open
5 * pull from a kept fork into the default branch. Pushes that arrive during
6 * a pass coalesce into one more pass, which reads the tip as it stands then.
7 */
8
9 import type { App } from "../app.ts";
10 import { combine } from "../git/combined.ts";
11 import { repoDir } from "../git/repos.ts";
12 import { resolveOid } from "../git/merge.ts";
13 import { fromKeptFork } from "./landing_kind.ts";
14 import { listOpenPulls } from "./pulls.ts";
15 import type { Repo } from "./repos.ts";
16
17 interface Lane {
18 /** The running pass; undefined when idle. */
19 running: Promise<void> | undefined;
20 /** A push arrived during the running pass. */
21 again: boolean;
22 }
23
24 const lanes = new Map<string, Lane>();
25 /** Passes started per repository, for tests and the log. */
26 const passes = new Map<string, number>();
27
28 export const recombinePasses = (repoId: string): number => passes.get(repoId) ?? 0;
29
30 /** One pass: every open kept-fork pull into the default branch, combined with main as it is now. */
31 async function pass(app: App, repo: Repo): Promise<void> {
32 passes.set(repo.id, recombinePasses(repo.id) + 1);
33 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
34 for (const pull of listOpenPulls(app.db, repo.id)) {
35 if (app.closing.signal.aborted) return;
36 if (pull.baseBranch !== repo.defaultBranch || pull.sealed || !fromKeptFork(app.db, pull)) continue;
37 try {
38 const baseOid = await resolveOid(dir, `refs/heads/${repo.defaultBranch}`);
39 const headOid = (await resolveOid(dir, `refs/pull/${pull.number}/head`)) ?? pull.headOid;
40 if (baseOid === undefined || !headOid) continue;
41 await combine(dir, { number: pull.number, baseOid, headOid });
42 } catch (error) {
43 app.log.warn("could not recombine a pull", { repo: `${repo.ownerName}/${repo.name}`, number: pull.number, error: String(error) });
44 }
45 }
46 }
47
48 /**
49 * Ask for a pass. Starts one when idle; during one, marks that exactly one
50 * more is owed. Resolves when the passes it joined are done.
51 */
52 export function recombine(app: App, repo: Repo): Promise<void> {
53 let lane = lanes.get(repo.id);
54 if (lane === undefined) {
55 lane = { running: undefined, again: false };
56 lanes.set(repo.id, lane);
57 }
58 if (lane.running !== undefined) {
59 lane.again = true;
60 return lane.running;
61 }
62 const mine = lane;
63 mine.running = (async () => {
64 try {
65 do {
66 mine.again = false;
67 await pass(app, repo);
68 } while (mine.again);
69 } finally {
70 mine.running = undefined;
71 }
72 })();
73 return mine.running;
74 }
src/http/server.ts +14
@@ -68,6 +68,7 @@ import {
6868 getRepoById,
6969 } from "../core/repos.ts";
7070 import { trackPullHead } from "../core/pulls.ts";
71 import { recombine } from "../core/recombine.ts";
7172 import { createRepo, repositoryLimitMessage, resolveOwner } from "../actions/repos.ts";
7273 import { sessionOf } from "../core/sessions.ts";
7374 import { scopeAllows, verifyToken } from "../core/tokens.ts";
@@ -2052,6 +2053,19 @@ async function handleHook(app: App, request: Request): Promise<Response> {
20522053 error: String(error),
20532054 }),
20542055 );
2056 // Main moved: each kept fork's combined branch is rebuilt on it, off the
2057 // hook's answer (`core/recombine.ts`, #9994).
2058 if (
2059 input.updates.some(
2060 (u) => u.ref === `refs/heads/${repo.defaultBranch}` && !ZERO_OID.test(u.newOid),
2061 )
2062 )
2063 void recombine(app, repo).catch((error) =>
2064 app.log.warn("could not recombine", {
2065 repo: `${repo.ownerName}/${repo.name}`,
2066 error: String(error),
2067 }),
2068 );
20552069 // Every push is scanned for secrets and advisories (`checks/scan.ts`,
20562070 // #9593). Off the hook's answer too: a finding is recorded, not refused.
20572071 void queueScanPush(app, repo, input.updates).catch((error) =>
test/combined-fork.test.ts +11 −4
@@ -181,14 +181,21 @@ describe("a conflicting fork pull is combined on our side (#9012)", () => {
181181 expect((await git(["log", "-1", "--format=%P", retip], bare)).split(" ")).toEqual([bobNext, aliceMain]);
182182 expect(await previewCommit(bare, getPull(app.db, repo.id, number)!)).toBe(retip);
183183
184 // Main moves on its own: the card says the preview is stale, and on what it was built.
184 // Main moves on its own: the push recombines (#9994), so the card is
185 // never left saying the preview was built on a main that has moved.
185186 await Bun.write(join(a, "four.txt"), "four\n");
186187 await git(["add", "."], a);
187188 await git(["commit", "-m", "alice's four"], a);
188189 await git(["push", "origin", "main"], a);
189 const stale = await (await call(`/alice/app/pulls/${number}`, alice)).text();
190 expect(stale).toContain(`Stale: main has moved since ${aliceMain.slice(0, 10)}`);
191 expect(await screenText()).toContain("Combined with main · stale");
190 const aliceFour = await git(["rev-parse", "HEAD"], a);
191 for (let i = 0; i < 100; i++) {
192 const parents = await git(["log", "-1", "--format=%P", `refs/heads/combined/${number}`], bare);
193 if (parents.includes(aliceFour)) break;
194 await Bun.sleep(50);
195 }
196 const fresh = await (await call(`/alice/app/pulls/${number}`, alice)).text();
197 expect(fresh).not.toContain("Stale: main has moved");
198 expect(await screenText()).not.toContain("Combined with main · stale");
192199
193200 // The merge lands the combined branch: his head is an ancestor of main.
194201 const merged = await call(`/api/v1/repos/alice/app/pulls/${number}/merge`, alice, { strategy: "merge" });
test/combined-recombine.test.ts added +135
@@ -0,0 +1,135 @@
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 { gitClientEnv } from "./helpers/git-client.ts";
8 import { proveAll } from "./helpers/verified.ts";
9 import { promote } from "./helpers/members.ts";
10 import { getRepo } from "../src/core/repos.ts";
11 import { recombinePasses } from "../src/core/recombine.ts";
12 import { repoDir } from "../src/git/repos.ts";
13 import { tokenFor } from "./helpers/tokens.ts";
14
15 const gitEnv = gitClientEnv({
16 GIT_AUTHOR_NAME: "Jay Moran",
17 GIT_AUTHOR_EMAIL: "[email protected]",
18 GIT_COMMITTER_NAME: "Jay Moran",
19 GIT_COMMITTER_EMAIL: "[email protected]",
20 });
21
22
23 describe("a push to main alone recombines every kept fork's pull (#9994)", () => {
24 let dataDir: string;
25 let work: string;
26 let app: App;
27 let servers: Servers;
28 let base: string;
29 let alice = "";
30 let bob = "";
31 let number = 0;
32
33 async function git(args: string[], cwd: string): Promise<string> {
34 const p = Bun.spawn(["git", ...args], { cwd, env: gitEnv, stdout: "pipe", stderr: "pipe" });
35 const [out, err, code] = await Promise.all([
36 new Response(p.stdout).text(),
37 new Response(p.stderr).text(),
38 p.exited,
39 ]);
40 if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`);
41 return out.trim();
42 }
43 const call = (path: string, token: string, body?: unknown): Promise<Response> =>
44 fetch(`${base}${path}`, {
45 method: body === undefined ? "GET" : "POST",
46 headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
47 ...(body === undefined ? {} : { body: JSON.stringify(body) }),
48 });
49 const authed = (user: string, token: string) =>
50 `${base.replace("http://", `http://${user}:${token}@`)}`;
51 async function signup(username: string): Promise<string> {
52 await fetch(`${base}/signup`, {
53 method: "POST",
54 body: new URLSearchParams({ username, email: `${username}@f.dev`, password: "kept-fork-pass" }),
55 redirect: "manual",
56 });
57 return tokenFor(app, username, "admin", "k");
58 }
59 const bare = () => repoDir(app.reposRoot, "alice", "app");
60 const parents = async (): Promise<string[]> =>
61 (await git(["log", "-1", "--format=%P", `refs/heads/combined/${number}`], bare())).split(" ");
62 async function until(done: () => Promise<boolean>): Promise<void> {
63 for (let i = 0; i < 100; i++) {
64 if (await done()) return;
65 await Bun.sleep(50);
66 }
67 }
68
69 beforeAll(async () => {
70 dataDir = await mkdtemp(join(tmpdir(), "forge-rc-"));
71 work = await mkdtemp(join(tmpdir(), "forge-rc-work-"));
72 app = createApp({ dataDir, port: 0 });
73 servers = await serve(app);
74 base = servers.url;
75 alice = await signup("alice");
76 bob = await signup("bob");
77 proveAll(app);
78 promote(app.db);
79 await call("/api/v1/repos", alice, { name: "app", visibility: "public" });
80 await git(["clone", `${authed("alice", alice)}/alice/app.git`, "a"], work);
81 await Bun.write(join(work, "a/one.txt"), "one\n");
82 await git(["add", "."], join(work, "a"));
83 await git(["commit", "-m", "seed"], join(work, "a"));
84 await git(["push", "origin", "main"], join(work, "a"));
85 await call("/api/v1/repos/alice/app/forks", bob, {});
86 await git(["clone", `${authed("bob", bob)}/bob/app.git`, "b"], work);
87 await Bun.write(join(work, "b/bob.txt"), "bob\n");
88 await git(["add", "."], join(work, "b"));
89 await git(["commit", "-m", "bob's file"], join(work, "b"));
90 await git(["push", "origin", "main"], join(work, "b"));
91 const opened = await call("/api/v1/repos/alice/app/pulls", bob, {
92 title: "Bob's work",
93 base: "main",
94 head: "main",
95 headRepo: "bob/app",
96 });
97 expect(opened.status).toBe(201);
98 number = ((await opened.json()) as { number: number }).number;
99 });
100
101 afterAll(async () => {
102 servers.stop();
103 app.close();
104 await rm(dataDir, { recursive: true, force: true });
105 await rm(work, { recursive: true, force: true });
106 });
107
108 async function pushMain(file: string): Promise<string> {
109 const a = join(work, "a");
110 await Bun.write(join(a, file), `${file}\n`);
111 await git(["add", "."], a);
112 await git(["commit", "-m", `alice adds ${file}`], a);
113 await git(["push", "origin", "main"], a);
114 return git(["rev-parse", "HEAD"], a);
115 }
116
117 test("main moving alone puts the new main in combined/N", async () => {
118 const main = await pushMain("two.txt");
119 await until(async () => (await parents()).includes(main));
120 expect(await parents()).toContain(main);
121 expect(await git(["show", `refs/heads/combined/${number}:two.txt`], bare())).toBe("two.txt");
122 }, 30_000);
123
124 test("two quick pushes cost at most two passes", async () => {
125 const repo = getRepo(app.db, "alice", "app")!;
126 const before = recombinePasses(repo.id);
127 await pushMain("three.txt");
128 const main = await pushMain("four.txt");
129 await until(async () => (await parents()).includes(main));
130 expect(await parents()).toContain(main);
131 const ran = recombinePasses(repo.id) - before;
132 expect(ran).toBeGreaterThanOrEqual(1);
133 expect(ran).toBeLessThanOrEqual(2);
134 }, 30_000);
135 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs