Spaces:
Running
Running
File size: 9,583 Bytes
47d2384 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | #!/usr/bin/env python3
"""Fetch 15 real past PRs with human-written review comments from popular
open-source Python repos for use as ground-truth evaluation data.
Requires a GITHUB_TOKEN environment variable for higher API rate limits.
Output: ``eval_data/prs.jsonl`` (one JSON line per PR with pre-fetched diff,
``changed_files``, and ``ground_truth`` review comments).
"""
from __future__ import annotations
import json
import logging
import os
import sys
import time
from typing import Any, Optional
import requests
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
)
logger = logging.getLogger(__name__)
GITHUB_API_BASE = "https://api.github.com"
RAW_CONTENT_BASE = "https://raw.githubusercontent.com"
# Popular Python repos with active PR reviews β we will fetch up to 3
# merged PRs per repo that bear review comments.
REPOS = [
"psf/requests",
"pydantic/pydantic",
"fastapi/fastapi",
"django/django",
"pallets/flask",
]
PRS_PER_REPO = 3
TOTAL_TARGET = 15
OUTPUT_PATH = os.path.join(os.path.dirname(__file__), "prs.jsonl")
# βββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _headers() -> dict[str, str]:
headers: dict[str, str] = {
"Accept": "application/vnd.github.v3+json",
"User-Agent": "OpenCodeReviewEval/1.0",
}
token = os.environ.get("GITHUB_TOKEN", "").strip()
if token:
headers["Authorization"] = f"Bearer {token}"
return headers
def _fetch_all_pages(url: str, headers: dict) -> list[dict]:
"""Follow GitHub Link-header pagination."""
items: list[dict] = []
while url:
try:
resp = requests.get(url, headers=headers, timeout=30)
if resp.status_code != 200:
logger.warning("HTTP %s for %s", resp.status_code, url)
break
items.extend(resp.json())
except requests.RequestException as exc:
logger.warning("Request failed: %s", exc)
break
link = resp.headers.get("Link", "")
next_url: Optional[str] = None
for part in link.split(","):
part = part.strip()
if 'rel="next"' in part:
start = part.index("<") + 1
end = part.index(">")
next_url = part[start:end]
break
url = next_url
return items
def _find_prs(repo: str, headers: dict, max_count: int) -> list[dict]:
"""Find merged PRs that have at least one review comment."""
url = (
f"{GITHUB_API_BASE}/repos/{repo}/pulls?"
f"state=closed&sort=updated&direction=desc&per_page=100"
)
all_prs = _fetch_all_pages(url, headers)
candidates = [
pr for pr in all_prs
if pr.get("merged_at") and pr.get("review_comments", 0) > 0
]
logger.info(
" Found %d candidate PRs in %s (from %s total, %s merged)",
len(candidates), repo, len(all_prs),
sum(1 for p in all_prs if p.get("merged_at")),
)
return candidates[:max_count]
def _fetch_review_comments(
owner: str, repo: str, pr_number: int, headers: dict,
) -> list[dict]:
url = (
f"{GITHUB_API_BASE}/repos/{owner}/{repo}/pulls"
f"/{pr_number}/comments"
)
return _fetch_all_pages(url, headers)
def _fetch_changed_files(
owner: str, repo: str, pr_number: int, headers: dict,
) -> list[dict]:
url = (
f"{GITHUB_API_BASE}/repos/{owner}/{repo}/pulls"
f"/{pr_number}/files"
)
return _fetch_all_pages(url, headers)
def _fetch_file_content(owner: str, repo: str, ref: str, path: str) -> str:
url = f"{RAW_CONTENT_BASE}/{owner}/{repo}/{ref}/{path}"
try:
resp = requests.get(url, timeout=30)
if resp.status_code == 200:
return resp.text
return ""
except requests.RequestException:
return ""
def _build_ground_truth(review_comments: list[dict]) -> list[dict]:
"""Convert GitHub PR review comments to the ground-truth schema."""
gt = []
for rc in review_comments:
# Use the line number from the diff; fall back to the original line.
line = rc.get("line") or rc.get("original_line") or 0
gt.append({
"file_path": rc.get("path", ""),
"line": line,
"comment": rc.get("body", ""),
"reviewer": rc.get("user", {}).get("login", "unknown"),
"created_at": rc.get("created_at", ""),
})
return gt
# βββ Main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def main() -> None:
headers = _headers()
entries: list[dict] = []
if "GITHUB_TOKEN" not in os.environ:
logger.error(
"GITHUB_TOKEN is not set! The GitHub API enforces very "
"restrictive rate limits (60 req/hr) for unauthenticated "
"requests β the fetcher WILL FAIL with HTTP 403 errors.\n"
" Set the environment variable and re-run:\n"
" export GITHUB_TOKEN=ghp_..."
)
logger.info("Exiting β no GITHUB_TOKEN set")
sys.exit(1)
for repo in REPOS:
if len(entries) >= TOTAL_TARGET:
break
owner, repo_name = repo.split("/")
prs = _find_prs(repo, headers, PRS_PER_REPO)
for pr in prs:
if len(entries) >= TOTAL_TARGET:
break
pr_number = pr["number"]
title = pr.get("title", "") or "(no title)"
head_sha = pr["head"]["sha"]
logger.info(
"Fetching %s/%s#%d β %s",
owner, repo_name, pr_number, title[:80],
)
# --- Diff ---
diff_headers = {**headers, "Accept": "application/vnd.github.v3.diff"}
try:
diff_resp = requests.get(
pr["url"], headers=diff_headers, timeout=30,
)
diff_text = diff_resp.text if diff_resp.status_code == 200 else ""
except requests.RequestException as exc:
logger.warning(" Diff fetch failed: %s", exc)
diff_text = ""
# --- Changed files with content ---
files_data = _fetch_changed_files(owner, repo_name, pr_number, headers)
changed_files: list[dict] = []
for fd in files_data:
file_path = fd["filename"]
status = fd["status"]
patch = fd.get("patch") or ""
# Fetch file content from the head commit
content = _fetch_file_content(owner, repo_name, head_sha, file_path)
if status == "removed":
content = ""
# For fork PRs, try the head repo (different from base)
if not content:
head_full = pr["head"]["repo"].get("full_name", "")
if head_full and head_full != repo:
h_owner, h_repo = head_full.split("/", 1)
content = _fetch_file_content(
h_owner, h_repo, head_sha, file_path,
)
changed_files.append({
"path": file_path,
"status": status,
"content": content,
"diff_hunk": patch,
})
# --- Review comments (ground truth) ---
review_comments = _fetch_review_comments(
owner, repo_name, pr_number, headers,
)
ground_truth = _build_ground_truth(review_comments)
if not ground_truth:
logger.info(" Skipping β no review comments found")
continue
entry: dict[str, Any] = {
"id": f"{repo.replace('/', '_')}_{pr_number}",
"repo": repo,
"pr_number": pr_number,
"title": title,
"diff": diff_text,
"changed_files": changed_files,
"ground_truth": ground_truth,
}
entries.append(entry)
logger.info(
" -> %d files, %d review comments, diff=%s",
len(changed_files), len(ground_truth),
f"{len(diff_text)} B" if diff_text else "empty",
)
# Polite rate-limiting
time.sleep(0.5)
# --- Write output ---
os.makedirs(os.path.dirname(OUTPUT_PATH), exist_ok=True)
with open(OUTPUT_PATH, "w", encoding="utf-8") as f:
for entry in entries:
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
total_gt = sum(len(e["ground_truth"]) for e in entries)
logger.info(
"Wrote %d PRs to %s β %d ground-truth comments (avg %.1f/PR)",
len(entries), OUTPUT_PATH, total_gt,
total_gt / len(entries) if entries else 0,
)
# Summary line for easy terminal inspection
print(f"\n{'='*70}")
print(f" Dataset: {OUTPUT_PATH}")
print(f" PRs: {len(entries)}")
print(f" Ground-truth comments: {total_gt}")
for e in entries:
print(f" {e['id']:40s} {len(e['ground_truth']):3d} comments {e['title'][:50]}")
print(f"{'='*70}")
if __name__ == "__main__":
main()
|