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/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..a97c62f4a4 100644 --- a/.github/workflows/pr-issue-acceptance.yml +++ b/.github/workflows/pr-issue-acceptance.yml @@ -29,12 +29,17 @@ jobs: with: ref: ${{ github.sha }} persist-credentials: false - sparse-checkout: tasks/scripts/check_issue_acceptance.cjs + 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 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/.github/workflows/review-readiness.yml b/.github/workflows/review-readiness.yml index 45004da81e..c2b54a142b 100644 --- a/.github/workflows/review-readiness.yml +++ b/.github/workflows/review-readiness.yml @@ -51,7 +51,7 @@ jobs: shell: bash run: | set -euo pipefail - args=(--repo "$GH_REPO" --pr "$PR_NUMBER" --apply) + args=(--repo "$GH_REPO" --pr "$PR_NUMBER" --event "$PR_ACTION" --apply) if [ "$PR_ACTION" = opened ]; then args+=(--label-accepted-work) fi 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 c33842dd79..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 @@ -509,9 +509,13 @@ 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. -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. +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. 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_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 f8a0af76af..5be16cd65f 100644 --- a/tasks/scripts/check_review_readiness.py +++ b/tasks/scripts/check_review_readiness.py @@ -7,15 +7,7 @@ # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -"""Reconcile PR reviewers from the merged REVIEWERS.md and current PR metadata. - -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 -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 @@ -39,6 +31,18 @@ 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 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 = "" ADD_LABEL = """mutation($input: AddLabelsToLabelableInput!) { addLabelsToLabelable(input: $input) { labelable { __typename } } @@ -304,6 +308,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}" @@ -319,6 +329,131 @@ def _load_reviewers(repository: str, number: int, pr: dict) -> dict[str, set[str } +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 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 + while True: + 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 } } + pageInfo { hasNextPage endCursor } + } + } + }""", + 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 + ), + None, + ) + if member: + if member["permission"] in {"WRITE", "MAINTAIN", "ADMIN"}: + return True + break + if not members["pageInfo"]["hasNextPage"]: + break + cursor = members["pageInfo"]["endCursor"] + + for issue_number in _referenced_issues(repository, number, pr.get("body") or ""): + cursor = None + while True: + try: + data = _graphql( + """query($owner: String!, $repo: String!, $number: Int!, $cursor: String) { + repository(owner: $owner, name: $repo) { + issueOrPullRequest(number: $number) { + ... on Issue { + labels(first: 100, after: $cursor) { + nodes { name } + pageInfo { hasNextPage endCursor } + } + } + } + } + }""", + 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 = data["repository"]["issueOrPullRequest"] + if not issue or "labels" not in issue: + 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): return ( pr["state"], @@ -413,11 +548,12 @@ def _apply_review_requests( def check_review_readiness( - categories: dict[str, set[str]], + 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: @@ -443,6 +579,29 @@ 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 accepted is not True + and not _accepted_work(repository, number, pr) + ): + if apply: + _graphql( + """mutation($id: ID!) { + convertPullRequestToDraft(input: {pullRequestId: $id}) { + pullRequest { isDraft } + } + }""", + 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( "Allow edits by maintainers is off, so this PR is not ready for review. " @@ -454,6 +613,11 @@ def check_review_readiness( if pr["draft"] and not label_accepted_work: return None return ReviewPlan((), (), accepted, add_labels) + categories = ( + reviewers + if isinstance(reviewers, dict) + else parse_reviewers(reviewers.read_text(encoding="utf-8")) + ) selected = select_reviewers(categories, labels) selected -= {pr["user"]["login"].lower()} plan = replace( @@ -487,26 +651,83 @@ 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" + "--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"), + 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", + 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: - 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, - ) + if args.acceptance_only: + if args.event_path: + pr = json.loads(args.event_path.read_text(encoding="utf-8"))[ + "pull_request" + ] + else: + owner, repo = args.repo.split("/") + 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, + label_accepted_work=args.label_accepted_work, + maintainers=maintainers, + ) + result = ( + asdict(plan) if plan is not None else {"skipped": "closed or draft PR"} + ) except ( OSError, UnicodeError, @@ -517,13 +738,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 5fdc01c5cd..8a1f269846 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 @@ -27,14 +27,18 @@ } 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"): +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], } @@ -45,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, + }, + }, + } } } @@ -311,15 +332,481 @@ 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 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, + }, + }, + } + } + } + } + + 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, + ): + 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(), + ], + ): + 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_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, 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, + ): + 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: + 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: + 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_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_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", + 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}]}), + ) + with ( + self.subTest(error_type=error_type), + 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")) + ) + 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(), + ], + ): + 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_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, "_load_reviewers") as load, + self.assertRaises(subprocess.CalledProcessError), + ): + routing.check_review_readiness(ROSTER, 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(ROSTER, REPO, 42, apply=True) + + class ReconciliationTests(unittest.TestCase): - def run_routing(self, categories, *, snapshots, responses, apply=True): + 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) + 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): @@ -367,6 +854,21 @@ 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): @@ -461,7 +963,7 @@ 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) + routing.check_review_readiness(self.roster, REPO, 42, apply=True) self.assertTrue( all( call.kwargs.get("method", "GET") == "GET" @@ -519,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()), ): @@ -593,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( @@ -671,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"}, ) @@ -700,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) @@ -976,18 +1503,75 @@ def setUp(self): self.addCleanup(patcher.stop) os.environ.pop("GITHUB_ACTIONS", None) - def test_emits_plan_and_passes_apply_option(self): + 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") - output = io.StringIO() with ( mock.patch.object( - routing, - "check_review_readiness", - return_value=routing.ReviewPlan(("alice",), ()), - ) as check, - contextlib.redirect_stdout(output), + 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( @@ -998,11 +1582,12 @@ def test_emits_plan_and_passes_apply_option(self): REPO, "--pr", "42", - "--apply", ] ), 0, ) + accepted.assert_called_once() + api.assert_called_once_with(PULL) self.assertEqual( json.loads(output.getvalue()), { @@ -1013,14 +1598,6 @@ def test_emits_plan_and_passes_apply_option(self): "assignment_error": "", }, ) - 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: @@ -1054,21 +1631,363 @@ def test_opened_pr_option_loads_trusted_maintainer_roster(self): json.loads(output.getvalue())["add_labels"], ["state:accepted"] ) check.assert_called_once_with( - {"area:runtime": {"alice"}}, + roster, REPO, 42, apply=False, + event="opened", label_accepted_work=True, maintainers={"maintainer"}, ) - def test_invalid_registry_fails_before_contacting_github(self): + 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", "", (), {}, ("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, 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", *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") + 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": graphql_pr(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, "_load_reviewers", return_value=reviewers() + ) as load, + contextlib.redirect_stdout(output), + contextlib.redirect_stderr(errors), + ): + argv = ["--repo", REPO, "--pr", "42", "--reviewers", str(path)] + 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()), + {"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: + 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=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" + ), + ), + 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=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 + ) + 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, + ): + 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, + 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_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, 4 if apply else 1) + if apply: + 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")): + with self.subTest(value=value), tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "missing.md" + output = io.StringIO() + with ( + mock.patch.object(routing, "_api", return_value=value) as api, + mock.patch.object(routing, "_accepted_work") as accepted, + contextlib.redirect_stdout(output), + ): + self.assertEqual( + routing.main( + [ + "--reviewers", + str(path), + "--repo", + REPO, + "--pr", + "42", + ] + ), + 0, + ) + api.assert_called_once_with(PULL) + accepted.assert_not_called() + self.assertEqual( + json.loads(output.getvalue()), {"skipped": "closed or draft PR"} + ) + + def test_accepted_pr_with_invalid_registry_fails_before_reviewer_changes(self): with tempfile.TemporaryDirectory() as directory: 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), ): @@ -1085,7 +2004,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())