-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_pr.py
More file actions
261 lines (224 loc) · 7.7 KB
/
github_pr.py
File metadata and controls
261 lines (224 loc) · 7.7 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
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
"""
GitHub PR Tools - Read and comment on pull requests.
"""
import subprocess
from ..base import BaseTool, ToolResult
from ..registry import register_tool
from .base import validate_project_root
@register_tool
class GitHubPRReadTool(BaseTool):
"""List and view GitHub pull requests."""
@property
def name(self) -> str:
return "github_pr_read"
@property
def description(self) -> str:
return (
"Read GitHub pull requests. List PRs with filters or view a specific PR. "
"Can filter by state, base branch, author, and labels."
)
@property
def input_schema(self) -> dict:
return {
"type": "object",
"properties": {
"pr_number": {
"type": "integer",
"description": "View a specific PR by number",
},
"state": {
"type": "string",
"enum": ["open", "closed", "merged", "all"],
"description": "Filter by state (default: open)",
},
"base": {
"type": "string",
"description": "Filter by base branch",
},
"author": {
"type": "string",
"description": "Filter by author username",
},
"label": {
"type": "string",
"description": "Filter by label",
},
"search": {
"type": "string",
"description": "Search in title and body",
},
"limit": {
"type": "integer",
"description": "Number of PRs to list (default: 30)",
},
},
}
@property
def requires_grant_metadata(self) -> list[str]:
return ["project_root"]
def credential_keys(self) -> list[str]:
return []
async def execute(
self,
pr_number: int = None,
state: str = "open",
base: str = None,
author: str = None,
label: str = None,
search: str = None,
limit: int = 30,
**kwargs
) -> ToolResult:
project_root = self.get_grant_metadata("project_root")
valid, error = validate_project_root(project_root)
if not valid:
return ToolResult.fail(error)
try:
if pr_number:
return await self._view_pr(project_root, pr_number)
else:
return await self._list_prs(
project_root, state, base, author, label, search, limit
)
except FileNotFoundError:
return ToolResult.fail("GitHub CLI (gh) not installed")
except subprocess.TimeoutExpired:
return ToolResult.fail("Command timed out")
except Exception as e:
return ToolResult.fail(f"GitHub error: {str(e)}")
async def _list_prs(
self,
project_root: str,
state: str,
base: str = None,
author: str = None,
label: str = None,
search: str = None,
limit: int = 30,
) -> ToolResult:
"""List pull requests with filters."""
cmd = ["gh", "pr", "list", f"--state={state}", f"--limit={limit}"]
if base:
cmd.extend(["--base", base])
if author:
cmd.extend(["--author", author])
if label:
cmd.extend(["--label", label])
if search:
cmd.extend(["--search", search])
result = subprocess.run(
cmd,
cwd=project_root,
capture_output=True,
text=True,
timeout=30,
)
if result.returncode != 0:
stderr = result.stderr.lower()
if "not a git repository" in stderr:
return ToolResult.fail("Not a git repository")
if "no pull requests" in stderr or "no results" in stderr:
return ToolResult.ok({
"prs": [],
"message": "No pull requests found",
})
return ToolResult.fail(f"gh error: {result.stderr}")
output = result.stdout.strip()
if not output:
return ToolResult.ok({
"prs": [],
"message": "No pull requests found",
})
lines = output.split('\n')
return ToolResult.ok({
"state": state,
"count": len(lines),
"prs": output,
})
async def _view_pr(self, project_root: str, pr_number: int) -> ToolResult:
"""View a specific PR with comments."""
result = subprocess.run(
["gh", "pr", "view", str(pr_number), "--comments"],
cwd=project_root,
capture_output=True,
text=True,
timeout=30,
)
if result.returncode != 0:
stderr = result.stderr.lower()
if "not found" in stderr or "could not find" in stderr:
return ToolResult.fail(f"PR #{pr_number} not found")
return ToolResult.fail(f"gh error: {result.stderr}")
return ToolResult.ok({
"pr_number": pr_number,
"content": result.stdout.strip(),
})
@register_tool
class GitHubPRCommentTool(BaseTool):
"""Comment on a GitHub pull request."""
@property
def name(self) -> str:
return "github_pr_comment"
@property
def description(self) -> str:
return (
"Add a comment to a GitHub pull request. "
"Requires approval from the gatekeeper."
)
@property
def input_schema(self) -> dict:
return {
"type": "object",
"properties": {
"pr_number": {
"type": "integer",
"description": "PR number to comment on",
},
"body": {
"type": "string",
"description": "Comment text",
},
},
"required": ["pr_number", "body"],
}
@property
def requires_grant_metadata(self) -> list[str]:
return ["project_root"]
def credential_keys(self) -> list[str]:
return []
async def execute(
self,
pr_number: int,
body: str,
**kwargs
) -> ToolResult:
project_root = self.get_grant_metadata("project_root")
valid, error = validate_project_root(project_root)
if not valid:
return ToolResult.fail(error)
if not body or not body.strip():
return ToolResult.fail("Comment body cannot be empty")
try:
result = subprocess.run(
["gh", "pr", "comment", str(pr_number), "--body", body],
cwd=project_root,
capture_output=True,
text=True,
timeout=30,
)
if result.returncode != 0:
stderr = result.stderr.lower()
if "not found" in stderr or "could not find" in stderr:
return ToolResult.fail(f"PR #{pr_number} not found")
return ToolResult.fail(f"gh error: {result.stderr}")
return ToolResult.ok({
"pr_number": pr_number,
"status": "commented",
"message": f"Comment added to PR #{pr_number}",
})
except FileNotFoundError:
return ToolResult.fail("GitHub CLI (gh) not installed")
except subprocess.TimeoutExpired:
return ToolResult.fail("Command timed out")
except Exception as e:
return ToolResult.fail(f"GitHub error: {str(e)}")