-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap_meta.py
More file actions
104 lines (93 loc) · 3.44 KB
/
Copy pathbootstrap_meta.py
File metadata and controls
104 lines (93 loc) · 3.44 KB
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
#!/usr/bin/env python3
"""Create workspace/inputs/pr-<id>/meta.json from defaults if missing (no LLM)."""
from __future__ import annotations
import argparse
import json
import os
import sys
from pathlib import Path
def repo_root() -> Path:
return Path(__file__).resolve().parents[2]
def default_meta(pr_id: int) -> dict:
host = os.environ.get("CODE_REVIEW_GITCODE_HOST", "gitcode.com")
owner = os.environ.get("CODE_REVIEW_GITCODE_OWNER", "openeuler")
repo = os.environ.get("CODE_REVIEW_GITCODE_REPO", "yuanrong-datasystem")
upstream = os.environ.get(
"CODE_REVIEW_UPSTREAM_GIT",
f"https://{host}/{owner}/{repo}.git",
)
fork_url = os.environ.get(
"CODE_REVIEW_FORK_REMOTE_URL",
f"https://{host}/yaohaolin/{repo}.git",
)
fork_name = os.environ.get("CODE_REVIEW_FORK_REMOTE_NAME", "contributor")
branch_local = f"pr-{pr_id}-head"
return {
"pr_number": pr_id,
"pr_url": f"https://{host}/{owner}/{repo}/pull/{pr_id}",
"upstream_git": upstream,
"issues": {},
"related": {
"description": "",
"issues": [],
"rfcs": [],
"closes": [],
"comment": "Link this PR to GitCode issues and RFCs; used in review prompt traceability.",
},
"gitcode": {
"comment": "Optional. api_base_url defaults to https://api.gitcode.com.",
"api_base_url": "https://api.gitcode.com",
},
"notes": "Auto-generated by scripts/lib/bootstrap_meta.py (first run). "
"Set fork_fallback.branch from PR page if fetch fails; align base_ref/head_ref with the PR.",
"diff": {
"base_ref": "origin/master",
"head_ref": branch_local,
"exclude_paths": [],
"fetch": [
{
"comment": "Tries multiple refs; GitCode often needs fork_fallback.branch.",
"type": "pr_head",
"remote": "origin",
"pr": pr_id,
"local_branch": branch_local,
}
],
},
"fork_fallback": {
"comment": "If pr_head fails: set branch to the PR source/compare branch on GitCode.",
"remote_name": fork_name,
"remote_url": fork_url,
"branch": "REPLACE_WITH_SOURCE_BRANCH_FROM_PR_PAGE",
},
}
def write_meta(pr_id: int, root: Path, *, force: bool = False) -> Path:
out = root / "workspace" / "inputs" / f"pr-{pr_id}" / "meta.json"
if out.is_file() and not force:
return out
out.parent.mkdir(parents=True, exist_ok=True)
meta = default_meta(pr_id)
with open(out, "w", encoding="utf-8") as f:
json.dump(meta, f, indent=2, ensure_ascii=False)
f.write("\n")
print(f"Wrote {out}", file=sys.stderr)
return out
def main() -> None:
ap = argparse.ArgumentParser(description="Bootstrap workspace/inputs/pr-<id>/meta.json")
ap.add_argument("pr_id", type=int, help="PR number (folder id under workspace/inputs/)")
ap.add_argument(
"--repo-root",
type=Path,
default=None,
help="Repository root (default: parent of scripts/)",
)
ap.add_argument(
"--force",
action="store_true",
help="Overwrite existing meta.json",
)
args = ap.parse_args()
root = args.repo_root or repo_root()
write_meta(args.pr_id, root, force=args.force)
if __name__ == "__main__":
main()