forked from 0GiS0/ADO_CodingAgent
-
Notifications
You must be signed in to change notification settings - Fork 3
500 lines (408 loc) · 18.6 KB
/
copilot-reviewer-master.yml
File metadata and controls
500 lines (408 loc) · 18.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
name: 🤖 Copilot PR Reviewer (Master)
# This is a reusable workflow that can be called from other repositories.
# It contains the full implementation logic for the Copilot PR Reviewer.
#
# To use this workflow from another repository, create a caller workflow:
# jobs:
# review:
# uses: <org>/GHES_CodingAgent/.github/workflows/copilot-reviewer-master.yml@main
# secrets: inherit
on:
# Direct trigger (for this repository) - only on 'copilot' label
pull_request:
types: [labeled]
branches:
- main
- develop
- feature/*
# Reusable workflow trigger (for other repositories)
workflow_call:
secrets:
GH_TOKEN:
description: 'Classic PAT with repo and workflow scopes'
required: true
COPILOT_TOKEN:
description: 'Token for GitHub Copilot API access'
required: true
CONTEXT7_API_KEY:
description: 'API key for Context7 documentation service'
required: false
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: write
env:
MODEL: claude-haiku-4.5
COPILOT_VERSION: latest
ANALYSIS_DIR: ${{ github.workspace }}/pr-analysis
DIFF_FILE: ${{ github.workspace }}/pr-diff.json
jobs:
review:
runs-on: self-hosted
name: 🤖 Analyze PR and Generate Review
# Only run when the 'copilot' label is added
if: github.event.label.name == 'copilot'
steps:
- name: 📋 Show PR Information
run: |
echo "📋 Pull Request Information:"
echo " - Repository: ${{ github.repository }}"
echo " - PR #: ${{ github.event.pull_request.number }}"
echo " - Source Branch: ${{ github.event.pull_request.head.ref }}"
echo " - Target Branch: ${{ github.event.pull_request.base.ref }}"
echo " - Source Commit: ${{ github.event.pull_request.head.sha }}"
echo ""
echo "📁 Working directories:"
echo " - Analysis Dir: ${{ env.ANALYSIS_DIR }}"
echo " - Diff File: ${{ env.DIFF_FILE }}"
- name: 📂 Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: ⚙️ Setup Node.js 22.x
uses: actions/setup-node@v4
with:
node-version: '22.x'
- name: 🔍 Detect NPM Global Path
run: |
NPM_PREFIX=$(npm config get prefix)
echo "NPM_GLOBAL_PATH=${NPM_PREFIX}/lib/node_modules" >> $GITHUB_ENV
echo "NPM global path: ${NPM_PREFIX}/lib/node_modules"
- name: 📦 Cache Global NPM Packages
uses: actions/cache@v3
with:
key: npm-global-${{ runner.os }}-copilot-${{ env.COPILOT_VERSION }}
path: ${{ env.NPM_GLOBAL_PATH }}
restore-keys: |
npm-global-${{ runner.os }}-copilot-
- name: 📦 Install Copilot CLI
run: |
if ! command -v copilot &> /dev/null; then
echo "Installing @github/copilot@${{ env.COPILOT_VERSION }}..."
npm install -g @github/copilot@${{ env.COPILOT_VERSION }}
else
echo "✅ @github/copilot already installed (from cache)"
copilot --version
fi
- name: 🔍 Get PR Differences
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
echo "🌐 Get PR Differences using GitHub API"
echo "======================================="
GHES_HOST=$(echo "${{ github.server_url }}" | sed 's|https://||')
OWNER="${{ github.repository_owner }}"
REPO="${{ github.event.repository.name }}"
PR_NUMBER="${{ github.event.pull_request.number }}"
OUTPUT_FILE="${{ env.DIFF_FILE }}"
echo "📋 PR Information:"
echo " - GHES Host: $GHES_HOST"
echo " - Owner: $OWNER"
echo " - Repository: $REPO"
echo " - PR Number: $PR_NUMBER"
echo ""
# Determine API base URL
if [[ "$GHES_HOST" == "github.com" ]]; then
API_BASE="https://api.github.com"
else
API_BASE="https://$GHES_HOST/api/v3"
fi
echo "🔗 API Base URL: $API_BASE"
# Get PR details
echo "🔍 Fetching PR details..."
PR_URL="$API_BASE/repos/$OWNER/$REPO/pulls/$PR_NUMBER"
PR_RESPONSE=$(curl -s \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$PR_URL")
BASE_REF=$(echo "$PR_RESPONSE" | grep -o '"base":{"label":"[^"]*"' | cut -d'"' -f8 | cut -d: -f2- || echo "unknown")
HEAD_REF=$(echo "$PR_RESPONSE" | grep -o '"head":{"label":"[^"]*"' | cut -d'"' -f8 | cut -d: -f2- || echo "unknown")
echo " - Base Branch: $BASE_REF"
echo " - Head Branch: $HEAD_REF"
# Get changed files
echo "📄 Fetching changed files..."
FILES_URL="$API_BASE/repos/$OWNER/$REPO/pulls/$PR_NUMBER/files"
CHANGES_ARRAY=""
FIRST=true
FILE_COUNT=0
for PAGE in 1 2 3; do
FILES_RESPONSE=$(curl -s \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$FILES_URL?page=$PAGE&per_page=100")
if echo "$FILES_RESPONSE" | grep -q "^\\[\\]"; then
break
fi
FILENAMES=$(echo "$FILES_RESPONSE" | grep -o '"filename":"[^"]*"' | cut -d'"' -f4)
while IFS= read -r filename; do
if [ -n "$filename" ]; then
if [ "$FIRST" = false ]; then
CHANGES_ARRAY="$CHANGES_ARRAY,"
fi
CHANGES_ARRAY="$CHANGES_ARRAY
{ \"path\": \"$filename\", \"type\": \"modify\" }"
FIRST=false
FILE_COUNT=$((FILE_COUNT + 1))
echo " - $filename"
fi
done <<< "$FILENAMES"
done
echo "✅ Found $FILE_COUNT changed files"
# Create output JSON
cat > "$OUTPUT_FILE" << EOF
{
"pr_number": $PR_NUMBER,
"base_ref": "$BASE_REF",
"head_ref": "$HEAD_REF",
"changes": [$CHANGES_ARRAY
],
"statistics": { "total_changes": $FILE_COUNT }
}
EOF
echo "✅ Diff file created: $OUTPUT_FILE"
- name: 📁 Download Modified Files
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
echo "📁 Downloading Modified PR Files"
echo "================================="
GHES_HOST=$(echo "${{ github.server_url }}" | sed 's|https://||')
OWNER="${{ github.repository_owner }}"
REPO="${{ github.event.repository.name }}"
PR_NUMBER="${{ github.event.pull_request.number }}"
OUTPUT_DIR="${{ env.ANALYSIS_DIR }}"
# Determine API base URL
if [[ "$GHES_HOST" == "github.com" ]]; then
API_BASE="https://api.github.com"
else
API_BASE="https://$GHES_HOST/api/v3"
fi
# Create output directories
mkdir -p "$OUTPUT_DIR/source"
mkdir -p "$OUTPUT_DIR/target"
mkdir -p "$OUTPUT_DIR/metadata"
# Get files from the PR
echo "📄 Fetching changed files from PR..."
FILES_URL="$API_BASE/repos/$OWNER/$REPO/pulls/$PR_NUMBER/files"
PAGE=1
while [ $PAGE -le 10 ]; do
RESPONSE=$(curl -s \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$FILES_URL?page=$PAGE&per_page=100")
if echo "$RESPONSE" | grep -q "^\\[\\]"; then
break
fi
echo "$RESPONSE" | jq -r '.[] | .filename' 2>/dev/null >> "/tmp/pr_files_$$.txt" || true
PAGE=$((PAGE + 1))
done
if [ ! -f "/tmp/pr_files_$$.txt" ]; then
echo "⚠️ No modified files found in PR"
exit 0
fi
TOTAL_FILES=$(wc -l < "/tmp/pr_files_$$.txt" | tr -d ' ')
echo "✅ Found $TOTAL_FILES files to download"
# Get PR details for SHA references
PR_URL="$API_BASE/repos/$OWNER/$REPO/pulls/$PR_NUMBER"
PR_DATA=$(curl -s \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$PR_URL")
HEAD_SHA=$(echo "$PR_DATA" | jq -r '.head.sha // empty')
BASE_SHA=$(echo "$PR_DATA" | jq -r '.base.sha // empty')
SUCCESSFUL=0
FAILED=0
while IFS= read -r filepath; do
if [ -n "$filepath" ]; then
echo "📥 Downloading: $filepath"
# Create directory structure
mkdir -p "$OUTPUT_DIR/source/$(dirname "$filepath")"
# Download from source (head) branch
CONTENT_URL="$API_BASE/repos/$OWNER/$REPO/contents/$filepath?ref=$HEAD_SHA"
curl -s \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github.v3.raw" \
"$CONTENT_URL" > "$OUTPUT_DIR/source/$filepath" 2>/dev/null
if [ -s "$OUTPUT_DIR/source/$filepath" ]; then
echo " ✅ Downloaded"
SUCCESSFUL=$((SUCCESSFUL + 1))
else
echo " ⚠️ Could not download"
FAILED=$((FAILED + 1))
fi
fi
done < "/tmp/pr_files_$$.txt"
rm -f "/tmp/pr_files_$$.txt"
echo ""
echo "📊 Download Summary:"
echo " - Total files: $TOTAL_FILES"
echo " - Successful: $SUCCESSFUL"
echo " - Failed: $FAILED"
- name: 🤖 Analyze with GitHub Copilot CLI
env:
MODEL: ${{ env.MODEL }}
GH_TOKEN: ${{ secrets.COPILOT_TOKEN }}
run: |
echo "🤖 PR Analysis with GitHub Copilot CLI"
echo "======================================="
PR_DIRECTORY="${{ env.ANALYSIS_DIR }}/source"
OUTPUT_COMMENTS_DIR="${{ env.ANALYSIS_DIR }}/source/pr-comments"
if [ ! -d "$PR_DIRECTORY" ]; then
echo "❌ ERROR: Directory $PR_DIRECTORY does not exist"
exit 1
fi
mkdir -p "$OUTPUT_COMMENTS_DIR"
# Get list of files to analyze
echo "🔍 Scanning files in directory..."
cd "$PR_DIRECTORY"
FILES=($(find . -type f ! -path "*/pr-comments/*" ! -name "pr-comment*.md" ! -name ".*" | sed 's|^\./||'))
if [ ${#FILES[@]} -eq 0 ]; then
echo "⚠️ No files found to analyze"
exit 0
fi
echo "✅ Found ${#FILES[@]} files to analyze:"
for file in "${FILES[@]}"; do
echo " - $file"
done
# Build the list of files for the prompt
FILES_LIST=""
for file in "${FILES[@]}"; do
FILES_LIST="${FILES_LIST}- \`$file\`"$'\n'
done
# Create the analysis prompt
ANALYSIS_PROMPT="Analyze ALL the files in this Pull Request directory and generate a SEPARATE markdown file for EACH file analyzed.
📁 **Files to analyze:**
$FILES_LIST
🎯 **IMPORTANT INSTRUCTIONS:**
For EACH file listed above, you must:
1. Analyze the file thoroughly
2. Create a separate markdown file with the analysis if there is anything noteworthy (issues, recommendations)
3. Name each file following this pattern: replace \`/\` with \`_\` in the original path and add \`_analysis.md\` suffix
4. Save each file in the directory: \`$OUTPUT_COMMENTS_DIR/\`
📝 **Format for EACH analysis file:**
# 🔬 \$relative_path analysis
## 📊 Overview
Brief description of what this file does.
## ⚠️ Issues and Recommendations
List issues with type, description, code snippet, and recommendation.
## ✅ Summary
- **Overall Status:** ⚠️ Needs Attention / 🔴 Critical Issues / ✅ Good
- **Priority:** High/Medium/Low
- **Action Required:** Yes/No
Focus only on this single file. Be thorough but concise."
# Execute copilot for analysis
echo "🤖 Generating analysis with Copilot..."
copilot -p "$ANALYSIS_PROMPT" --allow-all-tools --model "$MODEL" 2>&1 || true
echo "✅ Copilot analysis completed"
# Summary
ANALYSIS_COUNT=$(ls -1 "$OUTPUT_COMMENTS_DIR"/*_analysis.md 2>/dev/null | wc -l || echo "0")
echo "📊 Analysis files generated: $ANALYSIS_COUNT"
- name: 💬 Publish Comment on PR
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
if: always()
run: |
echo "💬 Posting PR Review Comments"
echo "=============================="
GHES_HOST=$(echo "${{ github.server_url }}" | sed 's|https://||')
OWNER="${{ github.repository_owner }}"
REPO="${{ github.event.repository.name }}"
PR_NUMBER="${{ github.event.pull_request.number }}"
COMMENTS_DIR="${{ env.ANALYSIS_DIR }}/source/pr-comments"
# Determine API base URL
if [[ "$GHES_HOST" == "github.com" ]]; then
API_BASE="https://api.github.com"
else
API_BASE="https://$GHES_HOST/api/v3"
fi
# Verify comments directory exists
if [ ! -d "$COMMENTS_DIR" ]; then
echo "⚠️ No comments directory found"
exit 0
fi
# Get list of markdown files
COMMENT_FILES=($(find "$COMMENTS_DIR" -type f -name "*_analysis.md" 2>/dev/null | sort))
if [ ${#COMMENT_FILES[@]} -eq 0 ]; then
echo "⚠️ No analysis files found (this is normal if no issues were found)"
exit 0
fi
echo "📄 Found ${#COMMENT_FILES[@]} comment files to post"
SUCCESSFUL=0
FAILED=0
for comment_file in "${COMMENT_FILES[@]}"; do
filename=$(basename "$comment_file")
echo "📤 Posting: $filename"
# Read and escape content
COMMENT_CONTENT=$(cat "$comment_file")
ESCAPED_CONTENT=$(printf '%s' "$COMMENT_CONTENT" | \
sed 's/\\/\\\\/g' | \
sed 's/"/\\"/g' | \
sed ':a;N;$!ba;s/\n/\\n/g')
# Create payload
PAYLOAD="{\"body\": \"$ESCAPED_CONTENT\", \"event\": \"COMMENT\"}"
# Post as a pull request review
REVIEW_URL="$API_BASE/repos/$OWNER/$REPO/pulls/$PR_NUMBER/reviews"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
-X POST \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"$REVIEW_URL")
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
echo " ✅ Posted successfully"
SUCCESSFUL=$((SUCCESSFUL + 1))
else
echo " ❌ Failed (HTTP $HTTP_CODE)"
FAILED=$((FAILED + 1))
fi
sleep 1
done
echo ""
echo "📊 Posting Summary:"
echo " - Total: ${#COMMENT_FILES[@]}"
echo " - Successful: $SUCCESSFUL"
echo " - Failed: $FAILED"
- name: 📦 Upload Analysis Artifacts
if: always()
uses: actions/upload-artifact@v3
with:
name: pr-analysis-${{ github.event.pull_request.number }}
path: ${{ env.ANALYSIS_DIR }}/
retention-days: 30
if-no-files-found: warn
- name: 📊 Summary
if: always()
run: |
echo "## 📊 PR Review Analysis Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**PR Details:**" >> $GITHUB_STEP_SUMMARY
echo "- Repository: ${{ github.repository }}" >> $GITHUB_STEP_SUMMARY
echo "- PR #: ${{ github.event.pull_request.number }}" >> $GITHUB_STEP_SUMMARY
echo "- Author: ${{ github.event.pull_request.user.login }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ -d "${{ env.ANALYSIS_DIR }}/source/pr-comments" ]; then
COMMENT_COUNT=$(find "${{ env.ANALYSIS_DIR }}/source/pr-comments" -name "*_analysis.md" 2>/dev/null | wc -l)
echo "**Review Results:**" >> $GITHUB_STEP_SUMMARY
echo "- Comments Generated: $COMMENT_COUNT" >> $GITHUB_STEP_SUMMARY
echo "- Model Used: ${{ env.MODEL }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ $COMMENT_COUNT -gt 0 ]; then
echo "**Issues Found:**" >> $GITHUB_STEP_SUMMARY
find "${{ env.ANALYSIS_DIR }}/source/pr-comments" -name "*_analysis.md" | sort | while read file; do
echo "- $(basename "$file" _analysis.md)" >> $GITHUB_STEP_SUMMARY
done
else
echo "✅ No issues found!" >> $GITHUB_STEP_SUMMARY
fi
else
echo "**Review Results:**" >> $GITHUB_STEP_SUMMARY
echo "- Comments Generated: 0" >> $GITHUB_STEP_SUMMARY
echo "- Model Used: ${{ env.MODEL }}" >> $GITHUB_STEP_SUMMARY
echo "- Status: ✅ No issues found!" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Artifacts:**" >> $GITHUB_STEP_SUMMARY
echo "- Full analysis available in [pr-analysis artifact](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}#artifacts)" >> $GITHUB_STEP_SUMMARY