File size: 2,015 Bytes
348a067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Fetch a bounded, diff-backed subset of Code Review Bench.

This only downloads public PR diffs and keeps the expert comments as labels.
It never posts to GitHub or mutates repositories.
"""
from __future__ import annotations

import argparse
import json
from pathlib import Path

import requests
import pyarrow.parquet as pq
from huggingface_hub import hf_hub_download


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--limit", type=int, default=10)
    parser.add_argument("--output", type=Path, default=Path("eval_data/diff_backed_fixture.jsonl"))
    args = parser.parse_args()

    source = hf_hub_download(
        "code-review-bench/code-review-bench",
        filename="offline_golden_issues.parquet",
        repo_type="dataset",
    )
    rows = pq.read_table(source).to_pylist()
    grouped: dict[str, list[dict]] = {}
    for row in rows:
        grouped.setdefault(row["pr_url"], []).append(row)

    output: list[dict] = []
    for url, labels in list(grouped.items()):
        if len(output) >= args.limit:
            break
        try:
            response = requests.get(f"{url}.diff", timeout=20)
            response.raise_for_status()
        except requests.RequestException as exc:
            print(f"skip {url}: {exc}")
            continue
        if not response.text.strip():
            continue
        first = labels[0]
        output.append(
            {
                "task_id": url,
                "pr_url": url,
                "source_repo": first["source_repo"],
                "pr_title": first["pr_title"],
                "diff": response.text,
                "gold_issues": labels,
            }
        )

    args.output.parent.mkdir(parents=True, exist_ok=True)
    args.output.write_text(
        "".join(json.dumps(item, ensure_ascii=False) + "\n" for item in output),
        encoding="utf-8",
    )
    print(f"wrote {len(output)} fixtures to {args.output}")


if __name__ == "__main__":
    main()