|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Replay one frozen, now-merged PR through an installed ReviewBot release. |
| 3 | +
|
| 4 | +This evaluation-only adapter supplies the historical open snapshot and the |
| 5 | +frozen file list to the normal Direct pipeline. The GitHub transport is |
| 6 | +read-only, and the pipeline is always called with post=False. Production PR |
| 7 | +eligibility and publication code are neither imported from source nor changed. |
| 8 | +""" |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import os |
| 13 | +import re |
| 14 | +from dataclasses import replace |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +SHA = re.compile(r"[0-9a-f]{40}\Z") |
| 18 | +FILE_HEADER = re.compile(r"diff --git a/(.+) b/(.+)\Z") |
| 19 | + |
| 20 | + |
| 21 | +def frozen_files(diff: str) -> list[dict[str, object]]: |
| 22 | + """Project the checked-in Git patch into ReviewBot's ChangedFile fields.""" |
| 23 | + files: list[dict[str, object]] = [] |
| 24 | + current: dict[str, object] | None = None |
| 25 | + patch: list[str] = [] |
| 26 | + in_hunk = False |
| 27 | + |
| 28 | + def finish() -> None: |
| 29 | + if current is not None: |
| 30 | + current["patch"] = "\n".join(patch) + "\n" if patch else None |
| 31 | + files.append(current) |
| 32 | + |
| 33 | + for line in diff.splitlines(): |
| 34 | + if line.startswith("diff --git "): |
| 35 | + finish() |
| 36 | + match = FILE_HEADER.fullmatch(line) |
| 37 | + if match is None or match.group(1) != match.group(2): |
| 38 | + raise ValueError(f"unsupported frozen file header: {line}") |
| 39 | + current = { |
| 40 | + "path": match.group(2), "status": "modified", |
| 41 | + "additions": 0, "deletions": 0, |
| 42 | + } |
| 43 | + patch = [] |
| 44 | + in_hunk = False |
| 45 | + elif current is None: |
| 46 | + raise ValueError("frozen diff has content before its first file") |
| 47 | + elif line.startswith("new file mode "): |
| 48 | + current["status"] = "added" |
| 49 | + elif line.startswith("deleted file mode "): |
| 50 | + current["status"] = "removed" |
| 51 | + elif line.startswith("rename from ") or line.startswith("copy from "): |
| 52 | + raise ValueError("frozen diff rename/copy needs explicit projection") |
| 53 | + elif line.startswith("Binary files ") or line == "GIT binary patch": |
| 54 | + raise ValueError("binary frozen diffs need explicit projection") |
| 55 | + elif line.startswith("@@ "): |
| 56 | + in_hunk = True |
| 57 | + patch.append(line) |
| 58 | + elif in_hunk: |
| 59 | + patch.append(line) |
| 60 | + if line.startswith("+"): |
| 61 | + current["additions"] = int(current["additions"]) + 1 |
| 62 | + elif line.startswith("-"): |
| 63 | + current["deletions"] = int(current["deletions"]) + 1 |
| 64 | + finish() |
| 65 | + if not files: |
| 66 | + raise ValueError("frozen diff contains no files") |
| 67 | + paths = [str(item["path"]) for item in files] |
| 68 | + if len(paths) != len(set(paths)): |
| 69 | + raise ValueError("frozen diff repeats a file") |
| 70 | + return files |
| 71 | + |
| 72 | + |
| 73 | +def historical_pull(pull, *, pr: int, head: str, base: str): |
| 74 | + """Verify the live archived identity before presenting the frozen view.""" |
| 75 | + if pull.number != pr or pull.head_sha != head: |
| 76 | + raise ValueError(f"pr{pr} no longer has the pinned head") |
| 77 | + if pull.state != "closed" or not pull.merged_at: |
| 78 | + raise ValueError(f"pr{pr} is not a merged archived PR") |
| 79 | + return replace( |
| 80 | + pull, state="open", base_sha=base, merged_at="", merge_commit_sha="", |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def main() -> int: |
| 85 | + parser = argparse.ArgumentParser(description=__doc__) |
| 86 | + parser.add_argument("--pr", type=int, required=True) |
| 87 | + parser.add_argument("--expected-head", required=True) |
| 88 | + parser.add_argument("--base", required=True) |
| 89 | + parser.add_argument("--diff", type=Path, required=True) |
| 90 | + args = parser.parse_args() |
| 91 | + if os.environ.get("REVIEWBOT_EVAL_ARCHIVED_REPLAY") != "1": |
| 92 | + parser.error("archived replay requires REVIEWBOT_EVAL_ARCHIVED_REPLAY=1") |
| 93 | + if os.environ.get("POST_MODE") != "shadow": |
| 94 | + parser.error("archived replay requires POST_MODE=shadow") |
| 95 | + if os.environ.get("REVIEW_CONTEXT_MODE") != "no_discussion": |
| 96 | + parser.error("archived replay requires REVIEW_CONTEXT_MODE=no_discussion") |
| 97 | + if args.pr <= 0 or not SHA.fullmatch(args.expected_head) or not SHA.fullmatch(args.base): |
| 98 | + parser.error("invalid PR number or pinned commit SHA") |
| 99 | + if args.diff.name != f"pr{args.pr}.diff" or not args.diff.is_file(): |
| 100 | + parser.error("diff must be the frozen patch for the selected PR") |
| 101 | + |
| 102 | + # Import only the paired wheel installed in the runner's private venv. |
| 103 | + from omni_reviewbot.cli import build_runtime |
| 104 | + from omni_reviewbot.config import Settings |
| 105 | + from omni_reviewbot.models import ChangedFile |
| 106 | + |
| 107 | + settings = Settings.from_env() |
| 108 | + if settings.post or settings.review_context_mode != "no_discussion": |
| 109 | + parser.error("installed ReviewBot settings violate shadow replay") |
| 110 | + diff_files = [ |
| 111 | + ChangedFile(**fields) |
| 112 | + for fields in frozen_files(args.diff.read_text(encoding="utf-8")) |
| 113 | + ] |
| 114 | + runtime = build_runtime(settings, recover_running=False) |
| 115 | + github = runtime.github |
| 116 | + request = github._request |
| 117 | + |
| 118 | + def read_only(method, path, payload=None): |
| 119 | + if method != "GET" or payload is not None: |
| 120 | + raise RuntimeError("archived evaluation rejects GitHub writes") |
| 121 | + return request(method, path, payload) |
| 122 | + |
| 123 | + github._request = read_only |
| 124 | + original_get_pull = github.get_pull |
| 125 | + |
| 126 | + def get_pull(number): |
| 127 | + if number != args.pr: |
| 128 | + raise RuntimeError("archived evaluation rejects another PR") |
| 129 | + return historical_pull( |
| 130 | + original_get_pull(number), |
| 131 | + pr=args.pr, head=args.expected_head, base=args.base, |
| 132 | + ) |
| 133 | + |
| 134 | + def list_files(number): |
| 135 | + if number != args.pr: |
| 136 | + raise RuntimeError("archived evaluation rejects another PR") |
| 137 | + return list(diff_files) |
| 138 | + |
| 139 | + github.get_pull = get_pull |
| 140 | + github.list_files = list_files |
| 141 | + outcome = runtime.pipeline.run( |
| 142 | + pr_number=args.pr, expected_head=args.expected_head, post=False, |
| 143 | + ) |
| 144 | + print(outcome.body) |
| 145 | + print(f"status: {outcome.status}") |
| 146 | + metrics = outcome.metrics or {} |
| 147 | + if "review_context_mode" in metrics: |
| 148 | + print( |
| 149 | + f"review_context: {metrics['review_context_mode']} " |
| 150 | + f"({metrics['review_context_count']} threads)" |
| 151 | + ) |
| 152 | + if "changed_files" in metrics: |
| 153 | + print(f"changed_files: {metrics['changed_files']}") |
| 154 | + if outcome.artifact_path: |
| 155 | + print(f"artifact: {outcome.artifact_path}") |
| 156 | + return 0 |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + raise SystemExit(main()) |
0 commit comments