Public: anyone can read this; the crew can change it.
A person on a team as reviewer can approve or request changes on a pull, and still cannot merge it #12632
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12630-a-person-on-a-team-as-reviewer-can-appro → main 2 commits fork
3 files
changed +102
−12
src/actions/pulls.ts +9 −11
| @@ -17,7 +17,7 @@ import { resolveOpen } from "../core/decisions.ts"; | ||
| 17 | 17 | import type { App } from "../app.ts"; |
| 18 | 18 | import { applyBoxPass, holdPass, landHeldPass, type BoxPass } from "./box_pass.ts"; |
| 19 | 19 | import { landVision } from "../crew/vision.ts"; |
| 20 | import { crewRole, isCrew } from "../core/agents.ts"; | |
| 20 | import { crewRole } from "../core/agents.ts"; | |
| 21 | 21 | import { |
| 22 | 22 | getIssueById, |
| 23 | 23 | setAssignee, |
| @@ -95,6 +95,7 @@ import { | ||
| 95 | 95 | type AccessError, |
| 96 | 96 | } from "./access.ts"; |
| 97 | 97 | import { canWrite } from "../core/repos.ts"; |
| 98 | import { canVerdict } from "../core/verdict.ts"; | |
| 98 | 99 | import { refreshPullHead as refreshHead } from "../core/following.ts"; |
| 99 | 100 | import { getOriginRepo } from "../core/origins.ts"; |
| 100 | 101 | import { watchPullsSoon } from "../origins/hook.ts"; |
| @@ -723,8 +724,9 @@ export interface SubmitReviewInput { | ||
| 723 | 724 | * Pass a verdict on a pull. |
| 724 | 725 | * |
| 725 | 726 | * Two rules, and both are mechanical rather than advisory. **A verdict with |
| 726 | * authority takes write access**: anyone who can read the repo may leave a | |
| 727 | * `commented` review, and only a writer may approve or request changes — | |
| 727 | * authority takes standing** (`canVerdict`): anyone who can read the repo may | |
| 728 | * leave a `commented` review, and only a writer, the crew, or a person a team | |
| 729 | * names `reviewer` may approve or request changes — | |
| 728 | 730 | * otherwise "require review" would be satisfiable by any account that could |
| 729 | 731 | * see the repository. And **you cannot approve your own change**, which is |
| 730 | 732 | * the whole point of requiring one. |
| @@ -749,14 +751,10 @@ export async function submitReview( | ||
| 749 | 751 | if (!participant.ok) return participant; |
| 750 | 752 | const user = participant.value; |
| 751 | 753 | if (pull.repoId !== repo.id) return err("not_found"); |
| 752 | // A verdict that gates a merge is a writer's, or the crew's — a | |
| 753 | // reviewer on the crew requests changes and the builder answers, while | |
| 754 | // the merge itself stays a person's; a comment is anyone's. | |
| 755 | if ( | |
| 756 | input.state !== "commented" && | |
| 757 | !canWrite(app.db, repo, user) && | |
| 758 | !(user.kind === "agent" && isCrew(app.db, repo.id, user.id)) | |
| 759 | ) | |
| 754 | // A verdict that gates a merge is a writer's, the crew's, or a team | |
| 755 | // reviewer's (`canVerdict`); the merge itself stays a writer's, and a | |
| 756 | // comment is anyone's. | |
| 757 | if (input.state !== "commented" && !canVerdict(app.db, repo, user)) | |
| 760 | 758 | return err("forbidden"); |
| 761 | 759 | |
| 762 | 760 | const lines = input.comments ?? []; |
src/core/verdict.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | /** | |
| 2 | * Who may pass a verdict on a pull — approve it or request changes (#12630). | |
| 3 | * | |
| 4 | * A verdict that gates a merge is a writer's; the crew's agents, whose | |
| 5 | * reviewer asks for changes and whose builder answers; and a person whose | |
| 6 | * role on a team of the repository's org is `reviewer` — someone trusted to | |
| 7 | * judge a change without being trusted to land it. The merge itself stays | |
| 8 | * with the writers (`requireWriter` in `mergePull`), and a protected default | |
| 9 | * branch's own-approval rule still counts only a writer's (`pull_trust.ts`). | |
| 10 | * A `commented` review is anyone's who can read, and is not asked here. | |
| 11 | */ | |
| 12 | import type { Database } from "bun:sqlite"; | |
| 13 | import { isCrew } from "./agents.ts"; | |
| 14 | import type { User } from "./users.ts"; | |
| 15 | import { canWrite, type Repo } from "./repos.ts"; | |
| 16 | ||
| 17 | export function canVerdict(db: Database, repo: Repo, user: User): boolean { | |
| 18 | if (canWrite(db, repo, user)) return true; | |
| 19 | if (user.kind === "agent") return isCrew(db, repo.id, user.id); | |
| 20 | return user.kind === "user" && reviewsOnTeam(db, repo, user); | |
| 21 | } | |
| 22 | ||
| 23 | /** The person holds the `reviewer` role on a team of the org that owns the repository. */ | |
| 24 | function reviewsOnTeam(db: Database, repo: Repo, user: User): boolean { | |
| 25 | return ( | |
| 26 | db | |
| 27 | .query<{ n: number }, [string, string]>( | |
| 28 | `SELECT 1 AS n FROM team | |
| 29 | JOIN team_member ON team_member.team_id = team.id | |
| 30 | WHERE team.org_id = ?1 AND team_member.user_id = ?2 AND team_member.role = 'reviewer' | |
| 31 | LIMIT 1`, | |
| 32 | ) | |
| 33 | .get(repo.ownerId, user.id) !== null | |
| 34 | ); | |
| 35 | } |
test/reviews-states.test.ts +58 −1
| @@ -87,7 +87,7 @@ describe("review states, and the merge they gate", () => { | ||
| 87 | 87 | |
| 88 | 88 | await seedAdmin(app, { username: "ada", email: "[email protected]", password: PASSWORD }); |
| 89 | 89 | await mintToken("ada"); |
| 90 | for (const who of ["bob", "cas"]) await mkUser(who); | |
| 90 | for (const who of ["bob", "cas", "dee"]) await mkUser(who); | |
| 91 | 91 | |
| 92 | 92 | // An org, so there is more than one writer: a repo with one owner cannot |
| 93 | 93 | // express "somebody else approved this". |
| @@ -110,6 +110,24 @@ describe("review states, and the merge they gate", () => { | ||
| 110 | 110 | ).status, |
| 111 | 111 | ).toBe(201); |
| 112 | 112 | |
| 113 | // dee judges and does not land: a read team names them its reviewer (#12630). | |
| 114 | expect( | |
| 115 | ( | |
| 116 | await as("ada", "POST", "/api/v1/orgs/acme/teams", { | |
| 117 | name: "judges", | |
| 118 | permission: "read", | |
| 119 | }) | |
| 120 | ).status, | |
| 121 | ).toBe(201); | |
| 122 | expect( | |
| 123 | ( | |
| 124 | await as("ada", "POST", "/api/v1/orgs/acme/teams/judges/members", { | |
| 125 | user: "dee", | |
| 126 | role: "reviewer", | |
| 127 | }) | |
| 128 | ).status, | |
| 129 | ).toBe(201); | |
| 130 | ||
| 113 | 131 | expect( |
| 114 | 132 | ( |
| 115 | 133 | await as("ada", "POST", "/api/v1/repos", { |
| @@ -172,6 +190,45 @@ describe("review states, and the merge they gate", () => { | ||
| 172 | 190 | expect(listed.map((r) => r.state)).toEqual(["commented"]); |
| 173 | 191 | }); |
| 174 | 192 | |
| 193 | test("a team reviewer may approve and request changes, and still may not merge", async () => { | |
| 194 | await as("ada", "PATCH", "/api/v1/repos/acme/lib", { requireReview: true }); | |
| 195 | const number = await proposeFrom("bob", "judged", "judged\n", "judged work"); | |
| 196 | const pull = `/api/v1/repos/acme/lib/pulls/${number}`; | |
| 197 | ||
| 198 | const asked = await as("dee", "POST", `${pull}/review-states`, { | |
| 199 | state: "changes_requested", | |
| 200 | body: "one more pass", | |
| 201 | }); | |
| 202 | expect(asked.status).toBe(201); | |
| 203 | expect((await read<{ state: string }>(asked)).state).toBe("changes_requested"); | |
| 204 | ||
| 205 | const approved = await as("dee", "POST", `${pull}/review-states`, { | |
| 206 | state: "approved", | |
| 207 | }); | |
| 208 | expect(approved.status).toBe(201); | |
| 209 | expect((await read<{ state: string }>(approved)).state).toBe("approved"); | |
| 210 | ||
| 211 | // require_review counts their approval the way it counts a writer's. | |
| 212 | const status = await read<{ | |
| 213 | status: { reviews: { approved: number; mergeable: boolean } }; | |
| 214 | }>(await as("ada", "GET", pull)); | |
| 215 | expect(status.status.reviews.approved).toBe(1); | |
| 216 | expect(status.status.reviews.mergeable).toBe(true); | |
| 217 | ||
| 218 | // Judging is not landing: dee cannot write, so dee cannot merge. | |
| 219 | const merge = await as("dee", "POST", `${pull}/merge`, {}); | |
| 220 | expect(merge.status).toBe(403); | |
| 221 | expect((await read<{ error: string }>(merge)).error).toBe("forbidden"); | |
| 222 | ||
| 223 | // A writer's verdict and merge are unchanged. | |
| 224 | expect( | |
| 225 | (await as("ada", "POST", `${pull}/review-states`, { state: "approved" })).status, | |
| 226 | ).toBe(201); | |
| 227 | expect((await as("ada", "POST", `${pull}/merge`, {})).status).toBe(200); | |
| 228 | ||
| 229 | await as("ada", "PATCH", "/api/v1/repos/acme/lib", { requireReview: false }); | |
| 230 | }); | |
| 231 | ||
| 175 | 232 | test("you cannot approve your own pull request", async () => { |
| 176 | 233 | const number = await proposeFrom("bob", "topic-2", "three\n", "add three"); |
| 177 | 234 | const refused = await as( |
new above
offline