Public: anyone can read this; the crew can change it.
Take it back: the studio's verb on a shipped change #235
scout/forge:scout/190-take-it-back-the-studio-s-verb-on-a-ship → main 1 commit fork
5 files
changed +350
−7
src/http/server.ts +8 −1
| @@ -82,6 +82,7 @@ import { | ||
| 82 | 82 | modeSubmit, |
| 83 | 83 | saySubmit, |
| 84 | 84 | shipSubmit, |
| 85 | takeBackSubmit, | |
| 85 | 86 | tellSubmit, |
| 86 | 87 | } from "../web/studio/actions.ts"; |
| 87 | 88 | import { appStateFragment } from "../web/studio/previews.ts"; |
| @@ -567,10 +568,16 @@ export function buildRouter(): Router<Handler> { | ||
| 567 | 568 | router.add("POST", "/:owner/:name/issues/:number/assignee", (ctx) => |
| 568 | 569 | assigneeSubmit(ctx, "issue"), |
| 569 | 570 | ); |
| 570 | // The studio's three verbs on an app: tell, ship, say. | |
| 571 | // The studio's verbs on an app: tell, ship, say, and — on a shipped | |
| 572 | // change — take it back. | |
| 571 | 573 | router.add("POST", "/:owner/:name/tell", tellSubmit); |
| 572 | 574 | router.add("POST", "/:owner/:name/pulls/:number/ship", shipSubmit); |
| 573 | 575 | router.add("POST", "/:owner/:name/pulls/:number/say", saySubmit); |
| 576 | router.add( | |
| 577 | "POST", | |
| 578 | "/:owner/:name/pulls/:number/take-back", | |
| 579 | takeBackSubmit, | |
| 580 | ); | |
| 574 | 581 | // What an issue waits for: link and unlink, from the issue page. |
| 575 | 582 | router.add( |
| 576 | 583 | "POST", |
src/web/pages/search.ts +27 −4
| @@ -10,6 +10,8 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | import { search } from "../../actions/search.ts"; |
| 13 | import { getPull } from "../../core/pulls.ts"; | |
| 14 | import { canWrite, getRepo } from "../../core/repos.ts"; | |
| 13 | 15 | import { |
| 14 | 16 | searchKind, |
| 15 | 17 | type CodeHit, |
| @@ -379,7 +381,7 @@ interface Destructive { | ||
| 379 | 381 | */ |
| 380 | 382 | const DESTRUCTIVE: { |
| 381 | 383 | pattern: RegExp; |
| 382 | describe: (m: RegExpMatchArray) => Destructive; | |
| 384 | describe: (m: RegExpMatchArray, ctx: Ctx) => Destructive; | |
| 383 | 385 | }[] = [ |
| 384 | 386 | { |
| 385 | 387 | pattern: /^\/([^/]+)\/([^/]+)\/settings\/delete$/, |
| @@ -476,12 +478,33 @@ const DESTRUCTIVE: { | ||
| 476 | 478 | phrase: "", |
| 477 | 479 | }), |
| 478 | 480 | }, |
| 481 | { | |
| 482 | pattern: /^\/([^/]+)\/([^/]+)\/pulls\/(\d+)\/take-back$/, | |
| 483 | describe: (m, ctx) => { | |
| 484 | // The number in the URL is what the route itself will act on; the | |
| 485 | // title shown here is looked up fresh rather than trusted from a | |
| 486 | // link, so a crafted URL cannot put someone else's words on it — and | |
| 487 | // only shown to someone who could take the change back anyway, so | |
| 488 | // this page never becomes a way to read a private pull's title. | |
| 489 | const repo = getRepo(ctx.app.db, m[1]!, m[2]!); | |
| 490 | const writer = repo && canWrite(ctx.app.db, repo, ctx.auth.user); | |
| 491 | const pull = writer ? getPull(ctx.app.db, repo.id, Number(m[3])) : undefined; | |
| 492 | const name = pull ? `"${pull.title}"` : `change #${m[3]}`; | |
| 493 | return { | |
| 494 | verb: "Take it back", | |
| 495 | title: `Take back ${name}?`, | |
| 496 | detail: | |
| 497 | "This opens a new change that undoes exactly what it shipped — nothing changes until a person ships that new change, same as any other.", | |
| 498 | phrase: "", | |
| 499 | }; | |
| 500 | }, | |
| 501 | }, | |
| 479 | 502 | ]; |
| 480 | 503 | |
| 481 | function describeTarget(to: string): Destructive | undefined { | |
| 504 | function describeTarget(to: string, ctx: Ctx): Destructive | undefined { | |
| 482 | 505 | for (const entry of DESTRUCTIVE) { |
| 483 | 506 | const match = to.match(entry.pattern); |
| 484 | if (match) return entry.describe(match); | |
| 507 | if (match) return entry.describe(match, ctx); | |
| 485 | 508 | } |
| 486 | 509 | return undefined; |
| 487 | 510 | } |
| @@ -506,7 +529,7 @@ export function confirmPage(ctx: Ctx): Response { | ||
| 506 | 529 | ) { |
| 507 | 530 | return notFoundPage(ctx); |
| 508 | 531 | } |
| 509 | const known = describeTarget(to); | |
| 532 | const known = describeTarget(to, ctx); | |
| 510 | 533 | // Only routes this file knows are destructive can be confirmed. A crafted |
| 511 | 534 | // link therefore cannot invent reassuring words for a dangerous button, and |
| 512 | 535 | // cannot aim the form at an endpoint that was never meant for it. |
src/web/studio/actions.ts +66
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | import { addComment, setIssueStateThrough } from "../../actions/issues.ts"; |
| 12 | 12 | import { tellCrew } from "../../actions/crew.ts"; |
| 13 | 13 | import { mergePull } from "../../actions/pulls.ts"; |
| 14 | import { revertPull, type RevertPullError } from "../../actions/revert.ts"; | |
| 14 | 15 | import { getPull } from "../../core/pulls.ts"; |
| 15 | 16 | import { bringUpToDate } from "../../crew/autonomy.ts"; |
| 16 | 17 | import type { Ctx } from "../../http/context.ts"; |
| @@ -107,6 +108,71 @@ export async function shipSubmit(ctx: Ctx): Promise<Response> { | ||
| 107 | 108 | }); |
| 108 | 109 | } |
| 109 | 110 | |
| 111 | /** A take-back refusal in the studio's words. */ | |
| 112 | function takeBackWords(error: RevertPullError): string { | |
| 113 | if (typeof error === "string") { | |
| 114 | switch (error) { | |
| 115 | case "not_shipped": | |
| 116 | return "it has not shipped"; | |
| 117 | case "mirrored": | |
| 118 | return "the origin owns this history"; | |
| 119 | case "already_reverted": | |
| 120 | return "it has already been taken back"; | |
| 121 | case "unauthorized": | |
| 122 | return "sign in first"; | |
| 123 | case "forbidden": | |
| 124 | return "only a person can take a change back"; | |
| 125 | case "not_found": | |
| 126 | return "that change was not found"; | |
| 127 | } | |
| 128 | } | |
| 129 | switch (error.kind) { | |
| 130 | case "conflict": | |
| 131 | return `it conflicts with the current code in ${error.files.join(", ")}`; | |
| 132 | case "unsafe": | |
| 133 | return "git refused it"; | |
| 134 | case "base_moved": | |
| 135 | return "the base moved under it"; | |
| 136 | case "git": | |
| 137 | return `git said ${error.message}`; | |
| 138 | case "octopus": | |
| 139 | return "it has more than two parents"; | |
| 140 | case "branch_failed": | |
| 141 | return `it could not be made ready (${error.reason})`; | |
| 142 | case "pull_failed": | |
| 143 | return `the change could not be opened (${error.reason})`; | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | /** | |
| 148 | * POST /:owner/:name/pulls/:number/take-back — open a new change that | |
| 149 | * undoes exactly what a shipped one merged. Never touches the default | |
| 150 | * branch itself (`revertPull` does the computing); this only calls it and | |
| 151 | * says the result in the studio's words. | |
| 152 | */ | |
| 153 | export async function takeBackSubmit(ctx: Ctx): Promise<Response> { | |
| 154 | const repo = loadRepo(ctx); | |
| 155 | if (!repo) return notFoundPage(ctx); | |
| 156 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 157 | const form = await readForm(ctx, "rw"); | |
| 158 | if (form instanceof Response) return form; | |
| 159 | const base = repoBase(repo); | |
| 160 | const number = Number(ctx.params.number); | |
| 161 | const pull = Number.isInteger(number) | |
| 162 | ? getPull(ctx.app.db, repo.id, number) | |
| 163 | : undefined; | |
| 164 | if (!pull) return notFoundPage(ctx); | |
| 165 | const reverted = await revertPull(ctx.app, ctx.auth.user, repo, pull); | |
| 166 | if (!reverted.ok) | |
| 167 | return flashOrJson(ctx, `${base}/pulls/${pull.number}`, { | |
| 168 | tone: "danger", | |
| 169 | message: `That could not be taken back: ${takeBackWords(reverted.error)}.`, | |
| 170 | }); | |
| 171 | return flashOrJson(ctx, `${base}/pulls/${reverted.value.number}`, { | |
| 172 | message: `Opened "${reverted.value.title}" — ship it to take "${pull.title}" back.`, | |
| 173 | }); | |
| 174 | } | |
| 175 | ||
| 110 | 176 | /** POST /:owner/:name/pulls/:number/say { text } — feedback to the crew. */ |
| 111 | 177 | export async function saySubmit(ctx: Ctx): Promise<Response> { |
| 112 | 178 | const repo = loadRepo(ctx); |
src/web/studio/change.ts +30 −2
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | import { appsRuntime } from "../../apps/runtime.ts"; |
| 11 | 11 | import type { AppRecord } from "../../core/apps.ts"; |
| 12 | import { pullStatus, type Pull } from "../../core/pulls.ts"; | |
| 12 | import { pullStatus, revertOf, type Pull } from "../../core/pulls.ts"; | |
| 13 | 13 | import { canWrite, type Repo } from "../../core/repos.ts"; |
| 14 | 14 | import { checkSummary } from "../../core/runs.ts"; |
| 15 | 15 | import { repoDir } from "../../git/repos.ts"; |
| @@ -21,6 +21,7 @@ import { liveClientJs, liveMount } from "../live.ts"; | ||
| 21 | 21 | import { |
| 22 | 22 | btn, |
| 23 | 23 | card, |
| 24 | confirmAction, | |
| 24 | 25 | icon, |
| 25 | 26 | originTag, |
| 26 | 27 | stateBadge, |
| @@ -207,13 +208,40 @@ export async function studioPull( | ||
| 207 | 208 | }), |
| 208 | 209 | ); |
| 209 | 210 | |
| 211 | // A shipped change offers one more verb: open a new change that undoes | |
| 212 | // exactly what it shipped, for someone to ship. Never on a change still | |
| 213 | // open (nothing shipped to take back yet), never on a mirror (the origin | |
| 214 | // owns that history), never twice. The action re-checks all of this and | |
| 215 | // does the git work; this only decides whether to offer the button. | |
| 216 | const takeBackSection = when( | |
| 217 | pull.mergedAt !== null && | |
| 218 | writer && | |
| 219 | repo.origin === null && | |
| 220 | pull.originHeadRepo === null && | |
| 221 | revertOf(db, repo.id, pull.id) === undefined, | |
| 222 | () => | |
| 223 | card( | |
| 224 | html`<p class="muted small"> | |
| 225 | Take it back — opens a new change that undoes exactly what this | |
| 226 | one shipped, for a person to ship. | |
| 227 | </p> | |
| 228 | <div class="bar mt"> | |
| 229 | ${confirmAction({ | |
| 230 | action: `${thread}/take-back`, | |
| 231 | verb: "Take it back", | |
| 232 | size: "sm", | |
| 233 | })} | |
| 234 | </div>`, | |
| 235 | ), | |
| 236 | ); | |
| 237 | ||
| 210 | 238 | const talk = conversation(ctx, repo, pull, { openVerb: "opened this" }); |
| 211 | 239 | |
| 212 | 240 | const body = html`${repoHeader(ctx, repo, "pulls", { |
| 213 | 241 | compact: true, |
| 214 | 242 | ownTitle: true, |
| 215 | 243 | })} |
| 216 | ${head} ${checkLine} ${decisionSection} | |
| 244 | ${head} ${checkLine} ${decisionSection} ${takeBackSection} | |
| 217 | 245 | ${lookCard(ctx, repo, record, pull, pull.headOid)} |
| 218 | 246 | <div class="split"> |
| 219 | 247 | <div> |
test/studio.test.ts +219
| @@ -4859,6 +4859,225 @@ describe("a person's own change ships from its own studio page", () => { | ||
| 4859 | 4859 | }); |
| 4860 | 4860 | }); |
| 4861 | 4861 | |
| 4862 | /** | |
| 4863 | * Issue #190: a shipped change's studio page offers Take it back to a | |
| 4864 | * writer, behind the studio's existing confirm, and pressing it opens a new | |
| 4865 | * change that undoes it. | |
| 4866 | */ | |
| 4867 | describe("take it back: the studio's verb on a shipped change", () => { | |
| 4868 | let dataDir: string; | |
| 4869 | let app: App; | |
| 4870 | let servers: Servers; | |
| 4871 | let base: string; | |
| 4872 | let alice: User; | |
| 4873 | let repo: Repo; | |
| 4874 | let ownerCookie: string; | |
| 4875 | let readerCookie: string; | |
| 4876 | ||
| 4877 | const signIn = async (username: string): Promise<string> => | |
| 4878 | ( | |
| 4879 | ( | |
| 4880 | await fetch(`${base}/signin`, { | |
| 4881 | method: "POST", | |
| 4882 | body: new URLSearchParams({ | |
| 4883 | username, | |
| 4884 | password: "a-good-passphrase", | |
| 4885 | }), | |
| 4886 | redirect: "manual", | |
| 4887 | }) | |
| 4888 | ).headers.get("set-cookie") ?? "" | |
| 4889 | ).split(";")[0]!; | |
| 4890 | ||
| 4891 | const get = (path: string, cookie: string) => | |
| 4892 | fetch(`${base}${path}`, { | |
| 4893 | headers: { cookie: `${cookie}; forge_mode=studio` }, | |
| 4894 | redirect: "manual", | |
| 4895 | }); | |
| 4896 | const post = (path: string, cookie: string, origin = base) => | |
| 4897 | fetch(`${base}${path}`, { | |
| 4898 | method: "POST", | |
| 4899 | headers: { cookie, origin }, | |
| 4900 | redirect: "manual", | |
| 4901 | }); | |
| 4902 | ||
| 4903 | const shipChange = async ( | |
| 4904 | branch: string, | |
| 4905 | title: string, | |
| 4906 | content: string, | |
| 4907 | ): Promise<Pull> => { | |
| 4908 | const committed = await commitToBranch(app, alice, repo, { | |
| 4909 | branch, | |
| 4910 | base: "main", | |
| 4911 | message: title, | |
| 4912 | files: [{ path: "code.txt", content }], | |
| 4913 | }); | |
| 4914 | if (!committed.ok) throw new Error(JSON.stringify(committed.error)); | |
| 4915 | const opened = await openPull(app, alice, repo, { | |
| 4916 | title, | |
| 4917 | baseBranch: "main", | |
| 4918 | headBranch: branch, | |
| 4919 | }); | |
| 4920 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 4921 | const merged = await mergePull(app, alice, repo, opened.value, "squash"); | |
| 4922 | if (!merged.ok) throw new Error(JSON.stringify(merged.error)); | |
| 4923 | return getPull(app.db, repo.id, opened.value.number)!; | |
| 4924 | }; | |
| 4925 | ||
| 4926 | beforeAll(async () => { | |
| 4927 | dataDir = await mkdtemp(join(tmpdir(), "forge-take-back-")); | |
| 4928 | app = createApp({ dataDir, port: 0 }); | |
| 4929 | servers = await serve(app); | |
| 4930 | base = servers.url; | |
| 4931 | const a = await createUser(app.db, { | |
| 4932 | username: "alice", | |
| 4933 | email: "[email protected]", | |
| 4934 | password: "a-good-passphrase", | |
| 4935 | adminIfFirst: true, | |
| 4936 | }); | |
| 4937 | if (!a.ok) throw new Error(a.error); | |
| 4938 | alice = a.value; | |
| 4939 | const r = await createUser(app.db, { | |
| 4940 | username: "eve", | |
| 4941 | email: "[email protected]", | |
| 4942 | password: "a-good-passphrase", | |
| 4943 | }); | |
| 4944 | if (!r.ok) throw new Error(r.error); | |
| 4945 | ownerCookie = await signIn("alice"); | |
| 4946 | readerCookie = await signIn("eve"); | |
| 4947 | const made = await createRepo(app, alice, { | |
| 4948 | name: "orchard", | |
| 4949 | visibility: "public", | |
| 4950 | }); | |
| 4951 | if (!made.ok) throw new Error(String(made.error)); | |
| 4952 | repo = made.value; | |
| 4953 | const seeded = await commitToBranch(app, alice, repo, { | |
| 4954 | message: "Start", | |
| 4955 | files: [ | |
| 4956 | { | |
| 4957 | path: "package.json", | |
| 4958 | content: | |
| 4959 | '{"name":"orchard","scripts":{"start":"bun run server.ts"}}\n', | |
| 4960 | }, | |
| 4961 | { | |
| 4962 | path: "server.ts", | |
| 4963 | content: | |
| 4964 | "Bun.serve({ port: Number(process.env.PORT), fetch: () => new Response('ok') });\n", | |
| 4965 | }, | |
| 4966 | { path: "code.txt", content: "alpha\n" }, | |
| 4967 | ], | |
| 4968 | }); | |
| 4969 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 4970 | const enabled = await enableApp(app, alice, repo, { slug: "orchard" }); | |
| 4971 | if (!enabled.ok) throw new Error(String(enabled.error)); | |
| 4972 | }); | |
| 4973 | ||
| 4974 | afterAll(async () => { | |
| 4975 | servers.stop(); | |
| 4976 | app.close(); | |
| 4977 | await rm(dataDir, { recursive: true, force: true }); | |
| 4978 | }); | |
| 4979 | ||
| 4980 | test("a shipped change offers Take it back to a writer, not to a reader, and not while it is still open", async () => { | |
| 4981 | const branch = await commitToBranch(app, alice, repo, { | |
| 4982 | branch: "feature/loud", | |
| 4983 | base: "main", | |
| 4984 | message: "Make it loud", | |
| 4985 | files: [{ path: "code.txt", content: "alpha\nBETA\n" }], | |
| 4986 | }); | |
| 4987 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 4988 | const opened = await openPull(app, alice, repo, { | |
| 4989 | title: "Make it loud", | |
| 4990 | baseBranch: "main", | |
| 4991 | headBranch: "feature/loud", | |
| 4992 | }); | |
| 4993 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 4994 | const pull = opened.value; | |
| 4995 | ||
| 4996 | const whileOpen = await ( | |
| 4997 | await get(`/alice/orchard/pulls/${pull.number}`, ownerCookie) | |
| 4998 | ).text(); | |
| 4999 | expect(whileOpen).not.toContain("Take it back"); | |
| 5000 | ||
| 5001 | const merged = await mergePull(app, alice, repo, pull, "squash"); | |
| 5002 | if (!merged.ok) throw new Error(JSON.stringify(merged.error)); | |
| 5003 | ||
| 5004 | const ownerPage = await ( | |
| 5005 | await get(`/alice/orchard/pulls/${pull.number}`, ownerCookie) | |
| 5006 | ).text(); | |
| 5007 | expect(ownerPage).toContain("Take it back"); | |
| 5008 | expect(ownerPage).toContain( | |
| 5009 | `/-/confirm?to=%2Falice%2Forchard%2Fpulls%2F${pull.number}%2Ftake-back`, | |
| 5010 | ); | |
| 5011 | ||
| 5012 | const readerPage = await ( | |
| 5013 | await get(`/alice/orchard/pulls/${pull.number}`, readerCookie) | |
| 5014 | ).text(); | |
| 5015 | expect(readerPage).not.toContain("Take it back"); | |
| 5016 | ||
| 5017 | const confirm = await ( | |
| 5018 | await get( | |
| 5019 | `/-/confirm?to=${encodeURIComponent(`/alice/orchard/pulls/${pull.number}/take-back`)}`, | |
| 5020 | ownerCookie, | |
| 5021 | ) | |
| 5022 | ).text(); | |
| 5023 | expect(confirm).toContain("Take back"); | |
| 5024 | expect(confirm).toContain("Make it loud"); | |
| 5025 | expect(confirm).toContain("opens a new change that undoes exactly what it shipped"); | |
| 5026 | }); | |
| 5027 | ||
| 5028 | test("pressing Take it back opens a new change and lands on its studio page", async () => { | |
| 5029 | const shipped = await shipChange("feature/gamma", "Add gamma", "alpha\nGAMMA\n"); | |
| 5030 | ||
| 5031 | const taken = await post( | |
| 5032 | `/alice/orchard/pulls/${shipped.number}/take-back`, | |
| 5033 | ownerCookie, | |
| 5034 | ); | |
| 5035 | expect(taken.status).toBe(303); | |
| 5036 | const location = taken.headers.get("location")!; | |
| 5037 | expect(location).toBe(`/alice/orchard/pulls/${shipped.number + 1}`); | |
| 5038 | ||
| 5039 | const revertPage = await (await get(location, ownerCookie)).text(); | |
| 5040 | expect(revertPage).toContain(`Revert "Add gamma"`); | |
| 5041 | }); | |
| 5042 | ||
| 5043 | test("the POST is refused from another origin, and nothing is created", async () => { | |
| 5044 | const shipped = await shipChange("feature/delta", "Add delta", "alpha\nDELTA\n"); | |
| 5045 | ||
| 5046 | const forged = await post( | |
| 5047 | `/alice/orchard/pulls/${shipped.number}/take-back`, | |
| 5048 | ownerCookie, | |
| 5049 | "https://evil.example", | |
| 5050 | ); | |
| 5051 | expect(forged.status).toBe(403); | |
| 5052 | expect(getPull(app.db, repo.id, shipped.number + 1)).toBeUndefined(); | |
| 5053 | }); | |
| 5054 | ||
| 5055 | test("a refusal is said in the studio's own words, and takes nothing back twice", async () => { | |
| 5056 | const shipped = await shipChange("feature/epsilon", "Add epsilon", "alpha\nEPSILON\n"); | |
| 5057 | const first = await post( | |
| 5058 | `/alice/orchard/pulls/${shipped.number}/take-back`, | |
| 5059 | ownerCookie, | |
| 5060 | ); | |
| 5061 | expect(first.status).toBe(303); | |
| 5062 | ||
| 5063 | const again = await post( | |
| 5064 | `/alice/orchard/pulls/${shipped.number}/take-back`, | |
| 5065 | ownerCookie, | |
| 5066 | ); | |
| 5067 | expect(again.status).toBe(303); | |
| 5068 | const location = again.headers.get("location")!; | |
| 5069 | expect(location).toBe(`/alice/orchard/pulls/${shipped.number}`); | |
| 5070 | const flash = decodeURIComponent(again.headers.get("set-cookie") ?? ""); | |
| 5071 | expect(flash).toContain("already been taken back"); | |
| 5072 | ||
| 5073 | // Only one revert pull exists — the second attempt created nothing. | |
| 5074 | expect( | |
| 5075 | getPull(app.db, repo.id, shipped.number + 1)?.title, | |
| 5076 | ).toBe('Revert "Add epsilon"'); | |
| 5077 | expect(getPull(app.db, repo.id, shipped.number + 2)).toBeUndefined(); | |
| 5078 | }); | |
| 5079 | }); | |
| 5080 | ||
| 4862 | 5081 | describe("the studio's crew panel on a forge with no agents yet", () => { |
| 4863 | 5082 | let dataDir: string; |
| 4864 | 5083 | let app: App; |
new above
offline