From d85ec95ed5d8ba88b1a4002e17c25238310b6cf8 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 | 125 ++++++++++++-- tasks/scripts/check_review_readiness_test.py | 168 ++++++++++++++++++- 2 files changed, 281 insertions(+), 12 deletions(-) diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index f8a0af76af..f972aca570 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. A failed reviewer request or removal is noted on the PR and does not fail the run. The parser and planning helpers are importable. No state is stored between runs. @@ -319,6 +319,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": @@ -443,6 +526,26 @@ def check_review_readiness( add_labels = (ACCEPTED_LABEL,) if apply: _add_accepted_label(repository, pr, evidence) + if not pr["draft"] and 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 needs_maintainer_edits(pr, repository): raise MaintainerEditsError( "Allow edits by maintainers is off, so this PR is not ready for review. " @@ -487,7 +590,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 5fdc01c5cd..b4ef1f7a63 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -29,12 +29,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], } @@ -311,7 +313,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 40238bf612298fc4df987a42a22bb759d5f3c5a1 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 | 161 +++++----- tasks/scripts/check_review_readiness_test.py | 306 +++++++++---------- 3 files changed, 238 insertions(+), 240 deletions(-) diff --git a/.github/workflows/review-readiness.yml b/.github/workflows/review-readiness.yml index 45004da81e..736c1a0de5 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 f972aca570..e04df83ec1 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -329,8 +329,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" @@ -340,71 +369,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): @@ -506,8 +529,7 @@ 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}" @@ -526,7 +548,7 @@ def check_review_readiness( add_labels = (ACCEPTED_LABEL,) if apply: _add_accepted_label(repository, pr, evidence) - if not pr["draft"] and not _accepted_work(repository, number, pr): + if not pr["draft"] and event in {"opened", "ready_for_review"} and not _accepted_work(repository, number, pr): if apply: _api( "graphql", @@ -589,6 +611,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" ) @@ -597,18 +624,8 @@ 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, diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index b4ef1f7a63..356492aaa9 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -315,138 +315,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 ( @@ -462,10 +466,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), ): @@ -478,15 +479,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( @@ -533,6 +534,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): @@ -1142,6 +1156,23 @@ def setUp(self): self.addCleanup(patcher.stop) os.environ.pop("GITHUB_ACTIONS", None) + 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" @@ -1180,52 +1211,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 273513ba19c032747ef0f52c3d4d0376fd3f9ede 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 e04df83ec1..7055332741 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -304,6 +304,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}" @@ -328,15 +334,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 } } @@ -344,9 +390,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), @@ -360,46 +406,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"]): @@ -524,7 +536,7 @@ def _apply_review_requests( def check_review_readiness( - categories: dict[str, set[str]], + reviewers: Path, repository: str, number: int, *, @@ -550,19 +562,13 @@ def check_review_readiness( _add_accepted_label(repository, pr, evidence) if not pr["draft"] and event in {"opened", "ready_for_review"} and 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"]}, - }, + _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 " @@ -579,6 +585,7 @@ def check_review_readiness( if pr["draft"] and not label_accepted_work: return None return ReviewPlan((), (), accepted, add_labels) + categories = parse_reviewers(reviewers.read_text(encoding="utf-8")) selected = select_reviewers(categories, labels) selected -= {pr["user"]["login"].lower()} plan = replace( @@ -623,9 +630,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, diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index 356492aaa9..1ba09ac045 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -27,6 +27,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=""): @@ -276,11 +278,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: @@ -354,6 +358,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", @@ -364,19 +382,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): @@ -419,7 +441,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": @@ -430,22 +452,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: @@ -460,7 +491,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() @@ -470,7 +501,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): @@ -478,15 +509,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): @@ -641,13 +678,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")) @@ -1156,6 +1191,68 @@ def setUp(self): self.addCleanup(patcher.stop) os.environ.pop("GITHUB_ACTIONS", None) + 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" @@ -1211,16 +1308,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), ): @@ -1237,7 +1335,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 3d491743bf727af9d434e88687061444295d64fe 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 | 2 +- .agents/skills/review-github-pr/SKILL.md | 15 +- .github/CODEOWNERS | 3 - .github/workflows/pr-issue-acceptance.yml | 16 +- AGENTS.md | 2 +- CONTRIBUTING.md | 4 +- tasks/scripts/check_issue_acceptance.cjs | 82 ---------- tasks/scripts/check_issue_acceptance.test.cjs | 150 ------------------ tasks/scripts/check_review_readiness.py | 47 ++++-- tasks/scripts/check_review_readiness_test.py | 141 +++++++++++++++- 10 files changed, 195 insertions(+), 267 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 22c6af5277..d1797c8793 100644 --- a/.agents/skills/create-github-pr/SKILL.md +++ b/.agents/skills/create-github-pr/SKILL.md @@ -24,7 +24,7 @@ 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 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 afb45c7cb7..65feb209c4 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -33,9 +33,6 @@ /.agents/agents/labeler/ @ai-openshell/maintainers /.agents/skills/label-issue @ai-openshell/maintainers -# PR issue acceptance checks -/tasks/scripts/check_issue_acceptance.cjs @ai-openshell/maintainers -/tasks/scripts/check_issue_acceptance.test.cjs @ai-openshell/maintainers # Auditable SBOM check /tasks/scripts/verify-image-sbom.sh @ai-openshell/maintainers diff --git a/.github/workflows/pr-issue-acceptance.yml b/.github/workflows/pr-issue-acceptance.yml index 16c027250a..d4a9bca074 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 473cb48f32..2a9a3b355b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -195,7 +195,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. Create PRs with `gh pr create`, which enables "Allow edits by maintainers" by default; never pass `--no-maintainer-edit`. Review Readiness fails a ready PR from a personal fork when that setting is off. Fix a PR that lacks it with `gh api -X PATCH repos/OWNER/REPO/pulls/NUMBER -F maintainer_can_modify=true`. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c33842dd79..36cd8b7dd7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -509,7 +509,9 @@ Every PR must close an existing issue, except automated dependency updates and t 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, and other labels 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 3cf3950557..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 || 'ai-openshell'}/openshell/issues/${issue.number}`, - repository: { name: 'openshell', owner: { login: issue.owner || 'ai-openshell' } }, - })), - 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: 'ai-openshell', 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 ai-openshell\/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 7055332741..8f647be774 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. @@ -39,6 +40,11 @@ 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" +) + ASSIGNMENT_MARKER = "" ADD_LABEL = """mutation($input: AddLabelsToLabelableInput!) { addLabelsToLabelable(input: $input) { labelable { __typename } } @@ -570,10 +576,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) if needs_maintainer_edits(pr, repository): raise MaintainerEditsError( "Allow edits by maintainers is off, so this PR is not ready for review. " @@ -623,16 +626,36 @@ 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, @@ -643,13 +666,9 @@ def main(argv: list[str] | None = None) -> int: ) as exc: report_failure(exc) return 1 - if plan is not None and plan.assignment_error: + if not args.acceptance_only and plan is not None and plan.assignment_error: report_assignment_failure(plan.assignment_error) - 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 1ba09ac045..bfdb9526f3 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 @@ -1191,6 +1191,124 @@ def setUp(self): self.addCleanup(patcher.stop) os.environ.pop("GITHUB_ACTIONS", 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)}, 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): @@ -1340,5 +1458,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 25fd7342ca4d62e89d0ffbcf44cd150d9fb00716 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 | 2 +- .agents/skills/review-github-pr/SKILL.md | 15 ++++--- .github/workflows/pr-issue-acceptance.yml | 3 +- AGENTS.md | 2 +- tasks/scripts/check_review_readiness.py | 43 +++++++++----------- tasks/scripts/check_review_readiness_test.py | 40 +++++++++++++++--- 6 files changed, 65 insertions(+), 40 deletions(-) diff --git a/.agents/skills/create-github-pr/SKILL.md b/.agents/skills/create-github-pr/SKILL.md index d1797c8793..22c6af5277 100644 --- a/.agents/skills/create-github-pr/SKILL.md +++ b/.agents/skills/create-github-pr/SKILL.md @@ -24,7 +24,7 @@ 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 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 d4a9bca074..9262019002 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 2a9a3b355b..473cb48f32 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -195,7 +195,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. Create PRs with `gh pr create`, which enables "Allow edits by maintainers" by default; never pass `--no-maintainer-edit`. Review Readiness fails a ready PR from a personal fork when that setting is off. Fix a PR that lacks it with `gh api -X PATCH repos/OWNER/REPO/pulls/NUMBER -F maintainer_can_modify=true`. diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index 8f647be774..b336b91e33 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -7,16 +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. -A failed reviewer request or removal is noted on the PR and does not fail the run. -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 @@ -42,7 +33,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" ) ASSIGNMENT_MARKER = "" @@ -382,7 +373,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 @@ -421,7 +412,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 } @@ -438,7 +428,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 @@ -621,6 +611,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"), @@ -639,15 +633,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 bfdb9526f3..00bede3bfe 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -372,13 +372,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), @@ -1195,15 +1196,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) @@ -1215,6 +1217,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: @@ -1234,7 +1237,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()), @@ -1284,6 +1293,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, @@ -1476,6 +1503,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 c3278300159947ee7724ca0b8b3cb0b2024330e2 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 | 8 +- .github/workflows/review-readiness.yml | 11 +- CI.md | 2 +- CONTRIBUTING.md | 6 +- tasks/scripts/check_review_readiness.py | 145 ++- tasks/scripts/check_review_readiness_test.py | 1036 +++++++++++++----- 7 files changed, 899 insertions(+), 312 deletions(-) diff --git a/.agents/skills/create-github-pr/SKILL.md b/.agents/skills/create-github-pr/SKILL.md index 22c6af5277..06b139ba08 100644 --- a/.agents/skills/create-github-pr/SKILL.md +++ b/.agents/skills/create-github-pr/SKILL.md @@ -270,7 +270,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 9262019002..a97c62f4a4 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 + .agents/agents/labeler/bin/acceptance.py + tasks/scripts/check_maintainer_approval.py sparse-checkout-cone-mode: false - name: Check PR acceptance @@ -39,5 +42,4 @@ jobs: PR_NUMBER: ${{ github.event.pull_request.number }} run: | python3 tasks/scripts/check_review_readiness.py \ - --repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only \ - --event-path "$GITHUB_EVENT_PATH" + --repo "$GH_REPO" --pr "$PR_NUMBER" --acceptance-only diff --git a/.github/workflows/review-readiness.yml b/.github/workflows/review-readiness.yml index 736c1a0de5..c2b54a142b 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 e5bf40de38..3c23e615a5 100644 --- a/CI.md +++ b/CI.md @@ -758,7 +758,7 @@ Maintainers should add ready PRs to the queue rather than pressing a direct merg | `.github/actions/setup-rust/action.yml` | Restores the Rust target and registry cache. Other branches can restore only their own caches and `main`'s, so per-binary release build caches are saved only on `main`. | | `.github/workflows/rust-cache.yml` | Builds the Branch Checks test targets on each push to `main` to save the `rust-checks` cache that PRs and merge groups restore. Skips the build when the cache already matches. | | `.github/workflows/required-ci-gates.yml` | Posts required PR-head and merge-group statuses for gated CI workflows, including for fork PRs. This is what branch protection and merge queue should require. | -| `.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 SDK and retains SARIF artifacts. | diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 36cd8b7dd7..7b328b94fc 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -139,7 +139,7 @@ Use GitHub closing references such as `Closes #123` or `Fixes #123`. The definition of accepted work is encoded in `.agents/agents/labeler/bin/acceptance.py` and should be directly imported where needed. -Acceptance classifies work, not implementation quality or approval to merge. Existing issue-linking rules, the separate `PR Issue Acceptance` check (which requires every linked issue to be accepted for community authors), and maintainer/code-owner approvals still apply. Acceptance does not certify that the diff implements the issue, and the existing Dependabot gate exemption does not automatically qualify a PR as accepted work. +Acceptance classifies work, not implementation quality or approval to merge. Existing issue-linking rules, the [automated acceptance checks](#pull-requests), and maintainer/code-owner approvals still apply. Acceptance does not certify that the diff implements the issue, and the existing Dependabot gate exemption does not automatically qualify a PR as accepted work. ### Labeling and Notifications @@ -511,9 +511,11 @@ Authors without write access to the target repository must link at least one iss 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 automated gate recognizes repository write access, `state:accepted` on the PR or any referenced issue, and the Dependabot exemption. It does not replace the contributor issue-linking requirements above or the [accepted-work definition](#accepted-work) used for automatic labeling. + 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. +Automated dependency update PRs, including Dependabot PRs, are exempt from the separate issue and closing-reference requirement. The automated acceptance gate also exempts Dependabot PRs. Human-authored dependency updates follow the normal issue requirement. These exceptions do not change applicable verification, review, or private vulnerability disclosure requirements. Authors with `WRITE`, `MAINTAIN`, or `ADMIN` access may open a PR that only fixes a broken build or flaky tests without a separate issue. Describe the failure in the PR instead. Authors without write access follow the normal issue and acceptance requirements. diff --git a/tasks/scripts/check_review_readiness.py b/tasks/scripts/check_review_readiness.py index b336b91e33..5be16cd65f 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -33,7 +33,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 ) ASSIGNMENT_MARKER = "" @@ -322,15 +329,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) @@ -357,7 +355,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"]: @@ -373,7 +374,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 @@ -387,12 +392,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: @@ -420,7 +431,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", []) @@ -430,7 +444,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 @@ -532,14 +548,21 @@ def _apply_review_requests( 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": @@ -556,7 +579,12 @@ def check_review_readiness( add_labels = (ACCEPTED_LABEL,) if apply: _add_accepted_label(repository, pr, evidence) - if not pr["draft"] and event in {"opened", "ready_for_review"} and not _accepted_work(repository, number, pr): + 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( """mutation($id: ID!) { @@ -566,6 +594,13 @@ 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) if needs_maintainer_edits(pr, repository): raise MaintainerEditsError( @@ -578,7 +613,11 @@ def check_review_readiness( if pr["draft"] and not label_accepted_work: return None return ReviewPlan((), (), accepted, add_labels) - categories = parse_reviewers(reviewers.read_text(encoding="utf-8")) + 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( @@ -612,17 +651,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( @@ -634,25 +676,58 @@ 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, diff --git a/tasks/scripts/check_review_readiness_test.py b/tasks/scripts/check_review_readiness_test.py index 00bede3bfe..8a1f269846 100644 --- a/tasks/scripts/check_review_readiness_test.py +++ b/tasks/scripts/check_review_readiness_test.py @@ -49,6 +49,23 @@ def fork(owner, *, full_name=None, owner_type="User"): "repo": { "full_name": full_name or f"{owner}/repo", "owner": {"login": owner, "type": owner_type}, + }, + } + + +def graphql_pr(value, *, cursor=None): + return { + "repository": { + "pullRequest": { + **value, + "labels": { + "nodes": value["labels"], + "pageInfo": { + "hasNextPage": cursor is not None, + "endCursor": cursor, + }, + }, + } } } @@ -278,13 +295,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: @@ -320,175 +335,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), ): @@ -498,7 +772,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), ): @@ -516,7 +794,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, @@ -524,8 +804,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( @@ -578,9 +860,11 @@ 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() @@ -680,10 +964,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")) @@ -735,6 +1021,7 @@ class AssignmentFailureTests(unittest.TestCase): def run_routing(self, responses): with ( mock.patch.dict(os.environ, {}, clear=True), + mock.patch.object(routing, "_accepted_work", return_value=True), mock.patch.object(routing, "_api", side_effect=responses) as api, mock.patch.object(routing, "_load_reviewers", return_value=reviewers()), ): @@ -809,6 +1096,29 @@ def test_cli_passes_with_a_warning_annotation(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, "evaluate_pr_accepted_work", return_value=None), + 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 test_accepted_label_event_carries_structured_rationale(self): with ( mock.patch.object( @@ -887,6 +1197,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"}, ) @@ -916,7 +1227,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) @@ -1192,25 +1503,228 @@ def setUp(self): self.addCleanup(patcher.stop) os.environ.pop("GITHUB_ACTIONS", None) + 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=graphql_pr(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_acceptance_only_rerun_observes_new_acceptance_label(self): + with ( + mock.patch.object( + routing, + "_graphql", + side_effect=[ + graphql_pr(pr()), + graphql_pr(pr("state:accepted")), + ], + ) as query, + mock.patch.object( + routing, + "_accepted_work", + side_effect=lambda _repo, _number, value: any( + label["name"] == "state:accepted" for label in value["labels"] + ), + ), + mock.patch.object(routing, "_api", side_effect=AssertionError("No writes")), + contextlib.redirect_stdout(io.StringIO()), + contextlib.redirect_stderr(io.StringIO()), + ): + args = ["--repo", REPO, "--pr", "42", "--acceptance-only"] + self.assertEqual(routing.main(args), 1) + self.assertEqual(routing.main(args), 0) + self.assertEqual(query.call_count, 2) + + 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") + 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, "_load_reviewers", return_value=reviewers()), + contextlib.redirect_stdout(io.StringIO()) as 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": [], + "accepted_work": None, + "add_labels": [], + "assignment_error": "", + }, + ) + + 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": [], + "assignment_error": "", + }, + ) + 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") @@ -1219,7 +1733,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: @@ -1231,8 +1745,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), ): @@ -1246,29 +1761,83 @@ 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": [], + "assignment_error": "", + }, + ) 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() @@ -1281,21 +1850,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) @@ -1304,10 +1884,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() @@ -1321,65 +1911,43 @@ 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"]) - - 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": []}) + self.assertIn( + "convertPullRequestToDraft", + api.call_args_list[1].kwargs["data"]["query"], + ) def test_closed_and_draft_prs_skip_missing_roster_and_acceptance(self): for value in (pr(draft=True), pr(state="closed")): @@ -1391,70 +1959,24 @@ def test_closed_and_draft_prs_skip_missing_roster_and_acceptance(self): mock.patch.object(routing, "_accepted_work") as accepted, contextlib.redirect_stdout(output), ): - self.assertEqual(routing.main([ - "--reviewers", str(path), "--repo", REPO, "--pr", "42", - ]), 0) + 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 ( - 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, + json.loads(output.getvalue()), {"skipped": "closed or draft PR"} ) - self.assertEqual( - json.loads(output.getvalue()), - { - "add": ["alice"], - "remove": [], - "accepted_work": None, - "add_labels": [], - "assignment_error": "", - }, - ) - check.assert_called_once_with( - path, REPO, 42, apply=True, event="opened" - ) def test_accepted_pr_with_invalid_registry_fails_before_reviewer_changes(self): with tempfile.TemporaryDirectory() as directory: @@ -1462,7 +1984,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), @@ -1485,27 +2009,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()