Public: anyone can read this; the crew can change it.
A merged change in a fork has an 'offer upstream' action #15095
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15063-a-merged-change-in-a-fork-has-an-offer-u → main 4 commits fork 4 threads · 4 unresolved
docs/features/offer-commit-upstream.md +10
| @@ -69,3 +69,13 @@ one is. | ||
| 69 | 69 | |
| 70 | 70 | If the pull fails to open, the branch this offer cut is deleted. After a pull is closed the |
| 71 | 71 | `offer/` branch stays in the fork, and the same commit can be offered again on it. |
| 72 | ||
| 73 | ## Offer a merged pull | |
| 74 | ||
| 75 | On a fork's merged pull, a writer sees **Offer upstream** with a preview (`<details>`) of the title | |
| 76 | and description that would be filed. Pressing it offers the commit the pull landed on, through | |
| 77 | the same action as above (`offerPull` in `src/actions/offer_pull.ts`), with the pull's own | |
| 78 | description in the body, and lands on the receipt. In the terminal, `O` on the pull screen does the | |
| 79 | same (`src/tui/screens/pull.ts`). A fork with no upstream, an archived upstream or an unmerged | |
| 80 | pull is refused in `OFFER_PULL_WORDS`. Offering again, even at the same instant, returns the first | |
| 81 | offer's receipt and files nothing. The offered branch is the fork's history up to that commit. |
src/actions/offer_commit.ts +6 −1
| @@ -76,6 +76,11 @@ export async function offerCommit( | ||
| 76 | 76 | fork: Repo, |
| 77 | 77 | rev: string, |
| 78 | 78 | subject: string, |
| 79 | /** | |
| 80 | * A description to carry in the pull's body, after the line naming the fork (a merged pull's | |
| 81 | * own). | |
| 82 | */ | |
| 83 | described = "", | |
| 79 | 84 | ): Promise<Result<CommitOffered, OfferCommitRefusal>> { |
| 80 | 85 | const writer = requireWriter(app, actor, fork); |
| 81 | 86 | if (!writer.ok) return writer; |
| @@ -97,7 +102,7 @@ export async function offerCommit( | ||
| 97 | 102 | const checked = await checkOfCommit(app, writer.value, fork, oid); |
| 98 | 103 | const body = |
| 99 | 104 | `Offered from ${fork.ownerName}/${fork.name}, a fork of this repository: ` + |
| 100 | `commit ${oid.slice(0, 7)}.\n\n${verdictWords(checked.ok ? checked.value : null)}`; | |
| 105 | `commit ${oid.slice(0, 7)}.\n\n${described.trim() === "" ? "" : `${described.trim()}\n\n`}${verdictWords(checked.ok ? checked.value : null)}`; | |
| 101 | 106 | const pullTitle = |
| 102 | 107 | subject.trim() === "" ? `Commit ${oid.slice(0, 7)} from ${fork.ownerName}/${fork.name}` : subject.trim(); |
| 103 | 108 | const opened = await openPull(app, writer.value, parent, { |
src/actions/offer_pull.ts added +151
| @@ -0,0 +1,151 @@ | ||
| 1 | /** | |
| 2 | * A merged pull in a fork, offered upstream (#15063): the commit it landed on is offered to the | |
| 3 | * parent (`offerCommit`) carrying the pull's own description. `previewOfferPull` says what would | |
| 4 | * be filed and where, with no write; `offerPull` files it and returns the receipt. A fork with no | |
| 5 | * upstream, an unmerged pull or a stranger is refused in words; offering the same pull again, even | |
| 6 | * at the same instant, folds into the first offer and returns its receipt, never a second pull. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { App } from "../app.ts"; | |
| 10 | import { forkOfferReceiptOf } from "../core/fork_offer.ts"; | |
| 11 | import { getPull, openPullFromHead } from "../core/pulls.ts"; | |
| 12 | import { canRead, getRepoById, type Repo } from "../core/repos.ts"; | |
| 13 | import type { User } from "../core/users.ts"; | |
| 14 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 15 | import { requireWriter } from "./access.ts"; | |
| 16 | import { offerBranch, offerCommit, type OfferCommitRefusal } from "./offer_commit.ts"; | |
| 17 | ||
| 18 | export type OfferPullRefusal = | |
| 19 | | "not_found" | |
| 20 | | "unauthorized" | |
| 21 | | "forbidden" | |
| 22 | | "no_upstream" | |
| 23 | | "upstream_archived" | |
| 24 | | "not_merged" | |
| 25 | | "already_offered" | |
| 26 | | "not_opened"; | |
| 27 | ||
| 28 | export const OFFER_PULL_WORDS: Readonly<Record<OfferPullRefusal, string>> = { | |
| 29 | not_found: "That pull or its upstream cannot be found.", | |
| 30 | unauthorized: "Sign in to offer a pull upstream.", | |
| 31 | forbidden: "Only someone who can write to this fork may offer its pulls upstream.", | |
| 32 | no_upstream: "This fork follows no upstream, so there is nowhere to offer this to.", | |
| 33 | upstream_archived: "The upstream is archived and accepts no offers until it is unarchived.", | |
| 34 | not_merged: "Only a merged pull can be offered upstream.", | |
| 35 | already_offered: | |
| 36 | "This pull's commit is already in an open pull upstream that did not come from an offer here, so there is no receipt to show. Look for it among the upstream's open pulls.", | |
| 37 | not_opened: "The offer could not be filed upstream. Nothing was changed; try again.", | |
| 38 | }; | |
| 39 | ||
| 40 | export interface OfferPullPreview { | |
| 41 | readonly title: string; | |
| 42 | readonly body: string; | |
| 43 | readonly commit: string; | |
| 44 | readonly upstream: string; | |
| 45 | /** The receipt of the offer already filed for this pull, when there is one. */ | |
| 46 | readonly receipt: string | null; | |
| 47 | } | |
| 48 | ||
| 49 | export interface PullOffered { | |
| 50 | readonly receipt: string; | |
| 51 | readonly upstream: string; | |
| 52 | readonly number: number; | |
| 53 | /** True when the pull had been offered before: this is the first offer's receipt. */ | |
| 54 | readonly folded: boolean; | |
| 55 | } | |
| 56 | ||
| 57 | interface Ground { | |
| 58 | readonly parent: Repo; | |
| 59 | readonly title: string; | |
| 60 | readonly body: string; | |
| 61 | readonly oid: string; | |
| 62 | readonly writer: User; | |
| 63 | } | |
| 64 | ||
| 65 | function ground(app: App, actor: User | undefined, fork: Repo, number: number): Result<Ground, OfferPullRefusal> { | |
| 66 | const writer = requireWriter(app, actor, fork); | |
| 67 | if (!writer.ok) return writer; | |
| 68 | const parent = fork.forkParentId === null ? undefined : getRepoById(app.db, fork.forkParentId); | |
| 69 | if (parent === undefined) return err("no_upstream"); | |
| 70 | if (!canRead(app.db, parent, writer.value)) return err("no_upstream"); | |
| 71 | if (parent.archivedAt !== null) return err("upstream_archived"); | |
| 72 | const pull = getPull(app.db, fork.id, number); | |
| 73 | if (pull === undefined) return err("not_found"); | |
| 74 | if (pull.mergedAt === null || pull.mergedOid === null) return err("not_merged"); | |
| 75 | return ok({ parent, title: pull.title, body: pull.body, oid: pull.mergedOid, writer: writer.value }); | |
| 76 | } | |
| 77 | ||
| 78 | export function previewOfferPull( | |
| 79 | app: App, | |
| 80 | actor: User | undefined, | |
| 81 | fork: Repo, | |
| 82 | number: number, | |
| 83 | ): Result<OfferPullPreview, OfferPullRefusal> { | |
| 84 | const g = ground(app, actor, fork, number); | |
| 85 | if (!g.ok) return g; | |
| 86 | const open = openPullFromHead(app.db, g.value.parent.id, fork.id, offerBranch(g.value.oid)); | |
| 87 | const receipt = open === undefined ? null : (forkOfferReceiptOf(app.db, open.id) ?? null); | |
| 88 | return ok({ | |
| 89 | title: g.value.title, | |
| 90 | body: g.value.body, | |
| 91 | commit: g.value.oid, | |
| 92 | upstream: `${g.value.parent.ownerName}/${g.value.parent.name}`, | |
| 93 | receipt, | |
| 94 | }); | |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Offers in flight, by fork, commit and actor: a second tap joins the first. Gone when each | |
| 99 | * settles. | |
| 100 | */ | |
| 101 | const flyingOf = new WeakMap<App, Map<string, Promise<Result<PullOffered, OfferPullRefusal>>>>(); | |
| 102 | ||
| 103 | function flightsOf(app: App) { | |
| 104 | let m = flyingOf.get(app); | |
| 105 | if (m === undefined) flyingOf.set(app, (m = new Map())); | |
| 106 | return m; | |
| 107 | } | |
| 108 | ||
| 109 | export function offerPull( | |
| 110 | app: App, | |
| 111 | actor: User | undefined, | |
| 112 | fork: Repo, | |
| 113 | number: number, | |
| 114 | ): Promise<Result<PullOffered, OfferPullRefusal>> { | |
| 115 | const g = ground(app, actor, fork, number); | |
| 116 | if (!g.ok) return Promise.resolve(g); | |
| 117 | const key = `${fork.id}:${g.value.oid}`; | |
| 118 | const flying = flightsOf(app); | |
| 119 | const running = flying.get(key); | |
| 120 | if (running !== undefined) return running.then((r) => (r.ok ? ok({ ...r.value, folded: true }) : r)); | |
| 121 | const run = file(app, fork, g.value); | |
| 122 | flying.set(key, run); | |
| 123 | return run.finally(() => flying.delete(key)); | |
| 124 | } | |
| 125 | ||
| 126 | async function file(app: App, fork: Repo, g: Ground): Promise<Result<PullOffered, OfferPullRefusal>> { | |
| 127 | const upstream = `${g.parent.ownerName}/${g.parent.name}`; | |
| 128 | const open = openPullFromHead(app.db, g.parent.id, fork.id, offerBranch(g.oid)); | |
| 129 | const had = open === undefined ? undefined : forkOfferReceiptOf(app.db, open.id); | |
| 130 | if (open !== undefined && had !== undefined) return ok({ receipt: had, upstream, number: open.number, folded: true }); | |
| 131 | const made = await offerCommit(app, g.writer, fork, g.oid, g.title, g.body); | |
| 132 | if (!made.ok) return err(refusalOf(made.error)); | |
| 133 | return ok({ receipt: made.value.receipt, upstream, number: made.value.pull.number, folded: false }); | |
| 134 | } | |
| 135 | ||
| 136 | /** What the offer's own refusal means for a merged pull; `ground` already settled the rest. */ | |
| 137 | function refusalOf(why: OfferCommitRefusal): OfferPullRefusal { | |
| 138 | switch (why) { | |
| 139 | case "forbidden": | |
| 140 | case "unauthorized": | |
| 141 | case "not_found": | |
| 142 | case "upstream_archived": | |
| 143 | case "already_offered": | |
| 144 | return why; | |
| 145 | case "not_a_fork": | |
| 146 | return "no_upstream"; | |
| 147 | case "no_such_commit": | |
| 148 | case "not_opened": | |
| 149 | return "not_opened"; | |
| 150 | } | |
| 151 | } |
src/core/fork_offer.ts +10
| @@ -154,3 +154,13 @@ export function markForkOfferWithdrawn(db: Database, issueId: string, offererId: | ||
| 154 | 154 | export function mayWithdrawOffer(actor: Pick<User, "id">, fork: Pick<Repo, "ownerId">, offerAuthorId: string): boolean { |
| 155 | 155 | return actor.id === offerAuthorId || actor.id === fork.ownerId; |
| 156 | 156 | } |
| 157 | ||
| 158 | /** | |
| 159 | * The receipt already kept for the pull `issueId` (the first, when there are several), or | |
| 160 | * undefined. | |
| 161 | */ | |
| 162 | export function forkOfferReceiptOf(db: Database, issueId: string): string | undefined { | |
| 163 | return db | |
| 164 | .query<{ id: string }, [string]>("SELECT id FROM fork_offer WHERE issue_id = ?1 ORDER BY created_at LIMIT 1") | |
| 165 | .get(issueId)?.id; | |
| 166 | } |
src/http/server.ts +2 −1
| @@ -252,7 +252,7 @@ import { | ||
| 252 | 252 | branchDelete, |
| 253 | 253 | branchesPage, |
| 254 | 254 | } from "../web/pages/branches.ts"; |
| 255 | import { offerCommitPost, offerWithdrawPost } from "../web/pages/offer-upstream.ts"; | |
| 255 | import { offerCommitPost, offerPullPost, offerWithdrawPost } from "../web/pages/offer-upstream.ts"; | |
| 256 | 256 | import { offerReceiptPage } from "../web/pages/offer-receipt.ts"; |
| 257 | 257 | import { iterationsPage, noteSubmit } from "../web/pages/iterations.ts"; |
| 258 | 258 | import { mainSubmit, registerSubmit, retireSubmit, valuesSubmit, variantBoardPage } from "../web/pages/app_variants.ts"; |
| @@ -1727,6 +1727,7 @@ export function buildRouter(): Router<Handler> { | ||
| 1727 | 1727 | router.add("GET", "/:owner/:name/waves/:version", wavePage); |
| 1728 | 1728 | router.add("POST", "/:owner/:name/branches", branchCreate); |
| 1729 | 1729 | router.add("POST", "/:owner/:name/commit/:oid/offer", offerCommitPost); |
| 1730 | router.add("POST", "/:owner/:name/pulls/:number/offer", offerPullPost); | |
| 1730 | 1731 | router.add("POST", "/:owner/:name/offers/:number/withdraw", offerWithdrawPost); |
| 1731 | 1732 | router.add("POST", "/:owner/:name/branches/:branch/delete", branchDelete); |
| 1732 | 1733 |
src/tui/screens/pull.ts +29
| @@ -26,6 +26,7 @@ | ||
| 26 | 26 | * `n`/`p` walk the unsettled ones while the cursor is inside a thread. |
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | import { OFFER_PULL_WORDS, offerPull, previewOfferPull } from "../../actions/offer_pull.ts"; | |
| 29 | 30 | import { outsideClause } from "../../web/outside.ts"; |
| 30 | 31 | import { teamHoldRefusal, waitingOnTeams } from "../../core/teams_words.ts"; |
| 31 | 32 | import { crewFilters, guardedRefusal, isStrangerPull, strangerGuard, strangerNext } from "../../crew/stranger.ts"; |
| @@ -371,6 +372,8 @@ export function pullScreen( | ||
| 371 | 372 | getPull(f.db, repo.id, number)?.variantGroup != null; |
| 372 | 373 | scroll.top = keep.top ?? 0; |
| 373 | 374 | |
| 375 | /** Whether `O` is on offer, settled once per load of the pull (`mayOffer`). */ | |
| 376 | let offerable: boolean | undefined; | |
| 374 | 377 | const reload = async (): Promise<void> => { |
| 375 | 378 | const read = await src.issue(number); |
| 376 | 379 | if (!read.ok) { |
| @@ -379,6 +382,7 @@ export function pullScreen( | ||
| 379 | 382 | return; |
| 380 | 383 | } |
| 381 | 384 | detail = read.value; |
| 385 | offerable = undefined; | |
| 382 | 386 | facts = read.value.pull; |
| 383 | 387 | diff = facts?.diff ?? { files: [], additions: 0, deletions: 0 }; |
| 384 | 388 | // Review comments are anchored rows in *our* database; a live browse has |
| @@ -995,6 +999,26 @@ export function pullScreen( | ||
| 995 | 999 | ); |
| 996 | 1000 | }; |
| 997 | 1001 | |
| 1002 | /** | |
| 1003 | * Whether `O` is on offer: a merged pull in a fork whose upstream takes it (`previewOfferPull`). | |
| 1004 | */ | |
| 1005 | const mayOffer = (): boolean => | |
| 1006 | (offerable ??= repo !== undefined && f.user !== undefined && previewOfferPull(f.app, f.user, repo, number).ok); | |
| 1007 | ||
| 1008 | const offerUp = (): void => { | |
| 1009 | if (repo === undefined) return; | |
| 1010 | const target = repo; | |
| 1011 | runAsync(ctx, async () => { | |
| 1012 | const offered = await offerPull(f.app, f.user, target, number); | |
| 1013 | if (!offered.ok) ctx.toast(OFFER_PULL_WORDS[offered.error], "danger"); | |
| 1014 | else | |
| 1015 | ctx.toast( | |
| 1016 | `${offered.value.folded ? "Already offered" : "Offered"} upstream as ${offered.value.upstream}#${offered.value.number}.`, | |
| 1017 | "success", | |
| 1018 | ); | |
| 1019 | }); | |
| 1020 | }; | |
| 1021 | ||
| 998 | 1022 | /** |
| 999 | 1023 | * Whether `T` takes this back: a shipped pull on a local repository, for a |
| 1000 | 1024 | * person who may write. The same gate `revertPull` holds; it decides only |
| @@ -1212,6 +1236,7 @@ export function pullScreen( | ||
| 1212 | 1236 | binding(["m"], "merge", { disabled: state() !== "ready" }), |
| 1213 | 1237 | ...(wall !== undefined && mayWrite() ? [binding(["w"], "the access wall: allow or send back")] : []), |
| 1214 | 1238 | ...(mayLandPast() ? [binding(["L"], "land past the gate")] : []), |
| 1239 | ...(mayOffer() ? [binding(["O"], "offer this merged pull upstream")] : []), | |
| 1215 | 1240 | ...(mayLandSlice() ? [binding(["S"], "land a slice")] : []), |
| 1216 | 1241 | ...(tab === 1 && view.threadHere() !== null |
| 1217 | 1242 | ? [ |
| @@ -1450,6 +1475,10 @@ export function pullScreen( | ||
| 1450 | 1475 | landSlices(); |
outdated
The |
||
| 1451 | 1476 | return true; |
| 1452 | 1477 | } |
| 1478 | if (e.key === "O" && mayOffer()) { | |
| 1479 | offerUp(); | |
| 1480 | return true; | |
| 1481 | } | |
| 1453 | 1482 | if (e.key === "L" && mayLandPast()) { |
| 1454 | 1483 | landPast(); |
| 1455 | 1484 | return true; |
src/web/pages/offer-upstream.ts +44 −1
| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { OFFER_COMMIT_WORDS, offerCommit } from "../../actions/offer_commit.ts"; |
| 10 | import { OFFER_PULL_WORDS, offerPull, previewOfferPull } from "../../actions/offer_pull.ts"; | |
| 10 | 11 | import { OFFER_WITHDRAW_WORDS, withdrawOffer } from "../../actions/offer_withdraw.ts"; |
| 11 | 12 | import type { UpstreamOffer } from "../../actions/offer_upstream.ts"; |
| 13 | import type { Pull } from "../../core/pulls.ts"; | |
| 12 | 14 | import { canWrite, getRepoById, type Repo } from "../../core/repos.ts"; |
| 13 | 15 | import { isSafeOid } from "../../git/guard.ts"; |
| 14 | 16 | import { commitLog } from "../../git/read.ts"; |
| @@ -18,7 +20,7 @@ import { loadRepo, notFoundPage, repoBase, signInFirst } from "../chrome.ts"; | ||
| 18 | 20 | import { readForm } from "../form.ts"; |
| 19 | 21 | import { html, type Html } from "../html.ts"; |
| 20 | 22 | import { flashTo } from "../layout.ts"; |
| 21 | import { btn } from "../ui.ts"; | |
| 23 | import { btn, disclosure } from "../ui.ts"; | |
| 22 | 24 | |
| 23 | 25 | export function offerUpstream( |
| 24 | 26 | repo: Repo, |
| @@ -98,3 +100,44 @@ export async function offerWithdrawPost(ctx: Ctx): Promise<Response> { | ||
| 98 | 100 | } |
| 99 | 101 | return flashTo(ctx, `/offers/${done.value.receipt}`, { message: "Offer withdrawn. Its commits stay in this fork." }); |
| 100 | 102 | } |
| 103 | ||
| 104 | /** | |
outdated
Two blank lines here; the gate wants one. |
||
| 105 | * The trail of the merged box: the preview and the button, or the way to the receipt; null when no | |
| 106 | * offer applies. | |
| 107 | */ | |
| 108 | export function offerPullCard(ctx: Ctx, repo: Repo, pull: Pull): Html | null { | |
| 109 | if (repo.forkParentId === null || ctx.auth.user === undefined) return null; | |
| 110 | const preview = previewOfferPull(ctx.app, ctx.auth.user, repo, pull.number); | |
| 111 | if (!preview.ok) return null; | |
| 112 | const p = preview.value; | |
| 113 | if (p.receipt !== null) return html`<p class="mt small"><a href="/offers/${p.receipt}">Offered to ${p.upstream}</a></p>`; | |
| 114 | return html`<form class="mt" method="post" action="${repoBase(repo)}/pulls/${pull.number}/offer"> | |
| 115 | ${disclosure( | |
| 116 | html`Preview what ${p.upstream} would receive`, | |
| 117 | html`<p class="small"><strong>${p.title}</strong></p> | |
| 118 | <p class="small">${p.body === "" ? "No description." : p.body}</p> | |
| 119 | <p class="small muted">It offers commit ${p.commit.slice(0, 7)} and every commit of this fork before it that ${p.upstream} lacks, not only this pull's own change.</p>`, | |
outdated
Principles 1 and 6: the preview shows the title and description but not that the offer carries the fork's history up to this commit. It also puts a hand-rolled |
||
| 120 | { small: true }, | |
| 121 | )} | |
| 122 | ${btn("Offer upstream", { size: "sm", icon: "merge" })} | |
| 123 | </form>`; | |
| 124 | } | |
| 125 | ||
| 126 | // POST /:owner/:name/pulls/:number/offer | |
| 127 | export async function offerPullPost(ctx: Ctx): Promise<Response> { | |
| 128 | const repo = loadRepo(ctx); | |
| 129 | if (!repo) return notFoundPage(ctx); | |
| 130 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 131 | const form = await readForm(ctx, "rw"); | |
| 132 | if (form instanceof Response) return form; | |
| 133 | const number = Number(ctx.params.number); | |
| 134 | if (!Number.isSafeInteger(number)) return notFoundPage(ctx); | |
| 135 | const offered = await offerPull(ctx.app, ctx.auth.user, repo, number); | |
| 136 | if (!offered.ok) | |
| 137 | return flashTo(ctx, `${repoBase(repo)}/pulls/${number}`, { tone: "danger", message: OFFER_PULL_WORDS[offered.error] }); | |
| 138 | return flashTo(ctx, `/offers/${offered.value.receipt}`, { | |
| 139 | message: offered.value.folded | |
| 140 | ? `Already offered as #${offered.value.number}; this is its receipt.` | |
| 141 | : `Offered upstream as #${offered.value.number}.`, | |
| 142 | }); | |
| 143 | } |
src/web/studio/change.ts +2
| @@ -100,6 +100,7 @@ import { changeHref } from "./nouns.ts"; | ||
| 100 | 100 | import { pullWord, servingHistory, stateDot, wordDot, type WordCtx } from "./words.ts"; |
| 101 | 101 | import { getHold } from "../../core/holds.ts"; |
| 102 | 102 | import { backToWave } from "../../story/waves.ts"; |
| 103 | import { offerPullCard } from "../pages/offer-upstream.ts"; | |
| 103 | 104 | import { pullRoster } from "./pull_roster.ts"; |
| 104 | 105 | import { readableNotes } from "../../actions/desk.ts"; |
| 105 | 106 | import { holdLines } from "../desk_strip.ts"; |
| @@ -823,6 +824,7 @@ async function changeConversation( | ||
| 823 | 824 | ${when(showAllLooks, () => look)} |
| 824 | 825 | ${talk.node} |
| 825 | 826 | ${reply} |
| 827 | ${offerPullCard(ctx, repo, pull)} | |
| 826 | 828 | ${more} |
| 827 | 829 | </div> |
| 828 | 830 | <aside>${film}<div id="pull-preview">${preview}</div></aside> |
test/offer-pull.test.ts added +179
| @@ -0,0 +1,179 @@ | ||
| 1 | /** | |
| 2 | * A merged pull in a fork, offered upstream (#15063): previewed, receipted, refused with no | |
| 3 | * upstream, and a repeat (even a concurrent one) folded into the first. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { OFFER_PULL_WORDS, offerPull, previewOfferPull } from "../src/actions/offer_pull.ts"; | |
| 10 | import { mergePull, openPull } from "../src/actions/pulls.ts"; | |
| 11 | import { commitToBranch, createRepo, forkRepo, setArchived } from "../src/actions/repos.ts"; | |
| 12 | import { setRole } from "../src/actions/roles.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { getIssueById } from "../src/core/issues.ts"; | |
| 15 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | let dataDir: string; | |
| 23 | let app: App; | |
| 24 | let servers: Servers; | |
| 25 | let owner: User; | |
| 26 | let ravi: User; | |
| 27 | let fork: Repo; | |
| 28 | let solo: Repo; | |
| 29 | let merged = 0; | |
| 30 | let openOne = 0; | |
| 31 | ||
| 32 | const mk = async (username: string, first: boolean): Promise<User> => { | |
| 33 | const made = await createUser(app.db, { | |
| 34 | origin: "seed", | |
| 35 | trusted: true, | |
| 36 | username, | |
| 37 | email: `${username}@forge.local`, | |
| 38 | password: "a-good-passphrase", | |
| 39 | adminIfFirst: first, | |
| 40 | }); | |
| 41 | if (!made.ok) throw new Error(made.error); | |
| 42 | return made.value; | |
| 43 | }; | |
| 44 | ||
| 45 | const shipIn = async (repo: Repo, who: User, branch: string, body: string, land: boolean): Promise<number> => { | |
| 46 | await commitToBranch(app, who, repo, { | |
| 47 | branch, | |
| 48 | base: "main", | |
| 49 | message: branch, | |
| 50 | files: [{ path: `${branch}.txt`, content: "x\n" }], | |
| 51 | }); | |
| 52 | const opened = await openPull(app, who, repo, { title: `Pull ${branch}`, body, baseBranch: "main", headBranch: branch }); | |
| 53 | if (!opened.ok) throw new Error(JSON.stringify(opened.error)); | |
| 54 | if (land) { | |
| 55 | const m = await mergePull(app, who, repo, opened.value, "merge"); | |
| 56 | if (!m.ok) throw new Error(JSON.stringify(m.error)); | |
| 57 | } | |
| 58 | return opened.value.number; | |
| 59 | }; | |
| 60 | ||
| 61 | beforeAll(async () => { | |
| 62 | dataDir = await mkdtemp(join(tmpdir(), "forge-offer-pull-")); | |
| 63 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 64 | servers = await serve(app); | |
| 65 | const hank = await mk("hank", true); | |
| 66 | owner = await mk("olive", false); | |
| 67 | ravi = await mk("ravi", false); | |
| 68 | for (const n of ["olive", "ravi"]) setRole(app, hank, n, "member"); | |
| 69 | const r = await createRepo(app, owner, { name: "yard", visibility: "public" }); | |
| 70 | if (!r.ok) throw new Error(String(r.error)); | |
| 71 | await commitToBranch(app, owner, r.value, { message: "first", files: [{ path: "a.txt", content: "a\n" }] }); | |
| 72 | const f = await forkRepo(app, ravi, r.value); | |
| 73 | if (!f.ok) throw new Error(String(f.error)); | |
| 74 | fork = getRepo(app.db, "ravi", "yard")!; | |
| 75 | merged = await shipIn(fork, ravi, "feat", "Adds a feature, for everyone.", true); | |
| 76 | openOne = await shipIn(fork, ravi, "wip", "not yet", false); | |
| 77 | const s = await createRepo(app, ravi, { name: "solo", visibility: "public" }); | |
| 78 | if (!s.ok) throw new Error(String(s.error)); | |
| 79 | solo = s.value; | |
| 80 | await commitToBranch(app, ravi, solo, { message: "first", files: [{ path: "a.txt", content: "a\n" }] }); | |
| 81 | }); | |
| 82 | ||
| 83 | afterAll(async () => { | |
| 84 | await servers.stop(); | |
| 85 | await rm(dataDir, { recursive: true, force: true }); | |
| 86 | }); | |
| 87 | ||
| 88 | test("the preview says what would be filed, and writes nothing", () => { | |
| 89 | const p = previewOfferPull(app, ravi, fork, merged); | |
| 90 | if (!p.ok) throw new Error(p.error); | |
| 91 | expect(p.value.title).toBe("Pull feat"); | |
| 92 | expect(p.value.body).toBe("Adds a feature, for everyone."); | |
| 93 | expect(p.value.upstream).toBe("olive/yard"); | |
| 94 | expect(p.value.receipt).toBeNull(); | |
| 95 | expect(app.db.query("SELECT count(*) AS n FROM fork_offer").get()).toEqual({ n: 0 }); | |
| 96 | }); | |
| 97 | ||
| 98 | test("an unmerged pull and a repository with no upstream are refused in words", async () => { | |
| 99 | const unmerged = await offerPull(app, ravi, fork, openOne); | |
| 100 | if (unmerged.ok) throw new Error("offered an unmerged pull"); | |
| 101 | expect(unmerged.error).toBe("not_merged"); | |
| 102 | const none = await offerPull(app, ravi, solo, 1); | |
| 103 | if (none.ok) throw new Error("offered with no upstream"); | |
| 104 | expect(none.error).toBe("no_upstream"); | |
| 105 | expect(OFFER_PULL_WORDS.no_upstream).toContain("no upstream"); | |
| 106 | }); | |
| 107 | ||
| 108 | test("the card shows on the merged pull page of a fork's writer only", async () => { | |
| 109 | const signIn = async (u: string): Promise<string> => | |
| 110 | ((await fetch(`${servers.url}/signin`, { method: "POST", body: new URLSearchParams({ username: u, password: "a-good-passphrase" }), redirect: "manual" })).headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 111 | const mine = await (await fetch(`${servers.url}/ravi/yard/pulls/${merged}`, { headers: { cookie: await signIn("ravi") } })).text(); | |
| 112 | expect(mine).toContain("Offer upstream"); | |
| 113 | expect(mine).toContain(`action="/ravi/yard/pulls/${merged}/offer"`); | |
| 114 | const unmergedPage = await (await fetch(`${servers.url}/ravi/yard/pulls/${openOne}`, { headers: { cookie: await signIn("ravi") } })).text(); | |
| 115 | expect(unmergedPage).not.toContain(`action="/ravi/yard/pulls/${openOne}/offer"`); | |
| 116 | const theirs = await (await fetch(`${servers.url}/ravi/yard/pulls/${merged}`, { headers: { cookie: await signIn("olive") } })).text(); | |
| 117 | expect(theirs).not.toContain(`/pulls/${merged}/offer`); | |
| 118 | }); | |
| 119 | ||
| 120 | test("offering files a pull upstream with the description and a receipt; a repeat folds into it", async () => { | |
| 121 | const first = await offerPull(app, ravi, fork, merged); | |
| 122 | if (!first.ok) throw new Error(first.error); | |
| 123 | expect(first.value.folded).toBe(false); | |
| 124 | const row = app.db.query<{ issue_id: string }, [string]>("SELECT issue_id FROM fork_offer WHERE id = ?").get(first.value.receipt)!; | |
| 125 | const filed = getIssueById(app.db, row.issue_id)!; | |
| 126 | expect(filed.title).toBe("Pull feat"); | |
| 127 | expect(filed.body).toContain("Adds a feature, for everyone."); | |
| 128 | expect(filed.body).toContain("ravi/yard"); | |
| 129 | ||
| 130 | const again = await offerPull(app, ravi, fork, merged); | |
| 131 | if (!again.ok) throw new Error(again.error); | |
| 132 | expect(again.value.folded).toBe(true); | |
| 133 | expect(again.value.receipt).toBe(first.value.receipt); | |
| 134 | expect(again.value.number).toBe(first.value.number); | |
| 135 | const p = previewOfferPull(app, ravi, fork, merged); | |
| 136 | expect(p.ok && p.value.receipt).toBe(first.value.receipt); | |
| 137 | expect(app.db.query("SELECT count(*) AS n FROM fork_offer").get()).toEqual({ n: 1 }); | |
| 138 | }); | |
| 139 | ||
| 140 | test("two taps at once make one offer", async () => { | |
| 141 | const n = await shipIn(fork, ravi, "twice", "Twice over.", true); | |
| 142 | const [a, b] = await Promise.all([offerPull(app, ravi, fork, n), offerPull(app, ravi, fork, n)]); | |
| 143 | if (!a.ok || !b.ok) throw new Error("refused"); | |
| 144 | expect(a.value.receipt).toBe(b.value.receipt); | |
| 145 | expect(app.db.query("SELECT count(*) AS n FROM fork_offer").get()).toEqual({ n: 2 }); | |
| 146 | }); | |
| 147 | ||
| 148 | test("the post lands on the receipt, and a stranger is refused", async () => { | |
| 149 | const signIn = async (u: string): Promise<string> => | |
| 150 | ((await fetch(`${servers.url}/signin`, { method: "POST", body: new URLSearchParams({ username: u, password: "a-good-passphrase" }), redirect: "manual" })).headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 151 | const post = async (cookie: string, origin = servers.url): Promise<Response> => | |
| 152 | fetch(`${servers.url}/ravi/yard/pulls/${merged}/offer`, { method: "POST", headers: { cookie, origin }, redirect: "manual" }); | |
| 153 | const ok = await post(await signIn("ravi")); | |
| 154 | expect(ok.status).toBe(303); | |
| 155 | expect(ok.headers.get("location")).toMatch(/^\/offers\/[0-9a-f]{32}/); | |
| 156 | expect((await post(await signIn("ravi"), "https://evil.example")).status).toBe(403); | |
| 157 | const refused = await offerPull(app, owner, fork, merged); | |
| 158 | if (refused.ok) throw new Error("a stranger offered"); | |
| 159 | expect(refused.error).toBe("forbidden"); | |
| 160 | }); | |
| 161 | ||
| 162 | test("an archived upstream refuses the offer in the words made for it, and takes it again unarchived", async () => { | |
| 163 | const fresh = await shipIn(fork, ravi, "late", "Arrives while archived.", true); | |
| 164 | const set = async (archived: boolean) => { | |
| 165 | const done = await setArchived(app, owner, getRepo(app.db, "olive", "yard")!, archived, false); | |
| 166 | if (!done.ok) throw new Error(done.error); | |
| 167 | }; | |
| 168 | await set(true); | |
| 169 | try { | |
| 170 | const refused = await offerPull(app, ravi, fork, fresh); | |
| 171 | if (refused.ok) throw new Error("offered into an archived upstream"); | |
| 172 | expect(refused.error).toBe("upstream_archived"); | |
| 173 | expect(OFFER_PULL_WORDS.upstream_archived).toContain("archived"); | |
| 174 | expect(previewOfferPull(app, ravi, fork, fresh).ok).toBe(false); | |
| 175 | } finally { | |
| 176 | await set(false); | |
| 177 | } | |
| 178 | expect((await offerPull(app, ravi, fork, fresh)).ok).toBe(true); | |
| 179 | }); |
test/tui-pull-offer.test.ts added +125
| @@ -0,0 +1,125 @@ | ||
| 1 | /** | |
| 2 | * The terminal's pull screen offers a merged fork pull upstream with `O` (#15063): bound for a | |
| 3 | * writer on a merged pull of a fork, for no one else; pressing it toasts the offer, and again | |
| 4 | * toasts that it was already offered. | |
| 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 { mergePull, openPull } from "../src/actions/pulls.ts"; | |
| 11 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 14 | import { createUser, type User } from "../src/core/users.ts"; | |
| 15 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 16 | import { pullScreen } from "../src/tui/screens/pull.ts"; | |
| 17 | import { local } from "../src/tui/screens/where.ts"; | |
| 18 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | import { settleApp } from "./helpers/tui.ts"; | |
| 21 | import { promote } from "./helpers/members.ts"; | |
| 22 | ||
| 23 | quietLogs(); | |
| 24 | ||
| 25 | let dataDir: string; | |
| 26 | let app: App; | |
| 27 | ||
| 28 | beforeAll(async () => { | |
| 29 | dataDir = await mkdtemp(join(tmpdir(), "forge-tui-offer-")); | |
| 30 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 31 | }); | |
| 32 | ||
| 33 | afterAll(async () => { | |
| 34 | app.close(); | |
| 35 | await rm(dataDir, { recursive: true, force: true }); | |
| 36 | }); | |
| 37 | ||
| 38 | async function person(username: string, first = false): Promise<User> { | |
| 39 | const made = await createUser(app.db, { | |
| 40 | origin: "seed", | |
| 41 | trusted: true, | |
| 42 | username, | |
| 43 | email: `${username}@forge.local`, | |
| 44 | password: "a-good-passphrase", | |
| 45 | adminIfFirst: first, | |
| 46 | }); | |
| 47 | if (!made.ok) throw new Error(String(made.error)); | |
| 48 | return made.value; | |
| 49 | } | |
| 50 | ||
| 51 | test("O is bound for a writer on a merged fork pull only; it offers, and a repeat says already offered", async () => { | |
| 52 | const alice = await person("alice", true); | |
| 53 | const bob = await person("bob"); | |
| 54 | const carol = await person("carol"); | |
| 55 | promote(app.db, bob); | |
| 56 | promote(app.db, carol); | |
| 57 | const r = await createRepo(app, alice, { name: "lib", visibility: "public" }); | |
| 58 | if (!r.ok) throw new Error(String(r.error)); | |
| 59 | await commitToBranch(app, alice, r.value, { message: "first", files: [{ path: "a.txt", content: "a\n" }] }); | |
| 60 | const f = await forkRepo(app, bob, r.value); | |
| 61 | if (!f.ok) throw new Error(String(f.error)); | |
| 62 | const fork: Repo = getRepo(app.db, "bob", "lib")!; | |
| 63 | const ship = async (branch: string, merge: boolean): Promise<number> => { | |
| 64 | await commitToBranch(app, bob, fork, { | |
| 65 | branch, | |
| 66 | base: "main", | |
| 67 | message: branch, | |
| 68 | files: [{ path: `${branch}.txt`, content: "x\n" }], | |
| 69 | }); | |
| 70 | const o = await openPull(app, bob, fork, { title: `Pull ${branch}`, body: "why", baseBranch: "main", headBranch: branch }); | |
| 71 | if (!o.ok) throw new Error(JSON.stringify(o.error)); | |
| 72 | if (merge) { | |
| 73 | const m = await mergePull(app, bob, fork, o.value, "merge"); | |
| 74 | if (!m.ok) throw new Error(JSON.stringify(m.error)); | |
| 75 | } | |
| 76 | return o.value.number; | |
| 77 | }; | |
| 78 | const merged = await ship("feat", true); | |
| 79 | const open = await ship("wip", false); | |
| 80 | ||
| 81 | const screen = async (user: User, number: number) => { | |
| 82 | const { app: tui, session } = createForgeApp( | |
| 83 | app, | |
| 84 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user }, | |
| 85 | { terminal: headlessTerminal(160, 44), exitHooks: false }, | |
| 86 | ); | |
| 87 | tui.start(); | |
| 88 | tui.ctx.push(pullScreen(session, tui.ctx, local(session, fork), number)); | |
| 89 | await settleApp(tui); | |
| 90 | const press = async (key: string) => { | |
| 91 | tui.dispatch({ type: "key", key, ctrl: false, alt: false, shift: false }); | |
| 92 | await settleApp(tui); | |
| 93 | }; | |
| 94 | return { tui, press, text: () => tui.screen.toText() }; | |
| 95 | }; | |
| 96 | const offered = () => app.db.query<{ n: number }, []>("SELECT count(*) AS n FROM fork_offer").get()!.n; | |
| 97 | ||
| 98 | // A person who cannot write to the fork, and a writer on a pull not yet merged: no key, no offer. | |
| 99 | for (const [who, n] of [[carol, merged], [bob, open]] as const) { | |
| 100 | const s = await screen(who, n); | |
| 101 | try { | |
| 102 | expect(s.text()).not.toContain("offer this merged pull upstream"); | |
| 103 | await s.press("O"); | |
| 104 | expect(s.text()).not.toContain("Offered upstream"); | |
| 105 | } finally { | |
| 106 | s.tui.stop(); | |
| 107 | } | |
| 108 | } | |
| 109 | expect(offered()).toBe(0); | |
| 110 | ||
| 111 | const s = await screen(bob, merged); | |
| 112 | try { | |
| 113 | await s.press("?"); | |
| 114 | expect(s.text()).toContain("offer this merged pull upstream"); | |
| 115 | await s.press("escape"); | |
| 116 | await s.press("O"); | |
| 117 | expect(s.text()).toContain("Offered upstream as alice/lib#"); | |
| 118 | expect(offered()).toBe(1); | |
| 119 | await s.press("O"); | |
| 120 | expect(s.text()).toContain("Already offered upstream as alice/lib#"); | |
| 121 | expect(offered()).toBe(1); | |
| 122 | } finally { | |
| 123 | s.tui.stop(); | |
| 124 | } | |
| 125 | }, 60_000); |
This ternary is convoluted.
already_offeredand every other refusal other thanforbidden(e.g. unauthorized, upstream refusals) all collapse tonot_openedwith 'try again'. When an open pull has no receipt, that is a retry that can never succeed. Map the refusals explicitly, and give that case its own words.