|
7 | 7 | import xml.etree.ElementTree as ET |
8 | 8 | from pathlib import Path |
9 | 9 | from datetime import datetime, timezone |
| 10 | +import subprocess |
| 11 | +import tempfile |
| 12 | +import json |
| 13 | +import os |
10 | 14 |
|
11 | 15 | # ============================================================================= |
12 | 16 | # Utility Functions |
@@ -172,19 +176,15 @@ def find_line_number_of_change(original_content, old_value): |
172 | 176 | # ============================================================================= |
173 | 177 |
|
174 | 178 | def process_image_list(image_list): |
175 | | - """Process a list of images - should be URLs from gh CLI upload""" |
| 179 | + """Process a list of images - can be URLs or local file paths""" |
176 | 180 | if not image_list: |
177 | 181 | return [] |
178 | 182 |
|
179 | 183 | processed_images = [] |
180 | 184 | for img in image_list: |
181 | | - # Accept URLs as-is (these should be GitHub user-attachments URLs from gh CLI) |
182 | | - if img.startswith(('http://', 'https://')): |
183 | | - processed_images.append(img) |
184 | | - else: |
185 | | - # For local paths, warn the user |
186 | | - print(f"⚠️ Local file path provided: {img}") |
187 | | - print(f" Images must be uploaded via gh CLI first to get GitHub CDN URLs") |
| 185 | + # Accept both URLs and local file paths |
| 186 | + # Local paths will be handled by gh CLI when creating the comment |
| 187 | + processed_images.append(img) |
188 | 188 |
|
189 | 189 | return processed_images |
190 | 190 |
|
@@ -250,35 +250,61 @@ def create_pr_suggestion(repo_owner, repo_name, pr_number, calibration_file, xml |
250 | 250 | comment_body += f"\n\n" |
251 | 251 |
|
252 | 252 | if existing_comment_id: |
253 | | - # Update existing comment |
| 253 | + # Update existing comment using gh CLI |
254 | 254 | print(f"Updating existing comment {existing_comment_id}...") |
255 | | - update_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/comments/{existing_comment_id}" |
256 | | - update_data = {'body': comment_body} |
257 | | - response = requests.patch(update_url, headers=headers, json=update_data) |
258 | 255 |
|
259 | | - if response.status_code == 200: |
| 256 | + # Write comment body to temp file |
| 257 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f: |
| 258 | + f.write(comment_body) |
| 259 | + temp_file = f.name |
| 260 | + |
| 261 | + try: |
| 262 | + # gh api to update comment |
| 263 | + result = subprocess.run( |
| 264 | + ['gh', 'api', |
| 265 | + f'/repos/{repo_owner}/{repo_name}/issues/comments/{existing_comment_id}', |
| 266 | + '-X', 'PATCH', |
| 267 | + '-F', f'body=@{temp_file}'], |
| 268 | + capture_output=True, |
| 269 | + text=True, |
| 270 | + check=True |
| 271 | + ) |
260 | 272 | print("✅ Existing PR comment updated successfully") |
261 | | - return response.json() |
262 | | - else: |
263 | | - print(f"❌ Failed to update existing comment: {response.status_code}") |
264 | | - print(f" Response: {response.text}") |
| 273 | + return json.loads(result.stdout) |
| 274 | + except subprocess.CalledProcessError as e: |
| 275 | + print(f"❌ Failed to update existing comment: {e}") |
| 276 | + print(f" Error: {e.stderr}") |
265 | 277 | return None |
| 278 | + finally: |
| 279 | + os.unlink(temp_file) |
266 | 280 | else: |
267 | | - # Create new regular PR comment |
| 281 | + # Create new regular PR comment using gh CLI |
268 | 282 | print("Creating new PR comment...") |
269 | | - comment_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/{pr_number}/comments" |
270 | 283 |
|
271 | | - comment_data = {'body': comment_body} |
| 284 | + # Write comment body to temp file |
| 285 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f: |
| 286 | + f.write(comment_body) |
| 287 | + temp_file = f.name |
272 | 288 |
|
273 | | - response = requests.post(comment_url, headers=headers, json=comment_data) |
274 | | - |
275 | | - if response.status_code == 201: |
| 289 | + try: |
| 290 | + # gh pr comment will automatically upload local image files |
| 291 | + result = subprocess.run( |
| 292 | + ['gh', 'pr', 'comment', str(pr_number), |
| 293 | + '--repo', f'{repo_owner}/{repo_name}', |
| 294 | + '--body-file', temp_file], |
| 295 | + capture_output=True, |
| 296 | + text=True, |
| 297 | + check=True |
| 298 | + ) |
276 | 299 | print("✅ New PR comment created successfully") |
277 | | - return response.json() |
278 | | - else: |
279 | | - print(f"❌ Failed to create PR comment: {response.status_code}") |
280 | | - print(f" Response: {response.text}") |
| 300 | + # gh pr comment returns URL, parse to get comment data |
| 301 | + return {'html_url': result.stdout.strip()} |
| 302 | + except subprocess.CalledProcessError as e: |
| 303 | + print(f"❌ Failed to create PR comment: {e}") |
| 304 | + print(f" Error: {e.stderr}") |
281 | 305 | return None |
| 306 | + finally: |
| 307 | + os.unlink(temp_file) |
282 | 308 |
|
283 | 309 | def find_existing_bot_comment(repo_owner, repo_name, pr_number, bot_comment_base, xml_file, line_number, github_token): |
284 | 310 | """Find existing bot comment on the specific line""" |
|
0 commit comments