"""Minimal PR-review agent. Runs the OpenAI Agents SDK with one tool that fetches a unified diff from GitHub. Prints a short review to stdout. Usage: uv sync uv run python pr_review.py Auth: optional GITHUB_TOKEN env var. Anonymous calls also work for public repos under standard rate limits. """ from __future__ import annotations import os import sys import httpx from agents import Agent, Runner, function_tool @function_tool def get_pr_diff(owner: str, repo: str, number: int) -> str: """Fetch the unified diff for a GitHub pull request.""" url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{number}" headers = {"Accept": "application/vnd.github.v3.diff"} if token := os.environ.get("GITHUB_TOKEN"): headers["Authorization"] = f"Bearer {token}" response = httpx.get(url, headers=headers, timeout=30.0) response.raise_for_status() return response.text SYSTEM_PROMPT = """You are a senior code reviewer. Given a unified diff, write a short review focused on: - correctness issues - missing or insufficient tests - unclear or risky changes Be concise. Use bullet points. Skip praise. If the diff is empty or unreadable, say so.""" def main() -> int: if len(sys.argv) != 4: print("Usage: uv run python pr_review.py ", file=sys.stderr) return 1 owner, repo, number_str = sys.argv[1], sys.argv[2], sys.argv[3] try: number = int(number_str) except ValueError: print(f"pr_number must be an integer, got: {number_str!r}", file=sys.stderr) return 1 agent = Agent( name="pr-reviewer", instructions=SYSTEM_PROMPT, tools=[get_pr_diff], model="gpt-5", ) prompt = f"Review pull request {number} in {owner}/{repo}. Use the get_pr_diff tool." result = Runner.run_sync(agent, prompt) print(result.final_output) return 0 if __name__ == "__main__": raise SystemExit(main())