|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import pytest |
| 20 | +import yaml |
| 21 | + |
| 22 | +ROOT = Path(__file__).resolve().parents[3] |
| 23 | + |
| 24 | + |
| 25 | +def test_review_redirect_is_least_privilege_and_does_not_execute_review() -> None: |
| 26 | + """Require the complete trusted-command gate and notice-only permissions.""" |
| 27 | + workflow = yaml.safe_load((ROOT / ".github/workflows/claude-review.yml").read_text()) |
| 28 | + job = workflow["jobs"]["redirect-to-review"] |
| 29 | + condition = job["if"] |
| 30 | + assert " ".join(condition.split()) == ( |
| 31 | + "github.event_name == 'issue_comment' && " |
| 32 | + "github.event.issue.pull_request && " |
| 33 | + "github.event.comment.user.type != 'Bot' && " |
| 34 | + 'contains(fromJSON(\'["OWNER", "MEMBER", "COLLABORATOR"]\'), ' |
| 35 | + "github.event.comment.author_association) && " |
| 36 | + "(github.event.comment.body == '/claude review' || " |
| 37 | + "github.event.comment.body == '/claude strict-review')" |
| 38 | + ) |
| 39 | + assert job["permissions"] == {"pull-requests": "write"} |
| 40 | + assert "uses" not in job |
| 41 | + assert "secrets" not in job |
| 42 | + assert len(job["steps"]) == 1 |
| 43 | + step = job["steps"][0] |
| 44 | + assert "uses" not in step |
| 45 | + assert step["run"] == 'gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$NOTICE"' |
| 46 | + assert "${{" not in step["run"] |
| 47 | + assert "model=claude" in step["env"]["NOTICE"] |
| 48 | + assert "model=codex" in step["env"]["NOTICE"] |
| 49 | + assert "/review help" in step["env"]["NOTICE"] |
| 50 | + assert job["env"]["REVIEW_COMMAND"] == ( |
| 51 | + "${{ github.event.comment.body == '/claude strict-review' && '/review mode=strict' || '/review' }}" |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def test_formal_review_rubric_is_inert_and_uses_formal_submission() -> None: |
| 56 | + """Keep the repository rubric inert and bound to formal submission.""" |
| 57 | + rubric = (ROOT / "skills/pr-review/SKILL.md").read_text() |
| 58 | + metadata = yaml.safe_load(rubric.split("---", 2)[1]) |
| 59 | + assert metadata["name"] == "pr-review" |
| 60 | + assert metadata["disable-model-invocation"] is True |
| 61 | + assert metadata["user_invocable"] is False |
| 62 | + assert "mode=light" in rubric |
| 63 | + assert "mode=strict" in rubric |
| 64 | + assert "Do not run GitHub commands or" in rubric |
| 65 | + assert "Never approve an incomplete" in rubric |
| 66 | + |
| 67 | + |
| 68 | +def test_review_redirect_workflow_has_no_other_execution_or_permissions() -> None: |
| 69 | + """Reject automatic review triggers and additional privileged jobs.""" |
| 70 | + workflow = yaml.safe_load((ROOT / ".github/workflows/claude-review.yml").read_text()) |
| 71 | + assert workflow["permissions"] == {} |
| 72 | + assert set(workflow["jobs"]) == {"redirect-to-review"} |
| 73 | + events = workflow.get("on", workflow.get(True)) |
| 74 | + assert events == {"issue_comment": {"types": ["created"]}} |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "notice", |
| 79 | + [ |
| 80 | + "Use /review", |
| 81 | + "Use /review mode=strict", |
| 82 | + "$(touch injected) `touch injected` ' \" ;\n/review", |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_notice_is_passed_as_one_literal_argument(tmp_path: Path, notice: str) -> None: |
| 86 | + """Ensure notice text reaches gh unchanged without shell evaluation.""" |
| 87 | + workflow = yaml.safe_load((ROOT / ".github/workflows/claude-review.yml").read_text()) |
| 88 | + script = workflow["jobs"]["redirect-to-review"]["steps"][0]["run"] |
| 89 | + gh = tmp_path / "gh" |
| 90 | + gh.write_text('#!/bin/sh\nprintf \'%s\\0\' "$@" > "$CAPTURE"\n') |
| 91 | + gh.chmod(0o755) |
| 92 | + capture = tmp_path / "args" |
| 93 | + env = dict( |
| 94 | + os.environ, |
| 95 | + PS1="", |
| 96 | + PATH=str(tmp_path) + os.pathsep + os.environ["PATH"], |
| 97 | + PR_NUMBER="123", |
| 98 | + REPO="NVIDIA-NeMo/example", |
| 99 | + NOTICE=notice, |
| 100 | + CAPTURE=str(capture), |
| 101 | + ) |
| 102 | + env.pop("BASH_ENV", None) |
| 103 | + env.pop("ENV", None) |
| 104 | + subprocess.run(["/bin/bash", "-eu", "-c", script], cwd=tmp_path, env=env, check=True) |
| 105 | + assert capture.read_bytes().split(b"\0") == [ |
| 106 | + b"pr", |
| 107 | + b"comment", |
| 108 | + b"123", |
| 109 | + b"--repo", |
| 110 | + b"NVIDIA-NeMo/example", |
| 111 | + b"--body", |
| 112 | + notice.encode(), |
| 113 | + b"", |
| 114 | + ] |
| 115 | + assert not (tmp_path / "injected").exists() |
| 116 | + |
| 117 | + |
| 118 | +def test_legacy_inference_execution_is_removed() -> None: |
| 119 | + """Prevent retired inference runners from reappearing in workflows.""" |
| 120 | + workflows = ROOT / ".github/workflows" |
| 121 | + for path in (*workflows.glob("*.yml"), *workflows.glob("*.yaml")): |
| 122 | + text = path.read_text() |
| 123 | + for retired in ("_claude_review", "claude-code-action", "auto-review:"): |
| 124 | + assert retired not in text, f"{path.name} contains {retired}" |
| 125 | + |
| 126 | + |
| 127 | +def test_review_guidance_documents_activation_and_notice_limits() -> None: |
| 128 | + """Keep user guidance consistent with the trusted notice-only migration.""" |
| 129 | + guidance = (ROOT / "CONTRIBUTING.md").read_text().split("## Pull-request reviews", 1)[1] |
| 130 | + for requirement in ( |
| 131 | + "`/review`", |
| 132 | + "`/review mode=strict`", |
| 133 | + "model=claude", |
| 134 | + "protected `main`", |
| 135 | + "Ready plugin snapshot", |
| 136 | + "exact command", |
| 137 | + "owner, member, or collaborator", |
| 138 | + "never run or automatically request a review", |
| 139 | + ): |
| 140 | + assert requirement in guidance |
0 commit comments