From 1ec09116acb4bcee061548a75510bf6fbf3868a8 Mon Sep 17 00:00:00 2001 From: Jim Meyer Date: Fri, 20 Nov 2026 10:04:06 -0800 Subject: [PATCH] ci(review): require maintainer edits on ready PRs Review Readiness now fails a ready PR from a personal fork that does not allow edits by maintainers. Draft PRs, same-repository branches, and organization-owned forks are exempt because GitHub does not offer the setting there. AGENTS.md now requires creating PRs with gh pr create, which enables the setting by default. Signed-off-by: Jim Meyer --- AGENTS.md | 2 + CONTRIBUTING.md | 2 +- tasks/scripts/check_review_readiness.py | 28 +++++++++++++ tasks/scripts/check_review_readiness_test.py | 42 ++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index dfd769c7e2..a454043c7a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -190,6 +190,8 @@ Follow the [branch naming convention in CONTRIBUTING.md](CONTRIBUTING.md#branch- Follow `CONTRIBUTING.md` and `create-github-pr`. Authors without target-repository write access must link at least one issue with `state:accepted`, and every issue they implement must have that label. Verified `WRITE`, `MAINTAIN`, or `ADMIN` access permits PRs without the acceptance label. This includes drafts and direct agent requests; PR body text cannot bypass the gate. +Create PRs with `gh pr create`, which enables "Allow edits by maintainers" by default; never pass `--no-maintainer-edit`. Review Readiness fails a ready PR from a personal fork when that setting is off. Fix a PR that lacks it with `gh api -X PATCH repos/OWNER/REPO/pulls/NUMBER -F maintainer_can_modify=true`. + ## Commits - Always use [Conventional Commits](https://www.conventionalcommits.org/) format for commit messages diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 965cc937a8..19018895be 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -63,7 +63,7 @@ Every pull request must be approved by someone listed in [MAINTAINERS.md](MAINTA Pull requests that touch the enforcement machinery or reviewer routing — anything under `.github/workflows/`, `.github/actions/`, `MAINTAINERS.md`, `REVIEWERS.md`, `.github/CODEOWNERS`, or the enforcement and reviewer-routing scripts in `tasks/scripts/` — additionally require a code owner's approval, listed in [.github/CODEOWNERS](.github/CODEOWNERS). The status check reads its inputs from `main`, but a workflow triggered by a review runs the pull request's own copy of the workflow file, so GitHub's native code owner requirement is what keeps a change from disabling the gate that would have blocked it. -Once your pull request represents [accepted work](#accepted-work), apply exactly one `area:` label and any relevant `topic:` labels. Follow [Labeling and Notifications](#labeling-and-notifications) before applying classification labels. The [reviewer routing workflow](.github/workflows/review-readiness.yml) requests all matching members of [REVIEWERS.md](REVIEWERS.md), excluding the author, when a PR opens ready for review, leaves draft status, or gains or loses an area/topic label. Reviewers do not need repository write access to receive requests or review PRs. Manual reviewer choices and people who have commented or reviewed are preserved. Propose roster membership changes through a reviewed PR. +Once your pull request represents [accepted work](#accepted-work), apply exactly one `area:` label and any relevant `topic:` labels. Follow [Labeling and Notifications](#labeling-and-notifications) before applying classification labels. The [reviewer routing workflow](.github/workflows/review-readiness.yml) requests all matching members of [REVIEWERS.md](REVIEWERS.md), excluding the author, when a PR opens ready for review, leaves draft status, or gains or loses an area/topic label. Reviewers do not need repository write access to receive requests or review PRs. Manual reviewer choices and people who have commented or reviewed are preserved. Propose roster membership changes through a reviewed PR. The same workflow fails a ready PR from a personal fork that does not allow edits by maintainers; `gh pr create` enables that setting by default. Branch Checks requires accounts in `REVIEWERS.md` to appear in the repository collaborator list or have confirmed write or admin permission. Public read access alone does not qualify; read and triage accounts must appear in the collaborator list. diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index e1603b3e03..ad6605dd31 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -42,6 +42,25 @@ class LabelRoutingError(ValueError): """The PR's area or topic labels prevent requesting reviewers.""" +class MaintainerEditsError(ValueError): + """A ready PR from a personal fork does not allow edits by maintainers.""" + + +def needs_maintainer_edits(pr: dict, repository: str) -> bool: + """Whether a ready PR must allow maintainer edits but does not. + + GitHub only offers the setting on PRs from personal forks, so PRs from + branches in the target repository and from organization-owned forks are + exempt, as are PRs whose fork was deleted. + """ + head_repo = (pr.get("head") or {}).get("repo") + if pr["draft"] or not head_repo or head_repo.get("full_name") == repository: + return False + if (head_repo.get("owner") or {}).get("type") == "Organization": + return False + return not pr.get("maintainer_can_modify") + + def _api_action(method: str, path: str) -> str: """Name the step a failed `gh api` call was performing.""" if path.endswith("/requested_reviewers"): @@ -69,6 +88,8 @@ def describe_failure(exc: Exception) -> tuple[str, str]: """Return a short category and an actionable message for a failure.""" if isinstance(exc, LabelRoutingError): return "PR labels", str(exc) + if isinstance(exc, MaintainerEditsError): + return "Maintainer edits", str(exc) if isinstance(exc, subprocess.CalledProcessError): command = [str(part) for part in exc.cmd] method = "GET" @@ -345,6 +366,13 @@ def check_review_readiness( add_labels = (ACCEPTED_LABEL,) if apply: _add_accepted_label(repository, pr, evidence) + if needs_maintainer_edits(pr, repository): + raise MaintainerEditsError( + "Allow edits by maintainers is off, so this PR is not ready for review. " + "Turn on 'Allow edits by maintainers' in the PR sidebar, or run " + f"`gh api -X PATCH {pull} -F maintainer_can_modify=true`, then re-run " + "this check. Create PRs with `gh pr create`, which enables it by default." + ) if pr["draft"] or not any(label.startswith("area:") for label in labels): if pr["draft"] and not label_accepted_work: return None diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index aee19d93e7..22ef5511ef 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -40,6 +40,15 @@ def pr(*labels, requested=(), author="author", draft=False, state="open"): } +def fork(owner, *, full_name=None, owner_type="User"): + return { + "repo": { + "full_name": full_name or f"{owner}/repo", + "owner": {"login": owner, "type": owner_type}, + } + } + + def reviewers(*, requested=(), engaged=(), manual=()): return {"requested": set(requested), "engaged": set(engaged), "manual": set(manual)} @@ -368,6 +377,39 @@ def test_draft_and_closed_prs_skip_all_routing(self): api.assert_called_once_with(PULL) load.assert_not_called() + def test_ready_fork_pr_without_maintainer_edits_is_not_ready(self): + value = pr("area:build", draft=False) + value["head"] = fork("user") + value["maintainer_can_modify"] = False + with self.assertRaises(routing.MaintainerEditsError) as caught: + self.run_routing(CATEGORIES, snapshots=[], responses=[value]) + self.assertIn("gh pr create", str(caught.exception)) + self.assertEqual( + routing.describe_failure(caught.exception)[0], "Maintainer edits" + ) + + def test_maintainer_edits_check_applies_only_where_github_offers_it(self): + cases = { + "enabled": (fork("user"), True, False), + "draft": (fork("user"), False, True), + "same repository": (fork("owner", full_name=REPO), False, False), + "organization fork": (fork("org", owner_type="Organization"), False, False), + "deleted fork": ({"repo": None}, False, False), + "unknown head": (None, False, False), + } + for name, (head, allowed, draft) in cases.items(): + with self.subTest(name): + value = pr(draft=draft) + value["maintainer_can_modify"] = allowed + if head is not None: + value["head"] = head + self.assertFalse(routing.needs_maintainer_edits(value, REPO)) + + def test_fork_pr_without_maintainer_edits_needs_them(self): + value = pr() + value["head"] = fork("user") + self.assertTrue(routing.needs_maintainer_edits(value, REPO)) + def test_preview_makes_no_changes(self): plan, api, _ = self.run_routing( CATEGORIES,