-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodereview.sh
More file actions
executable file
·614 lines (533 loc) · 23.6 KB
/
codereview.sh
File metadata and controls
executable file
·614 lines (533 loc) · 23.6 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/bin/bash
# codereview.sh - Unified Automated Code Review Script (Claude, Gemini & Codex)
# Usage: codereview.sh review.md <URL>
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
print_status() {
local status="$1"
local message="$2"
case $status in
"info") echo -e "${CYAN}\u2139${NC} $message" ;;
"success") echo -e "${GREEN}\u2705${NC} $message" ;;
"warning") echo -e "${YELLOW}\u26a0\ufe0f${NC} $message" ;;
"error") echo -e "${RED}\u274c${NC} $message" ;;
"progress") echo -e "${BLUE}\ud83d\udd04${NC} $message" ;;
esac
}
# Parse arguments including debug flag
DEBUG_MODE=false
CONTEXT_FILE=""
URLS=()
while [[ $# -gt 0 ]]; do
case $1 in
--debug)
DEBUG_MODE=true
shift
;;
-*)
print_status "error" "Unknown option: $1"
exit 1
;;
*)
if [ -z "$CONTEXT_FILE" ]; then
CONTEXT_FILE="$1"
else
URLS+=("$1")
fi
shift
;;
esac
done
# Argument check
if [ -z "$CONTEXT_FILE" ] || [ ${#URLS[@]} -eq 0 ]; then
echo "Usage: $(basename $0) [--debug] review.md <URL1> [URL2] [URL3] ..."
echo "Examples:"
echo " Single PR: $(basename $0) review.md https://github.com/user/repo/pull/123"
echo " Multiple PRs: $(basename $0) review.md https://github.com/user/repo1/pull/123 https://github.com/user/repo2/pull/456"
echo " Debug mode: $(basename $0) --debug review.md https://github.com/user/repo/pull/123"
exit 1
fi
if [ "$DEBUG_MODE" = true ]; then
print_status "info" "🐛 Debug mode enabled"
fi
# Handle relative vs absolute paths for context file
if [[ "$CONTEXT_FILE" != /* ]]; then
CONTEXT_FILE="$(pwd)/$CONTEXT_FILE"
fi
# Check if context file exists
if [ ! -f "$CONTEXT_FILE" ]; then
print_status "error" "Context file '$CONTEXT_FILE' not found"
exit 1
fi
# Tool detection
HAS_CLAUDE=false
HAS_GEMINI=false
HAS_CODEX=false
if command -v claude &>/dev/null; then
HAS_CLAUDE=true
fi
if command -v gemini &>/dev/null; then
HAS_GEMINI=true
fi
if command -v codex &>/dev/null; then
HAS_CODEX=true
fi
if ! $HAS_CLAUDE && ! $HAS_GEMINI && ! $HAS_CODEX; then
print_status "error" "No supported CLI found (Claude, Gemini, or Codex). Please install at least one."
exit 1
fi
# Select tool if multiple are present
AVAILABLE_TOOLS=()
if $HAS_CLAUDE; then
AVAILABLE_TOOLS+=("claude")
fi
if $HAS_GEMINI; then
AVAILABLE_TOOLS+=("gemini")
fi
if $HAS_CODEX; then
AVAILABLE_TOOLS+=("codex")
fi
# Helper function to select CLI tool interactively or by default
select_cli_tool() {
local tools=("${AVAILABLE_TOOLS[@]}")
if [ ${#tools[@]} -gt 1 ]; then
print_status "info" "Multiple CLIs detected: ${tools[*]}"
echo "Select which CLI to use for the review:"
select TOOL in "${tools[@]}"; do
case $TOOL in
claude|gemini|codex) SELECTED_TOOL="$TOOL"; break ;;
*) echo "Invalid option. Please select a valid number." ;;
esac
done
else
SELECTED_TOOL="${tools[0]}"
print_status "info" "Using $SELECTED_TOOL CLI."
fi
}
# Check if a preferred CLI is specified via environment variable
if [ -n "$PREFERRED_CLI" ]; then
# Validate that the preferred CLI is available
if [[ " ${AVAILABLE_TOOLS[*]} " =~ " $PREFERRED_CLI " ]]; then
SELECTED_TOOL="$PREFERRED_CLI"
print_status "info" "Using preferred CLI: $SELECTED_TOOL"
else
print_status "warning" "Preferred CLI '$PREFERRED_CLI' not available. Available CLIs: ${AVAILABLE_TOOLS[*]}"
select_cli_tool
fi
elif [ ${#AVAILABLE_TOOLS[@]} -gt 1 ]; then
select_cli_tool
else
SELECTED_TOOL="${AVAILABLE_TOOLS[0]}"
print_status "info" "Using $SELECTED_TOOL CLI."
fi
# Config and prompt logic
if [ "$SELECTED_TOOL" = "claude" ]; then
CLAUDE_CONFIG="$HOME/.claude.json"
if [ ! -f "$CLAUDE_CONFIG" ]; then
print_status "error" "Claude config file '$CLAUDE_CONFIG' not found"
print_status "info" "Please ensure you have Claude Code configured with MCP servers"
exit 1
fi
if ! grep -q '"github"' "$CLAUDE_CONFIG"; then
print_status "warning" "GitHub MCP not found in Claude config"
print_status "info" "Please configure GitHub MCP first with: claude mcp add github"
fi
elif [ "$SELECTED_TOOL" = "gemini" ]; then
GEMINI_CONFIG="$HOME/.gemini/settings.json"
if [ ! -f "$GEMINI_CONFIG" ]; then
print_status "error" "Gemini config file '$GEMINI_CONFIG' not found"
print_status "info" "Please ensure you have Gemini configured with MCP servers"
exit 1
fi
if ! grep -q '"github"' "$GEMINI_CONFIG"; then
print_status "warning" "GitHub MCP not found in Gemini config"
print_status "info" "Please configure GitHub MCP first in .gemini/settings.json"
fi
elif [ "$SELECTED_TOOL" = "codex" ]; then
# Codex config verification
CODEX_CONFIG="$HOME/.codex/config.toml"
if [ ! -f "$CODEX_CONFIG" ]; then
print_status "error" "Codex config file '$CODEX_CONFIG' not found"
print_status "info" "Please ensure you have Codex configured with MCP servers"
print_status "info" "Create config with: mkdir -p ~/.codex && codex init"
exit 1
fi
# Check if GitHub MCP is configured in TOML format
if ! grep -q -i "github" "$CODEX_CONFIG"; then
print_status "warning" "GitHub MCP not found in Codex config"
print_status "info" "Please configure GitHub MCP in your ~/.codex/config.toml file"
print_status "info" "Add MCP server configuration for GitHub in your TOML config"
fi
fi
# Validate URLs format before processing
print_status "info" "Validating ${#URLS[@]} URL(s)..."
for url in "${URLS[@]}"; do
if [[ ! $url =~ github\.com/([^/]+)/([^/]+)/pull/([0-9]+) ]]; then
print_status "error" "Invalid GitHub PR URL format: $url"
exit 1
fi
done
print_status "success" "All URLs validated successfully"
print_status "success" "Prerequisites check passed"
# Prompt generation functions
execute_automated_claude_review() {
local context_content
context_content=$(cat "$CONTEXT_FILE" | sed "s|{URL_PARAMETER}|{URL_PARAMETER}|g")
cat << EOF
## 🚨 CRITICAL TOOL RESTRICTIONS
**MANDATORY**: You MUST ONLY use GitHub MCP server tools. DO NOT use any bash commands, CLI tools, or external commands.
**FORBIDDEN COMMANDS**:
- gh (GitHub CLI)
- curl
- git commands
- bash commands for GitHub operations
**REQUIRED**: Use ONLY these GitHub MCP tools:
- github:get_pull_request
- github:get_pull_request_files
- github:get_file_contents
- github:create_pull_request_review
---
$context_content
## 🤖 AUTOMATED EXECUTION MODE
### Required Actions (Execute in sequence):
1. **Parse URL**: Extract owner, repo, and pull request number from the provided GitHub PR URL
2. **Get PR Details**: Use GitHub MCP tool \`github:get_pull_request\` with the extracted owner, repo, and pull_number
3. **Get PR Files**: Use GitHub MCP tool \`github:get_pull_request_files\` with the extracted owner, repo, and pull_number
4. **Analyze Key Files**: Use GitHub MCP tool \`github:get_file_contents\` for the most important changed files (max 3-5 files)
5. **Generate Review**: Create comprehensive code review following the specified format (DO NOT include any footer or signature such as "Generated with Claude Code")
6. **Post Review**: Use GitHub MCP tool \`github:create_pull_request_review\` with the extracted owner, repo, and pull_number
- body: [Complete review content]
- event: "COMMENT" or "REQUEST_CHANGES" or "APPROVE" based on findings
### Output Format Required:
\`\`\`json
{
\"status\": \"success|error\",
\"review_posted\": true|false,
\"review_id\": \"review_id_if_successful\",
\"event_type\": \"COMMENT|REQUEST_CHANGES|APPROVE\",
\"files_analyzed\": number,
\"issues_found\": number,
\"summary\": \"Brief summary of what was done\"
}
\`\`\`
**IMPORTANT**: Execute all steps automatically using ONLY GitHub MCP tools.
**CRITICAL**: Do NOT add any footer, signature, or attribution like "Generated with Claude Code" at the end of your review. The review should end with the Summary section only.
---
## 🚀 START AUTOMATED REVIEW NOW
EOF
}
execute_automated_gemini_review() {
local context_content
context_content=$(cat "$CONTEXT_FILE" | sed "s|{URL_PARAMETER}|{URL_PARAMETER}|g")
cat << EOF
## 🚨 CRITICAL TOOL RESTRICTIONS
**MANDATORY**: You MUST ONLY use GitHub MCP server tools. DO NOT use any bash commands, CLI tools, or external commands.
**FORBIDDEN COMMANDS**:
- gh (GitHub CLI)
- curl
- git commands
- bash commands for GitHub operations
**REQUIRED**: Use ONLY these GitHub MCP tools:
- github:get_pull_request OR mcp_github_get_pull_request
- github:get_pull_request_files OR mcp_github_get_pull_request_files
- github:get_file_contents OR mcp_github_get_file_contents
- github:create_pull_request_review OR mcp_github_create_pull_request_review
---
$context_content
## 🤖 AUTOMATED EXECUTION MODE
### Required Actions (Execute in sequence):
1. **Parse URL**: Extract owner, repo, and pull request number from the provided GitHub PR URL
2. **Get PR Details**: Use available GitHub MCP tool (try \`github:get_pull_request\` first, if not available use \`mcp_github_get_pull_request\`) with the extracted owner, repo, and pull_number
3. **Get PR Files**: Use available GitHub MCP tool (try \`github:get_pull_request_files\` first, if not available use \`mcp_github_get_pull_request_files\`) with the extracted owner, repo, and pull_number
4. **Analyze Key Files**: Use available GitHub MCP tool (try \`github:get_file_contents\` first, if not available use \`mcp_github_get_file_contents\`) for the most important changed files (max 3-5 files)
5. **Generate Review**: Create comprehensive code review following the specified format (DO NOT include any footer or signature such as "Generated with Gemini")
6. **Post Review**: Use available GitHub MCP tool (try \`github:create_pull_request_review\` first, if not available use \`mcp_github_create_pull_request_review\`) with the extracted owner, repo, and pull_number
- body: [Complete review content]
- event: "COMMENT" or "REQUEST_CHANGES" or "APPROVE" based on findings
### Output Format Required:
\`\`\`json
{
\"status\": \"success|error\",
\"review_posted\": true|false,
\"review_id\": \"review_id_if_successful\",
\"event_type\": \"COMMENT|REQUEST_CHANGES|APPROVE\",
\"files_analyzed\": number,
\"issues_found\": number,
\"summary\": \"Brief summary of what was done\"
}
\`\`\`
**IMPORTANT**: Execute all steps automatically using ONLY GitHub MCP tools.
**CRITICAL**: Do NOT add any footer, signature, or attribution like "Generated with Gemini" at the end of your review. The review should end with the Summary section only.
---
## 🚀 START AUTOMATED REVIEW NOW
EOF
}
execute_automated_codex_review() {
local context_content
context_content=$(cat "$CONTEXT_FILE" | sed "s|{URL_PARAMETER}|{URL_PARAMETER}|g")
cat << EOF
## 🚨 CRITICAL TOOL RESTRICTIONS
**MANDATORY**: You MUST ONLY use GitHub MCP server tools. DO NOT use any bash commands, CLI tools, or external commands.
**FORBIDDEN COMMANDS**:
- gh (GitHub CLI)
- curl
- git commands
- bash commands for GitHub operations
**REQUIRED**: Use ONLY these GitHub MCP tools:
- github:get_pull_request OR mcp_github_get_pull_request
- github:get_pull_request_files OR mcp_github_get_pull_request_files
- github:get_file_contents OR mcp_github_get_file_contents
- github:create_pull_request_review OR mcp_github_create_pull_request_review
---
$context_content
## 🤖 AUTOMATED EXECUTION MODE
### GitHub Information
- **URL**: $url
- **Owner**: $owner
- **Repository**: $repo
- **Pull Request**: #$pr_number
### Required Actions (Execute in sequence):
1. **Get PR Details**: Use available GitHub MCP tool (try \`github:get_pull_request\` first, if not available use \`mcp_github_get_pull_request\`) with owner: "$owner", repo: "$repo", pull_number: $pr_number
2. **Get PR Files**: Use available GitHub MCP tool (try \`github:get_pull_request_files\` first, if not available use \`mcp_github_get_pull_request_files\`) with owner: "$owner", repo: "$repo", pull_number: $pr_number
3. **Analyze Key Files**: Use available GitHub MCP tool (try \`github:get_file_contents\` first, if not available use \`mcp_github_get_file_contents\`) for the most important changed files (max 3-5 files)
4. **Generate Review**: Create comprehensive code review following the specified format (DO NOT include any footer or signature such as "Generated with Codex")
5. **Post Review**: Use available GitHub MCP tool (try \`github:create_pull_request_review\` first, if not available use \`mcp_github_create_pull_request_review\`) with:
- owner: "$owner"
- repo: "$repo"
- pull_number: $pr_number
- body: [Complete review content]
- event: "COMMENT" or "REQUEST_CHANGES" or "APPROVE" based on findings
### Output Format Required:
\`\`\`json
{
\"status\": \"success|error\",
\"review_posted\": true|false,
\"review_id\": \"review_id_if_successful\",
\"event_type\": \"COMMENT|REQUEST_CHANGES|APPROVE\",
\"files_analyzed\": number,
\"issues_found\": number,
\"summary\": \"Brief summary of what was done\"
}
\`\`\`
**IMPORTANT**: Execute all steps automatically using ONLY GitHub MCP tools.
**CRITICAL**: Do NOT add any footer, signature, or attribution like "Generated with Codex" at the end of your review. The review should end with the Summary section only.
---
## 🚀 START AUTOMATED REVIEW NOW
EOF
}
# Initialize counters for summary report
TOTAL_PRS=${#URLS[@]}
SUCCESSFUL_REVIEWS=0
FAILED_REVIEWS=0
REVIEW_RESULTS=()
# Generate single reusable prompt file
PROMPT_FILE=""
# Try to create temporary file with better error handling
if command -v mktemp &> /dev/null; then
PROMPT_FILE="$(mktemp /tmp/codereview_prompt.XXXXXX.md 2>/dev/null)"
if [ -z "$PROMPT_FILE" ]; then
# Fallback: try without .md extension
PROMPT_FILE="$(mktemp /tmp/codereview_prompt.XXXXXX 2>/dev/null)"
if [ -z "$PROMPT_FILE" ]; then
# Last resort: use timestamp-based filename
PROMPT_FILE="/tmp/codereview_prompt_$(date +%s)_$$.md"
touch "$PROMPT_FILE" || {
print_status "error" "Failed to create temporary file in /tmp"
exit 1
}
fi
fi
else
# mktemp not available, use timestamp-based filename
PROMPT_FILE="/tmp/codereview_prompt_$(date +%s)_$$.md"
touch "$PROMPT_FILE" || {
print_status "error" "Failed to create temporary file in /tmp"
exit 1
}
fi
# Set up cleanup trap
cleanup_temp_file() {
if [ -n "$PROMPT_FILE" ] && [ -f "$PROMPT_FILE" ]; then
rm -f "$PROMPT_FILE"
fi
}
trap cleanup_temp_file EXIT INT TERM
print_status "progress" "Generating reusable review prompt template..."
if [ "$SELECTED_TOOL" = "claude" ]; then
execute_automated_claude_review > "$PROMPT_FILE"
elif [ "$SELECTED_TOOL" = "gemini" ]; then
execute_automated_gemini_review > "$PROMPT_FILE"
elif [ "$SELECTED_TOOL" = "codex" ]; then
execute_automated_codex_review > "$PROMPT_FILE"
fi
# Verify prompt file was created successfully
if [ -z "$PROMPT_FILE" ] || [ ! -f "$PROMPT_FILE" ]; then
print_status "error" "Failed to create prompt template file"
exit 1
fi
print_status "success" "Prompt template created successfully"
print_status "info" "Starting batch review process for $TOTAL_PRS Pull Request(s) using $SELECTED_TOOL"
# Process each URL
for i in "${!URLS[@]}"; do
URL="${URLS[$i]}"
CURRENT_PR=$((i + 1))
print_status "progress" "[$CURRENT_PR/$TOTAL_PRS] Processing PR: $URL"
# Extract repo info for current URL
if [[ $URL =~ github\.com/([^/]+)/([^/]+)/pull/([0-9]+) ]]; then
OWNER="${BASH_REMATCH[1]}"
REPO="${BASH_REMATCH[2]}"
PR_NUMBER="${BASH_REMATCH[3]}"
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] Analyzing: $OWNER/$REPO PR #$PR_NUMBER"
else
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] Invalid URL format: $URL"
FAILED_REVIEWS=$((FAILED_REVIEWS + 1))
REVIEW_RESULTS+=("❌ $URL - Invalid URL format")
continue
fi
# Use the shared prompt file with current PR URL as parameter
# Execute with selected CLI
print_status "progress" "[$CURRENT_PR/$TOTAL_PRS] Executing $SELECTED_TOOL for PR #$PR_NUMBER..."
SUCCESS=false
REVIEW_OUTPUT=""
if [ "$SELECTED_TOOL" = "claude" ]; then
cd "$HOME"
if [ "$DEBUG_MODE" = true ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Current directory: $(pwd)"
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Claude config exists: $([ -f "$HOME/.claude.json" ] && echo "yes" || echo "no")"
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Prompt file size: $(wc -c < "$PROMPT_FILE") bytes"
fi
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] Executing Claude CLI..."
REVIEW_OUTPUT=$(claude "$(cat "$PROMPT_FILE")
## 🎯 CURRENT PULL REQUEST TO REVIEW:
**URL**: $URL
Please analyze this specific Pull Request URL." 2>&1)
CLAUDE_EXIT_CODE=$?
if [ "$DEBUG_MODE" = true ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Claude exit code: $CLAUDE_EXIT_CODE"
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Output length: ${#REVIEW_OUTPUT} characters"
fi
echo "$REVIEW_OUTPUT"
if [ $CLAUDE_EXIT_CODE -eq 0 ]; then
SUCCESS=true
else
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] Claude CLI failed with exit code: $CLAUDE_EXIT_CODE"
# Check for common error patterns in output
if echo "$REVIEW_OUTPUT" | grep -qi "authentication\|unauthorized\|access denied\|token"; then
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] 🔒 Authentication issue detected - check GitHub token/permissions"
elif echo "$REVIEW_OUTPUT" | grep -qi "not found\|404\|does not exist"; then
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] 🔍 Resource not found - check if PR exists and is accessible"
elif echo "$REVIEW_OUTPUT" | grep -qi "rate limit\|too many requests"; then
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] ⏰ Rate limit exceeded - wait before retrying"
elif echo "$REVIEW_OUTPUT" | grep -qi "network\|connection\|timeout"; then
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] 🌐 Network connectivity issue"
elif echo "$REVIEW_OUTPUT" | grep -qi "mcp.*not found\|server.*not available"; then
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] ⚙️ MCP server issue - check GitHub MCP configuration"
fi
fi
elif [ "$SELECTED_TOOL" = "gemini" ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] Executing Gemini CLI..."
REVIEW_OUTPUT=$(gemini -p "$(cat "$PROMPT_FILE")
## 🎯 CURRENT PULL REQUEST TO REVIEW:
**URL**: $URL
Please analyze this specific Pull Request URL." 2>&1)
GEMINI_EXIT_CODE=$?
echo "$REVIEW_OUTPUT"
if [ $GEMINI_EXIT_CODE -eq 0 ]; then
SUCCESS=true
else
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] Gemini CLI failed with exit code: $GEMINI_EXIT_CODE"
fi
elif [ "$SELECTED_TOOL" = "codex" ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] Executing Codex CLI..."
REVIEW_OUTPUT=$(codex "$(cat "$PROMPT_FILE")
## 🎯 CURRENT PULL REQUEST TO REVIEW:
**URL**: $URL
Please analyze this specific Pull Request URL." 2>&1)
CODEX_EXIT_CODE=$?
echo "$REVIEW_OUTPUT"
if [ $CODEX_EXIT_CODE -eq 0 ]; then
SUCCESS=true
else
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] Codex CLI failed with exit code: $CODEX_EXIT_CODE"
fi
fi
# Verify review was actually posted by checking output
REVIEW_POSTED=false
if [ "$SUCCESS" = true ] && [ -n "$REVIEW_OUTPUT" ]; then
if [ "$DEBUG_MODE" = true ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Checking output for review posting indicators..."
fi
# Check for indicators that review was posted successfully
if echo "$REVIEW_OUTPUT" | grep -qE "(review_posted.*true|review.*created|successfully.*posted|Review posted|review_id)" || \
echo "$REVIEW_OUTPUT" | grep -qE '("status".*"success"|review.*successful)'; then
REVIEW_POSTED=true
if [ "$DEBUG_MODE" = true ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Found success indicators in output"
fi
elif echo "$REVIEW_OUTPUT" | grep -qE "(Error|Failed|error|failed|review_posted.*false)"; then
REVIEW_POSTED=false
print_status "warning" "[$CURRENT_PR/$TOTAL_PRS] ⚠️ CLI succeeded but review posting failed"
if [ "$DEBUG_MODE" = true ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Found error indicators in output"
echo "$REVIEW_OUTPUT" | grep -E "(Error|Failed|error|failed|review_posted.*false)" | head -3
fi
else
print_status "warning" "[$CURRENT_PR/$TOTAL_PRS] ⚠️ Unable to verify if review was posted - check output above"
if [ "$DEBUG_MODE" = true ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: No clear success/failure indicators found"
fi
fi
elif [ "$DEBUG_MODE" = true ]; then
print_status "info" "[$CURRENT_PR/$TOTAL_PRS] 🐛 Debug: Cannot verify review posting - SUCCESS=$SUCCESS, OUTPUT_LENGTH=${#REVIEW_OUTPUT}"
fi
# Record result based on actual review posting success
if [ "$SUCCESS" = true ] && [ "$REVIEW_POSTED" = true ]; then
print_status "success" "[$CURRENT_PR/$TOTAL_PRS] ✅ Review confirmed posted for PR #$PR_NUMBER"
SUCCESSFUL_REVIEWS=$((SUCCESSFUL_REVIEWS + 1))
REVIEW_RESULTS+=("✅ $URL - Review posted successfully")
elif [ "$SUCCESS" = true ] && [ "$REVIEW_POSTED" = false ]; then
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] ❌ CLI ran but review posting failed for PR #$PR_NUMBER"
FAILED_REVIEWS=$((FAILED_REVIEWS + 1))
REVIEW_RESULTS+=("❌ $URL - Review posting failed")
else
print_status "error" "[$CURRENT_PR/$TOTAL_PRS] ❌ CLI execution failed for PR #$PR_NUMBER"
FAILED_REVIEWS=$((FAILED_REVIEWS + 1))
REVIEW_RESULTS+=("❌ $URL - CLI execution failed")
fi
# No cleanup needed - using shared prompt file
# Add a small delay between reviews to avoid overwhelming the API
if [ $CURRENT_PR -lt $TOTAL_PRS ]; then
sleep 2
fi
done
# Final summary report
echo
print_status "info" "📊 BATCH REVIEW SUMMARY REPORT"
echo "═══════════════════════════════════════"
echo "📈 Total PRs processed: $TOTAL_PRS"
echo "✅ Successful reviews: $SUCCESSFUL_REVIEWS"
echo "❌ Failed reviews: $FAILED_REVIEWS"
echo "🔧 Tool used: $SELECTED_TOOL"
echo
echo "📋 Detailed Results:"
for result in "${REVIEW_RESULTS[@]}"; do
echo " $result"
done
echo
if [ $SUCCESSFUL_REVIEWS -gt 0 ]; then
print_status "info" "🔗 Check your GitHub PRs for the posted reviews"
fi
# Cleanup shared prompt file
rm -f "$PROMPT_FILE"
if [ $FAILED_REVIEWS -eq 0 ]; then
print_status "success" "🎉 All reviews completed successfully!"
exit 0
else
print_status "warning" "⚠️ Some reviews failed. Check the summary above for details."
exit 1
fi