Public: anyone can read this; the crew can change it.
Pointing a follow at another upstream clears the old upstream's pending proposal and ahead count #15056
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15043-pointing-a-follow-at-another-upstream-cl → main 3 commits fork
3 files
changed +132
−6
src/actions/upstream_follow.ts +27 −2
| @@ -41,6 +41,7 @@ import { | ||
| 41 | 41 | getFollow, |
| 42 | 42 | followAad, |
| 43 | 43 | listFollowing, |
| 44 | inheritsPending, | |
| 44 | 45 | noteCheck, |
| 45 | 46 | putFollow, |
| 46 | 47 | severFollow, |
| @@ -85,7 +86,7 @@ import { basicHeader, gitAuthEnv, redact, secretsIn } from "../origins/credentia | ||
| 85 | 86 | import { vettedEnv } from "../origins/git-mirror.ts"; |
| 86 | 87 | import { forkFollowMode } from "../core/fork_follow.ts"; |
| 87 | 88 | import { settleOntoFollowed } from "../core/upstream_settle.ts"; |
| 88 | import { openIssue } from "./issues.ts"; | |
| 89 | import { openIssue, setIssueState as closeIssue } from "./issues.ts"; | |
| 89 | 90 | import { migrateUrl } from "./migrate.ts"; |
| 90 | 91 | import { openPull } from "./pulls.ts"; |
| 91 | 92 | |
| @@ -467,6 +468,11 @@ export interface Target { | ||
| 467 | 468 | readonly credentialEnc?: string; |
| 468 | 469 | readonly via?: string; |
| 469 | 470 | readonly repoRowId?: string; |
| 471 | /** | |
| 472 | * `pendingId` was opened for the upstream this follow pointed at before it moved | |
| 473 | * (`inheritsPending`). | |
| 474 | */ | |
| 475 | readonly inherited?: boolean; | |
| 470 | 476 | } |
| 471 | 477 | |
| 472 | 478 | /** What a check of a repository's own upstream reads and writes. */ |
| @@ -477,6 +483,7 @@ export const targetOf = (app: App, repo: Repo, follow: UpstreamFollow): Target = | ||
| 477 | 483 | proposal: proposalBranch, |
| 478 | 484 | note: (r) => noteCheck(app.db, repo.id, r), |
| 479 | 485 | stamp: sourceOfUrl(follow.url), |
| 486 | inherited: inheritsPending(follow), | |
| 480 | 487 | }); |
| 481 | 488 | |
| 482 | 489 | async function checkOnce(app: App, repo: Repo): Promise<Result<Checked, CheckError>> { |
| @@ -640,7 +647,23 @@ async function checkTarget(app: App, repo: Repo, follow: Target): Promise<Result | ||
| 640 | 647 | // What this repository holds that the upstream does not: told on the page, never acted on. |
| 641 | 648 | const ahead = await aheadCount(dir, staged, main); |
| 642 | 649 | const noted = follow.note; |
| 643 | follow = { ...follow, note: (r) => noted({ ahead, ...r }) }; | |
| 650 | // A proposal opened for the upstream this follow pointed at before it moved is carried to the new | |
| 651 | // upstream when this check proposes, and closed when it does not: never left pending beside a | |
| 652 | // second one. | |
| 653 | let carried = false; | |
| 654 | const letGo = (): { pendingId?: null } => { | |
| 655 | if (follow.inherited !== true || follow.pendingId === null || carried) return {}; | |
| 656 | const was = getIssueById(app.db, follow.pendingId); | |
| 657 | if (was !== undefined && was.kind === "pull" && was.state === "open") { | |
| 658 | const closed = closeIssue(app, by, repo, was, "closed", { system: true }); | |
| 659 | if (!closed.ok) app.log.error("old upstream's proposal not closed", { repo: repo.id, pull: was.id }); | |
| 660 | } | |
| 661 | return { pendingId: null }; | |
| 662 | }; | |
| 663 | follow = { | |
| 664 | ...follow, | |
| 665 | note: (r) => noted({ ahead, ...r, ...(r.tip !== undefined && r.pendingId === undefined ? letGo() : {}) }), | |
| 666 | }; | |
| 644 | 667 | if (behind === 0) { |
| 645 | 668 | if (follow.sourceId === undefined) settleConflict(app, repo.id); |
| 646 | 669 | follow.note({ at, tip: staged, behind, error: "" }); |
| @@ -712,8 +735,10 @@ async function checkTarget(app: App, repo: Repo, follow: Target): Promise<Result | ||
| 712 | 735 | if (known !== undefined) { |
| 713 | 736 | // This tip was proposed already, whatever came of it: nothing new. |
| 714 | 737 | number = known.number; |
| 738 | carried = known.id === follow.pendingId; | |
| 715 | 739 | } else if (open) { |
| 716 | 740 | // The upstream moved on: the open pull follows it instead of a second opening. |
| 741 | carried = true; | |
| 717 | 742 | retargetPull(app.db, pending.id, name, staged); |
| 718 | 743 | came(pending.id); |
| 719 | 744 | await writeRef(dir, `refs/pull/${pending.number}/head`, staged); |
src/core/upstream_follow.ts +11 −2
| @@ -135,7 +135,9 @@ export function listFollowing(db: Database): UpstreamFollow[] { | ||
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * Set where a repository follows and whether it does. A new URL or branch |
| 138 | * forgets the last reading — it was of another upstream. | |
| 138 | * forgets the last reading — it was of another upstream. The open proposal | |
| 139 | * (`pendingId`) stays so the next check can carry it to the new upstream; a | |
| 140 | * check that has nothing to carry closes it (`inheritsPending`). | |
| 139 | 141 | */ |
| 140 | 142 | export function putFollow( |
| 141 | 143 | db: Database, |
| @@ -191,12 +193,19 @@ export function putFollow( | ||
| 191 | 193 | if (moved) { |
| 192 | 194 | db.query("DELETE FROM repo_upstream_check WHERE repo_id = ?1").run(input.repoId); |
| 193 | 195 | db.query( |
| 194 | "UPDATE repo_upstream SET checked_at = NULL, tip = '', behind = 0, error = '' WHERE repo_id = ?1", | |
| 196 | "UPDATE repo_upstream SET checked_at = NULL, tip = '', behind = 0, ahead = 0, error = '' WHERE repo_id = ?1", | |
| 195 | 197 | ).run(input.repoId); |
| 196 | 198 | } |
| 197 | 199 | return getFollow(db, input.repoId)!; |
| 198 | 200 | } |
| 199 | 201 | |
| 202 | /** | |
| 203 | * Whether the follow's pending proposal was opened for an upstream it no longer follows: a move | |
| 204 | * clears the tip, and only a check that read the new upstream writes one again (a failed check | |
| 205 | * leaves it). | |
| 206 | */ | |
| 207 | export const inheritsPending = (f: UpstreamFollow): boolean => f.pendingId !== null && f.tip === ""; | |
| 208 | ||
| 200 | 209 | /** Choose what the forge does with new upstream commits from now on; false when the repository follows nothing. */ |
| 201 | 210 | export function setFollowTaking(db: Database, repoId: string, mode: FollowTaking): boolean { |
| 202 | 211 | return db.query("UPDATE repo_upstream SET mode = ?2 WHERE repo_id = ?1").run(repoId, mode).changes > 0; |
test/upstream_follow.test.ts +94 −2
| @@ -126,15 +126,25 @@ describe("the follow row", () => { | ||
| 126 | 126 | ]); |
| 127 | 127 | expect(listFollowing(app.db)).toEqual([]); |
| 128 | 128 | put("https://example.com/a/b.git", true); |
| 129 | noteCheck(app.db, repo.id, { at: "2026-10-21T00:00:00.000Z", tip: "abc", behind: 3, error: "" }); | |
| 130 | expect(getFollow(app.db, repo.id)).toMatchObject({ enabled: true, tip: "abc", behind: 3 }); | |
| 129 | noteCheck(app.db, repo.id, { at: "2026-10-21T00:00:00.000Z", tip: "abc", behind: 3, error: "", ahead: 2 }); | |
| 130 | expect(getFollow(app.db, repo.id)).toMatchObject({ enabled: true, tip: "abc", behind: 3, ahead: 2 }); | |
| 131 | 131 | expect(listFollowing(app.db).map((f) => f.repoId)).toEqual([repo.id]); |
| 132 | 132 | expect(put("https://example.com/c/d.git", true)).toMatchObject({ |
| 133 | 133 | checkedAt: null, |
| 134 | 134 | tip: "", |
| 135 | 135 | behind: 0, |
| 136 | ahead: 0, | |
| 136 | 137 | }); |
| 137 | 138 | }); |
| 139 | ||
| 140 | test("a new branch forgets the ahead count too", () => { | |
| 141 | const put = (branch: string) => | |
| 142 | putFollow(app.db, { repoId: repo.id, url: "https://example.com/c/d.git", branch, enabled: true, byId: alice.id, originId: "" }); | |
| 143 | put("main"); | |
| 144 | noteCheck(app.db, repo.id, { at: "2026-10-21T00:00:00.000Z", tip: "abc", behind: 1, error: "", ahead: 4 }); | |
| 145 | expect(put("main").ahead).toBe(4); | |
| 146 | expect(put("next")).toMatchObject({ tip: "", behind: 0, ahead: 0 }); | |
| 147 | }); | |
| 138 | 148 | }); |
| 139 | 149 | |
| 140 | 150 | describe("who hears of it", () => { |
| @@ -404,6 +414,88 @@ describe("checking an upstream (layer 2)", () => { | ||
| 404 | 414 | } |
| 405 | 415 | }); |
| 406 | 416 | |
| 417 | const openPulls = (r: Repo) => listPulls(app.db, r.id, { state: "open" }); | |
| 418 | ||
| 419 | test("a move to an upstream with new commits leaves one open proposal, for the new upstream", async () => { | |
| 420 | const t = await staged("moved-on", "base\n", "new\n"); | |
| 421 | try { | |
| 422 | const first = ok(await checkUpstream(app, t.r)); | |
| 423 | const blob = await t.git(["hash-object", "-w", "--stdin"], "elsewhere\n"); | |
| 424 | const tree = await t.git(["mktree"], `100644 blob ${blob}\tf.txt\n`); | |
| 425 | const base = await t.git(["rev-parse", `${t.up}^`]); | |
| 426 | const other = await t.git(["commit-tree", tree, "-p", base, "-m", "elsewhere"]); | |
| 427 | await t.git(["update-ref", "refs/upstream-follow/next", other]); | |
| 428 | const srv = upstream(() => `${other}\trefs/heads/next\n`); | |
| 429 | putFollow(app.db, { repoId: t.r.id, url: srv.url, branch: "next", enabled: true, byId: alice.id, originId: "" }); | |
| 430 | try { | |
| 431 | const second = ok(await checkUpstream(app, t.r)); | |
| 432 | expect(second.outcome).toBe("proposed"); | |
| 433 | const open = openPulls(t.r); | |
| 434 | expect(open.map((p) => p.number)).toEqual([first.pull!]); | |
| 435 | expect(open[0]!.headOid).toBe(other); | |
| 436 | expect(open[0]!.body).toContain(srv.url); | |
| 437 | expect(getFollow(app.db, t.r.id)!.pendingId).toBe(open[0]!.id); | |
| 438 | } finally { | |
| 439 | srv.server.stop(true); | |
| 440 | } | |
| 441 | } finally { | |
| 442 | t.srv.server.stop(true); | |
| 443 | } | |
| 444 | }); | |
| 445 | ||
| 446 | test("a move to an upstream with nothing new closes the old upstream's proposal, even after a failed check", async () => { | |
| 447 | const t = await staged("moved-level", "base\n", "new\n"); | |
| 448 | try { | |
| 449 | const first = ok(await checkUpstream(app, t.r)); | |
| 450 | const old = getFollow(app.db, t.r.id)!.pendingId!; | |
| 451 | const level = await t.git(["rev-parse", `refs/heads/${t.r.defaultBranch}`]); | |
| 452 | // First an upstream that cannot be read: the old proposal waits for a reading. | |
| 453 | const dead = upstream(() => ""); | |
| 454 | putFollow(app.db, { repoId: t.r.id, url: dead.url, branch: "level", enabled: true, byId: alice.id, originId: "" }); | |
| 455 | try { | |
| 456 | expect((await checkUpstream(app, t.r)).ok).toBe(false); | |
| 457 | } finally { | |
| 458 | dead.server.stop(true); | |
| 459 | } | |
| 460 | expect(getFollow(app.db, t.r.id)!.pendingId).toBe(old); | |
| 461 | await t.git(["update-ref", "refs/upstream-follow/level", level]); | |
| 462 | const srv = upstream(() => `${level}\trefs/heads/level\n`); | |
| 463 | putFollow(app.db, { repoId: t.r.id, url: srv.url, branch: "level", enabled: true, byId: alice.id, originId: "" }); | |
| 464 | try { | |
| 465 | const second = ok(await checkUpstream(app, t.r)); | |
| 466 | expect([second.behind, second.outcome]).toEqual([0, "none"]); | |
| 467 | expect(openPulls(t.r)).toEqual([]); | |
| 468 | expect(getPull(app.db, t.r.id, first.pull!)!.state).toBe("closed"); | |
| 469 | expect(getFollow(app.db, t.r.id)!.pendingId).toBeNull(); | |
| 470 | } finally { | |
| 471 | srv.server.stop(true); | |
| 472 | } | |
| 473 | } finally { | |
| 474 | t.srv.server.stop(true); | |
| 475 | } | |
| 476 | }); | |
| 477 | ||
| 478 | test("a check that stays on the same upstream leaves its open proposal alone", async () => { | |
| 479 | const t = await staged("stays", "base\n", "new\n"); | |
| 480 | try { | |
| 481 | const first = ok(await checkUpstream(app, t.r)); | |
| 482 | const level = await t.git(["rev-parse", `refs/heads/${t.r.defaultBranch}`]); | |
| 483 | await t.git(["update-ref", "refs/upstream-follow/main", level]); | |
| 484 | t.srv.server.stop(true); | |
| 485 | const srv = upstream(() => `${level}\trefs/heads/main\n`); | |
| 486 | // The same address, so the row keeps its reading and nothing is inherited. | |
| 487 | app.db.query("UPDATE repo_upstream SET url = ?2 WHERE repo_id = ?1").run(t.r.id, srv.url); | |
| 488 | try { | |
| 489 | expect(ok(await checkUpstream(app, t.r)).outcome).toBe("none"); | |
| 490 | expect(openPulls(t.r).map((p) => p.number)).toEqual([first.pull!]); | |
| 491 | } finally { | |
| 492 | srv.server.stop(true); | |
| 493 | } | |
| 494 | } finally { | |
| 495 | t.srv.server.stop(true); | |
| 496 | } | |
| 497 | }); | |
| 498 | ||
| 407 | 499 | test("a closed proposal for the same tip is not reopened", async () => { |
| 408 | 500 | const t = await staged("closed", "base\n", "new\n"); |
| 409 | 501 | try { |
new above
offline