-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
63 lines (47 loc) · 1.98 KB
/
Copy pathgithub.py
File metadata and controls
63 lines (47 loc) · 1.98 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
import json
from pathlib import Path
import subprocess
def run_gh(repo: str, *args: str) -> str:
cmd = ["gh", "--repo", repo] + list(args)
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"gh failed: {result.stderr}")
return result.stdout
def run_gh_api(api_path: str) -> str:
cmd = ["gh", "api", api_path, "--paginate"]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"gh api failed: {result.stderr}")
return result.stdout
def fetch_pr_meta(repo: str, pr_number: int) -> dict:
return json.loads(
run_gh(
repo,
"pr",
"view",
str(pr_number),
"--json",
"number,title,body,url,state,author,baseRefName,headRefName",
)
)
def fetch_comments(repo: str, pr_number: int) -> list:
output = run_gh_api(f"repos/{repo}/issues/{pr_number}/comments")
return json.loads(output) if output.strip() else []
def fetch_reviews(repo: str, pr_number: int) -> list:
output = run_gh_api(f"repos/{repo}/pulls/{pr_number}/reviews")
return json.loads(output) if output.strip() else []
def fetch_review_comments(repo: str, pr_number: int) -> list:
output = run_gh_api(f"repos/{repo}/pulls/{pr_number}/comments")
return json.loads(output) if output.strip() else []
def publish_comment(repo: str, pr_number: int, body_file: str) -> None:
path = Path(body_file)
if not path.exists():
raise FileNotFoundError(f"Comment file not found: {body_file}")
content = path.read_text()
if not content.strip():
raise ValueError("Comment file is empty")
run_gh(repo, "pr", "comment", str(pr_number), "--body-file", str(path))
def update_pr_title(repo: str, pr_number: int, title: str) -> None:
if not title.strip():
raise ValueError("PR title is empty")
run_gh(repo, "pr", "edit", str(pr_number), "--title", title)