diff --git a/.github/scripts/cli_pr_review.py b/.github/scripts/cli_pr_review.py index 708998e34f8..d74cf8675b4 100644 --- a/.github/scripts/cli_pr_review.py +++ b/.github/scripts/cli_pr_review.py @@ -7,6 +7,7 @@ import json import os import subprocess +from datetime import datetime, timezone from pathlib import Path from urllib.parse import quote @@ -20,13 +21,17 @@ def github_api(path): return json.loads(subprocess.check_output(["gh", "api", path], text=True)) -def validate_request(event_name, event, api=github_api): +def validate_request(event_name, event, api=github_api, audit=None): """Return the PR number if allowed, or None to skip. Tests supply a fake API.""" + if audit is None: + audit = {} + audit.update(decision="skipped", permissions=[]) if event["repository"]["full_name"] != REPOSITORY: return None # 1. Identify the request: an automatic PR event or an explicit /review comment. requester = None + audit["requester"] = {key: event["sender"][key] for key in ("id", "login", "type")} if event_name == "pull_request_target": if event["action"] not in PR_ACTIONS: return None @@ -45,6 +50,8 @@ def validate_request(event_name, event, api=github_api): return None number = event["issue"]["number"] requester = comment["user"]["login"] + audit["comment_id"] = comment["id"] + audit["requester"] = {key: comment["user"][key] for key in ("id", "login", "type")} else: return None @@ -52,6 +59,12 @@ def validate_request(event_name, event, api=github_api): raise ValueError("Expected a positive PR number") # 2. Fetch the current PR. It may have changed while this event waited for a runner. pr = api(f"repos/{REPOSITORY}/pulls/{number}") + audit.update( + pr_number=number, + head_sha=pr["head"]["sha"], + base_ref=pr["base"]["ref"], + author={key: pr["user"][key] for key in ("id", "login", "type")}, + ) head_repo = pr["head"]["repo"] if ( pr["state"] != "open" @@ -76,15 +89,45 @@ def validate_request(event_name, event, api=github_api): users.add(requester) for user in sorted(users): permission = api(f"repos/{REPOSITORY}/collaborators/{quote(user, safe='')}/permission") + audit["permissions"].append( + { + "login": user, + "permission": permission["permission"], + "checked_at": datetime.now(timezone.utc).isoformat(), + } + ) if permission["permission"] not in WRITE_PERMISSIONS: + audit["decision"] = "denied" return None + audit["decision"] = "allowed" return number def main(): """Read GitHub's event file and tell the next job whether it may proceed.""" event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text()) - number = validate_request(os.environ["GITHUB_EVENT_NAME"], event) + audit = { + "schema_version": 1, + "kind": "authorization", + "repository": REPOSITORY, + "repository_id": int(os.environ["GITHUB_REPOSITORY_ID"]), + "run_id": int(os.environ["GITHUB_RUN_ID"]), + "run_attempt": int(os.environ["GITHUB_RUN_ATTEMPT"]), + "workflow_sha": os.environ["GITHUB_WORKFLOW_SHA"], + "workflow_ref": os.environ["GITHUB_WORKFLOW_REF"], + "event": os.environ["GITHUB_EVENT_NAME"], + "actor": {"login": os.environ["GITHUB_ACTOR"], "id": int(os.environ["GITHUB_ACTOR_ID"])}, + "triggering_actor_login": os.environ["GITHUB_TRIGGERING_ACTOR"], + } + try: + number = validate_request(os.environ["GITHUB_EVENT_NAME"], event, audit=audit) + except Exception: + audit["decision"] = "error" + raise + finally: + # Preserve the actual permission checks, including denials and partial API failures. + # Do not include comment text, PR bodies, credentials, or the full event payload. + Path(os.environ["RUNNER_TEMP"], "review-authorization.json").write_text(json.dumps(audit) + "\n") if number is None: # An ineligible request is a normal skip, not a broken workflow. with Path(os.environ["GITHUB_STEP_SUMMARY"]).open("a") as summary: @@ -93,6 +136,7 @@ def main(): # These outputs unlock the credential-bearing job. Write them only after all checks pass. with Path(os.environ["GITHUB_OUTPUT"]).open("a") as output: output.write(f"eligible=true\npr_number={number}\n") + output.write(f"request_context={json.dumps(audit)}\n") if __name__ == "__main__": diff --git a/.github/scripts/test_cli_pr_review.py b/.github/scripts/test_cli_pr_review.py index 14ba97f0205..e9babba0296 100644 --- a/.github/scripts/test_cli_pr_review.py +++ b/.github/scripts/test_cli_pr_review.py @@ -21,10 +21,11 @@ def setUp(self): "base": {"ref": "main"}, "head": {"sha": "a" * 40, "repo": {"full_name": "databricks/cli", "fork": False}}, "draft": False, - "user": {"login": "author", "type": "User"}, + "user": {"id": 10, "login": "author", "type": "User"}, } self.automatic = { "repository": {"full_name": "databricks/cli"}, + "sender": {"id": 11, "login": "requester", "type": "User"}, "action": "opened", "number": 7400, # Keep the old event separate so tests can change the current PR afterward. @@ -32,12 +33,14 @@ def setUp(self): } self.comment = { "repository": {"full_name": "databricks/cli"}, + "sender": {"id": 11, "login": "requester", "type": "User"}, "action": "created", "issue": {"number": 7400, "pull_request": {}}, "comment": { + "id": 55, "body": "/review", "author_association": "MEMBER", - "user": {"login": "requester", "type": "User"}, + "user": {"id": 11, "login": "requester", "type": "User"}, }, } self.permissions = {"author": "write", "requester": "maintain"} @@ -65,7 +68,7 @@ def test_ineligible_prs(self): ("head", {"sha": "a" * 40, "repo": {"full_name": "other/cli", "fork": True}}), ("head", {"sha": "a" * 40, "repo": {"full_name": "databricks/cli", "fork": True}}), ("head", {"sha": "a" * 40, "repo": None}), - ("user", {"login": "bot", "type": "Bot"}), + ("user", {"id": 12, "login": "bot", "type": "Bot"}), ] original = copy.deepcopy(self.pr) # Change one property at a time and check both automatic and comment requests. @@ -142,7 +145,7 @@ def test_comment_filter(self): ("body", " /review extra "), ("body", "/reviewer"), ("body", "$(touch /tmp/should-not-execute)"), - ("user", {"login": "bot", "type": "Bot"}), + ("user", {"id": 12, "login": "bot", "type": "Bot"}), ]: with self.subTest(key=key, value=value): event = copy.deepcopy(self.comment) @@ -222,6 +225,15 @@ def test_main_outputs_only_after_authorization(self): event_path.write_text(json.dumps(self.comment)) env = { "GITHUB_EVENT_PATH": str(event_path), + "RUNNER_TEMP": str(root), + "GITHUB_REPOSITORY_ID": "100", + "GITHUB_RUN_ID": "200", + "GITHUB_RUN_ATTEMPT": "2", + "GITHUB_WORKFLOW_SHA": "c" * 40, + "GITHUB_WORKFLOW_REF": "databricks/cli/.github/workflows/cli-pr-review.yml@refs/heads/main", + "GITHUB_ACTOR": "requester", + "GITHUB_ACTOR_ID": "11", + "GITHUB_TRIGGERING_ACTOR": "rerunner", "GITHUB_EVENT_NAME": "issue_comment", "GITHUB_OUTPUT": str(root / "output"), "GITHUB_STEP_SUMMARY": str(root / "summary"), @@ -232,11 +244,19 @@ def test_main_outputs_only_after_authorization(self): replies = [self.pr, {"permission": "write"}, {"permission": permission}] with patch("subprocess.check_output", side_effect=[json.dumps(r) for r in replies]): review.main() + receipt = json.loads((root / "review-authorization.json").read_text()) + self.assertEqual(receipt["decision"], "denied" if permission == "read" else "allowed") + self.assertEqual(receipt["permissions"][-1]["permission"], permission) + self.assertEqual(receipt["requester"]["id"], 11) + self.assertEqual(receipt["triggering_actor_login"], "rerunner") + self.assertNotIn("body", json.dumps(receipt)) if permission == "read": self.assertFalse((root / "output").exists()) self.assertIn("skipping", (root / "summary").read_text()) else: - self.assertEqual((root / "output").read_text(), "eligible=true\npr_number=7400\n") + self.assertTrue( + (root / "output").read_text().startswith("eligible=true\npr_number=7400\nrequest_context=") + ) if __name__ == "__main__": diff --git a/.github/workflows/cli-pr-review.yml b/.github/workflows/cli-pr-review.yml index f9cb613c01a..ff72bc8a1f2 100644 --- a/.github/workflows/cli-pr-review.yml +++ b/.github/workflows/cli-pr-review.yml @@ -37,6 +37,7 @@ jobs: outputs: eligible: ${{ steps.request.outputs.eligible }} pr_number: ${{ steps.request.outputs.pr_number }} + request_context: ${{ steps.request.outputs.request_context }} steps: # Pin the validator to the trusted workflow revision, not the PR head. @@ -52,6 +53,15 @@ jobs: GH_TOKEN: ${{ github.token }} run: python3 .github/scripts/cli_pr_review.py + - name: Preserve authorization decision + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: cli-review-authorization-${{ github.run_attempt }} + path: ${{ runner.temp }}/review-authorization.json + if-no-files-found: error + retention-days: 90 + review: needs: validate if: needs.validate.outputs.eligible == 'true' && github.ref == 'refs/heads/main' @@ -79,10 +89,14 @@ jobs: repositories: eng-dev-ecosystem permission-actions: write - - name: Dispatch and wait for this review + - name: Dispatch this review + id: dispatch env: GH_TOKEN: ${{ steps.token.outputs.token }} PR_NUMBER: ${{ needs.validate.outputs.pr_number }} + REQUEST_CONTEXT: ${{ needs.validate.outputs.request_context }} + DISPATCH_APP_ID: ${{ secrets.DECO_WORKFLOW_TRIGGER_APP_ID }} + DISPATCH_INSTALLATION_ID: ${{ steps.token.outputs.installation-id }} run: |- set -euo pipefail # This API version returns the exact run, even when other PRs trigger reviews. @@ -90,17 +104,42 @@ jobs: gh api --method POST \ repos/databricks-eng/eng-dev-ecosystem/actions/workflows/cli-pr-review.yml/dispatches \ -H 'X-GitHub-Api-Version: 2026-03-10' \ - -f ref=main -f "inputs[pull_request_number]=$PR_NUMBER" > "$RUNNER_TEMP/review-run.json" + -f ref=main -f "inputs[pull_request_number]=$PR_NUMBER" \ + -f "inputs[request_context]=$REQUEST_CONTEXT" > "$RUNNER_TEMP/review-run.json" run_id=$(jq -er '.workflow_run_id | select(type == "number")' "$RUNNER_TEMP/review-run.json") + jq -n --argjson backend "$run_id" --argjson context "$REQUEST_CONTEXT" \ + --arg app "$DISPATCH_APP_ID" --arg installation "$DISPATCH_INSTALLATION_ID" \ + '{schema_version: 1, kind: "dispatch", backend_repository: "databricks-eng/eng-dev-ecosystem", + backend_run_id: $backend, source_run_id: $context.run_id, + source_run_attempt: $context.run_attempt, source_repository_id: $context.repository_id, + dispatcher_app_id: $app, dispatcher_installation_id: $installation, + dispatched_at: (now | todate)}' > "$RUNNER_TEMP/review-dispatch.json" echo "[Review run](https://github.com/databricks-eng/eng-dev-ecosystem/actions/runs/$run_id)" >> "$GITHUB_STEP_SUMMARY" echo 'To request another review, comment `/review` on the PR.' >> "$GITHUB_STEP_SUMMARY" + echo "run_id=$run_id" >> "$GITHUB_OUTPUT" + + - name: Preserve dispatch receipt + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 + with: + name: cli-review-dispatch-${{ github.run_attempt }} + path: ${{ runner.temp }}/review-dispatch.json + if-no-files-found: warn + retention-days: 90 + + - name: Wait for this review + env: + GH_TOKEN: ${{ steps.token.outputs.token }} + REVIEW_RUN_ID: ${{ steps.dispatch.outputs.run_id }} + run: |- + set -euo pipefail # A newly dispatched run can briefly return 404 before GitHub indexes it. for attempt in {1..12}; do - if gh run view "$run_id" --repo databricks-eng/eng-dev-ecosystem --json databaseId >/dev/null 2>&1; then - gh run watch "$run_id" --repo databricks-eng/eng-dev-ecosystem --interval 15 --exit-status + if gh run view "$REVIEW_RUN_ID" --repo databricks-eng/eng-dev-ecosystem --json databaseId >/dev/null 2>&1; then + gh run watch "$REVIEW_RUN_ID" --repo databricks-eng/eng-dev-ecosystem --interval 15 --exit-status exit 0 fi sleep 5 done - echo "::error::Dispatched review $run_id did not become visible within one minute." + echo "::error::Dispatched review $REVIEW_RUN_ID did not become visible within one minute." exit 1