-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub_tool.py
More file actions
105 lines (88 loc) · 3.47 KB
/
github_tool.py
File metadata and controls
105 lines (88 loc) · 3.47 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
105
import subprocess
import json
import time
import socket
from typing import Optional, List, Dict
import re
class GitHubTool:
def __init__(self, base_branch: str, max_retries: int, logger):
self.base_branch = base_branch
self.max_retries = max_retries
self.logger = logger
self.retry_delay = 5
def wait_for_internet(self) -> bool:
"""Wait for internet connection"""
try:
socket.create_connection(("api.github.com", 443), timeout=5)
return True
except OSError:
return False
def run(self, command: List[str]) -> subprocess.CompletedProcess:
"""Run GitHub CLI command with retries"""
for attempt in range(self.max_retries):
if not self.wait_for_internet():
time.sleep(5)
continue
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
return result
self.logger.warning(
f"Attempt {attempt+1}/{self.max_retries} failed: {' '.join(command)}"
)
if attempt < self.max_retries - 1:
time.sleep(self.retry_delay * (2 ** attempt))
except subprocess.TimeoutExpired:
self.logger.warning(f"Command timed out on attempt {attempt+1}")
if attempt < self.max_retries - 1:
time.sleep(self.retry_delay * (2 ** attempt))
raise Exception(f"Command failed after {self.max_retries} attempts: {' '.join(command)}")
def check_rate_limit(self) -> Dict:
"""Check GitHub API rate limit"""
try:
result = self.run(["gh", "api", "rate_limit"])
data = json.loads(result.stdout)
core = data.get("resources", {}).get("core", {})
remaining = core.get("remaining", 5000)
reset_time = core.get("reset", 0)
print(f"🔎 GitHub API Remaining: {remaining}")
return {
"remaining": remaining,
"reset_time": reset_time
}
except Exception as e:
print(f"⚠ Rate limit check failed: {e}")
return {"remaining": 5000, "reset_time": 0}
def create_pr(self, title: str, body: str, head: str) -> Optional[str]:
"""Create a pull request"""
try:
result = self.run([
"gh", "pr", "create",
"--title", title,
"--body", body,
"--base", self.base_branch,
"--head", head
])
match = re.search(r'/(\d+)$', result.stdout.strip())
if match:
pr_number = match.group(1)
self.logger.info(f"Created PR #{pr_number}")
return pr_number
return None
except Exception as e:
self.logger.error(f"Failed to create PR: {e}")
raise
def merge_pr(self, branch: str) -> bool:
"""Merge a pull request"""
try:
self.run(["gh", "pr", "merge", branch, "--merge", "--auto"])
self.logger.info(f"Merged PR {branch}")
return True
except Exception as e:
self.logger.error(f"Failed to merge PR {branch}: {e}")
return False