From db05a0529d198f37fdf7c47c19eef0659399cec4 Mon Sep 17 00:00:00 2001 From: Jim Meyer Date: Fri, 23 Oct 2026 05:53:09 +0000 Subject: [PATCH 1/6] ci(review): draft unaccepted work in readiness check Signed-off-by: Jim Meyer --- tasks/scripts/check_review_readiness.py | 152 +++++++++++++---- tasks/scripts/check_review_readiness_test.py | 168 ++++++++++++++++++- 2 files changed, 283 insertions(+), 37 deletions(-) diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index 46a3f1aefa..084f45bb35 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -7,11 +7,11 @@ # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -"""Reconcile PR reviewers from the merged REVIEWERS.md and current PR metadata. +"""Validate accepted work and reconcile reviewers from the merged REVIEWERS.md. -Preview with --repo OWNER/REPO --pr NUMBER; add --apply to update requests. -For newly opened PRs, --label-accepted-work also adds state:accepted if qualified. -Request all matching roster members, excluding the author. Preserve +Preview with --repo OWNER/REPO --pr NUMBER; add --apply to update the PR. +Unaccepted work is moved to draft before any reviewer requests are changed. +Request all matching members with write access, excluding the author. Preserve manual choices, unknown request provenance, and anyone who commented or reviewed. The parser and planning helpers are importable. No state is stored between runs. """ @@ -200,6 +200,94 @@ def _load_reviewers(repository: str, number: int, pr: dict) -> dict[str, set[str } +def _eligible_reviewers(repository: str, logins: set[str]) -> set[str]: + eligible = set() + for login in sorted(logins): + value = _api(f"repos/{repository}/collaborators/{login}/permission") + if value.get("permission") in {"write", "maintain", "admin"}: + eligible.add(login) + return eligible + + +def _accepted_work(repository: str, number: int, pr: dict) -> bool: + """Accept write access or a reference to an accepted issue filed by the author.""" + author = pr["user"]["login"].lower() + escaped = re.escape(repository) + references = ( + rf"https://github\.com/{escaped}/issues/(\d+)\b" + rf"|(? ReviewPlan | None: - """Plan labeling and routing; callers serialize runs for the same PR. - - Enable label_accepted_work for newly opened PRs, including drafts. Acceptance - is independent of readiness and classification; only reviewer routing needs - a ready PR with an area label. Labeling never removes an existing label. - """ + """Validate acceptance, then route. Callers must serialize runs for the same PR.""" pull = f"repos/{repository}/pulls/{number}" pr = _api(pull) if pr["state"] != "open": return None - accepted = None - add_labels = () - labels = {label["name"] for label in pr["labels"]} - if label_accepted_work: - accepted = check_pr_accepted_work( - repository, pr, maintainers or set(), api=_api + if not _accepted_work(repository, number, pr): + if apply: + _api( + "graphql", + method="POST", + data={ + "query": """ + mutation($id: ID!) { + convertPullRequestToDraft(input: {pullRequestId: $id}) { + pullRequest { isDraft } + } + } + """, + "variables": {"id": pr["node_id"]}, + }, + ) + raise ValueError( + "PR is not accepted work: the author needs repository write access " + "or a referenced state:accepted issue they submitted themselves" ) - if accepted and ACCEPTED_LABEL not in labels: - add_labels = (ACCEPTED_LABEL,) - if apply: - _api( - f"repos/{repository}/issues/{number}/labels", - method="POST", - data={"labels": list(add_labels)}, - ) - if pr["draft"] or not any(label.startswith("area:") for label in labels): - if pr["draft"] and not label_accepted_work: - return None - return ReviewPlan((), (), accepted, add_labels) - selected = select_reviewers(categories, labels) - selected -= {pr["user"]["login"].lower()} - plan = replace( - plan_review_requests(selected, **_load_reviewers(repository, number, pr)), - accepted_work=accepted, - add_labels=add_labels, - ) + selected = select_reviewers(categories, {label["name"] for label in pr["labels"]}) + selected = _eligible_reviewers(repository, selected - {pr["user"]["login"].lower()}) + plan = plan_review_requests(selected, **_load_reviewers(repository, number, pr)) if apply: endpoint = f"{pull}/requested_reviewers" if plan.add: @@ -287,7 +367,7 @@ def main(argv: list[str] | None = None) -> int: parser.add_argument("--repo", required=True, help="target OWNER/REPO") parser.add_argument("--pr", required=True, type=int, help="pull request number") parser.add_argument( - "--apply", action="store_true", help="apply requests; otherwise preview JSON" + "--apply", action="store_true", help="update requests or draft unaccepted work" ) args = parser.parse_args(argv) if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", args.repo) or args.pr <= 0: diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index 67ec2ea0a4..f8e4ee27e2 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -28,12 +28,14 @@ PULL = "repos/owner/repo/pulls/42" -def pr(*labels, requested=(), author="author", draft=False, state="open"): +def pr(*labels, requested=(), author="author", draft=False, state="open", body=""): return { "number": 42, "state": state, "draft": draft, "user": {"login": author}, + "node_id": "PR_node_42", + "body": body, "labels": [{"name": label} for label in labels], "requested_reviewers": [{"login": login} for login in requested], } @@ -259,7 +261,171 @@ def test_loads_all_engagement_and_timeline_feeds(self): self.assertIn("/timeline?per_page=100", api.call_args.args[0]) +class AcceptanceTests(unittest.TestCase): + @staticmethod + def response( + *, accepted=(), linked=(), permission="READ", collaborator="author", + repository=REPO, more=(), + ): + def page(key): + return { + "hasNextPage": key in more, + "endCursor": "next-page" if key in more else None, + } + + return {"data": {"repository": { + "collaborators": { + "edges": [{"node": {"login": collaborator}, "permission": permission}], + "pageInfo": page("collaboratorCursor"), + }, + "issues": { + "nodes": [{"number": n} for n in accepted], + "pageInfo": page("acceptedCursor"), + }, + "pullRequest": {"closingIssuesReferences": { + "nodes": [ + {"number": n, "repository": {"nameWithOwner": repository}} + for n in linked + ], + "pageInfo": page("linkedCursor"), + }}, + }}} + + def test_write_maintain_and_admin_authors_need_no_issue(self): + for permission in ("WRITE", "MAINTAIN", "ADMIN"): + with ( + self.subTest(permission=permission), + mock.patch.object( + routing, "_api", return_value=self.response(permission=permission) + ), + ): + self.assertTrue(routing._accepted_work(REPO, 42, pr(body="See #9999999"))) + + def test_collaborator_search_result_must_match_the_pr_author(self): + with mock.patch.object( + routing, "_api", + return_value=self.response(permission="ADMIN", collaborator="someone-else"), + ): + self.assertFalse(routing._accepted_work(REPO, 42, pr())) + + def test_queries_only_accepted_issues_submitted_by_the_pr_author(self): + with mock.patch.object(routing, "_api", return_value=self.response()) as api: + self.assertFalse( + routing._accepted_work(REPO, 42, pr(author="AUTHOR", body="Related to #1")) + ) + query = api.call_args.kwargs["data"]["query"] + self.assertIn('labels: ["state:accepted"]', query) + self.assertIn("filterBy: {createdBy: $author}", query) + self.assertIn("states: [OPEN, CLOSED]", query) + self.assertEqual(api.call_args.kwargs["data"]["variables"]["author"], "author") + + def test_description_and_closing_references_must_intersect_own_accepted_issues(self): + for body in ( + "", "Related to #1", "Related to owner/repo#1", "Related to OWNER/REPO#1", + "Related to https://github.com/owner/repo/issues/1", + ): + for accepted, expected in (((), False), ((2,), False), ((1,), True)): + with ( + self.subTest(body=body, accepted=accepted), + mock.patch.object( + routing, "_api", + return_value=self.response( + accepted=accepted, linked=() if body else (1,), + ), + ), + ): + self.assertEqual( + routing._accepted_work(REPO, 42, pr(body=body)), expected + ) + + def test_one_qualifying_issue_is_enough(self): + with mock.patch.object( + routing, "_api", return_value=self.response(accepted=(2,), linked=(1, 2)) + ): + self.assertTrue(routing._accepted_work(REPO, 42, pr())) + + def test_reads_later_accepted_issue_closing_reference_and_collaborator_pages(self): + for cursor in ("acceptedCursor", "linkedCursor", "collaboratorCursor"): + if cursor == "acceptedCursor": + first = self.response(linked=(1,), more=(cursor,)) + second = self.response(accepted=(1,)) + elif cursor == "linkedCursor": + first = self.response(accepted=(1,), more=(cursor,)) + second = self.response(linked=(1,)) + else: + first = self.response(more=(cursor,)) + second = self.response(permission="WRITE") + with ( + self.subTest(cursor=cursor), + mock.patch.object(routing, "_api", side_effect=[first, second]) as api, + ): + self.assertTrue(routing._accepted_work(REPO, 42, pr())) + self.assertEqual( + api.call_args.kwargs["data"]["variables"][cursor], "next-page" + ) + + def test_cross_repository_references_do_not_qualify(self): + with mock.patch.object( + routing, "_api", + return_value=self.response(accepted=(1,), linked=(1,), repository="other/repo"), + ): + self.assertFalse(routing._accepted_work( + REPO, 42, + pr(body="Related to other/repo#1 and https://github.com/other/repo/issues/1"), + )) + + def test_rejection_drafts_before_label_validation_or_reviewer_changes(self): + for apply in (False, True): + with ( + self.subTest(apply=apply), + mock.patch.object(routing, "_api", side_effect=[pr(), None]) as api, + mock.patch.object(routing, "_accepted_work", return_value=False), + mock.patch.object(routing, "_load_reviewers") as load, + ): + with self.assertRaisesRegex(ValueError, "not accepted work"): + routing.check_review_readiness(CATEGORIES, REPO, 42, apply=apply) + load.assert_not_called() + self.assertEqual(api.call_count, 2 if apply else 1) + if apply: + self.assertEqual(api.call_args.args, ("graphql",)) + self.assertEqual(api.call_args.kwargs["method"], "POST") + self.assertIn( + "convertPullRequestToDraft", api.call_args.kwargs["data"]["query"] + ) + self.assertEqual( + api.call_args.kwargs["data"]["variables"], {"id": "PR_node_42"} + ) + + def test_lookup_errors_fail_without_drafting_or_requesting_reviewers(self): + with ( + mock.patch.object(routing, "_api", side_effect=[ + pr(), subprocess.CalledProcessError(1, "gh"), + ]) as api, + mock.patch.object(routing, "_load_reviewers") as load, + self.assertRaises(subprocess.CalledProcessError), + ): + routing.check_review_readiness(CATEGORIES, REPO, 42, apply=True) + self.assertEqual(api.call_count, 2) + load.assert_not_called() + + def test_draft_conversion_errors_propagate(self): + with ( + mock.patch.object( + routing, "_api", + side_effect=[pr(), subprocess.CalledProcessError(1, "gh")], + ), + mock.patch.object(routing, "_accepted_work", return_value=False), + self.assertRaises(subprocess.CalledProcessError), + ): + routing.check_review_readiness(CATEGORIES, REPO, 42, apply=True) + + class ReconciliationTests(unittest.TestCase): + def setUp(self): + self.accepted = mock.patch.object(routing, "_accepted_work", return_value=True) + self.accepted.start() + self.addCleanup(self.accepted.stop) + def run_routing(self, categories, *, snapshots, responses, apply=True): with ( mock.patch.object(routing, "_api", side_effect=responses) as api, From 7acea87a725f78481f04d92881beebea6737f57c Mon Sep 17 00:00:00 2001 From: Jim Meyer Date: Mon, 26 Oct 2026 21:06:41 +0000 Subject: [PATCH 2/6] ci(review): check only referenced work at readiness Signed-off-by: Jim Meyer --- .github/workflows/review-readiness.yml | 11 +- tasks/scripts/check_review_readiness.py | 172 ++++++----- tasks/scripts/check_review_readiness_test.py | 308 +++++++++---------- 3 files changed, 242 insertions(+), 249 deletions(-) diff --git a/.github/workflows/review-readiness.yml b/.github/workflows/review-readiness.yml index cb8936d6d1..d68977df00 100644 --- a/.github/workflows/review-readiness.yml +++ b/.github/workflows/review-readiness.yml @@ -47,12 +47,7 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} GH_REPO: ${{ github.repository }} PR_NUMBER: ${{ github.event.pull_request.number }} - PR_ACTION: ${{ github.event.action }} - shell: bash + PR_EVENT: ${{ github.event.action }} run: | - set -euo pipefail - args=(--repo "$GH_REPO" --pr "$PR_NUMBER" --apply) - if [ "$PR_ACTION" = opened ]; then - args+=(--label-accepted-work) - fi - python3 tasks/scripts/check_review_readiness.py "${args[@]}" + python3 tasks/scripts/check_review_readiness.py \ + --repo "$GH_REPO" --pr "$PR_NUMBER" --event "$PR_EVENT" --apply diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index 084f45bb35..6d39d0383b 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -210,8 +210,37 @@ def _eligible_reviewers(repository: str, logins: set[str]) -> set[str]: def _accepted_work(repository: str, number: int, pr: dict) -> bool: - """Accept write access or a reference to an accepted issue filed by the author.""" + """Accept write access or a referenced accepted issue filed by the PR author.""" + owner, repo = repository.split("/") author = pr["user"]["login"].lower() + variables = {"owner": owner, "repo": repo} + cursor = None + while True: + result = _api("graphql", method="POST", data={ + "query": """query($owner: String!, $repo: String!, $author: String!, $cursor: String) { + repository(owner: $owner, name: $repo) { + collaborators(first: 100, query: $author, after: $cursor) { + edges { permission node { login } } + pageInfo { hasNextPage endCursor } + } + } + }""", + "variables": variables | {"author": author, "cursor": cursor}, + }) + members = result["data"]["repository"]["collaborators"] + member = next( + (edge for edge in members["edges"] + if edge["node"]["login"].lower() == author), + None, + ) + if member: + if member["permission"] in {"WRITE", "MAINTAIN", "ADMIN"}: + return True + break + if not members["pageInfo"]["hasNextPage"]: + break + cursor = members["pageInfo"]["endCursor"] + escaped = re.escape(repository) references = ( rf"https://github\.com/{escaped}/issues/(\d+)\b" @@ -221,71 +250,65 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: int(url_number or short_number) for url_number, short_number in re.findall(references, pr.get("body") or "", re.I) } - - owner, repo = repository.split("/") - cursors = dict.fromkeys(("acceptedCursor", "linkedCursor", "collaboratorCursor")) - accepted = set() + cursor = None while True: - result = _api( - "graphql", - method="POST", - data={ - "query": """ - query($owner: String!, $repo: String!, $number: Int!, $author: String!, - $acceptedCursor: String, $linkedCursor: String, $collaboratorCursor: String) { - repository(owner: $owner, name: $repo) { - collaborators(first: 100, query: $author, after: $collaboratorCursor) { - edges { permission node { login } } - pageInfo { hasNextPage endCursor } - } - issues(first: 100, after: $acceptedCursor, states: [OPEN, CLOSED], - labels: ["state:accepted"], filterBy: {createdBy: $author}) { - nodes { number } - pageInfo { hasNextPage endCursor } - } - pullRequest(number: $number) { - closingIssuesReferences(first: 100, after: $linkedCursor) { - nodes { number repository { nameWithOwner } } - pageInfo { hasNextPage endCursor } - } - } + result = _api("graphql", method="POST", data={ + "query": """query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $number) { + closingIssuesReferences(first: 100, after: $cursor) { + nodes { number repository { nameWithOwner } } + pageInfo { hasNextPage endCursor } } } - """, - "variables": { - "owner": owner, - "repo": repo, - "number": number, - "author": author, - **cursors, - }, - }, - ) - data = result["data"]["repository"] - if any( - edge["node"]["login"].lower() == author - and edge["permission"] in {"WRITE", "MAINTAIN", "ADMIN"} - for edge in data["collaborators"]["edges"] - ): - return True - linked = data["pullRequest"]["closingIssuesReferences"] + } + }""", + "variables": variables | {"number": number, "cursor": cursor}, + }) + linked = result["data"]["repository"]["pullRequest"]["closingIssuesReferences"] referenced.update( issue["number"] for issue in linked["nodes"] if issue["repository"]["nameWithOwner"].lower() == repository.lower() ) - accepted.update(issue["number"] for issue in data["issues"]["nodes"]) - if referenced & accepted: - return True - pages = { - "acceptedCursor": data["issues"], - "linkedCursor": linked, - "collaboratorCursor": data["collaborators"], - } - if not any(page["pageInfo"]["hasNextPage"] for page in pages.values()): - return False - for key, page in pages.items(): - cursors[key] = page["pageInfo"]["endCursor"] or cursors[key] + if not linked["pageInfo"]["hasNextPage"]: + break + cursor = linked["pageInfo"]["endCursor"] + + for issue_number in sorted(referenced): + cursor = None + while True: + try: + result = _api("graphql", method="POST", data={ + "query": """query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { + repository(owner: $owner, name: $repo) { + issueOrPullRequest(number: $number) { + ... on Issue { + author { login } + labels(first: 100, after: $cursor) { + nodes { name } + pageInfo { hasNextPage endCursor } + } + } + } + } + }""", + "variables": variables | {"number": issue_number, "cursor": cursor}, + }) + except subprocess.CalledProcessError as exc: + errors = json.loads(exc.stdout or "{}").get("errors", []) + if errors and all(error.get("type") == "NOT_FOUND" for error in errors): + break + raise + issue = result["data"]["repository"]["issueOrPullRequest"] + if not issue or (issue.get("author") or {}).get("login", "").lower() != author: + break + if any(label["name"] == "state:accepted" for label in issue["labels"]["nodes"]): + return True + if not issue["labels"]["pageInfo"]["hasNextPage"]: + break + cursor = issue["labels"]["pageInfo"]["endCursor"] + return False def _routing_key(pr: dict): @@ -306,15 +329,16 @@ def check_review_readiness( number: int, *, apply=False, - label_accepted_work=False, - maintainers: set[str] | None = None, + event="opened", ) -> ReviewPlan | None: """Validate acceptance, then route. Callers must serialize runs for the same PR.""" pull = f"repos/{repository}/pulls/{number}" pr = _api(pull) if pr["state"] != "open": return None - if not _accepted_work(repository, number, pr): + if event in {"opened", "ready_for_review"} and not _accepted_work( + repository, number, pr + ): if apply: _api( "graphql", @@ -366,6 +390,11 @@ def main(argv: list[str] | None = None) -> int: ) parser.add_argument("--repo", required=True, help="target OWNER/REPO") parser.add_argument("--pr", required=True, type=int, help="pull request number") + parser.add_argument( + "--event", default="opened", + choices=("opened", "ready_for_review", "labeled", "unlabeled"), + help="PR event; acceptance is checked on opened and ready_for_review", + ) parser.add_argument( "--apply", action="store_true", help="update requests or draft unaccepted work" ) @@ -374,27 +403,10 @@ def main(argv: list[str] | None = None) -> int: parser.error("--repo must be OWNER/REPO and --pr must be positive") try: categories = parse_reviewers(args.reviewers.read_text(encoding="utf-8")) - maintainers = ( - parse_maintainers(args.maintainers.read_text(encoding="utf-8")) - if args.label_accepted_work - else None - ) plan = check_review_readiness( - categories, - args.repo, - args.pr, - apply=args.apply, - label_accepted_work=args.label_accepted_work, - maintainers=maintainers, + categories, args.repo, args.pr, apply=args.apply, event=args.event ) - except ( - OSError, - UnicodeError, - ValueError, - KeyError, - TypeError, - subprocess.CalledProcessError, - ) as exc: + except (OSError, UnicodeError, ValueError, subprocess.CalledProcessError) as exc: print(f"Review readiness: {exc}", file=sys.stderr) return 1 print( diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index f8e4ee27e2..67a24d107e 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -263,138 +263,142 @@ def test_loads_all_engagement_and_timeline_feeds(self): class AcceptanceTests(unittest.TestCase): @staticmethod - def response( - *, accepted=(), linked=(), permission="READ", collaborator="author", - repository=REPO, more=(), - ): - def page(key): - return { - "hasNextPage": key in more, - "endCursor": "next-page" if key in more else None, - } - - return {"data": {"repository": { - "collaborators": { - "edges": [{"node": {"login": collaborator}, "permission": permission}], - "pageInfo": page("collaboratorCursor"), - }, - "issues": { - "nodes": [{"number": n} for n in accepted], - "pageInfo": page("acceptedCursor"), + def member(permission="READ", login="author", cursor=None): + return {"data": {"repository": {"collaborators": { + "edges": [{"permission": permission, "node": {"login": login}}], + "pageInfo": {"hasNextPage": cursor is not None, "endCursor": cursor}, + }}}} + + @staticmethod + def linked(*numbers, repository=REPO, cursor=None): + return {"data": {"repository": {"pullRequest": {"closingIssuesReferences": { + "nodes": [{"number": n, "repository": {"nameWithOwner": repository}} for n in numbers], + "pageInfo": {"hasNextPage": cursor is not None, "endCursor": cursor}, + }}}}} + + @staticmethod + def issue(author="author", accepted=True, cursor=None): + return {"data": {"repository": {"issueOrPullRequest": { + "author": {"login": author}, + "labels": { + "nodes": [{"name": "state:accepted"}] if accepted else [], + "pageInfo": {"hasNextPage": cursor is not None, "endCursor": cursor}, }, - "pullRequest": {"closingIssuesReferences": { - "nodes": [ - {"number": n, "repository": {"nameWithOwner": repository}} - for n in linked - ], - "pageInfo": page("linkedCursor"), - }}, - }}} - - def test_write_maintain_and_admin_authors_need_no_issue(self): + }}}} + + def test_write_maintain_and_admin_return_without_looking_up_issues(self): for permission in ("WRITE", "MAINTAIN", "ADMIN"): with ( self.subTest(permission=permission), - mock.patch.object( - routing, "_api", return_value=self.response(permission=permission) - ), + mock.patch.object(routing, "_api", return_value=self.member(permission)) as api, ): self.assertTrue(routing._accepted_work(REPO, 42, pr(body="See #9999999"))) + api.assert_called_once() + self.assertNotIn("pullRequest", api.call_args.kwargs["data"]["query"]) def test_collaborator_search_result_must_match_the_pr_author(self): - with mock.patch.object( - routing, "_api", - return_value=self.response(permission="ADMIN", collaborator="someone-else"), - ): + with mock.patch.object(routing, "_api", side_effect=[ + self.member("ADMIN", login="someone-else"), self.linked(), + ]): self.assertFalse(routing._accepted_work(REPO, 42, pr())) - def test_queries_only_accepted_issues_submitted_by_the_pr_author(self): - with mock.patch.object(routing, "_api", return_value=self.response()) as api: - self.assertFalse( - routing._accepted_work(REPO, 42, pr(author="AUTHOR", body="Related to #1")) - ) - query = api.call_args.kwargs["data"]["query"] - self.assertIn('labels: ["state:accepted"]', query) - self.assertIn("filterBy: {createdBy: $author}", query) - self.assertIn("states: [OPEN, CLOSED]", query) - self.assertEqual(api.call_args.kwargs["data"]["variables"]["author"], "author") - - def test_description_and_closing_references_must_intersect_own_accepted_issues(self): + def test_description_and_closing_references_require_same_submitter_and_label(self): for body in ( "", "Related to #1", "Related to owner/repo#1", "Related to OWNER/REPO#1", "Related to https://github.com/owner/repo/issues/1", ): - for accepted, expected in (((), False), ((2,), False), ((1,), True)): + for author, accepted, expected in ( + ("AUTHOR", True, True), ("someone-else", True, False), ("author", False, False), + ): with ( - self.subTest(body=body, accepted=accepted), - mock.patch.object( - routing, "_api", - return_value=self.response( - accepted=accepted, linked=() if body else (1,), - ), - ), + self.subTest(body=body, author=author, accepted=accepted), + mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked() if body else self.linked(1), + self.issue(author=author, accepted=accepted), + ]) as api, ): - self.assertEqual( - routing._accepted_work(REPO, 42, pr(body=body)), expected - ) + self.assertEqual(routing._accepted_work(REPO, 42, pr(body=body)), expected) + self.assertEqual(api.call_args.kwargs["data"]["variables"]["number"], 1) + + def test_one_qualifying_issue_is_enough_and_duplicate_references_are_read_once(self): + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked(1, 2), self.issue(accepted=False), self.issue(), + ]) as api: + self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1 and #2"))) + self.assertEqual(api.call_count, 4) + + def test_paginates_permission_lookup_before_querying_issues(self): + with mock.patch.object(routing, "_api", side_effect=[ + self.member(login="someone-else", cursor="next-page"), self.member("WRITE"), + ]) as api: + self.assertTrue(routing._accepted_work(REPO, 42, pr())) + self.assertEqual(api.call_count, 2) + self.assertEqual(api.call_args.kwargs["data"]["variables"]["cursor"], "next-page") - def test_one_qualifying_issue_is_enough(self): - with mock.patch.object( - routing, "_api", return_value=self.response(accepted=(2,), linked=(1, 2)) - ): + def test_reads_later_closing_reference_pages(self): + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked(cursor="next-page"), self.linked(1), self.issue(), + ]) as api: self.assertTrue(routing._accepted_work(REPO, 42, pr())) + self.assertEqual(api.call_args_list[2].kwargs["data"]["variables"]["cursor"], "next-page") - def test_reads_later_accepted_issue_closing_reference_and_collaborator_pages(self): - for cursor in ("acceptedCursor", "linkedCursor", "collaboratorCursor"): - if cursor == "acceptedCursor": - first = self.response(linked=(1,), more=(cursor,)) - second = self.response(accepted=(1,)) - elif cursor == "linkedCursor": - first = self.response(accepted=(1,), more=(cursor,)) - second = self.response(linked=(1,)) - else: - first = self.response(more=(cursor,)) - second = self.response(permission="WRITE") - with ( - self.subTest(cursor=cursor), - mock.patch.object(routing, "_api", side_effect=[first, second]) as api, - ): - self.assertTrue(routing._accepted_work(REPO, 42, pr())) - self.assertEqual( - api.call_args.kwargs["data"]["variables"][cursor], "next-page" - ) + def test_reads_later_label_pages_on_the_referenced_issue(self): + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked(1), + self.issue(accepted=False, cursor="next-page"), self.issue(), + ]) as api: + self.assertTrue(routing._accepted_work(REPO, 42, pr())) + self.assertEqual(api.call_args.kwargs["data"]["variables"]["cursor"], "next-page") def test_cross_repository_references_do_not_qualify(self): - with mock.patch.object( - routing, "_api", - return_value=self.response(accepted=(1,), linked=(1,), repository="other/repo"), - ): - self.assertFalse(routing._accepted_work( - REPO, 42, - pr(body="Related to other/repo#1 and https://github.com/other/repo/issues/1"), - )) + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked(1, repository="other/repo"), + ]) as api: + self.assertFalse(routing._accepted_work(REPO, 42, pr(body=( + "Related to other/repo#1 and https://github.com/other/repo/issues/1" + )))) + self.assertEqual(api.call_count, 2) - def test_rejection_drafts_before_label_validation_or_reviewer_changes(self): - for apply in (False, True): + def test_missing_issues_do_not_qualify_and_other_graphql_errors_propagate(self): + for error_type in ("NOT_FOUND", "FORBIDDEN"): + error = subprocess.CalledProcessError( + 1, "gh", output=json.dumps({"errors": [{"type": error_type}]}), + ) with ( - self.subTest(apply=apply), - mock.patch.object(routing, "_api", side_effect=[pr(), None]) as api, - mock.patch.object(routing, "_accepted_work", return_value=False), - mock.patch.object(routing, "_load_reviewers") as load, + self.subTest(error_type=error_type), + mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked(), error, + ]), ): - with self.assertRaisesRegex(ValueError, "not accepted work"): - routing.check_review_readiness(CATEGORIES, REPO, 42, apply=apply) - load.assert_not_called() - self.assertEqual(api.call_count, 2 if apply else 1) - if apply: - self.assertEqual(api.call_args.args, ("graphql",)) - self.assertEqual(api.call_args.kwargs["method"], "POST") - self.assertIn( - "convertPullRequestToDraft", api.call_args.kwargs["data"]["query"] - ) - self.assertEqual( - api.call_args.kwargs["data"]["variables"], {"id": "PR_node_42"} - ) + if error_type == "NOT_FOUND": + self.assertFalse(routing._accepted_work(REPO, 42, pr(body="See #1"))) + else: + with self.assertRaises(subprocess.CalledProcessError): + routing._accepted_work(REPO, 42, pr(body="See #1")) + + def test_pull_request_references_do_not_qualify(self): + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked(), + {"data": {"repository": {"issueOrPullRequest": {}}}}, + ]): + self.assertFalse(routing._accepted_work(REPO, 42, pr(body="See #1"))) + + def test_creation_and_ready_transition_reject_before_reviewer_changes(self): + for event in ("opened", "ready_for_review"): + for apply in (False, True): + with ( + self.subTest(event=event, apply=apply), + mock.patch.object(routing, "_api", side_effect=[pr(), None]) as api, + mock.patch.object(routing, "_accepted_work", return_value=False), + mock.patch.object(routing, "_load_reviewers") as load, + ): + with self.assertRaisesRegex(ValueError, "not accepted work"): + routing.check_review_readiness(CATEGORIES, REPO, 42, apply=apply, event=event) + load.assert_not_called() + self.assertEqual(api.call_count, 2 if apply else 1) + if apply: + self.assertIn("convertPullRequestToDraft", api.call_args.kwargs["data"]["query"]) + self.assertEqual(api.call_args.kwargs["data"]["variables"], {"id": "PR_node_42"}) def test_lookup_errors_fail_without_drafting_or_requesting_reviewers(self): with ( @@ -410,10 +414,7 @@ def test_lookup_errors_fail_without_drafting_or_requesting_reviewers(self): def test_draft_conversion_errors_propagate(self): with ( - mock.patch.object( - routing, "_api", - side_effect=[pr(), subprocess.CalledProcessError(1, "gh")], - ), + mock.patch.object(routing, "_api", side_effect=[pr(), subprocess.CalledProcessError(1, "gh")]), mock.patch.object(routing, "_accepted_work", return_value=False), self.assertRaises(subprocess.CalledProcessError), ): @@ -426,15 +427,15 @@ def setUp(self): self.accepted.start() self.addCleanup(self.accepted.stop) - def run_routing(self, categories, *, snapshots, responses, apply=True): + def run_routing(self, categories, *, snapshots, responses, apply=True, event="opened"): with ( mock.patch.object(routing, "_api", side_effect=responses) as api, mock.patch.object( routing, "_load_reviewers", side_effect=snapshots ) as load, ): - plan = routing.check_review_readiness(categories, REPO, 42, apply=apply) - return plan, api, load + plan = routing.check_review_readiness(categories, REPO, 42, apply=apply, event=event) + return plan, api, load, eligible def test_initial_assignment_excludes_author_without_extra_refresh(self): plan, api, load = self.run_routing( @@ -481,6 +482,19 @@ def test_empty_area_does_not_block_topic_reviewers(self): ) self.assertEqual(plan.add, ("carol",)) + def test_label_changes_reconcile_without_rechecking_acceptance(self): + for event in ("labeled", "unlabeled"): + with ( + self.subTest(event=event), + mock.patch.object(routing, "_accepted_work") as accepted, + ): + plan, _, _, _ = self.run_routing( + CATEGORIES, snapshots=[reviewers()], + responses=[pr("area:build"), None], event=event, + ) + self.assertEqual(plan.add, ("dave",)) + accepted.assert_not_called() + def test_draft_and_closed_prs_skip_all_routing(self): for value in (pr(draft=True), pr(state="closed")): with self.subTest(value=value): @@ -769,6 +783,23 @@ def test_label_write_failure_does_not_request_reviewers(self): class CliTests(unittest.TestCase): + def test_passes_label_event_to_reconciliation(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "REVIEWERS.md" + path.write_text(DOCUMENT, encoding="utf-8") + with ( + mock.patch.object( + routing, "check_review_readiness", + return_value=routing.ReviewPlan((), ()), + ) as check, + contextlib.redirect_stdout(io.StringIO()), + ): + self.assertEqual(routing.main([ + "--reviewers", str(path), "--repo", REPO, "--pr", "42", + "--event", "labeled", "--apply", + ]), 0) + self.assertEqual(check.call_args.kwargs, {"apply": True, "event": "labeled"}) + def test_emits_plan_and_passes_apply_option(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "REVIEWERS.md" @@ -806,52 +837,7 @@ def test_emits_plan_and_passes_apply_option(self): }, ) check.assert_called_once_with( - {"area:runtime": {"alice"}}, - REPO, - 42, - apply=True, - label_accepted_work=False, - maintainers=None, - ) - - def test_opened_pr_option_loads_trusted_maintainer_roster(self): - with tempfile.TemporaryDirectory() as directory: - roster = Path(directory) / "REVIEWERS.md" - roster.write_text(DOCUMENT, encoding="utf-8") - maintainers = Path(directory) / "MAINTAINERS.md" - maintainers.write_text("[@maintainer](https://github.com/maintainer)") - with ( - mock.patch.object( - routing, - "check_review_readiness", - return_value=routing.ReviewPlan((), (), True, ("state:accepted",)), - ) as check, - contextlib.redirect_stdout(io.StringIO()) as output, - ): - result = routing.main( - [ - "--repo", - REPO, - "--pr", - "42", - "--reviewers", - str(roster), - "--maintainers", - str(maintainers), - "--label-accepted-work", - ] - ) - self.assertEqual(result, 0) - self.assertEqual( - json.loads(output.getvalue())["add_labels"], ["state:accepted"] - ) - check.assert_called_once_with( - {"area:runtime": {"alice"}}, - REPO, - 42, - apply=False, - label_accepted_work=True, - maintainers={"maintainer"}, + {"area:runtime": {"alice"}}, REPO, 42, apply=True, event="opened" ) def test_invalid_registry_fails_before_contacting_github(self): From ff328fb9fbaa4526b5f5f37bfbb8894a79b421f9 Mon Sep 17 00:00:00 2001 From: Jim Meyer Date: Mon, 26 Oct 2026 23:49:58 +0000 Subject: [PATCH 3/6] refactor(review): short-circuit acceptance before loading roster Signed-off-by: Jim Meyer --- tasks/scripts/check_review_readiness.py | 130 ++++++++------- tasks/scripts/check_review_readiness_test.py | 160 +++++++++++++++---- 2 files changed, 197 insertions(+), 93 deletions(-) diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index 6d39d0383b..1c1845530e 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -185,6 +185,12 @@ def _api(path: str, *, method: str = "GET", data=None, pages: bool = False): return value +def _graphql(query: str, **variables): + return _api( + "graphql", method="POST", data={"query": query, "variables": variables} + )["data"] + + def _load_reviewers(repository: str, number: int, pr: dict) -> dict[str, set[str]]: root = f"repos/{repository}" pull = f"{root}/pulls/{number}" @@ -209,15 +215,55 @@ def _eligible_reviewers(repository: str, logins: set[str]) -> set[str]: return eligible +def _referenced_issues(repository: str, number: int, body: str): + """Yield description references first, then closing links, without duplicates.""" + escaped = re.escape(repository) + references = ( + rf"https://github\.com/{escaped}/issues/(\d+)\b" + rf"|(? bool: """Accept write access or a referenced accepted issue filed by the PR author.""" owner, repo = repository.split("/") author = pr["user"]["login"].lower() - variables = {"owner": owner, "repo": repo} cursor = None while True: - result = _api("graphql", method="POST", data={ - "query": """query($owner: String!, $repo: String!, $author: String!, $cursor: String) { + data = _graphql( + """query($owner: String!, $repo: String!, $author: String!, $cursor: String) { repository(owner: $owner, name: $repo) { collaborators(first: 100, query: $author, after: $cursor) { edges { permission node { login } } @@ -225,9 +271,9 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: } } }""", - "variables": variables | {"author": author, "cursor": cursor}, - }) - members = result["data"]["repository"]["collaborators"] + owner=owner, repo=repo, author=author, cursor=cursor, + ) + members = data["repository"]["collaborators"] member = next( (edge for edge in members["edges"] if edge["node"]["login"].lower() == author), @@ -241,46 +287,12 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: break cursor = members["pageInfo"]["endCursor"] - escaped = re.escape(repository) - references = ( - rf"https://github\.com/{escaped}/issues/(\d+)\b" - rf"|(? bool: } } }""", - "variables": variables | {"number": issue_number, "cursor": cursor}, - }) + owner=owner, repo=repo, number=issue_number, cursor=cursor, + ) except subprocess.CalledProcessError as exc: errors = json.loads(exc.stdout or "{}").get("errors", []) if errors and all(error.get("type") == "NOT_FOUND" for error in errors): break raise - issue = result["data"]["repository"]["issueOrPullRequest"] + issue = data["repository"]["issueOrPullRequest"] if not issue or (issue.get("author") or {}).get("login", "").lower() != author: break if any(label["name"] == "state:accepted" for label in issue["labels"]["nodes"]): @@ -324,7 +336,7 @@ def _routing_key(pr: dict): def check_review_readiness( - categories: dict[str, set[str]], + reviewers: Path, repository: str, number: int, *, @@ -340,24 +352,19 @@ def check_review_readiness( repository, number, pr ): if apply: - _api( - "graphql", - method="POST", - data={ - "query": """ - mutation($id: ID!) { - convertPullRequestToDraft(input: {pullRequestId: $id}) { - pullRequest { isDraft } - } - } - """, - "variables": {"id": pr["node_id"]}, - }, + _graphql( + """mutation($id: ID!) { + convertPullRequestToDraft(input: {pullRequestId: $id}) { + pullRequest { isDraft } + } + }""", + id=pr["node_id"], ) raise ValueError( "PR is not accepted work: the author needs repository write access " "or a referenced state:accepted issue they submitted themselves" ) + categories = parse_reviewers(reviewers.read_text(encoding="utf-8")) selected = select_reviewers(categories, {label["name"] for label in pr["labels"]}) selected = _eligible_reviewers(repository, selected - {pr["user"]["login"].lower()}) plan = plan_review_requests(selected, **_load_reviewers(repository, number, pr)) @@ -402,9 +409,8 @@ def main(argv: list[str] | None = None) -> int: if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", args.repo) or args.pr <= 0: parser.error("--repo must be OWNER/REPO and --pr must be positive") try: - categories = parse_reviewers(args.reviewers.read_text(encoding="utf-8")) plan = check_review_readiness( - categories, args.repo, args.pr, apply=args.apply, event=args.event + args.reviewers, args.repo, args.pr, apply=args.apply, event=args.event ) except (OSError, UnicodeError, ValueError, subprocess.CalledProcessError) as exc: print(f"Review readiness: {exc}", file=sys.stderr) diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index 67a24d107e..471124925d 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -26,6 +26,8 @@ } REPO = "owner/repo" PULL = "repos/owner/repo/pulls/42" +ROSTER = Path("REVIEWERS.md") +DRAFTED = {"data": {"convertPullRequestToDraft": {"pullRequest": {"isDraft": True}}}} def pr(*labels, requested=(), author="author", draft=False, state="open", body=""): @@ -224,11 +226,13 @@ def test_new_automatic_request_after_removal_is_not_protected(self): class GitHubTests(unittest.TestCase): - def test_api_rejects_empty_paginated_response(self): - with mock.patch.object(routing.subprocess, "run") as run: - run.return_value.stdout = "" - with self.assertRaisesRegex(ValueError, "paginated"): - routing._api("endpoint", pages=True) + def test_graphql_passes_variables_and_unwraps_response(self): + with mock.patch.object(routing, "_api", return_value={"data": {"value": 1}}) as api: + self.assertEqual(routing._graphql("query", number=42), {"value": 1}) + api.assert_called_once_with( + "graphql", method="POST", + data={"query": "query", "variables": {"number": 42}}, + ) def test_api_reads_all_pages(self): with mock.patch.object(routing.subprocess, "run") as run: @@ -302,6 +306,20 @@ def test_collaborator_search_result_must_match_the_pr_author(self): ]): self.assertFalse(routing._accepted_work(REPO, 42, pr())) + def test_qualifying_description_reference_skips_all_closing_link_queries(self): + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.issue(), + ]) as api: + self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1"))) + self.assertEqual(api.call_count, 2) + + def test_qualifying_closing_reference_skips_later_pages(self): + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), self.linked(1, cursor="unneeded-page"), self.issue(), + ]) as api: + self.assertTrue(routing._accepted_work(REPO, 42, pr())) + self.assertEqual(api.call_count, 3) + def test_description_and_closing_references_require_same_submitter_and_label(self): for body in ( "", "Related to #1", "Related to owner/repo#1", "Related to OWNER/REPO#1", @@ -312,19 +330,23 @@ def test_description_and_closing_references_require_same_submitter_and_label(sel ): with ( self.subTest(body=body, author=author, accepted=accepted), - mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked() if body else self.linked(1), - self.issue(author=author, accepted=accepted), - ]) as api, + mock.patch.object(routing, "_api", side_effect=( + [self.member(), self.linked(1), self.issue(author, accepted)] + if not body else + [self.member(), self.issue(author, accepted)] + ([] if expected else [self.linked()]) + )) as api, ): self.assertEqual(routing._accepted_work(REPO, 42, pr(body=body)), expected) - self.assertEqual(api.call_args.kwargs["data"]["variables"]["number"], 1) + lookups = [call.kwargs["data"]["variables"]["number"] + for call in api.call_args_list + if "issueOrPullRequest" in call.kwargs["data"]["query"]] + self.assertEqual(lookups, [1]) def test_one_qualifying_issue_is_enough_and_duplicate_references_are_read_once(self): with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked(1, 2), self.issue(accepted=False), self.issue(), + self.member(), self.issue(accepted=False), self.linked(1, 2), self.issue(), ]) as api: - self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1 and #2"))) + self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1"))) self.assertEqual(api.call_count, 4) def test_paginates_permission_lookup_before_querying_issues(self): @@ -367,7 +389,7 @@ def test_missing_issues_do_not_qualify_and_other_graphql_errors_propagate(self): with ( self.subTest(error_type=error_type), mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked(), error, + self.member(), error, self.linked(), ]), ): if error_type == "NOT_FOUND": @@ -378,22 +400,31 @@ def test_missing_issues_do_not_qualify_and_other_graphql_errors_propagate(self): def test_pull_request_references_do_not_qualify(self): with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked(), - {"data": {"repository": {"issueOrPullRequest": {}}}}, + self.member(), {"data": {"repository": {"issueOrPullRequest": {}}}}, + self.linked(), ]): self.assertFalse(routing._accepted_work(REPO, 42, pr(body="See #1"))) + def test_missing_description_reference_falls_back_to_qualifying_closing_link(self): + missing = subprocess.CalledProcessError( + 1, "gh", output=json.dumps({"errors": [{"type": "NOT_FOUND"}]}), + ) + with mock.patch.object(routing, "_api", side_effect=[ + self.member(), missing, self.linked(2), self.issue(), + ]): + self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1"))) + def test_creation_and_ready_transition_reject_before_reviewer_changes(self): for event in ("opened", "ready_for_review"): for apply in (False, True): with ( self.subTest(event=event, apply=apply), - mock.patch.object(routing, "_api", side_effect=[pr(), None]) as api, + mock.patch.object(routing, "_api", side_effect=[pr(), DRAFTED]) as api, mock.patch.object(routing, "_accepted_work", return_value=False), mock.patch.object(routing, "_load_reviewers") as load, ): with self.assertRaisesRegex(ValueError, "not accepted work"): - routing.check_review_readiness(CATEGORIES, REPO, 42, apply=apply, event=event) + routing.check_review_readiness(ROSTER, REPO, 42, apply=apply, event=event) load.assert_not_called() self.assertEqual(api.call_count, 2 if apply else 1) if apply: @@ -408,7 +439,7 @@ def test_lookup_errors_fail_without_drafting_or_requesting_reviewers(self): mock.patch.object(routing, "_load_reviewers") as load, self.assertRaises(subprocess.CalledProcessError), ): - routing.check_review_readiness(CATEGORIES, REPO, 42, apply=True) + routing.check_review_readiness(ROSTER, REPO, 42, apply=True) self.assertEqual(api.call_count, 2) load.assert_not_called() @@ -418,7 +449,7 @@ def test_draft_conversion_errors_propagate(self): mock.patch.object(routing, "_accepted_work", return_value=False), self.assertRaises(subprocess.CalledProcessError), ): - routing.check_review_readiness(CATEGORIES, REPO, 42, apply=True) + routing.check_review_readiness(ROSTER, REPO, 42, apply=True) class ReconciliationTests(unittest.TestCase): @@ -426,15 +457,21 @@ def setUp(self): self.accepted = mock.patch.object(routing, "_accepted_work", return_value=True) self.accepted.start() self.addCleanup(self.accepted.stop) + self.roster = mock.Mock(spec=Path) + self.roster.read_text.return_value = DOCUMENT + parser = mock.patch.object(routing, "parse_reviewers", return_value=CATEGORIES) + self.parser = parser.start() + self.addCleanup(parser.stop) def run_routing(self, categories, *, snapshots, responses, apply=True, event="opened"): + self.parser.return_value = categories with ( mock.patch.object(routing, "_api", side_effect=responses) as api, mock.patch.object( routing, "_load_reviewers", side_effect=snapshots ) as load, ): - plan = routing.check_review_readiness(categories, REPO, 42, apply=apply, event=event) + plan = routing.check_review_readiness(self.roster, REPO, 42, apply=apply, event=event) return plan, api, load, eligible def test_initial_assignment_excludes_author_without_extra_refresh(self): @@ -556,13 +593,11 @@ def test_changed_routing_or_draft_status_stops_removals(self): ), ): with self.assertRaisesRegex(ValueError, "routing changed"): - routing.check_review_readiness(CATEGORIES, REPO, 42, apply=True) - self.assertTrue( - all( - call.kwargs.get("method", "GET") == "GET" - for call in api.call_args_list - ) - ) + routing.check_review_readiness(self.roster, REPO, 42, apply=True) + self.assertTrue(all( + call.kwargs.get("method", "GET") == "GET" + for call in api.call_args_list + )) def test_new_commit_and_unrelated_label_do_not_interrupt_routing(self): before = pr("area:build", requested=("alice", "dave")) @@ -783,6 +818,68 @@ def test_label_write_failure_does_not_request_reviewers(self): class CliTests(unittest.TestCase): + def test_rejected_pr_does_not_need_a_readable_valid_roster(self): + for contents in (None, "not a registry"): + for apply in (False, True): + with self.subTest(contents=contents, apply=apply), tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "REVIEWERS.md" + if contents is not None: + path.write_text(contents, encoding="utf-8") + output, errors = io.StringIO(), io.StringIO() + with ( + mock.patch.object(routing, "_api", side_effect=[pr(), DRAFTED]) as api, + mock.patch.object(routing, "_accepted_work", return_value=False), + mock.patch.object(routing, "parse_reviewers") as parse, + mock.patch.object(routing, "_load_reviewers") as load, + contextlib.redirect_stdout(output), + contextlib.redirect_stderr(errors), + ): + argv = ["--reviewers", str(path), "--repo", REPO, "--pr", "42"] + self.assertEqual(routing.main(argv + (["--apply"] if apply else [])), 1) + self.assertIn("not accepted work", errors.getvalue()) + self.assertEqual(output.getvalue(), "") + parse.assert_not_called() + load.assert_not_called() + self.assertEqual(api.call_count, 2 if apply else 1) + if apply: + self.assertIn("convertPullRequestToDraft", api.call_args.kwargs["data"]["query"]) + + def test_accepted_pr_reads_real_roster_and_previews_reviewers(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "REVIEWERS.md" + path.write_text(DOCUMENT, encoding="utf-8") + output = io.StringIO() + with ( + mock.patch.object(routing, "_api", return_value=pr("area:runtime")) as api, + mock.patch.object(routing, "_accepted_work", return_value=True) as accepted, + mock.patch.object(routing, "_eligible_reviewers", return_value={"alice"}), + mock.patch.object(routing, "_load_reviewers", return_value=reviewers()), + contextlib.redirect_stdout(output), + ): + self.assertEqual(routing.main([ + "--reviewers", str(path), "--repo", REPO, "--pr", "42", + ]), 0) + accepted.assert_called_once() + api.assert_called_once_with(PULL) + self.assertEqual(json.loads(output.getvalue()), {"add": ["alice"], "remove": []}) + + def test_closed_and_draft_prs_skip_missing_roster_and_acceptance(self): + for value in (pr(draft=True), pr(state="closed")): + with self.subTest(value=value), tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "missing.md" + output = io.StringIO() + with ( + mock.patch.object(routing, "_api", return_value=value) as api, + mock.patch.object(routing, "_accepted_work") as accepted, + contextlib.redirect_stdout(output), + ): + self.assertEqual(routing.main([ + "--reviewers", str(path), "--repo", REPO, "--pr", "42", + ]), 0) + api.assert_called_once_with(PULL) + accepted.assert_not_called() + self.assertEqual(json.loads(output.getvalue()), {"skipped": "closed or draft PR"}) + def test_passes_label_event_to_reconciliation(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "REVIEWERS.md" @@ -837,16 +934,17 @@ def test_emits_plan_and_passes_apply_option(self): }, ) check.assert_called_once_with( - {"area:runtime": {"alice"}}, REPO, 42, apply=True, event="opened" + path, REPO, 42, apply=True, event="opened" ) - def test_invalid_registry_fails_before_contacting_github(self): + def test_accepted_pr_with_invalid_registry_fails_before_reviewer_changes(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "REVIEWERS.md" path.write_text("not a registry", encoding="utf-8") output, errors = io.StringIO(), io.StringIO() with ( - mock.patch.object(routing, "_api") as api, + mock.patch.object(routing, "_api", return_value=pr("area:runtime")) as api, + mock.patch.object(routing, "_accepted_work", return_value=True), contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors), ): @@ -863,7 +961,7 @@ def test_invalid_registry_fails_before_contacting_github(self): ), 1, ) - api.assert_not_called() + api.assert_called_once_with(PULL) self.assertEqual(output.getvalue(), "") self.assertIn("no area/topic", errors.getvalue()) From 24fc95f99cac2fa4f88d63a2495c1f9a8e097723 Mon Sep 17 00:00:00 2001 From: Jim Meyer Date: Tue, 27 Oct 2026 00:08:04 +0000 Subject: [PATCH 4/6] refactor(review): share acceptance policy across PR checks Signed-off-by: Jim Meyer --- .agents/skills/create-github-pr/SKILL.md | 10 +- .agents/skills/review-github-pr/SKILL.md | 15 +- .github/CODEOWNERS | 6 +- .github/workflows/pr-issue-acceptance.yml | 16 +- AGENTS.md | 2 +- CONTRIBUTING.md | 6 +- tasks/scripts/check_issue_acceptance.cjs | 82 ---------- tasks/scripts/check_issue_acceptance.test.cjs | 150 ------------------ tasks/scripts/check_review_readiness.py | 44 +++-- tasks/scripts/check_review_readiness_test.py | 141 +++++++++++++++- 10 files changed, 200 insertions(+), 272 deletions(-) delete mode 100644 tasks/scripts/check_issue_acceptance.cjs delete mode 100644 tasks/scripts/check_issue_acceptance.test.cjs diff --git a/.agents/skills/create-github-pr/SKILL.md b/.agents/skills/create-github-pr/SKILL.md index 957dc8bd60..213ff6b1a6 100644 --- a/.agents/skills/create-github-pr/SKILL.md +++ b/.agents/skills/create-github-pr/SKILL.md @@ -24,14 +24,16 @@ to the target repository, not their fork. For the authenticated `gh` user: ```bash gh repo view OWNER/REPO --json nameWithOwner,viewerPermission -gh issue view ISSUE_NUMBER --repo OWNER/REPO --json number,url,labels +gh issue view ISSUE_NUMBER --repo OWNER/REPO --json number,url,labels,author ``` Authors with verified `WRITE`, `MAINTAIN`, or `ADMIN` access may proceed without `state:accepted`; keep the issue link and note the missing label. Everyone else -must link at least one target-repository issue and have `state:accepted` on each. -Otherwise, preserve local work and report which issue needs acceptance. A direct -request, roadmap placement, other labels, or vouch status does not bypass this gate. +must reference at least one target-repository issue they submitted themselves +that has `state:accepted`. Related description references and closing links both +qualify; other references need not qualify. Otherwise, preserve local work and +report the missing qualification. A direct request, roadmap placement, other +labels, or vouch status does not bypass this gate. When acting for another author, verify their permissions. Resolve failed lookups before proceeding; retry sandbox network failures outside the sandbox before diff --git a/.agents/skills/review-github-pr/SKILL.md b/.agents/skills/review-github-pr/SKILL.md index e71a3182bf..06b9f0aac1 100644 --- a/.agents/skills/review-github-pr/SKILL.md +++ b/.agents/skills/review-github-pr/SKILL.md @@ -46,18 +46,19 @@ Record the **title**, **body**, **headRefName**, and **baseRefName** for use in ### Check Issue Acceptance -Inspect every closing issue reference (paginate as needed) and fetch its current -repository and labels. Verify the PR author's permission in the target repository: +Inspect closing issue references (paginate as needed) and issue references in the +PR description. Fetch their current repository, labels, and submitter. Verify the +PR author's permission in the target repository: ```bash gh api repos/OWNER/REPO/collaborators/AUTHOR/permission --jq .permission ``` -Call out each linked issue missing `state:accepted`, with an issue link. Verified -`write`, `maintain`, or `admin` access permits the exception; report it as context, -not a blocking concern. Other authors need at least one target-repository issue, -and every linked issue must be accepted; report violations under **Potential -Concerns**. PR body text such as `No issue required` does not bypass this gate. +Verified `write`, `maintain`, or `admin` access permits the exception; report it as +context, not a blocking concern. Other authors need at least one referenced issue +in the target repository with `state:accepted` submitted by the PR author. Other +references need not qualify. Report violations under **Potential Concerns**. PR +body text such as `No issue required` does not bypass this gate. Report failed lookups as unverified and continue the code review. For a branch without a PR, note that issue acceptance was not verified. diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 78f5034728..fb8e55eed9 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -31,15 +31,11 @@ /tasks/scripts/check_maintainer_approval_test.py @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr /tasks/scripts/alert_maintainer_change_test.py @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr -# Reviewer routing registry and helper +# Reviewer routing registry and shared acceptance helper /REVIEWERS.md @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr /tasks/scripts/check_review_readiness.py @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr /tasks/scripts/check_review_readiness_test.py @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr -# PR issue acceptance checks -/tasks/scripts/check_issue_acceptance.cjs @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr -/tasks/scripts/check_issue_acceptance.test.cjs @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr - # Accepted-work classification /tasks/scripts/label_accepted_work.py @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr /tasks/scripts/label_accepted_work_test.py @NVIDIA/openshell-codeowners @mrunalp @sjenning @derekwaynecarr diff --git a/.github/workflows/pr-issue-acceptance.yml b/.github/workflows/pr-issue-acceptance.yml index 742647d763..1e78c0d544 100644 --- a/.github/workflows/pr-issue-acceptance.yml +++ b/.github/workflows/pr-issue-acceptance.yml @@ -29,12 +29,14 @@ jobs: with: ref: ${{ github.sha }} persist-credentials: false - sparse-checkout: tasks/scripts/check_issue_acceptance.cjs + sparse-checkout: tasks/scripts/check_review_readiness.py sparse-checkout-cone-mode: false - - name: Check author access and linked issue acceptance - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 - with: - script: | - const check = require('./tasks/scripts/check_issue_acceptance.cjs'); - await check({ github, context, core }); + - name: Check PR acceptance + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_REPO: ${{ github.repository }} + PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + python3 tasks/scripts/check_review_readiness.py \ + --repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only diff --git a/AGENTS.md b/AGENTS.md index 012969da9f..82bb81fd25 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -186,7 +186,7 @@ Follow the [branch naming convention in CONTRIBUTING.md](CONTRIBUTING.md#branch- ## Pull Requests -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. +Follow `CONTRIBUTING.md` and `create-github-pr`. Authors without target-repository write access must reference at least one target-repository issue they submitted that has `state:accepted`. Related references and closing links qualify. Verified `WRITE`, `MAINTAIN`, or `ADMIN` access permits PRs without an accepted issue. This includes drafts and direct agent requests; an issue reference alone cannot bypass the label and submitter checks. ## Commits diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 65ba634845..978b19c4b6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -495,9 +495,11 @@ For release notes, describe the user-facing change in a few short, easy-to-read Every PR must close an existing issue, except automated dependency updates and the build and flaky-test fixes described below. In the PR's **Related Issue** section, use `Closes #NNN` for the issue covering that PR's scope. Split multi-PR work into a closable issue for each PR; a tracking issue can link them. Security fixes follow the private disclosure process in [SECURITY.md](SECURITY.md). -Authors without write access to the target repository must link at least one issue with `state:accepted`, and every linked issue must be accepted. This applies to drafts and direct requests to agents. Authors with verified `WRITE`, `MAINTAIN`, or `ADMIN` access may create a PR without the acceptance label; they must still follow the issue-linking requirement above. PR body text, roadmap placement, other labels, and vouch status do not bypass acceptance. +Authors without write access to the target repository must reference at least one issue in that repository with `state:accepted` that they submitted themselves. Related references in the PR description, such as `Related to #NNN`, and closing links both qualify. One qualifying issue is sufficient; other references need not qualify. This applies to drafts and direct requests to agents. Authors with verified `WRITE`, `MAINTAIN`, or `ADMIN` access may create a PR without an accepted issue; they must still follow the issue-linking requirement above. Roadmap placement, other labels, and vouch status do not bypass acceptance. -The `PR Issue Acceptance` workflow reports the native `OpenShell / PR Issue Acceptance` job result for the triggering PR when it is opened, edited, reopened, or updated with new commits. It does not publish a shared commit status; lookup and execution errors fail the job. Link accepted issues in this repository using `Fixes #NNN` or `Closes #NNN`; GitHub recognizes closing references when a PR targets the default branch. Rerun the workflow after issue labels change. Making the job required for merging and configuring any required Actions event policy are separate repository settings; merge-queue support is not included. +Both acceptance checks use `tasks/scripts/check_review_readiness.py`. The `PR Issue Acceptance` workflow reports the native `OpenShell / PR Issue Acceptance` job result when a PR is opened, edited, reopened, or updated with new commits, including drafts. It only reports acceptance and does not change the PR. `Review Readiness` checks the same policy when a ready PR is opened or leaves draft; it moves rejected work to draft before requesting reviewers. Area/topic label changes only reconcile reviewers. + +The checks do not publish a shared commit status; lookup and execution errors fail the job. Description references support `#NNN`, `OWNER/REPO#NNN`, and GitHub issue URLs. Rerun `PR Issue Acceptance` after issue labels change. Making the job required for merging and configuring any required Actions event policy are separate repository settings; merge-queue support is not included. Automated dependency update PRs, including Dependabot PRs, are exempt from the separate issue and closing-reference requirement. Human-authored dependency updates follow the normal issue requirement. The exception does not bypass `PR Issue Acceptance` for authors without repository write access or change applicable verification, review, or private vulnerability disclosure requirements. diff --git a/tasks/scripts/check_issue_acceptance.cjs b/tasks/scripts/check_issue_acceptance.cjs deleted file mode 100644 index b8420f8859..0000000000 --- a/tasks/scripts/check_issue_acceptance.cjs +++ /dev/null @@ -1,82 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -module.exports = async function checkIssueAcceptance({ github, context, core }) { - const { owner, repo } = context.repo; - const { data: pr } = await github.rest.pulls.get({ - owner, repo, pull_number: context.payload.pull_request.number, - }); - const author = pr.user.login; - - if (author === 'dependabot[bot]') { - core.info('Dependabot PRs are exempt from issue acceptance.'); - return; - } - - async function evaluate() { - try { - const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ - owner, repo, username: author, - }); - // The REST API maps maintain to write and triage to read. - if (['write', 'maintain', 'admin'].includes(data.permission)) { - core.info(`${author} has repository write access; acceptance gate passed.`); - return; - } - } catch (error) { - if (error.status !== 404) throw error; - } - - const issues = []; - let cursor = null; - do { - const result = await github.graphql(` - query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { - repository(owner: $owner, name: $repo) { - pullRequest(number: $number) { - closingIssuesReferences(first: 100, after: $cursor) { - nodes { number url repository { name owner { login } } } - pageInfo { hasNextPage endCursor } - } - } - } - } - `, { owner, repo, number: pr.number, cursor }); - const linked = result.repository.pullRequest.closingIssuesReferences; - issues.push(...linked.nodes); - cursor = linked.pageInfo.hasNextPage ? linked.pageInfo.endCursor : null; - } while (cursor); - - if (issues.length === 0) { - return 'Community PRs must link an issue with state:accepted using ' - + 'Fixes #NNN or Closes #NNN.'; - } - - const unaccepted = []; - for (const issue of issues) { - if (`${issue.repository.owner.login}/${issue.repository.name}`.toLowerCase() - !== `${owner}/${repo}`.toLowerCase()) { - return `Acceptance must be recorded in ${owner}/${repo}, not ${issue.url}.`; - } - const labels = await github.paginate(github.rest.issues.listLabelsOnIssue, { - owner: issue.repository.owner.login, - repo: issue.repository.name, - issue_number: issue.number, - per_page: 100, - }); - if (!labels.some(label => label.name === 'state:accepted')) { - unaccepted.push(issue.url); - } - } - - if (unaccepted.length) { - return 'Community PRs require state:accepted on every linked issue. ' - + `Ask a maintainer to accept: ${unaccepted.join(', ')}. ` - + 'After acceptance, rerun this workflow.'; - } - core.info(`All ${issues.length} linked issue(s) have state:accepted.`); - } - - const failure = await evaluate(); - if (failure) core.setFailed(failure); -}; diff --git a/tasks/scripts/check_issue_acceptance.test.cjs b/tasks/scripts/check_issue_acceptance.test.cjs deleted file mode 100644 index 46746f62cb..0000000000 --- a/tasks/scripts/check_issue_acceptance.test.cjs +++ /dev/null @@ -1,150 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -const assert = require('node:assert/strict'); -const test = require('node:test'); -const checkIssueAcceptance = require('./check_issue_acceptance.cjs'); - -async function check({ - permission = 'read', issues = [], body = '', permissionError, - number = 7, headSha = 'pr-head', pullError, issueError, labelError, - author = 'contributor', actor = 'maintainer', -} = {}) { - const failures = []; - const github = { - rest: { - pulls: { - get: async ({ pull_number }) => { - assert.equal(pull_number, number); - if (pullError) throw new Error('PR lookup failed'); - return { data: { - number, user: { login: author }, head: { sha: headSha }, body, - } }; - }, - }, - repos: { - getCollaboratorPermissionLevel: async ({ username }) => { - assert.equal(username, author); - if (permissionError) throw Object.assign(new Error('lookup failed'), { status: permissionError }); - return { data: { permission } }; - }, - createCommitStatus() { assert.fail('PR decisions must not publish commit statuses'); }, - }, - issues: { listLabelsOnIssue() {} }, - }, - graphql: async (_query, variables) => { - assert.equal(variables.number, number); - if (issueError) throw new Error('Issue lookup failed'); - return { repository: { pullRequest: { closingIssuesReferences: { - nodes: issues.map(issue => ({ - number: issue.number, - url: `https://github.com/${issue.owner || 'NVIDIA'}/OpenShell/issues/${issue.number}`, - repository: { name: 'OpenShell', owner: { login: issue.owner || 'NVIDIA' } }, - })), - pageInfo: { hasNextPage: false, endCursor: null }, - } } } }; - }, - paginate: async (_method, { issue_number }) => { - if (labelError) throw new Error('Label lookup failed'); - return issues.find(issue => issue.number === issue_number).labels || []; - }, - }; - await checkIssueAcceptance({ - github, - context: { - repo: { owner: 'NVIDIA', repo: 'OpenShell' }, - payload: { pull_request: { number } }, - actor, sha: 'base-commit', serverUrl: 'https://github.com', runId: 42, - }, - core: { info() {}, setFailed: message => failures.push(message) }, - }); - return failures; -} - -test('Dependabot PRs pass without permission or issue lookups', async () => { - assert.deepEqual(await check({ - author: 'dependabot[bot]', permissionError: 403, issueError: true, - }), []); -}); - -test('other bots still require an accepted issue', async () => { - const failures = await check({ author: 'renovate[bot]' }); - assert.equal(failures.length, 1); - assert.match(failures[0], /must link an issue with state:accepted/); -}); - -test('Dependabot triggering another author\'s PR does not bypass acceptance', async () => { - const failures = await check({ actor: 'dependabot[bot]' }); - assert.equal(failures.length, 1); - assert.match(failures[0], /must link an issue with state:accepted/); -}); - -for (const permission of ['write', 'maintain', 'admin']) { - test(`${permission} access allows an issue-less PR`, async () => { - assert.deepEqual(await check({ permission }), []); - }); -} - -for (const body of ['', 'No issue required: typo fix', '']) { - test(`community PR without linked issues fails for body ${JSON.stringify(body)}`, async () => { - const failures = await check({ body }); - assert.equal(failures.length, 1); - assert.match(failures[0], /must link an issue with state:accepted/); - }); -} - -test('all linked issues must be accepted, regardless of PR body or triggering actor', async () => { - const failures = await check({ - body: 'No issue required: small fix', - issues: [{ number: 1, labels: [{ name: 'state:accepted' }] }, { number: 2 }], - }); - assert.equal(failures.length, 1); - assert.match(failures[0], /issues\/2/); -}); - -test('accepted issues in the target repository pass', async () => { - assert.deepEqual(await check({ issues: [{ number: 1, labels: [{ name: 'state:accepted' }] }] }), []); -}); - -test('accepted issues in another repository cannot bypass the gate', async () => { - const failures = await check({ issues: [{ number: 1, owner: 'contributor', labels: [{ name: 'state:accepted' }] }] }); - assert.equal(failures.length, 1); - assert.match(failures[0], /Acceptance must be recorded in NVIDIA\/OpenShell/); -}); - -test('a non-collaborator still needs an accepted issue', async () => { - assert.equal((await check({ permissionError: 404, body: 'No issue required: typo fix' })).length, 1); -}); - -test('permission lookup failures do not grant an exemption', async () => { - await assert.rejects(check({ permissionError: 403 }), /lookup failed/); -}); - -for (const [option, message] of [ - ['pullError', /PR lookup failed/], - ['issueError', /Issue lookup failed/], - ['labelError', /Label lookup failed/], -]) { - test(`${option} fails the workflow invocation`, async () => { - await assert.rejects(check({ [option]: true, issues: [{ number: 1 }] }), message); - }); -} - -test('PRs sharing a head SHA evaluate their own issue references in either order', async () => { - const rejected = { number: 7, headSha: 'shared-head', issues: [{ number: 1 }] }; - const accepted = { - number: 8, headSha: 'shared-head', - issues: [{ number: 2, labels: [{ name: 'state:accepted' }] }], - }; - for (const pair of [[rejected, accepted], [accepted, rejected]]) { - const results = await Promise.all(pair.map(input => check(input))); - pair.forEach((input, index) => { - assert.equal(results[index].length, input.number === 7 ? 1 : 0); - }); - } -}); - -test('a failed refresh rejects after an earlier successful evaluation', async () => { - assert.deepEqual(await check({ permission: 'write' }), []); - await assert.rejects(check({ permission: 'write', pullError: true }), /PR lookup failed/); -}); diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index 1c1845530e..b535df4854 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -10,6 +10,7 @@ """Validate accepted work and reconcile reviewers from the merged REVIEWERS.md. Preview with --repo OWNER/REPO --pr NUMBER; add --apply to update the PR. +Use --acceptance-only to report acceptance without routing or changing the PR. Unaccepted work is moved to draft before any reviewer requests are changed. Request all matching members with write access, excluding the author. Preserve manual choices, unknown request provenance, and anyone who commented or reviewed. @@ -32,6 +33,10 @@ HEADER = ["Name", "GitHub ID", "Company/Organization"] LABEL = re.compile(r"(?:area|topic):[a-z0-9]+(?:[-:][a-z0-9]+)*") PROFILE = re.compile(r"\[@([a-z0-9-]+)\]\(https://github\.com/([a-z0-9-]+)/?\)", re.I) +ACCEPTANCE_ERROR = ( + "PR is not accepted work: the author needs repository write access " + "or a referenced state:accepted issue they submitted themselves" +) @dataclass(frozen=True) @@ -360,10 +365,7 @@ def check_review_readiness( }""", id=pr["node_id"], ) - raise ValueError( - "PR is not accepted work: the author needs repository write access " - "or a referenced state:accepted issue they submitted themselves" - ) + raise ValueError(ACCEPTANCE_ERROR) categories = parse_reviewers(reviewers.read_text(encoding="utf-8")) selected = select_reviewers(categories, {label["name"] for label in pr["labels"]}) selected = _eligible_reviewers(repository, selected - {pr["user"]["login"].lower()}) @@ -402,24 +404,40 @@ def main(argv: list[str] | None = None) -> int: choices=("opened", "ready_for_review", "labeled", "unlabeled"), help="PR event; acceptance is checked on opened and ready_for_review", ) - parser.add_argument( + mode = parser.add_mutually_exclusive_group() + mode.add_argument( + "--acceptance-only", action="store_true", + help="report acceptance without reading the roster or changing the PR", + ) + mode.add_argument( "--apply", action="store_true", help="update requests or draft unaccepted work" ) args = parser.parse_args(argv) if not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", args.repo) or args.pr <= 0: parser.error("--repo must be OWNER/REPO and --pr must be positive") try: - plan = check_review_readiness( - args.reviewers, args.repo, args.pr, apply=args.apply, event=args.event - ) + if args.acceptance_only: + owner, repo = args.repo.split("/") + pr = _graphql( + """query($owner: String!, $repo: String!, $number: Int!) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $number) { user: author { login } body } + } + }""", + owner=owner, repo=repo, number=args.pr, + )["repository"]["pullRequest"] + if not _accepted_work(args.repo, args.pr, pr): + raise ValueError(ACCEPTANCE_ERROR) + result = {"accepted": True} + else: + plan = check_review_readiness( + args.reviewers, args.repo, args.pr, apply=args.apply, event=args.event + ) + result = asdict(plan) if plan is not None else {"skipped": "closed or draft PR"} except (OSError, UnicodeError, ValueError, subprocess.CalledProcessError) as exc: print(f"Review readiness: {exc}", file=sys.stderr) return 1 - print( - json.dumps( - asdict(plan) if plan is not None else {"skipped": "closed or draft PR"} - ) - ) + print(json.dumps(result)) return 0 diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index 471124925d..00d76769a5 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -"""Offline tests for roster parsing and PR reviewer reconciliation.""" +"""Offline tests for PR acceptance, roster parsing, and reviewer reconciliation.""" from __future__ import annotations @@ -818,6 +818,124 @@ def test_label_write_failure_does_not_request_reviewers(self): class CliTests(unittest.TestCase): + def test_both_cli_modes_use_the_same_acceptance_policy(self): + cases = [ + ("WRITE", "", (), {}, True), + ("READ", "Related to #1", (), {1: ("author", True)}, True), + ("READ", "Closes #1", (1,), {1: ("someone-else", True)}, False), + ("READ", "Closes #1 and #2", (1, 2), + {1: ("author", True), 2: ("author", False)}, True), + ("READ", "Related to #1", (), {1: ("author", False)}, False), + ("READ", "", (), {}, False), + ] + for acceptance_only in (False, True): + for permission, body, linked, issues, expected in cases: + with self.subTest(acceptance_only=acceptance_only, body=body, issues=issues), tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "REVIEWERS.md" + path.write_text(DOCUMENT, encoding="utf-8") + value = pr("area:runtime", body=body) + + def api(endpoint, **kwargs): + if endpoint == PULL: + return value + self.assertEqual(endpoint, "graphql") + query = kwargs["data"]["query"] + self.assertNotIn("mutation", query) + if "user: author" in query: + self.assertEqual(kwargs["data"]["variables"]["number"], 42) + return {"data": {"repository": {"pullRequest": value}}} + if "collaborators" in query: + return AcceptanceTests.member(permission) + if "closingIssuesReferences" in query: + return AcceptanceTests.linked(*linked) + self.assertIn("issueOrPullRequest", query) + author, accepted = issues[kwargs["data"]["variables"]["number"]] + return AcceptanceTests.issue(author, accepted) + + output, errors = io.StringIO(), io.StringIO() + with ( + mock.patch.object(routing, "_api", side_effect=api), + mock.patch.object(routing, "_eligible_reviewers", return_value={"alice"}) as eligible, + mock.patch.object(routing, "_load_reviewers", return_value=reviewers()) as load, + contextlib.redirect_stdout(output), + contextlib.redirect_stderr(errors), + ): + argv = ["--repo", REPO, "--pr", "42", "--reviewers", str(path)] + status = routing.main(argv + (["--acceptance-only"] if acceptance_only else [])) + self.assertEqual(status, 0 if expected else 1) + if expected: + self.assertEqual(json.loads(output.getvalue()), + {"accepted": True} if acceptance_only else {"add": ["alice"], "remove": []}) + else: + self.assertIn(routing.ACCEPTANCE_ERROR, errors.getvalue()) + if acceptance_only: + eligible.assert_not_called() + load.assert_not_called() + + def test_acceptance_only_checks_drafts_and_closed_prs_without_routing(self): + for value in (pr(), pr(draft=True), pr(state="closed")): + for accepted in (False, True): + with self.subTest(value=value, accepted=accepted): + output, errors = io.StringIO(), io.StringIO() + with ( + mock.patch.object(routing, "_graphql", return_value={"repository": {"pullRequest": value}}) as query, + mock.patch.object(routing, "_accepted_work", return_value=accepted) as check, + mock.patch.object(routing, "check_review_readiness") as route, + mock.patch.object(routing, "parse_reviewers") as parse, + mock.patch.object(routing.Path, "read_text", side_effect=AssertionError("Acceptance-only must not read the roster")), + contextlib.redirect_stdout(output), + contextlib.redirect_stderr(errors), + ): + status = routing.main(["--repo", REPO, "--pr", "42", "--acceptance-only"]) + self.assertEqual(status, 0 if accepted else 1) + check.assert_called_once_with(REPO, 42, value) + query.assert_called_once() + self.assertNotIn("mutation", query.call_args.args[0]) + route.assert_not_called() + parse.assert_not_called() + + def test_acceptance_only_lookup_errors_fail_without_routing(self): + for metadata_error in (False, True): + error = subprocess.CalledProcessError(1, "gh") + with ( + self.subTest(metadata_error=metadata_error), + mock.patch.object(routing, "_graphql", side_effect=error if metadata_error else None, + return_value={"repository": {"pullRequest": pr()}}), + mock.patch.object(routing, "_accepted_work", side_effect=error) as check, + mock.patch.object(routing, "check_review_readiness") as route, + contextlib.redirect_stdout(io.StringIO()), + contextlib.redirect_stderr(io.StringIO()), + ): + self.assertEqual(routing.main(["--repo", REPO, "--pr", "42", "--acceptance-only"]), 1) + route.assert_not_called() + if metadata_error: + check.assert_not_called() + + def test_acceptance_only_cannot_be_combined_with_apply(self): + with ( + mock.patch.object(routing, "_api") as api, + contextlib.redirect_stderr(io.StringIO()), + self.assertRaises(SystemExit) as error, + ): + routing.main(["--repo", REPO, "--pr", "42", "--acceptance-only", "--apply"]) + self.assertEqual(error.exception.code, 2) + api.assert_not_called() + + def test_acceptance_only_refreshes_metadata_for_each_pr_and_run(self): + for number in (7, 8, 7): + value = pr(body=f"Related to #{number}") + with ( + self.subTest(number=number), + mock.patch.object(routing, "_graphql", return_value={"repository": {"pullRequest": value}}) as query, + mock.patch.object(routing, "_accepted_work", return_value=True) as check, + contextlib.redirect_stdout(io.StringIO()), + ): + self.assertEqual(routing.main([ + "--repo", REPO, "--pr", str(number), "--acceptance-only", + ]), 0) + self.assertEqual(query.call_args.kwargs["number"], number) + check.assert_called_once_with(REPO, number, value) + def test_rejected_pr_does_not_need_a_readable_valid_roster(self): for contents in (None, "not a registry"): for apply in (False, True): @@ -966,5 +1084,26 @@ def test_accepted_pr_with_invalid_registry_fails_before_reviewer_changes(self): self.assertIn("no area/topic", errors.getvalue()) +class WorkflowTests(unittest.TestCase): + def test_legacy_workflow_keeps_its_status_events_and_read_only_permissions(self): + root = Path(__file__).resolve().parents[2] + workflow = (root / ".github/workflows/pr-issue-acceptance.yml").read_text() + self.assertIn("name: PR Issue Acceptance\n", workflow) + self.assertIn(" name: OpenShell / PR Issue Acceptance\n", workflow) + self.assertIn("types: [opened, edited, reopened, synchronize]", workflow) + permissions = workflow.split("permissions:\n", 1)[1].split("\n\n", 1)[0] + self.assertEqual(dict(line.strip().split(": ") for line in permissions.splitlines()), + {"contents": "read", "issues": "read", "pull-requests": "read"}) + self.assertIn("ref: ${{ github.sha }}", workflow) + self.assertIn("persist-credentials: false", workflow) + self.assertIn("sparse-checkout: tasks/scripts/check_review_readiness.py", workflow) + self.assertIn("GH_REPO: ${{ github.repository }}", workflow) + self.assertIn("PR_NUMBER: ${{ github.event.pull_request.number }}", workflow) + command = workflow.split(" run: |\n", 1)[1] + self.assertIn("python3 tasks/scripts/check_review_readiness.py", command) + self.assertIn('--repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only', command) + self.assertNotIn("--apply", command) + + if __name__ == "__main__": unittest.main() From d0f31b06fe69039454dc2f0832d6cd2e84e5abc0 Mon Sep 17 00:00:00 2001 From: Jim Meyer Date: Tue, 27 Oct 2026 02:01:44 +0000 Subject: [PATCH 5/6] refactor(review): narrow acceptance changes after review Signed-off-by: Jim Meyer --- .agents/skills/create-github-pr/SKILL.md | 10 ++--- .agents/skills/review-github-pr/SKILL.md | 15 ++++--- .github/workflows/pr-issue-acceptance.yml | 3 +- AGENTS.md | 2 +- CONTRIBUTING.md | 6 +-- tasks/scripts/check_review_readiness.py | 42 ++++++++++---------- tasks/scripts/check_review_readiness_test.py | 40 ++++++++++++++++--- 7 files changed, 69 insertions(+), 49 deletions(-) diff --git a/.agents/skills/create-github-pr/SKILL.md b/.agents/skills/create-github-pr/SKILL.md index 213ff6b1a6..957dc8bd60 100644 --- a/.agents/skills/create-github-pr/SKILL.md +++ b/.agents/skills/create-github-pr/SKILL.md @@ -24,16 +24,14 @@ to the target repository, not their fork. For the authenticated `gh` user: ```bash gh repo view OWNER/REPO --json nameWithOwner,viewerPermission -gh issue view ISSUE_NUMBER --repo OWNER/REPO --json number,url,labels,author +gh issue view ISSUE_NUMBER --repo OWNER/REPO --json number,url,labels ``` Authors with verified `WRITE`, `MAINTAIN`, or `ADMIN` access may proceed without `state:accepted`; keep the issue link and note the missing label. Everyone else -must reference at least one target-repository issue they submitted themselves -that has `state:accepted`. Related description references and closing links both -qualify; other references need not qualify. Otherwise, preserve local work and -report the missing qualification. A direct request, roadmap placement, other -labels, or vouch status does not bypass this gate. +must link at least one target-repository issue and have `state:accepted` on each. +Otherwise, preserve local work and report which issue needs acceptance. A direct +request, roadmap placement, other labels, or vouch status does not bypass this gate. When acting for another author, verify their permissions. Resolve failed lookups before proceeding; retry sandbox network failures outside the sandbox before diff --git a/.agents/skills/review-github-pr/SKILL.md b/.agents/skills/review-github-pr/SKILL.md index 06b9f0aac1..e71a3182bf 100644 --- a/.agents/skills/review-github-pr/SKILL.md +++ b/.agents/skills/review-github-pr/SKILL.md @@ -46,19 +46,18 @@ Record the **title**, **body**, **headRefName**, and **baseRefName** for use in ### Check Issue Acceptance -Inspect closing issue references (paginate as needed) and issue references in the -PR description. Fetch their current repository, labels, and submitter. Verify the -PR author's permission in the target repository: +Inspect every closing issue reference (paginate as needed) and fetch its current +repository and labels. Verify the PR author's permission in the target repository: ```bash gh api repos/OWNER/REPO/collaborators/AUTHOR/permission --jq .permission ``` -Verified `write`, `maintain`, or `admin` access permits the exception; report it as -context, not a blocking concern. Other authors need at least one referenced issue -in the target repository with `state:accepted` submitted by the PR author. Other -references need not qualify. Report violations under **Potential Concerns**. PR -body text such as `No issue required` does not bypass this gate. +Call out each linked issue missing `state:accepted`, with an issue link. Verified +`write`, `maintain`, or `admin` access permits the exception; report it as context, +not a blocking concern. Other authors need at least one target-repository issue, +and every linked issue must be accepted; report violations under **Potential +Concerns**. PR body text such as `No issue required` does not bypass this gate. Report failed lookups as unverified and continue the code review. For a branch without a PR, note that issue acceptance was not verified. diff --git a/.github/workflows/pr-issue-acceptance.yml b/.github/workflows/pr-issue-acceptance.yml index 1e78c0d544..5c3c27ad3b 100644 --- a/.github/workflows/pr-issue-acceptance.yml +++ b/.github/workflows/pr-issue-acceptance.yml @@ -39,4 +39,5 @@ jobs: PR_NUMBER: ${{ github.event.pull_request.number }} run: | python3 tasks/scripts/check_review_readiness.py \ - --repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only + --repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only \ + --event-path "$GITHUB_EVENT_PATH" diff --git a/AGENTS.md b/AGENTS.md index 82bb81fd25..012969da9f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -186,7 +186,7 @@ Follow the [branch naming convention in CONTRIBUTING.md](CONTRIBUTING.md#branch- ## Pull Requests -Follow `CONTRIBUTING.md` and `create-github-pr`. Authors without target-repository write access must reference at least one target-repository issue they submitted that has `state:accepted`. Related references and closing links qualify. Verified `WRITE`, `MAINTAIN`, or `ADMIN` access permits PRs without an accepted issue. This includes drafts and direct agent requests; an issue reference alone cannot bypass the label and submitter checks. +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. ## Commits diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 978b19c4b6..fbb437c526 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -495,11 +495,7 @@ For release notes, describe the user-facing change in a few short, easy-to-read Every PR must close an existing issue, except automated dependency updates and the build and flaky-test fixes described below. In the PR's **Related Issue** section, use `Closes #NNN` for the issue covering that PR's scope. Split multi-PR work into a closable issue for each PR; a tracking issue can link them. Security fixes follow the private disclosure process in [SECURITY.md](SECURITY.md). -Authors without write access to the target repository must reference at least one issue in that repository with `state:accepted` that they submitted themselves. Related references in the PR description, such as `Related to #NNN`, and closing links both qualify. One qualifying issue is sufficient; other references need not qualify. This applies to drafts and direct requests to agents. Authors with verified `WRITE`, `MAINTAIN`, or `ADMIN` access may create a PR without an accepted issue; they must still follow the issue-linking requirement above. Roadmap placement, other labels, and vouch status do not bypass acceptance. - -Both acceptance checks use `tasks/scripts/check_review_readiness.py`. The `PR Issue Acceptance` workflow reports the native `OpenShell / PR Issue Acceptance` job result when a PR is opened, edited, reopened, or updated with new commits, including drafts. It only reports acceptance and does not change the PR. `Review Readiness` checks the same policy when a ready PR is opened or leaves draft; it moves rejected work to draft before requesting reviewers. Area/topic label changes only reconcile reviewers. - -The checks do not publish a shared commit status; lookup and execution errors fail the job. Description references support `#NNN`, `OWNER/REPO#NNN`, and GitHub issue URLs. Rerun `PR Issue Acceptance` after issue labels change. Making the job required for merging and configuring any required Actions event policy are separate repository settings; merge-queue support is not included. +Authors without write access to the target repository must link at least one issue with `state:accepted`, and every linked issue must be accepted. This applies to drafts and direct requests to agents. Authors with verified `WRITE`, `MAINTAIN`, or `ADMIN` access may create a PR without the acceptance label; they must still follow the issue-linking requirement above. PR body text, roadmap placement, other labels, and vouch status do not bypass acceptance. Automated dependency update PRs, including Dependabot PRs, are exempt from the separate issue and closing-reference requirement. Human-authored dependency updates follow the normal issue requirement. The exception does not bypass `PR Issue Acceptance` for authors without repository write access or change applicable verification, review, or private vulnerability disclosure requirements. diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index b535df4854..952434cb6b 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -7,15 +7,7 @@ # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -"""Validate accepted work and reconcile reviewers from the merged REVIEWERS.md. - -Preview with --repo OWNER/REPO --pr NUMBER; add --apply to update the PR. -Use --acceptance-only to report acceptance without routing or changing the PR. -Unaccepted work is moved to draft before any reviewer requests are changed. -Request all matching members with write access, excluding the author. Preserve -manual choices, unknown request provenance, and anyone who commented or reviewed. -The parser and planning helpers are importable. No state is stored between runs. -""" +"""Ensure pull requests are ready for review by the appropriate people.""" from __future__ import annotations @@ -35,7 +27,7 @@ PROFILE = re.compile(r"\[@([a-z0-9-]+)\]\(https://github\.com/([a-z0-9-]+)/?\)", re.I) ACCEPTANCE_ERROR = ( "PR is not accepted work: the author needs repository write access " - "or a referenced state:accepted issue they submitted themselves" + "or a referenced state:accepted issue" ) @@ -262,7 +254,7 @@ def _referenced_issues(repository: str, number: int, body: str): def _accepted_work(repository: str, number: int, pr: dict) -> bool: - """Accept write access or a referenced accepted issue filed by the PR author.""" + """Accept write access or a referenced accepted issue.""" owner, repo = repository.split("/") author = pr["user"]["login"].lower() cursor = None @@ -301,7 +293,6 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: repository(owner: $owner, name: $repo) { issueOrPullRequest(number: $number) { ... on Issue { - author { login } labels(first: 100, after: $cursor) { nodes { name } pageInfo { hasNextPage endCursor } @@ -318,7 +309,7 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: break raise issue = data["repository"]["issueOrPullRequest"] - if not issue or (issue.get("author") or {}).get("login", "").lower() != author: + if not issue or "labels" not in issue: break if any(label["name"] == "state:accepted" for label in issue["labels"]["nodes"]): return True @@ -399,6 +390,10 @@ def main(argv: list[str] | None = None) -> int: ) parser.add_argument("--repo", required=True, help="target OWNER/REPO") parser.add_argument("--pr", required=True, type=int, help="pull request number") + parser.add_argument( + "--event-path", type=Path, + help="PR event payload for --acceptance-only; otherwise fetch current PR metadata", + ) parser.add_argument( "--event", default="opened", choices=("opened", "ready_for_review", "labeled", "unlabeled"), @@ -417,15 +412,18 @@ def main(argv: list[str] | None = None) -> int: parser.error("--repo must be OWNER/REPO and --pr must be positive") try: if args.acceptance_only: - owner, repo = args.repo.split("/") - pr = _graphql( - """query($owner: String!, $repo: String!, $number: Int!) { - repository(owner: $owner, name: $repo) { - pullRequest(number: $number) { user: author { login } body } - } - }""", - owner=owner, repo=repo, number=args.pr, - )["repository"]["pullRequest"] + if args.event_path: + pr = json.loads(args.event_path.read_text(encoding="utf-8"))["pull_request"] + else: + owner, repo = args.repo.split("/") + pr = _graphql( + """query($owner: String!, $repo: String!, $number: Int!) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $number) { user: author { login } body } + } + }""", + owner=owner, repo=repo, number=args.pr, + )["repository"]["pullRequest"] if not _accepted_work(args.repo, args.pr, pr): raise ValueError(ACCEPTANCE_ERROR) result = {"accepted": True} diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index 00d76769a5..10cc062f31 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -320,13 +320,14 @@ def test_qualifying_closing_reference_skips_later_pages(self): self.assertTrue(routing._accepted_work(REPO, 42, pr())) self.assertEqual(api.call_count, 3) - def test_description_and_closing_references_require_same_submitter_and_label(self): + def test_description_and_closing_references_require_acceptance_not_same_submitter(self): for body in ( "", "Related to #1", "Related to owner/repo#1", "Related to OWNER/REPO#1", "Related to https://github.com/owner/repo/issues/1", ): for author, accepted, expected in ( - ("AUTHOR", True, True), ("someone-else", True, False), ("author", False, False), + ("AUTHOR", True, True), ("someone-else", True, True), + ("author", False, False), ("someone-else", False, False), ): with ( self.subTest(body=body, author=author, accepted=accepted), @@ -822,15 +823,16 @@ def test_both_cli_modes_use_the_same_acceptance_policy(self): cases = [ ("WRITE", "", (), {}, True), ("READ", "Related to #1", (), {1: ("author", True)}, True), - ("READ", "Closes #1", (1,), {1: ("someone-else", True)}, False), + ("READ", "Closes #1", (1,), {1: ("someone-else", True)}, True), ("READ", "Closes #1 and #2", (1, 2), {1: ("author", True), 2: ("author", False)}, True), ("READ", "Related to #1", (), {1: ("author", False)}, False), ("READ", "", (), {}, False), ] - for acceptance_only in (False, True): + for mode in ("preview", "acceptance-api", "acceptance-event"): + acceptance_only = mode != "preview" for permission, body, linked, issues, expected in cases: - with self.subTest(acceptance_only=acceptance_only, body=body, issues=issues), tempfile.TemporaryDirectory() as directory: + with self.subTest(mode=mode, body=body, issues=issues), tempfile.TemporaryDirectory() as directory: path = Path(directory) / "REVIEWERS.md" path.write_text(DOCUMENT, encoding="utf-8") value = pr("area:runtime", body=body) @@ -842,6 +844,7 @@ def api(endpoint, **kwargs): query = kwargs["data"]["query"] self.assertNotIn("mutation", query) if "user: author" in query: + self.assertNotEqual(mode, "acceptance-event") self.assertEqual(kwargs["data"]["variables"]["number"], 42) return {"data": {"repository": {"pullRequest": value}}} if "collaborators" in query: @@ -861,7 +864,13 @@ def api(endpoint, **kwargs): contextlib.redirect_stderr(errors), ): argv = ["--repo", REPO, "--pr", "42", "--reviewers", str(path)] - status = routing.main(argv + (["--acceptance-only"] if acceptance_only else [])) + if acceptance_only: + argv.append("--acceptance-only") + if mode == "acceptance-event": + event_path = Path(directory) / "event.json" + event_path.write_text(json.dumps({"pull_request": value})) + argv.extend(["--event-path", str(event_path)]) + status = routing.main(argv) self.assertEqual(status, 0 if expected else 1) if expected: self.assertEqual(json.loads(output.getvalue()), @@ -911,6 +920,24 @@ def test_acceptance_only_lookup_errors_fail_without_routing(self): if metadata_error: check.assert_not_called() + def test_invalid_event_payload_fails_without_api_calls_or_routing(self): + for contents in (None, "not JSON"): + with self.subTest(contents=contents), tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "event.json" + if contents is not None: + path.write_text(contents) + with ( + mock.patch.object(routing, "_api") as api, + mock.patch.object(routing, "check_review_readiness") as route, + contextlib.redirect_stderr(io.StringIO()), + ): + self.assertEqual(routing.main([ + "--repo", REPO, "--pr", "42", "--acceptance-only", + "--event-path", str(path), + ]), 1) + api.assert_not_called() + route.assert_not_called() + def test_acceptance_only_cannot_be_combined_with_apply(self): with ( mock.patch.object(routing, "_api") as api, @@ -1102,6 +1129,7 @@ def test_legacy_workflow_keeps_its_status_events_and_read_only_permissions(self) command = workflow.split(" run: |\n", 1)[1] self.assertIn("python3 tasks/scripts/check_review_readiness.py", command) self.assertIn('--repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only', command) + self.assertIn('--event-path "$GITHUB_EVENT_PATH"', command) self.assertNotIn("--apply", command) From be7d0b99fe97efbaf9f9d3b1ee55a66652401139 Mon Sep 17 00:00:00 2001 From: Jim Meyer Date: Tue, 27 Oct 2026 05:13:27 +0000 Subject: [PATCH 6/6] feat(review): explain automatic draft conversion once Signed-off-by: Jim Meyer --- .agents/skills/create-github-pr/SKILL.md | 3 +- .github/workflows/pr-issue-acceptance.yml | 5 +- .github/workflows/review-readiness.yml | 11 +- CI.md | 2 +- tasks/scripts/check_review_readiness.py | 183 +++- tasks/scripts/check_review_readiness_test.py | 950 ++++++++++++++----- 6 files changed, 850 insertions(+), 304 deletions(-) diff --git a/.agents/skills/create-github-pr/SKILL.md b/.agents/skills/create-github-pr/SKILL.md index 957dc8bd60..f99a23665f 100644 --- a/.agents/skills/create-github-pr/SKILL.md +++ b/.agents/skills/create-github-pr/SKILL.md @@ -261,7 +261,8 @@ Created PR [#123](https://github.com/OWNER/REPO/pull/123) Follow the shared [accepted-work definition](../../../CONTRIBUTING.md#accepted-work) and [Labeling and Notifications](../../../CONTRIBUTING.md#labeling-and-notifications) when applying classification labels. Review-readiness applies the acceptance label -to qualifying PRs when they open; this does not replace existing PR requirements. +to qualifying PRs when they open and moves unaccepted ready PRs back to draft. +This does not replace existing PR requirements. ### Monitor Workflow Run (Optional) diff --git a/.github/workflows/pr-issue-acceptance.yml b/.github/workflows/pr-issue-acceptance.yml index 5c3c27ad3b..a5e34633db 100644 --- a/.github/workflows/pr-issue-acceptance.yml +++ b/.github/workflows/pr-issue-acceptance.yml @@ -29,7 +29,10 @@ jobs: with: ref: ${{ github.sha }} persist-credentials: false - sparse-checkout: tasks/scripts/check_review_readiness.py + sparse-checkout: | + tasks/scripts/check_review_readiness.py + tasks/scripts/label_accepted_work.py + tasks/scripts/check_maintainer_approval.py sparse-checkout-cone-mode: false - name: Check PR acceptance diff --git a/.github/workflows/review-readiness.yml b/.github/workflows/review-readiness.yml index d68977df00..7855c4e800 100644 --- a/.github/workflows/review-readiness.yml +++ b/.github/workflows/review-readiness.yml @@ -47,7 +47,12 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} GH_REPO: ${{ github.repository }} PR_NUMBER: ${{ github.event.pull_request.number }} - PR_EVENT: ${{ github.event.action }} + PR_ACTION: ${{ github.event.action }} + shell: bash run: | - python3 tasks/scripts/check_review_readiness.py \ - --repo "$GH_REPO" --pr "$PR_NUMBER" --event "$PR_EVENT" --apply + set -euo pipefail + args=(--repo "$GH_REPO" --pr "$PR_NUMBER" --event "$PR_ACTION" --apply) + if [ "$PR_ACTION" = opened ]; then + args+=(--label-accepted-work) + fi + python3 tasks/scripts/check_review_readiness.py "${args[@]}" diff --git a/CI.md b/CI.md index 923a552267..0179391bd2 100644 --- a/CI.md +++ b/CI.md @@ -705,7 +705,7 @@ The bot's full administrator documentation is internal to NVIDIA. The only comma | `.github/actions/pr-merge-base/action.yml` | Composite action that resolves and fetches the merge-base commit for `pull-request/` push workflows. | | `.github/workflows/required-ci-gates.yml` | Posts required PR-head and merge-group statuses for gated CI workflows. This is what branch protection and merge queue should require. | | `.github/workflows/e2e-label-help.yml` | When a `test:e2e*` label is applied, posts a PR comment telling the maintainer the next manual step (re-run an existing workflow run, or `/ok to test ` to refresh the mirror). | -| `.github/workflows/review-readiness.yml` | Labels newly opened accepted-work PRs using the shared read-only acceptance library, including drafts; routes matching reviewers when a PR is ready and classified. | +| `.github/workflows/review-readiness.yml` | Labels newly opened accepted-work PRs using the shared read-only acceptance library, including drafts; moves unaccepted ready PRs back to draft with a one-time explanation; routes matching reviewers when a PR is ready and classified. | | `.github/workflows/workflow-security.yml` | Runs informational Actionlint and High-severity Zizmor reports on GitHub-hosted runners. | | `.github/workflows/dependency-review.yml` | Reports dependency changes when GitHub Dependency Graph is available; otherwise publishes a neutral warning. | | `.github/workflows/codeql.yml` | Runs nightly informational CodeQL analysis on `main` for Rust and the Python and TypeScript SDKs and retains SARIF artifacts. | diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index 952434cb6b..db767b397f 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -27,7 +27,14 @@ PROFILE = re.compile(r"\[@([a-z0-9-]+)\]\(https://github\.com/([a-z0-9-]+)/?\)", re.I) ACCEPTANCE_ERROR = ( "PR is not accepted work: the author needs repository write access " - "or a referenced state:accepted issue" + "or state:accepted on the PR or a referenced issue" +) +DRAFT_COMMENT_MARKER = "" +DRAFT_COMMENT = ( + "This PR has been automatically moved to draft because it's not associated " + "with an issue that is accepted work. You're welcome to continue to work on " + 'it while it\'s in draft mode; it will be allowed to move to "Ready for Review" ' + "when the issue or PR have been marked accepted.\n\n" + DRAFT_COMMENT_MARKER ) @@ -203,15 +210,6 @@ def _load_reviewers(repository: str, number: int, pr: dict) -> dict[str, set[str } -def _eligible_reviewers(repository: str, logins: set[str]) -> set[str]: - eligible = set() - for login in sorted(logins): - value = _api(f"repos/{repository}/collaborators/{login}/permission") - if value.get("permission") in {"write", "maintain", "admin"}: - eligible.add(login) - return eligible - - def _referenced_issues(repository: str, number: int, body: str): """Yield description references first, then closing links, without duplicates.""" escaped = re.escape(repository) @@ -238,7 +236,10 @@ def _referenced_issues(repository: str, number: int, body: str): } } }""", - owner=owner, repo=repo, number=number, cursor=cursor, + owner=owner, + repo=repo, + number=number, + cursor=cursor, ) linked = data["repository"]["pullRequest"]["closingIssuesReferences"] for issue in linked["nodes"]: @@ -254,7 +255,11 @@ def _referenced_issues(repository: str, number: int, body: str): def _accepted_work(repository: str, number: int, pr: dict) -> bool: - """Accept write access or a referenced accepted issue.""" + """Accept write access or acceptance recorded on the PR or a referenced issue.""" + if pr["user"]["login"].lower() == "dependabot[bot]": + return True + if any(label["name"] == "state:accepted" for label in pr["labels"]): + return True owner, repo = repository.split("/") author = pr["user"]["login"].lower() cursor = None @@ -268,12 +273,18 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: } } }""", - owner=owner, repo=repo, author=author, cursor=cursor, + owner=owner, + repo=repo, + author=author, + cursor=cursor, ) members = data["repository"]["collaborators"] member = next( - (edge for edge in members["edges"] - if edge["node"]["login"].lower() == author), + ( + edge + for edge in members["edges"] + if edge["node"]["login"].lower() == author + ), None, ) if member: @@ -301,7 +312,10 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: } } }""", - owner=owner, repo=repo, number=issue_number, cursor=cursor, + owner=owner, + repo=repo, + number=issue_number, + cursor=cursor, ) except subprocess.CalledProcessError as exc: errors = json.loads(exc.stdout or "{}").get("errors", []) @@ -311,7 +325,9 @@ def _accepted_work(repository: str, number: int, pr: dict) -> bool: issue = data["repository"]["issueOrPullRequest"] if not issue or "labels" not in issue: break - if any(label["name"] == "state:accepted" for label in issue["labels"]["nodes"]): + if any( + label["name"] == "state:accepted" for label in issue["labels"]["nodes"] + ): return True if not issue["labels"]["pageInfo"]["hasNextPage"]: break @@ -332,20 +348,45 @@ def _routing_key(pr: dict): def check_review_readiness( - reviewers: Path, + reviewers: Path | dict[str, set[str]], repository: str, number: int, *, apply=False, event="opened", + label_accepted_work=False, + maintainers: set[str] | None = None, ) -> ReviewPlan | None: - """Validate acceptance, then route. Callers must serialize runs for the same PR.""" + """Plan labeling and routing; callers serialize runs for the same PR. + + Enable label_accepted_work for newly opened PRs, including drafts. Acceptance + is independent of readiness and classification; only reviewer routing needs + a ready PR with an area label. Labeling never removes an existing label. + """ pull = f"repos/{repository}/pulls/{number}" pr = _api(pull) if pr["state"] != "open": return None - if event in {"opened", "ready_for_review"} and not _accepted_work( - repository, number, pr + accepted = None + add_labels = () + labels = {label["name"] for label in pr["labels"]} + if label_accepted_work: + accepted = check_pr_accepted_work( + repository, pr, maintainers or set(), api=_api + ) + if accepted and ACCEPTED_LABEL not in labels: + add_labels = (ACCEPTED_LABEL,) + if apply: + _api( + f"repos/{repository}/issues/{number}/labels", + method="POST", + data={"labels": list(add_labels)}, + ) + if ( + not pr["draft"] + and event in {"opened", "ready_for_review"} + and accepted is not True + and not _accepted_work(repository, number, pr) ): if apply: _graphql( @@ -356,11 +397,30 @@ def check_review_readiness( }""", id=pr["node_id"], ) + comments = f"repos/{repository}/issues/{number}/comments" + previous = _api(f"{comments}?per_page=100", pages=True) + if not any( + DRAFT_COMMENT_MARKER in (comment.get("body") or "") + for comment in previous + ): + _api(comments, method="POST", data={"body": DRAFT_COMMENT}) raise ValueError(ACCEPTANCE_ERROR) - categories = parse_reviewers(reviewers.read_text(encoding="utf-8")) - selected = select_reviewers(categories, {label["name"] for label in pr["labels"]}) - selected = _eligible_reviewers(repository, selected - {pr["user"]["login"].lower()}) - plan = plan_review_requests(selected, **_load_reviewers(repository, number, pr)) + if pr["draft"] or not any(label.startswith("area:") for label in labels): + if pr["draft"] and not label_accepted_work: + return None + return ReviewPlan((), (), accepted, add_labels) + categories = ( + reviewers + if isinstance(reviewers, dict) + else parse_reviewers(reviewers.read_text(encoding="utf-8")) + ) + selected = select_reviewers(categories, labels) + selected -= {pr["user"]["login"].lower()} + plan = replace( + plan_review_requests(selected, **_load_reviewers(repository, number, pr)), + accepted_work=accepted, + add_labels=add_labels, + ) if apply: endpoint = f"{pull}/requested_reviewers" if plan.add: @@ -391,17 +451,20 @@ def main(argv: list[str] | None = None) -> int: parser.add_argument("--repo", required=True, help="target OWNER/REPO") parser.add_argument("--pr", required=True, type=int, help="pull request number") parser.add_argument( - "--event-path", type=Path, + "--event-path", + type=Path, help="PR event payload for --acceptance-only; otherwise fetch current PR metadata", ) parser.add_argument( - "--event", default="opened", + "--event", + default="opened", choices=("opened", "ready_for_review", "labeled", "unlabeled"), help="PR event; acceptance is checked on opened and ready_for_review", ) mode = parser.add_mutually_exclusive_group() mode.add_argument( - "--acceptance-only", action="store_true", + "--acceptance-only", + action="store_true", help="report acceptance without reading the roster or changing the PR", ) mode.add_argument( @@ -413,26 +476,66 @@ def main(argv: list[str] | None = None) -> int: try: if args.acceptance_only: if args.event_path: - pr = json.loads(args.event_path.read_text(encoding="utf-8"))["pull_request"] + pr = json.loads(args.event_path.read_text(encoding="utf-8"))[ + "pull_request" + ] else: owner, repo = args.repo.split("/") - pr = _graphql( - """query($owner: String!, $repo: String!, $number: Int!) { - repository(owner: $owner, name: $repo) { - pullRequest(number: $number) { user: author { login } body } - } - }""", - owner=owner, repo=repo, number=args.pr, - )["repository"]["pullRequest"] + cursor, labels = None, [] + while True: + pr = _graphql( + """query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $number) { + user: author { login } + body + labels(first: 100, after: $cursor) { + nodes { name } + pageInfo { hasNextPage endCursor } + } + } + } + }""", + owner=owner, + repo=repo, + number=args.pr, + cursor=cursor, + )["repository"]["pullRequest"] + page = pr["labels"] + labels.extend(page["nodes"]) + if not page["pageInfo"]["hasNextPage"]: + break + cursor = page["pageInfo"]["endCursor"] + pr["labels"] = labels if not _accepted_work(args.repo, args.pr, pr): raise ValueError(ACCEPTANCE_ERROR) result = {"accepted": True} else: + maintainers = ( + parse_maintainers(args.maintainers.read_text(encoding="utf-8")) + if args.label_accepted_work + else None + ) plan = check_review_readiness( - args.reviewers, args.repo, args.pr, apply=args.apply, event=args.event + args.reviewers, + args.repo, + args.pr, + apply=args.apply, + event=args.event, + label_accepted_work=args.label_accepted_work, + maintainers=maintainers, + ) + result = ( + asdict(plan) if plan is not None else {"skipped": "closed or draft PR"} ) - result = asdict(plan) if plan is not None else {"skipped": "closed or draft PR"} - except (OSError, UnicodeError, ValueError, subprocess.CalledProcessError) as exc: + except ( + OSError, + UnicodeError, + ValueError, + KeyError, + TypeError, + subprocess.CalledProcessError, + ) as exc: print(f"Review readiness: {exc}", file=sys.stderr) return 1 print(json.dumps(result)) diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index 10cc062f31..1955a6c8f2 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -43,6 +43,23 @@ def pr(*labels, requested=(), author="author", draft=False, state="open", body=" } +def graphql_pr(value, *, cursor=None): + return { + "repository": { + "pullRequest": { + **value, + "labels": { + "nodes": value["labels"], + "pageInfo": { + "hasNextPage": cursor is not None, + "endCursor": cursor, + }, + }, + } + } + } + + def reviewers(*, requested=(), engaged=(), manual=()): return {"requested": set(requested), "engaged": set(engaged), "manual": set(manual)} @@ -226,13 +243,11 @@ def test_new_automatic_request_after_removal_is_not_protected(self): class GitHubTests(unittest.TestCase): - def test_graphql_passes_variables_and_unwraps_response(self): - with mock.patch.object(routing, "_api", return_value={"data": {"value": 1}}) as api: - self.assertEqual(routing._graphql("query", number=42), {"value": 1}) - api.assert_called_once_with( - "graphql", method="POST", - data={"query": "query", "variables": {"number": 42}}, - ) + def test_api_rejects_empty_paginated_response(self): + with mock.patch.object(routing.subprocess, "run") as run: + run.return_value.stdout = "" + with self.assertRaisesRegex(ValueError, "paginated"): + routing._api("endpoint", pages=True) def test_api_reads_all_pages(self): with mock.patch.object(routing.subprocess, "run") as run: @@ -268,175 +283,434 @@ def test_loads_all_engagement_and_timeline_feeds(self): class AcceptanceTests(unittest.TestCase): @staticmethod def member(permission="READ", login="author", cursor=None): - return {"data": {"repository": {"collaborators": { - "edges": [{"permission": permission, "node": {"login": login}}], - "pageInfo": {"hasNextPage": cursor is not None, "endCursor": cursor}, - }}}} + return { + "data": { + "repository": { + "collaborators": { + "edges": [{"permission": permission, "node": {"login": login}}], + "pageInfo": { + "hasNextPage": cursor is not None, + "endCursor": cursor, + }, + } + } + } + } @staticmethod def linked(*numbers, repository=REPO, cursor=None): - return {"data": {"repository": {"pullRequest": {"closingIssuesReferences": { - "nodes": [{"number": n, "repository": {"nameWithOwner": repository}} for n in numbers], - "pageInfo": {"hasNextPage": cursor is not None, "endCursor": cursor}, - }}}}} + return { + "data": { + "repository": { + "pullRequest": { + "closingIssuesReferences": { + "nodes": [ + { + "number": n, + "repository": {"nameWithOwner": repository}, + } + for n in numbers + ], + "pageInfo": { + "hasNextPage": cursor is not None, + "endCursor": cursor, + }, + } + } + } + } + } @staticmethod def issue(author="author", accepted=True, cursor=None): - return {"data": {"repository": {"issueOrPullRequest": { - "author": {"login": author}, - "labels": { - "nodes": [{"name": "state:accepted"}] if accepted else [], - "pageInfo": {"hasNextPage": cursor is not None, "endCursor": cursor}, - }, - }}}} + return { + "data": { + "repository": { + "issueOrPullRequest": { + "author": {"login": author}, + "labels": { + "nodes": [{"name": "state:accepted"}] if accepted else [], + "pageInfo": { + "hasNextPage": cursor is not None, + "endCursor": cursor, + }, + }, + } + } + } + } + + def test_dependabot_remains_exempt_without_lookups(self): + with mock.patch.object( + routing, "_api", side_effect=AssertionError("No lookups") + ): + self.assertTrue( + routing._accepted_work(REPO, 42, pr(author="dependabot[bot]")) + ) def test_write_maintain_and_admin_return_without_looking_up_issues(self): for permission in ("WRITE", "MAINTAIN", "ADMIN"): with ( self.subTest(permission=permission), - mock.patch.object(routing, "_api", return_value=self.member(permission)) as api, + mock.patch.object( + routing, "_api", return_value=self.member(permission) + ) as api, ): - self.assertTrue(routing._accepted_work(REPO, 42, pr(body="See #9999999"))) + self.assertTrue( + routing._accepted_work(REPO, 42, pr(body="See #9999999")) + ) api.assert_called_once() self.assertNotIn("pullRequest", api.call_args.kwargs["data"]["query"]) + def test_acceptance_recorded_on_pr_needs_no_further_lookups(self): + with mock.patch.object(routing, "_api") as api: + self.assertTrue(routing._accepted_work(REPO, 42, pr("state:accepted"))) + api.assert_not_called() + def test_collaborator_search_result_must_match_the_pr_author(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member("ADMIN", login="someone-else"), self.linked(), - ]): + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member("ADMIN", login="someone-else"), + self.linked(), + ], + ): self.assertFalse(routing._accepted_work(REPO, 42, pr())) def test_qualifying_description_reference_skips_all_closing_link_queries(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.issue(), - ]) as api: + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + self.issue(), + ], + ) as api: self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1"))) self.assertEqual(api.call_count, 2) def test_qualifying_closing_reference_skips_later_pages(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked(1, cursor="unneeded-page"), self.issue(), - ]) as api: + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + self.linked(1, cursor="unneeded-page"), + self.issue(), + ], + ) as api: self.assertTrue(routing._accepted_work(REPO, 42, pr())) self.assertEqual(api.call_count, 3) - def test_description_and_closing_references_require_acceptance_not_same_submitter(self): + def test_description_and_closing_references_require_acceptance_not_same_submitter( + self, + ): for body in ( - "", "Related to #1", "Related to owner/repo#1", "Related to OWNER/REPO#1", + "", + "Related to #1", + "Related to owner/repo#1", + "Related to OWNER/REPO#1", "Related to https://github.com/owner/repo/issues/1", ): for author, accepted, expected in ( - ("AUTHOR", True, True), ("someone-else", True, True), - ("author", False, False), ("someone-else", False, False), + ("AUTHOR", True, True), + ("someone-else", True, True), + ("author", False, False), + ("someone-else", False, False), ): with ( self.subTest(body=body, author=author, accepted=accepted), - mock.patch.object(routing, "_api", side_effect=( - [self.member(), self.linked(1), self.issue(author, accepted)] - if not body else - [self.member(), self.issue(author, accepted)] + ([] if expected else [self.linked()]) - )) as api, + mock.patch.object( + routing, + "_api", + side_effect=( + [ + self.member(), + self.linked(1), + self.issue(author, accepted), + ] + if not body + else [self.member(), self.issue(author, accepted)] + + ([] if expected else [self.linked()]) + ), + ) as api, ): - self.assertEqual(routing._accepted_work(REPO, 42, pr(body=body)), expected) - lookups = [call.kwargs["data"]["variables"]["number"] - for call in api.call_args_list - if "issueOrPullRequest" in call.kwargs["data"]["query"]] + self.assertEqual( + routing._accepted_work(REPO, 42, pr(body=body)), expected + ) + lookups = [ + call.kwargs["data"]["variables"]["number"] + for call in api.call_args_list + if "issueOrPullRequest" in call.kwargs["data"]["query"] + ] self.assertEqual(lookups, [1]) - def test_one_qualifying_issue_is_enough_and_duplicate_references_are_read_once(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.issue(accepted=False), self.linked(1, 2), self.issue(), - ]) as api: + def test_one_qualifying_issue_is_enough_and_duplicate_references_are_read_once( + self, + ): + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + self.issue(accepted=False), + self.linked(1, 2), + self.issue(), + ], + ) as api: self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1"))) self.assertEqual(api.call_count, 4) def test_paginates_permission_lookup_before_querying_issues(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(login="someone-else", cursor="next-page"), self.member("WRITE"), - ]) as api: + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(login="someone-else", cursor="next-page"), + self.member("WRITE"), + ], + ) as api: self.assertTrue(routing._accepted_work(REPO, 42, pr())) self.assertEqual(api.call_count, 2) - self.assertEqual(api.call_args.kwargs["data"]["variables"]["cursor"], "next-page") + self.assertEqual( + api.call_args.kwargs["data"]["variables"]["cursor"], "next-page" + ) def test_reads_later_closing_reference_pages(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked(cursor="next-page"), self.linked(1), self.issue(), - ]) as api: + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + self.linked(cursor="next-page"), + self.linked(1), + self.issue(), + ], + ) as api: self.assertTrue(routing._accepted_work(REPO, 42, pr())) - self.assertEqual(api.call_args_list[2].kwargs["data"]["variables"]["cursor"], "next-page") + self.assertEqual( + api.call_args_list[2].kwargs["data"]["variables"]["cursor"], "next-page" + ) def test_reads_later_label_pages_on_the_referenced_issue(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked(1), - self.issue(accepted=False, cursor="next-page"), self.issue(), - ]) as api: + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + self.linked(1), + self.issue(accepted=False, cursor="next-page"), + self.issue(), + ], + ) as api: self.assertTrue(routing._accepted_work(REPO, 42, pr())) - self.assertEqual(api.call_args.kwargs["data"]["variables"]["cursor"], "next-page") + self.assertEqual( + api.call_args.kwargs["data"]["variables"]["cursor"], "next-page" + ) def test_cross_repository_references_do_not_qualify(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), self.linked(1, repository="other/repo"), - ]) as api: - self.assertFalse(routing._accepted_work(REPO, 42, pr(body=( - "Related to other/repo#1 and https://github.com/other/repo/issues/1" - )))) + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + self.linked(1, repository="other/repo"), + ], + ) as api: + self.assertFalse( + routing._accepted_work( + REPO, + 42, + pr( + body=( + "Related to other/repo#1 and https://github.com/other/repo/issues/1" + ) + ), + ) + ) self.assertEqual(api.call_count, 2) def test_missing_issues_do_not_qualify_and_other_graphql_errors_propagate(self): for error_type in ("NOT_FOUND", "FORBIDDEN"): error = subprocess.CalledProcessError( - 1, "gh", output=json.dumps({"errors": [{"type": error_type}]}), + 1, + "gh", + output=json.dumps({"errors": [{"type": error_type}]}), ) with ( self.subTest(error_type=error_type), - mock.patch.object(routing, "_api", side_effect=[ - self.member(), error, self.linked(), - ]), + mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + error, + self.linked(), + ], + ), ): if error_type == "NOT_FOUND": - self.assertFalse(routing._accepted_work(REPO, 42, pr(body="See #1"))) + self.assertFalse( + routing._accepted_work(REPO, 42, pr(body="See #1")) + ) else: with self.assertRaises(subprocess.CalledProcessError): routing._accepted_work(REPO, 42, pr(body="See #1")) def test_pull_request_references_do_not_qualify(self): - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), {"data": {"repository": {"issueOrPullRequest": {}}}}, - self.linked(), - ]): + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + {"data": {"repository": {"issueOrPullRequest": {}}}}, + self.linked(), + ], + ): self.assertFalse(routing._accepted_work(REPO, 42, pr(body="See #1"))) def test_missing_description_reference_falls_back_to_qualifying_closing_link(self): missing = subprocess.CalledProcessError( - 1, "gh", output=json.dumps({"errors": [{"type": "NOT_FOUND"}]}), + 1, + "gh", + output=json.dumps({"errors": [{"type": "NOT_FOUND"}]}), ) - with mock.patch.object(routing, "_api", side_effect=[ - self.member(), missing, self.linked(2), self.issue(), - ]): + with mock.patch.object( + routing, + "_api", + side_effect=[ + self.member(), + missing, + self.linked(2), + self.issue(), + ], + ): self.assertTrue(routing._accepted_work(REPO, 42, pr(body="Related to #1"))) - def test_creation_and_ready_transition_reject_before_reviewer_changes(self): - for event in ("opened", "ready_for_review"): - for apply in (False, True): - with ( - self.subTest(event=event, apply=apply), - mock.patch.object(routing, "_api", side_effect=[pr(), DRAFTED]) as api, - mock.patch.object(routing, "_accepted_work", return_value=False), - mock.patch.object(routing, "_load_reviewers") as load, - ): - with self.assertRaisesRegex(ValueError, "not accepted work"): - routing.check_review_readiness(ROSTER, REPO, 42, apply=apply, event=event) - load.assert_not_called() - self.assertEqual(api.call_count, 2 if apply else 1) - if apply: - self.assertIn("convertPullRequestToDraft", api.call_args.kwargs["data"]["query"]) - self.assertEqual(api.call_args.kwargs["data"]["variables"], {"id": "PR_node_42"}) + def test_first_draft_conversion_comments_once_and_subsequent_attempts_are_silent( + self, + ): + comments = f"repos/{REPO}/issues/42/comments" + posted = {"user": {"type": "Bot"}, "body": ""} + with ( + mock.patch.object( + routing, + "_api", + side_effect=[ + pr(), + DRAFTED, + [], + posted, + pr(), + DRAFTED, + [posted], + pr(), + DRAFTED, + [posted], + ], + ) as api, + mock.patch.object(routing, "_accepted_work", return_value=False), + mock.patch.object(routing, "_load_reviewers") as load, + ): + for attempt in range(3): + with self.assertRaisesRegex(ValueError, "not accepted work"): + routing.check_review_readiness( + ROSTER, REPO, 42, apply=True, event="ready_for_review" + ) + if attempt == 0: + posted["body"] = api.call_args.kwargs["data"]["body"] + self.assertEqual( + sum( + "convertPullRequestToDraft" + in call.kwargs.get("data", {}).get("query", "") + for call in api.call_args_list + ), + 3, + ) + self.assertEqual( + sum( + call.args[0] == comments and call.kwargs.get("method") == "POST" + for call in api.call_args_list + ), + 1, + ) + load.assert_not_called() + + def test_existing_notice_suppresses_comment_and_all_pages_are_read(self): + comments = f"repos/{REPO}/issues/42/comments" + for previous, should_post in ( + ([{"user": {"type": "User"}, "body": routing.DRAFT_COMMENT}], False), + ([{"user": {"type": "Bot"}, "body": "unrelated comment"}], True), + ([{"user": None, "body": None}], True), + ( + [{"user": {"type": "Bot"}, "body": None}] * 100 + + [{"user": {"type": "Bot"}, "body": routing.DRAFT_COMMENT}], + False, + ), + ): + with ( + self.subTest(previous=previous, should_post=should_post), + mock.patch.object( + routing, + "_api", + side_effect=[ + pr(), + DRAFTED, + previous, + {}, + ], + ) as api, + mock.patch.object(routing, "_accepted_work", return_value=False), + self.assertRaisesRegex(ValueError, "not accepted work"), + ): + routing.check_review_readiness(ROSTER, REPO, 42, apply=True) + self.assertIn( + mock.call(f"{comments}?per_page=100", pages=True), api.call_args_list + ) + self.assertEqual( + any( + call.kwargs.get("method") == "POST" and call.args[0] == comments + for call in api.call_args_list + ), + should_post, + ) + + def test_comment_lookup_or_post_failure_still_leaves_pr_drafted(self): + for posting in (False, True): + error = subprocess.CalledProcessError(1, "gh") + with ( + self.subTest(posting=posting), + mock.patch.object( + routing, + "_api", + side_effect=( + [pr(), DRAFTED, [], error] + if posting + else [pr(), DRAFTED, error] + ), + ) as api, + mock.patch.object(routing, "_accepted_work", return_value=False), + mock.patch.object(routing, "_load_reviewers") as load, + self.assertRaises(subprocess.CalledProcessError), + ): + routing.check_review_readiness(ROSTER, REPO, 42, apply=True) + self.assertIn( + "convertPullRequestToDraft", + api.call_args_list[1].kwargs["data"]["query"], + ) + load.assert_not_called() def test_lookup_errors_fail_without_drafting_or_requesting_reviewers(self): with ( - mock.patch.object(routing, "_api", side_effect=[ - pr(), subprocess.CalledProcessError(1, "gh"), - ]) as api, + mock.patch.object( + routing, + "_api", + side_effect=[ + pr(), + subprocess.CalledProcessError(1, "gh"), + ], + ) as api, mock.patch.object(routing, "_load_reviewers") as load, self.assertRaises(subprocess.CalledProcessError), ): @@ -446,7 +720,11 @@ def test_lookup_errors_fail_without_drafting_or_requesting_reviewers(self): def test_draft_conversion_errors_propagate(self): with ( - mock.patch.object(routing, "_api", side_effect=[pr(), subprocess.CalledProcessError(1, "gh")]), + mock.patch.object( + routing, + "_api", + side_effect=[pr(), subprocess.CalledProcessError(1, "gh")], + ), mock.patch.object(routing, "_accepted_work", return_value=False), self.assertRaises(subprocess.CalledProcessError), ): @@ -464,7 +742,9 @@ def setUp(self): self.parser = parser.start() self.addCleanup(parser.stop) - def run_routing(self, categories, *, snapshots, responses, apply=True, event="opened"): + def run_routing( + self, categories, *, snapshots, responses, apply=True, event="opened" + ): self.parser.return_value = categories with ( mock.patch.object(routing, "_api", side_effect=responses) as api, @@ -472,8 +752,10 @@ def run_routing(self, categories, *, snapshots, responses, apply=True, event="op routing, "_load_reviewers", side_effect=snapshots ) as load, ): - plan = routing.check_review_readiness(self.roster, REPO, 42, apply=apply, event=event) - return plan, api, load, eligible + plan = routing.check_review_readiness( + self.roster, REPO, 42, apply=apply, event=event + ) + return plan, api, load def test_initial_assignment_excludes_author_without_extra_refresh(self): plan, api, load = self.run_routing( @@ -526,23 +808,15 @@ def test_label_changes_reconcile_without_rechecking_acceptance(self): self.subTest(event=event), mock.patch.object(routing, "_accepted_work") as accepted, ): - plan, _, _, _ = self.run_routing( - CATEGORIES, snapshots=[reviewers()], - responses=[pr("area:build"), None], event=event, + plan, _, _ = self.run_routing( + CATEGORIES, + snapshots=[reviewers()], + responses=[pr("area:build"), None], + event=event, ) self.assertEqual(plan.add, ("dave",)) accepted.assert_not_called() - def test_draft_and_closed_prs_skip_all_routing(self): - for value in (pr(draft=True), pr(state="closed")): - with self.subTest(value=value): - plan, api, load = self.run_routing( - CATEGORIES, snapshots=[], responses=[value] - ) - self.assertIsNone(plan) - api.assert_called_once_with(PULL) - load.assert_not_called() - def test_preview_makes_no_changes(self): plan, api, _ = self.run_routing( CATEGORIES, @@ -595,10 +869,12 @@ def test_changed_routing_or_draft_status_stops_removals(self): ): with self.assertRaisesRegex(ValueError, "routing changed"): routing.check_review_readiness(self.roster, REPO, 42, apply=True) - self.assertTrue(all( - call.kwargs.get("method", "GET") == "GET" - for call in api.call_args_list - )) + self.assertTrue( + all( + call.kwargs.get("method", "GET") == "GET" + for call in api.call_args_list + ) + ) def test_new_commit_and_unrelated_label_do_not_interrupt_routing(self): before = pr("area:build", requested=("alice", "dave")) @@ -633,6 +909,29 @@ def test_retry_after_partial_failure_uses_updated_github_metadata(self): class AcceptedWorkIntegrationTests(unittest.TestCase): + def test_unaccepted_new_ready_pr_is_drafted_before_roster_or_reviewer_reads(self): + with ( + mock.patch.object( + routing, "_api", side_effect=[pr(), DRAFTED, [], {}] + ) as api, + mock.patch.object(routing, "check_pr_accepted_work", return_value=False), + mock.patch.object(routing, "_accepted_work", return_value=False), + mock.patch.object(routing, "_load_reviewers") as load, + self.assertRaisesRegex(ValueError, "not accepted work"), + ): + routing.check_review_readiness( + Path("missing-roster"), + REPO, + 42, + apply=True, + label_accepted_work=True, + maintainers={"maintainer"}, + ) + self.assertIn( + "convertPullRequestToDraft", api.call_args_list[1].kwargs["data"]["query"] + ) + load.assert_not_called() + def run_check(self, value, *, accepted=True, apply=True, opened=True): with ( mock.patch.object(routing, "_api", side_effect=[value, None]) as api, @@ -648,6 +947,7 @@ def run_check(self, value, *, accepted=True, apply=True, opened=True): REPO, 42, apply=apply, + event="opened" if opened else "labeled", label_accepted_work=opened, maintainers={"maintainer"}, ) @@ -677,7 +977,7 @@ def test_new_ready_pr_can_be_labeled_before_area_classification(self): load.assert_not_called() def test_nonqualifying_pr_is_not_labeled(self): - plan, api, _, _ = self.run_check(pr(), accepted=False) + plan, api, _, _ = self.run_check(pr(draft=True), accepted=False) self.assertFalse(plan.accepted_work) self.assertEqual(plan.add_labels, ()) api.assert_called_once_with(PULL) @@ -819,25 +1119,131 @@ def test_label_write_failure_does_not_request_reviewers(self): class CliTests(unittest.TestCase): + def test_opened_pr_option_loads_trusted_maintainer_roster(self): + with tempfile.TemporaryDirectory() as directory: + roster = Path(directory) / "REVIEWERS.md" + roster.write_text(DOCUMENT, encoding="utf-8") + maintainers = Path(directory) / "MAINTAINERS.md" + maintainers.write_text("[@maintainer](https://github.com/maintainer)") + with ( + mock.patch.object( + routing, + "check_review_readiness", + return_value=routing.ReviewPlan((), (), True, ("state:accepted",)), + ) as check, + contextlib.redirect_stdout(io.StringIO()) as output, + ): + result = routing.main( + [ + "--repo", + REPO, + "--pr", + "42", + "--reviewers", + str(roster), + "--maintainers", + str(maintainers), + "--label-accepted-work", + ] + ) + self.assertEqual(result, 0) + self.assertEqual( + json.loads(output.getvalue())["add_labels"], ["state:accepted"] + ) + check.assert_called_once_with( + roster, + REPO, + 42, + apply=False, + event="opened", + label_accepted_work=True, + maintainers={"maintainer"}, + ) + + def test_emits_plan_and_passes_apply_option(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "REVIEWERS.md" + path.write_text(DOCUMENT, encoding="utf-8") + output = io.StringIO() + with ( + mock.patch.object( + routing, + "check_review_readiness", + return_value=routing.ReviewPlan(("alice",), ()), + ) as check, + contextlib.redirect_stdout(output), + ): + self.assertEqual( + routing.main( + [ + "--reviewers", + str(path), + "--repo", + REPO, + "--pr", + "42", + "--apply", + ] + ), + 0, + ) + self.assertEqual( + json.loads(output.getvalue()), + { + "add": ["alice"], + "remove": [], + "accepted_work": None, + "add_labels": [], + }, + ) + check.assert_called_once_with( + path, + REPO, + 42, + apply=True, + event="opened", + label_accepted_work=False, + maintainers=None, + ) + def test_both_cli_modes_use_the_same_acceptance_policy(self): cases = [ - ("WRITE", "", (), {}, True), - ("READ", "Related to #1", (), {1: ("author", True)}, True), - ("READ", "Closes #1", (1,), {1: ("someone-else", True)}, True), - ("READ", "Closes #1 and #2", (1, 2), - {1: ("author", True), 2: ("author", False)}, True), - ("READ", "Related to #1", (), {1: ("author", False)}, False), - ("READ", "", (), {}, False), + ("WRITE", "", (), {}, (), True), + ("READ", "", (), {}, ("state:accepted",), True), + ("READ", "Related to #1", (), {1: ("author", True)}, (), True), + ("READ", "Closes #1", (1,), {1: ("someone-else", True)}, (), True), + ( + "READ", + "Closes #1 and #2", + (1, 2), + {1: ("author", True), 2: ("author", False)}, + (), + True, + ), + ("READ", "Related to #1", (), {1: ("author", False)}, (), False), + ("READ", "", (), {}, (), False), ] for mode in ("preview", "acceptance-api", "acceptance-event"): acceptance_only = mode != "preview" - for permission, body, linked, issues, expected in cases: - with self.subTest(mode=mode, body=body, issues=issues), tempfile.TemporaryDirectory() as directory: + for permission, body, linked, issues, labels, expected in cases: + with ( + self.subTest(mode=mode, body=body, issues=issues, labels=labels), + tempfile.TemporaryDirectory() as directory, + ): path = Path(directory) / "REVIEWERS.md" path.write_text(DOCUMENT, encoding="utf-8") - value = pr("area:runtime", body=body) - - def api(endpoint, **kwargs): + value = pr("area:runtime", *labels, body=body) + + def api( + endpoint, + *, + value=value, + mode=mode, + permission=permission, + linked=linked, + issues=issues, + **kwargs, + ): if endpoint == PULL: return value self.assertEqual(endpoint, "graphql") @@ -846,7 +1252,7 @@ def api(endpoint, **kwargs): if "user: author" in query: self.assertNotEqual(mode, "acceptance-event") self.assertEqual(kwargs["data"]["variables"]["number"], 42) - return {"data": {"repository": {"pullRequest": value}}} + return {"data": graphql_pr(value)} if "collaborators" in query: return AcceptanceTests.member(permission) if "closingIssuesReferences" in query: @@ -858,8 +1264,9 @@ def api(endpoint, **kwargs): output, errors = io.StringIO(), io.StringIO() with ( mock.patch.object(routing, "_api", side_effect=api), - mock.patch.object(routing, "_eligible_reviewers", return_value={"alice"}) as eligible, - mock.patch.object(routing, "_load_reviewers", return_value=reviewers()) as load, + mock.patch.object( + routing, "_load_reviewers", return_value=reviewers() + ) as load, contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors), ): @@ -873,29 +1280,82 @@ def api(endpoint, **kwargs): status = routing.main(argv) self.assertEqual(status, 0 if expected else 1) if expected: - self.assertEqual(json.loads(output.getvalue()), - {"accepted": True} if acceptance_only else {"add": ["alice"], "remove": []}) + self.assertEqual( + json.loads(output.getvalue()), + {"accepted": True} + if acceptance_only + else { + "add": ["alice"], + "remove": [], + "accepted_work": None, + "add_labels": [], + }, + ) else: self.assertIn(routing.ACCEPTANCE_ERROR, errors.getvalue()) if acceptance_only: - eligible.assert_not_called() load.assert_not_called() + def test_acceptance_only_reads_later_pr_label_pages(self): + output = io.StringIO() + with ( + mock.patch.object( + routing, + "_graphql", + side_effect=[ + graphql_pr(pr("topic:policy"), cursor="next-page"), + graphql_pr(pr("state:accepted")), + ], + ) as query, + mock.patch.object( + routing, "_api", side_effect=AssertionError("No further lookups") + ), + mock.patch.object(routing, "check_review_readiness") as route, + contextlib.redirect_stdout(output), + ): + self.assertEqual( + routing.main( + [ + "--repo", + REPO, + "--pr", + "42", + "--acceptance-only", + ] + ), + 0, + ) + self.assertEqual(json.loads(output.getvalue()), {"accepted": True}) + self.assertEqual(query.call_args.kwargs["cursor"], "next-page") + route.assert_not_called() + def test_acceptance_only_checks_drafts_and_closed_prs_without_routing(self): for value in (pr(), pr(draft=True), pr(state="closed")): for accepted in (False, True): with self.subTest(value=value, accepted=accepted): output, errors = io.StringIO(), io.StringIO() with ( - mock.patch.object(routing, "_graphql", return_value={"repository": {"pullRequest": value}}) as query, - mock.patch.object(routing, "_accepted_work", return_value=accepted) as check, + mock.patch.object( + routing, "_graphql", return_value=graphql_pr(value) + ) as query, + mock.patch.object( + routing, "_accepted_work", return_value=accepted + ) as check, mock.patch.object(routing, "check_review_readiness") as route, mock.patch.object(routing, "parse_reviewers") as parse, - mock.patch.object(routing.Path, "read_text", side_effect=AssertionError("Acceptance-only must not read the roster")), + mock.patch.object( + routing.Path, + "read_text", + side_effect=AssertionError( + "Acceptance-only must not read the roster" + ), + ), contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors), ): - status = routing.main(["--repo", REPO, "--pr", "42", "--acceptance-only"]) + status = routing.main( + ["--repo", REPO, "--pr", "42", "--acceptance-only"] + ) self.assertEqual(status, 0 if accepted else 1) check.assert_called_once_with(REPO, 42, value) query.assert_called_once() @@ -908,21 +1368,32 @@ def test_acceptance_only_lookup_errors_fail_without_routing(self): error = subprocess.CalledProcessError(1, "gh") with ( self.subTest(metadata_error=metadata_error), - mock.patch.object(routing, "_graphql", side_effect=error if metadata_error else None, - return_value={"repository": {"pullRequest": pr()}}), - mock.patch.object(routing, "_accepted_work", side_effect=error) as check, + mock.patch.object( + routing, + "_graphql", + side_effect=error if metadata_error else None, + return_value=graphql_pr(pr()), + ), + mock.patch.object( + routing, "_accepted_work", side_effect=error + ) as check, mock.patch.object(routing, "check_review_readiness") as route, contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()), ): - self.assertEqual(routing.main(["--repo", REPO, "--pr", "42", "--acceptance-only"]), 1) + self.assertEqual( + routing.main(["--repo", REPO, "--pr", "42", "--acceptance-only"]), 1 + ) route.assert_not_called() if metadata_error: check.assert_not_called() def test_invalid_event_payload_fails_without_api_calls_or_routing(self): for contents in (None, "not JSON"): - with self.subTest(contents=contents), tempfile.TemporaryDirectory() as directory: + with ( + self.subTest(contents=contents), + tempfile.TemporaryDirectory() as directory, + ): path = Path(directory) / "event.json" if contents is not None: path.write_text(contents) @@ -931,10 +1402,20 @@ def test_invalid_event_payload_fails_without_api_calls_or_routing(self): mock.patch.object(routing, "check_review_readiness") as route, contextlib.redirect_stderr(io.StringIO()), ): - self.assertEqual(routing.main([ - "--repo", REPO, "--pr", "42", "--acceptance-only", - "--event-path", str(path), - ]), 1) + self.assertEqual( + routing.main( + [ + "--repo", + REPO, + "--pr", + "42", + "--acceptance-only", + "--event-path", + str(path), + ] + ), + 1, + ) api.assert_not_called() route.assert_not_called() @@ -948,111 +1429,57 @@ def test_acceptance_only_cannot_be_combined_with_apply(self): self.assertEqual(error.exception.code, 2) api.assert_not_called() - def test_acceptance_only_refreshes_metadata_for_each_pr_and_run(self): - for number in (7, 8, 7): - value = pr(body=f"Related to #{number}") - with ( - self.subTest(number=number), - mock.patch.object(routing, "_graphql", return_value={"repository": {"pullRequest": value}}) as query, - mock.patch.object(routing, "_accepted_work", return_value=True) as check, - contextlib.redirect_stdout(io.StringIO()), - ): - self.assertEqual(routing.main([ - "--repo", REPO, "--pr", str(number), "--acceptance-only", - ]), 0) - self.assertEqual(query.call_args.kwargs["number"], number) - check.assert_called_once_with(REPO, number, value) - def test_rejected_pr_does_not_need_a_readable_valid_roster(self): for contents in (None, "not a registry"): for apply in (False, True): - with self.subTest(contents=contents, apply=apply), tempfile.TemporaryDirectory() as directory: + with ( + self.subTest(contents=contents, apply=apply), + tempfile.TemporaryDirectory() as directory, + ): path = Path(directory) / "REVIEWERS.md" if contents is not None: path.write_text(contents, encoding="utf-8") output, errors = io.StringIO(), io.StringIO() with ( - mock.patch.object(routing, "_api", side_effect=[pr(), DRAFTED]) as api, - mock.patch.object(routing, "_accepted_work", return_value=False), + mock.patch.object( + routing, "_api", side_effect=[pr(), DRAFTED, [], {}] + ) as api, + mock.patch.object( + routing, "_accepted_work", return_value=False + ), mock.patch.object(routing, "parse_reviewers") as parse, mock.patch.object(routing, "_load_reviewers") as load, contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors), ): argv = ["--reviewers", str(path), "--repo", REPO, "--pr", "42"] - self.assertEqual(routing.main(argv + (["--apply"] if apply else [])), 1) + self.assertEqual( + routing.main(argv + (["--apply"] if apply else [])), 1 + ) self.assertIn("not accepted work", errors.getvalue()) self.assertEqual(output.getvalue(), "") parse.assert_not_called() load.assert_not_called() - self.assertEqual(api.call_count, 2 if apply else 1) + self.assertEqual(api.call_count, 4 if apply else 1) if apply: - self.assertIn("convertPullRequestToDraft", api.call_args.kwargs["data"]["query"]) + self.assertIn( + "convertPullRequestToDraft", + api.call_args_list[1].kwargs["data"]["query"], + ) def test_accepted_pr_reads_real_roster_and_previews_reviewers(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "REVIEWERS.md" path.write_text(DOCUMENT, encoding="utf-8") output = io.StringIO() - with ( - mock.patch.object(routing, "_api", return_value=pr("area:runtime")) as api, - mock.patch.object(routing, "_accepted_work", return_value=True) as accepted, - mock.patch.object(routing, "_eligible_reviewers", return_value={"alice"}), - mock.patch.object(routing, "_load_reviewers", return_value=reviewers()), - contextlib.redirect_stdout(output), - ): - self.assertEqual(routing.main([ - "--reviewers", str(path), "--repo", REPO, "--pr", "42", - ]), 0) - accepted.assert_called_once() - api.assert_called_once_with(PULL) - self.assertEqual(json.loads(output.getvalue()), {"add": ["alice"], "remove": []}) - - def test_closed_and_draft_prs_skip_missing_roster_and_acceptance(self): - for value in (pr(draft=True), pr(state="closed")): - with self.subTest(value=value), tempfile.TemporaryDirectory() as directory: - path = Path(directory) / "missing.md" - output = io.StringIO() - with ( - mock.patch.object(routing, "_api", return_value=value) as api, - mock.patch.object(routing, "_accepted_work") as accepted, - contextlib.redirect_stdout(output), - ): - self.assertEqual(routing.main([ - "--reviewers", str(path), "--repo", REPO, "--pr", "42", - ]), 0) - api.assert_called_once_with(PULL) - accepted.assert_not_called() - self.assertEqual(json.loads(output.getvalue()), {"skipped": "closed or draft PR"}) - - def test_passes_label_event_to_reconciliation(self): - with tempfile.TemporaryDirectory() as directory: - path = Path(directory) / "REVIEWERS.md" - path.write_text(DOCUMENT, encoding="utf-8") with ( mock.patch.object( - routing, "check_review_readiness", - return_value=routing.ReviewPlan((), ()), - ) as check, - contextlib.redirect_stdout(io.StringIO()), - ): - self.assertEqual(routing.main([ - "--reviewers", str(path), "--repo", REPO, "--pr", "42", - "--event", "labeled", "--apply", - ]), 0) - self.assertEqual(check.call_args.kwargs, {"apply": True, "event": "labeled"}) - - def test_emits_plan_and_passes_apply_option(self): - with tempfile.TemporaryDirectory() as directory: - path = Path(directory) / "REVIEWERS.md" - path.write_text(DOCUMENT, encoding="utf-8") - output = io.StringIO() - with ( + routing, "_api", return_value=pr("area:runtime") + ) as api, mock.patch.object( - routing, - "check_review_readiness", - return_value=routing.ReviewPlan(("alice",), ()), - ) as check, + routing, "_accepted_work", return_value=True + ) as accepted, + mock.patch.object(routing, "_load_reviewers", return_value=reviewers()), contextlib.redirect_stdout(output), ): self.assertEqual( @@ -1064,11 +1491,12 @@ def test_emits_plan_and_passes_apply_option(self): REPO, "--pr", "42", - "--apply", ] ), 0, ) + accepted.assert_called_once() + api.assert_called_once_with(PULL) self.assertEqual( json.loads(output.getvalue()), { @@ -1078,9 +1506,35 @@ def test_emits_plan_and_passes_apply_option(self): "add_labels": [], }, ) - check.assert_called_once_with( - path, REPO, 42, apply=True, event="opened" - ) + + def test_closed_and_draft_prs_skip_missing_roster_and_acceptance(self): + for value in (pr(draft=True), pr(state="closed")): + with self.subTest(value=value), tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "missing.md" + output = io.StringIO() + with ( + mock.patch.object(routing, "_api", return_value=value) as api, + mock.patch.object(routing, "_accepted_work") as accepted, + contextlib.redirect_stdout(output), + ): + self.assertEqual( + routing.main( + [ + "--reviewers", + str(path), + "--repo", + REPO, + "--pr", + "42", + ] + ), + 0, + ) + api.assert_called_once_with(PULL) + accepted.assert_not_called() + self.assertEqual( + json.loads(output.getvalue()), {"skipped": "closed or draft PR"} + ) def test_accepted_pr_with_invalid_registry_fails_before_reviewer_changes(self): with tempfile.TemporaryDirectory() as directory: @@ -1088,7 +1542,9 @@ def test_accepted_pr_with_invalid_registry_fails_before_reviewer_changes(self): path.write_text("not a registry", encoding="utf-8") output, errors = io.StringIO(), io.StringIO() with ( - mock.patch.object(routing, "_api", return_value=pr("area:runtime")) as api, + mock.patch.object( + routing, "_api", return_value=pr("area:runtime") + ) as api, mock.patch.object(routing, "_accepted_work", return_value=True), contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors), @@ -1111,27 +1567,5 @@ def test_accepted_pr_with_invalid_registry_fails_before_reviewer_changes(self): self.assertIn("no area/topic", errors.getvalue()) -class WorkflowTests(unittest.TestCase): - def test_legacy_workflow_keeps_its_status_events_and_read_only_permissions(self): - root = Path(__file__).resolve().parents[2] - workflow = (root / ".github/workflows/pr-issue-acceptance.yml").read_text() - self.assertIn("name: PR Issue Acceptance\n", workflow) - self.assertIn(" name: OpenShell / PR Issue Acceptance\n", workflow) - self.assertIn("types: [opened, edited, reopened, synchronize]", workflow) - permissions = workflow.split("permissions:\n", 1)[1].split("\n\n", 1)[0] - self.assertEqual(dict(line.strip().split(": ") for line in permissions.splitlines()), - {"contents": "read", "issues": "read", "pull-requests": "read"}) - self.assertIn("ref: ${{ github.sha }}", workflow) - self.assertIn("persist-credentials: false", workflow) - self.assertIn("sparse-checkout: tasks/scripts/check_review_readiness.py", workflow) - self.assertIn("GH_REPO: ${{ github.repository }}", workflow) - self.assertIn("PR_NUMBER: ${{ github.event.pull_request.number }}", workflow) - command = workflow.split(" run: |\n", 1)[1] - self.assertIn("python3 tasks/scripts/check_review_readiness.py", command) - self.assertIn('--repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only', command) - self.assertIn('--event-path "$GITHUB_EVENT_PATH"', command) - self.assertNotIn("--apply", command) - - if __name__ == "__main__": unittest.main()