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

The offers a fork has sent upstream are listed on its page with each one's state: waiting, taken or declined #14635

Merged opened this

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

Merged

torque/forge:torque/14590-the-offers-a-fork-has-sent-upstream-are → main 7 commits fork 1 thread · 1 unresolved

  • Merged. torque/14590-the-offers-a-fork-has-sent-upstream-are was merged into main as 1b25ebe.

Review

Sign in to review this change.
7 files changed +277 −6
docs/features/fork-offers-sent.md added +12
@@ -0,0 +1,12 @@
1 ---
2 topic: Your apps
3 row: A fork lists the offers it sent upstream, and where each stands
4 ---
5 # A fork lists the offers it sent upstream
6
7 A fork's page shows what it has offered its upstream, so the maker sees where each offer stands without asking.
8
9 - **Page:** the fork's home has a "Sent upstream" section: up to 20 pulls into the parent whose head is this fork, newest first, each with title, state and age, linking to the pull. The state is `waiting` (open), `taken` (merged) or `declined` (closed without a merge). No offers, or an upstream the viewer cannot read, shows nothing.
10 - **Terminal:** the fork's forks screen has one `sent #N title` row per offer, with the state and age; Enter opens the pull.
11
12 The rule is `sentOffers` in `src/core/fork_offers.ts`.
src/core/fork_offers.ts +42 −1
@@ -6,7 +6,7 @@
66 */
77
88 import type { Database } from "bun:sqlite";
9 import { visibleForks, type Repo } from "./repos.ts";
9 import { forkParentFor, visibleForks, type Repo } from "./repos.ts";
1010 import type { User } from "./users.ts";
1111
1212 export const MAX_FORK_OFFERS = 20;
@@ -46,3 +46,44 @@ export function forkOffers(
4646 createdAt: r.created_at,
4747 }));
4848 }
49
50 export type SentOfferState = "waiting" | "taken" | "declined";

Two behaviours here have no test: the parent-unreadable branch returning [], and the LIMIT 20 cap.

51
52 export interface SentOffer {
53 readonly number: number;
54 readonly title: string;
55 readonly state: SentOfferState;
56 readonly createdAt: string;
57 }
58
59 /**
60 * The offers this fork has sent its upstream, newest first: the pulls into
61 * the parent whose head lives here. Open is waiting, merged is taken, closed
62 * unmerged is declined. Nothing when the parent is gone or unreadable to the
63 * viewer — the pulls are the parent's to show.
64 */
65 export function sentOffers(
66 db: Database,
67 fork: Repo,
68 viewer: User | undefined,
69 ): SentOffer[] {
70 const parent = forkParentFor(db, fork, viewer);
71 if (parent === undefined) return [];
72 return db
73 .query<
74 { number: number; title: string; created_at: string; state: string; merged_at: string | null },
75 [string, string]
76 >(
77 `SELECT issue.number, issue.title, issue.created_at, issue.state, pull.merged_at
78 FROM pull JOIN issue ON issue.id = pull.issue_id
79 WHERE issue.repo_id = ?1 AND pull.head_repo_id = ?2
80 ORDER BY issue.created_at DESC, issue.number DESC LIMIT ${MAX_FORK_OFFERS}`,
81 )
82 .all(parent.id, fork.id)
83 .map((r) => ({
84 number: r.number,
85 title: r.title,
86 createdAt: r.created_at,
87 state: r.merged_at !== null ? "taken" : r.state === "open" ? "waiting" : "declined",
88 }));
89 }
src/tui/screens/forks.ts +17 −1
@@ -25,7 +25,7 @@ import { FOLLOW_CHOICES, FOLLOW_PHRASE, FOLLOWS_NOTHING, followChoiceLine, propo
2525 import { FOLLOW_MODE_REFUSALS, convergeWords, followModeOf, followStanding, followWords, setFollowMode, takeOrTell, takeWords } from "../../actions/follow.ts";
2626 import { incomingOf, type Incoming } from "../../actions/follow_incoming.ts";
2727 import { convergeFollowed, followedAs, followedOf } from "../../actions/upstream_sync.ts";
28 import { forkOffers } from "../../core/fork_offers.ts";
28 import { forkOffers, sentOffers } from "../../core/fork_offers.ts";
2929 import { agoWords } from "../../actions/follow_words.ts";
3030 import { STANDING_EVENTS } from "../../core/standing_events.ts";
3131 import { openProposal, proposeUpstream } from "../../actions/upstream.ts";
@@ -53,6 +53,8 @@ export interface ForkRow {
5353 words: string;
5454 heading: string;
5555 issue?: number;
56 /** A pull on `repo` (the upstream) that Enter opens: an offer this fork sent. */
57 pull?: number;
5658 }
5759
5860 /** The rows the screen draws, in order: pure so a test reads them without a terminal. */
@@ -167,6 +169,7 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
167169 let forkModes: Map<string, ForkFollowMode> = new Map();
168170 let following: ForkRow[] = [];
169171 let offered: ForkRow[] = [];
172 let sent: ForkRow[] = [];
170173 let relation: ForkRelation | undefined;
171174 let offer: string | undefined;
172175 /** The commits an update would bring and the tip they lead to: what a take is handed back. */
@@ -222,6 +225,7 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
222225 ...(open === undefined || relation === undefined ? [] : [openRow(open, relation.parent)]),
223226 ...following,
224227 ...offered,
228 ...sent,
225229 ...(ranked === undefined ? [] : forkRows(ranked, tree, forkModes)),
226230 ];
227231 const read = async (): Promise<void> => {
@@ -256,6 +260,17 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
256260 heading: "",
257261 issue: o.number,
258262 }));
263 const upstream = relation?.parent;
264 sent =
265 upstream === undefined
266 ? []
267 : sentOffers(f.db, repo, f.user).map((o) => ({
268 repo: upstream,
269 name: `sent #${o.number} ${o.title}`,
270 words: `${o.state} · ${agoWords(o.createdAt)}`,
271 heading: "",
272 pull: o.number,
273 }));
259274 ctx.invalidate();
260275 };
261276 const take = (): void => {
@@ -382,6 +397,7 @@ export function forksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
382397 if (e.key === "enter") {
383398 const row = list[state.index];
384399 if (open !== undefined && state.index === 0) openIt(open);
400 else if (row?.pull !== undefined) ctx.push(pullScreen(f, ctx, local(f, row.repo), row.pull));
385401 else if (row?.issue !== undefined) ctx.push(issueScreen(f, ctx, local(f, repo), row.issue));
386402 else if (row !== undefined) ctx.push(repoScreen(f, ctx, local(f, row.repo)));
387403 return true;
src/web/pages/fork-offers.ts +19 −1
@@ -1,7 +1,7 @@
11 /** The upstream's home: open offers from forks, one line each (#13403). */
22
33 import { agoWords } from "../../actions/follow_words.ts";
4 import type { ForkOffer } from "../../core/fork_offers.ts";
4 import type { ForkOffer, SentOffer } from "../../core/fork_offers.ts";
55 import type { Repo } from "../../core/repos.ts";
66 import { repoBase } from "../chrome.ts";
77 import { html, type Html } from "../html.ts";
@@ -20,3 +20,21 @@ export function forkOffersCard(repo: Pick<Repo, "ownerName" | "name">, offers: r
2020 )}</ul>
2121 </section>`;
2222 }
23
24 /** A fork's page: the offers it has sent upstream and where each stands. */
25 export function sentOffersCard(
26 parent: Pick<Repo, "ownerName" | "name">,
27 offers: readonly SentOffer[],
28 ): Html | null {
29 if (offers.length === 0) return null;
30 return html`<section aria-labelledby="sent-offers-h">
31 <h2 id="sent-offers-h">Sent upstream</h2>
32 <ul class="list">${offers.map(
33 (o) => html`<li>
34 <a class="row" href="${repoBase(parent)}/pulls/${o.number}"
35 ><span class="row-main"><span class="row-title">${o.title}</span>
36 <span class="row-meta"><span>${o.state}</span> <span>${time(o.createdAt, { text: agoWords(o.createdAt) })}</span></span></span></a>
37 </li>`,
38 )}</ul>
39 </section>`;
40 }
src/web/pages/repo.ts +3 −2
@@ -20,8 +20,8 @@ import { standingRegion } from "./fork-standing.ts";
2020 import { CONFLICT_WORDS, UNREAD_WORDS } from "../../origins/ahead.ts";
2121 import { upstreamOffer } from "../../actions/offer_upstream.ts";
2222 import { offerUpstream } from "./offer-upstream.ts";
23 import { forkOffers } from "../../core/fork_offers.ts";
24 import { forkOffersCard } from "./fork-offers.ts";
23 import { forkOffers, sentOffers } from "../../core/fork_offers.ts";
24 import { forkOffersCard, sentOffersCard } from "./fork-offers.ts";
2525 import { VERIFY_FIRST } from "../../actions/verify_words.ts";
2626 import { GUEST_REFUSALS, HELP_BUILD, HELP_BUILD_NEXT, isContributionFork, isGuestRefusal } from "../../core/contribute.ts";
2727 import { lacks, NOT_A_MEMBER } from "../../core/roles.ts";
@@ -1571,6 +1571,7 @@ async function codeHome(ctx: Ctx, repo: Repo): Promise<Response> {
15711571 })}${credit}${standing}${archivedLine(ctx, repo)}${walletLine(ctx, repo)}${reportLine(ctx, repo)}${movedLine(ctx, repo)}${noApp}${gets}
15721572 ${offerUpstream(repo, parent, offer)}
15731573 ${forkOffersCard(repo, forkOffers(ctx.app.db, repo, ctx.auth.user))}
1574 ${parent === undefined ? "" : sentOffersCard(parent, sentOffers(ctx.app.db, repo, ctx.auth.user))}
15741575 ${latest === null
15751576 ? ""
15761577 : html`<p class="muted latest-wave">Latest:
test/fork-offers-sent.test.ts added +122
@@ -0,0 +1,122 @@
1 /** A fork's page lists the offers it sent upstream with their state (#14590). */
2 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { commitToBranch, createRepo, forkRepo, updateRepoSettings } from "../src/actions/repos.ts";
7 import { mergePull, openPull } from "../src/actions/pulls.ts";
8 import { setIssueState } from "../src/actions/issues.ts";
9 import { getIssue } from "../src/core/issues.ts";
10 import { getPull } from "../src/core/pulls.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { sentOffers } from "../src/core/fork_offers.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import type { Repo } from "../src/core/repos.ts";
16 import { promote } from "./helpers/members.ts";
17
18 describe("sent offers", () => {
19 let dir: string;
20 let app: App;
21 let servers: Servers;
22 let alice: User;
23 let bob: User;
24 let up: Repo;
25 let fork: Repo;
26
27 const offer = async (branch: string, title: string) => {
28 const pushed = await commitToBranch(app, bob, fork, {
29 branch, message: title, files: [{ path: `${branch}.txt`, content: "x\n" }],
30 });
31 if (!pushed.ok) throw new Error(JSON.stringify(pushed.error));
32 const pull = await openPull(app, bob, up, {
33 title, baseBranch: "main", headBranch: branch, headRepo: fork,
34 });
35 if (!pull.ok) throw new Error(JSON.stringify(pull.error));
36 return pull.value;
37 };
38
39 beforeAll(async () => {
40 dir = await mkdtemp(join(tmpdir(), "forge-sent-offers-"));
41 app = createApp({ dataDir: dir, port: 0 });
42 servers = await serve(app);
43 const mk = async (username: string, first: boolean) => {
44 const made = await createUser(app.db, {
45 origin: "seed", trusted: true, username, email: `${username}@forge.local`,
46 password: "a-good-passphrase", adminIfFirst: first,
47 });
48 if (!made.ok) throw new Error(made.error);
49 return made.value;
50 };
51 alice = await mk("alice", true);
52 bob = await mk("bob", false);
53 promote(app.db, bob);
54 const made = await createRepo(app, alice, { name: "lib", visibility: "public" });
55 if (!made.ok) throw new Error(String(made.error));
56 up = made.value;
57 const seeded = await commitToBranch(app, alice, up, {
58 message: "Start", files: [{ path: "README.md", content: "# lib\n" }],
59 });
60 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
61 const f = await forkRepo(app, bob, up);
62 if (!f.ok) throw new Error(String(f.error));
63 fork = f.value;
64 });
65
66 afterAll(async () => {
67 servers.stop();
68 await rm(dir, { recursive: true, force: true });
69 });
70
71 test("an empty list shows nothing", async () => {
72 expect(sentOffers(app.db, fork, undefined)).toEqual([]);
73 const body = await (await fetch(`${servers.url}/bob/lib`)).text();
74 expect(body).not.toContain("Sent upstream");
75 });
76
77 test("waiting, taken and declined, newest first, on the page", async () => {
78 const w = await offer("a", "Waits");
79 const t = await offer("b", "Taken one");
80 const d = await offer("c", "Declined one");
81 const landed = await mergePull(app, alice, up, getPull(app.db, up.id, t.number)!, "merge");
82 if (!landed.ok) throw new Error(JSON.stringify(landed.error));
83 const declined = setIssueState(app, alice, up, getIssue(app.db, up.id, d.number)!, "closed");
84 if (!declined.ok) throw new Error(JSON.stringify(declined.error));
85 const list = sentOffers(app.db, fork, undefined);
86 expect(list.map((o) => [o.title, o.state])).toEqual([
87 ["Declined one", "declined"], ["Taken one", "taken"], ["Waits", "waiting"],
88 ]);
89 expect(list[2]!.number).toBe(w.number);
90 const body = await (await fetch(`${servers.url}/bob/lib`)).text();
91 expect(body).toMatch(/Sent upstream[\s\S]*Declined one[\s\S]*declined[\s\S]*Taken one[\s\S]*taken[\s\S]*Waits[\s\S]*waiting/);
92 });
93
94 test("an upstream pull from another repository is not listed", async () => {
95 expect(sentOffers(app.db, up, undefined)).toEqual([]);
96 });
97
98 test("at most 20 offers come back, newest first", async () => {
99 for (let i = 0; i < 18; i++) await offer(`n${i}`, `Extra ${i}`);
100 const total = app.db
101 .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM pull WHERE head_repo_id = ?")
102 .get(fork.id)!.n;
103 expect(total).toBe(21);
104 const list = sentOffers(app.db, fork, undefined);
105 expect(list).toHaveLength(20);
106 const numbers = list.map((o) => o.number);
107 expect(numbers).toEqual([...numbers].sort((a, b) => b - a));
108 // The oldest of the 21 (the first one opened) is the one left out.
109 expect(list.some((o) => o.title === "Waits")).toBe(false);
110 expect(list[0]!.title).toBe("Extra 17");
111 }, 120_000);
112
113 test("a viewer who cannot read the upstream sees nothing sent", async () => {
114 const hid = await updateRepoSettings(app, alice, up, { visibility: "private" });
115 if (!hid.ok) throw new Error(JSON.stringify(hid.error));
116 expect(sentOffers(app.db, fork, undefined)).toEqual([]);
117 const body = await (await fetch(`${servers.url}/bob/lib`)).text();
118 expect(body).not.toContain("Sent upstream");
119 // The author, who may read the upstream, still sees them.
120 expect(sentOffers(app.db, fork, alice).length).toBeGreaterThan(0);
121 });
122 });
test/tui-forks-offers.test.ts +62 −1
@@ -3,8 +3,11 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
33 import { mkdtemp, rm } from "node:fs/promises";
44 import { tmpdir } from "node:os";
55 import { join } from "node:path";
6 import { openPull } from "../src/actions/pulls.ts";
6 import { mergePull, openPull } from "../src/actions/pulls.ts";
77 import { commitToBranch, createRepo, forkRepo, updateRepoSettings } from "../src/actions/repos.ts";
8 import { setIssueState } from "../src/actions/issues.ts";
9 import { getIssue } from "../src/core/issues.ts";
10 import { getPull } from "../src/core/pulls.ts";
811 import { getRepo } from "../src/core/repos.ts";
912 import { createUser, type User } from "../src/core/users.ts";
1013 import { createApp, type App } from "../src/app.ts";
@@ -85,3 +88,61 @@ describe("the forks screen lists offers from forks", () => {
8588 t.stop();
8689 });
8790 });
91
92 describe("the forks screen lists the offers a fork sent upstream", () => {
93 test("a fork's screen shows its sent offers with their state", async () => {
94 const dataDir = await mkdtemp(join(tmpdir(), "forge-tui-forks-sent-"));
95 const app = createApp({ dataDir, port: 0 });
96 const mk = async (name: string) => {
97 const u = must(
98 await createUser(app.db, {
99 origin: "seed", trusted: true, username: name, email: `${name}@f.dev`,
100 password: "a-good-passphrase", adminIfFirst: name === "alice",
101 }),
102 );
103 promote(app.db, u);
104 return u;
105 };
106 const alice = await mk("alice");
107 const bob = await mk("bob");
108 const lib = must(await createRepo(app, alice, { name: "lib", visibility: "public" }));
109 must(await commitToBranch(app, alice, lib, { message: "base", files: [{ path: "a.txt", content: "a\n" }] }));
110 const fork = must(await forkRepo(app, bob, lib));
111 must(await commitToBranch(app, bob, fork, { branch: "f", message: "f", files: [{ path: "b.txt", content: "x\n" }] }));
112 must(await openPull(app, bob, lib, { title: "Mine", baseBranch: "main", headBranch: "f", headRepo: fork }));
113 for (const [branch, title] of [["g", "Took"], ["h", "Refused"]] as const) {
114 must(await commitToBranch(app, bob, fork, { branch, message: title, files: [{ path: `${branch}.txt`, content: "x\n" }] }));
115 must(await openPull(app, bob, lib, { title, baseBranch: "main", headBranch: branch, headRepo: fork }));
116 }
117 must(await mergePull(app, alice, lib, getPull(app.db, lib.id, 2)!, "merge"));
118 must(setIssueState(app, alice, lib, getIssue(app.db, lib.id, 3)!, "closed"));
119 const terminal = headlessTerminal(120, 30);
120 const { app: tui, session } = createForgeApp(
121 app,
122 { sshUsername: "bob", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: bob },
123 { terminal, exitHooks: false },
124 );
125 tui.start();
126 tui.ctx.push(forksScreen(session, tui.ctx, getRepo(app.db, "bob", "lib")!));
127 await settleUntil(tui, "sent #1 Mine");
128 const text = tui.screen.toText();
129 expect(text).toMatch(/sent #1 Mine\s+waiting/);
130 expect(text).toMatch(/sent #2 Took\s+taken/);
131 expect(text).toMatch(/sent #3 Refused\s+declined/);
132 // Enter on a sent row opens that pull on the upstream.
133 // Two rows of following precede the sent ones: down to #3, then Enter.
134 terminal.feed("jjj\r");
135 const deadline = Date.now() + 10_000;
136 for (;;) {
137 await settleApp(tui);
138 if (!tui.screen.toText().includes("sent #2 Took")) break;
139 if (Date.now() > deadline) throw new Error(`pull did not open:\n${tui.screen.toText()}`);
140 }
141 const pull = tui.screen.toText();
142 expect(pull).toContain("Took");
143 expect(pull).not.toContain("sent #3 Refused");
144 tui.stop?.();
145 app.close();
146 await rm(dataDir, { recursive: true, force: true });
147 }, 60_000);
148 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs