-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreview.py
More file actions
258 lines (203 loc) · 7.49 KB
/
review.py
File metadata and controls
258 lines (203 loc) · 7.49 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
import os
import json
import time
import requests
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
# -------------------------
# Configuration
# -------------------------
# File extensions to skip (binary files, generated code, etc.)
SKIP_EXTENSIONS = {
'.png', '.jpg', '.jpeg', '.gif', '.ico', '.svg', '.webp',
'.pdf', '.zip', '.tar', '.gz', '.tgz', '.rar',
'.exe', '.dll', '.so', '.dylib', '.bin', '.obj',
'.pyc', '.pyo', '.pyd', '.class', '.jar', '.war', '.ear',
'.o', '.a', '.lib', '.dll', '.so', '.dylib',
'.mo', '.pot', '.db', '.sqlite', '.sqlite3',
'.woff', '.woff2', '.ttf', '.eot',
}
# Directory patterns to skip
SKIP_DIRS = {
'node_modules', 'dist', 'build', '.git', 'vendor', 'Pods',
'.gradle', 'target', '__pycache__', '.venv', 'venv', 'env',
'bin', 'obj', '.idea', '.vscode', '.cache',
}
# Max number of comments to post (safety limit)
MAX_COMMENTS_PER_PR = 50
# GitHub API rate limit retry settings
MAX_RETRIES = 3
RETRY_DELAY = 2 # seconds (will be multiplied: 2, 4, 8)
# -------------------------
# Helper Functions
# -------------------------
def should_skip_file(filename):
"""Check if a file should be skipped based on extension or directory."""
# Check extension
ext = os.path.splitext(filename)[1].lower()
if ext in SKIP_EXTENSIONS:
return True
# Check if any skip directory is in the path
path_parts = filename.split('/')
for part in path_parts:
if part in SKIP_DIRS:
return True
# Skip common generated/minified patterns
basename = os.path.basename(filename).lower()
if any(pattern in basename for pattern in ['min.', '.min.', 'bundle.']):
return True
return False
def make_github_request(url, headers, params=None):
"""Make a request to GitHub API with rate limit handling and retry."""
for attempt in range(MAX_RETRIES):
try:
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
return response
# Handle rate limiting
if response.status_code in (403, 429):
remaining = response.headers.get('X-RateLimit-Remaining', '1')
reset_time = response.headers.get('X-RateLimit-Reset')
if remaining == '0' or response.status_code == 429:
wait_time = RETRY_DELAY * (2 ** attempt) # Exponential backoff
if reset_time:
# GitHub provides Unix timestamp when rate limit resets
reset_epoch = int(reset_time)
current_time = int(time.time())
wait_time = max(reset_epoch - current_time + 1, wait_time)
print(f"Rate limit hit (attempt {attempt + 1}/{MAX_RETRIES}). Waiting {wait_time}s...")
time.sleep(wait_time)
continue
# Other errors
response.raise_for_status()
except requests.exceptions.RequestException as e:
if attempt == MAX_RETRIES - 1:
raise
wait_time = RETRY_DELAY * (2 ** attempt)
print(f"Request failed: {e}. Retrying in {wait_time}s...")
time.sleep(wait_time)
raise Exception(f"Failed after {MAX_RETRIES} retries")
# -------------------------
# Main Review Function
# -------------------------
def process_review(repo: str, pr_number: int, installation_id: int):
"""
Process a PR review using GitHub App authentication.
Args:
repo: Repository name (e.g., "owner/repo")
pr_number: Pull request number
installation_id: GitHub App installation ID
"""
from github_app import GitHubAppAuth
print(f"Reviewing PR #{pr_number} in {repo}")
# Get GitHub App auth token
auth = GitHubAppAuth(
app_id=os.environ["GITHUB_APP_ID"],
private_key_path=os.environ["GITHUB_PRIVATE_KEY_PATH"],
installation_id=installation_id
)
token = auth.get_installation_token()
# Setup headers with installation token
headers = {
"Authorization": f"token {token}",
"Accept": "application/vnd.github+json"
}
# -------------------------
# Get changed files
# -------------------------
files_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}/files"
try:
response = make_github_request(files_url, headers)
files = response.json()
except Exception as e:
print(f"Failed to fetch files from GitHub: {e}")
return
# Filter files before review
filtered_files = []
skipped_count = 0
for file in files:
filename = file["filename"]
if should_skip_file(filename):
skipped_count += 1
print(f"Skipping {filename} (filtered)")
continue
filtered_files.append(file)
total_files = len(filtered_files)
print(f"{total_files} files to review (skipped {skipped_count})")
# -------------------------
# Review each file
# -------------------------
comments = []
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
for file in filtered_files:
filename = file["filename"]
if "patch" not in file:
continue
patch = file["patch"]
print(f"Reviewing {filename}")
prompt = f"""
You are a senior software engineer reviewing a pull request.
Review the following git diff for bugs, security issues, or bad practices.
File: {filename}
Diff:
{patch}
Return JSON in this format:
{{
"comments": [
{{
"line": <line number>,
"message": "<review comment>"
}}
]
}}
If there are no issues, return:
{{ "comments": [] }}
"""
try:
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{"role": "system", "content": "You are a senior software engineer with extreme technical expertise."},
{"role": "user", "content": prompt}
],
temperature=0
)
result = response.choices[0].message.content
data = json.loads(result)
for c in data["comments"]:
comments.append({
"path": filename,
"line": c["line"],
"body": c["message"]
})
except Exception as e:
print(f"LLM review failed for {filename}: {e}")
# -------------------------
# Get commit SHA
# -------------------------
pr_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
try:
pr_data = make_github_request(pr_url, headers).json()
commit_id = pr_data["head"]["sha"]
except Exception as e:
print(f"Failed to fetch PR data: {e}")
return
# -------------------------
# Post comments to PR
# -------------------------
for comment in comments:
print(f"Comment on {comment['path']}:{comment['line']}: {comment['body']}")
comment_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}/comments"
payload = {
"body": comment["body"],
"commit_id": commit_id,
"path": comment["path"],
"line": comment["line"]
}
try:
response = requests.post(comment_url, headers=headers, json=payload)
response.raise_for_status()
except Exception as e:
print(f"Failed to post comment: {e}")
print(f"Review complete: {len(comments)} comments posted.")