-
Notifications
You must be signed in to change notification settings - Fork 5.4k
469 lines (425 loc) · 20.3 KB
/
auto_assign_reviewers.yml
File metadata and controls
469 lines (425 loc) · 20.3 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
#
# Copyright (c) 2006-2025, RT-Thread Development Team
#
# SPDX-License-Identifier: Apache-2.0
#
# Change Logs:
# Date Author Notes
# 2025-01-21 kurisaW Initial version
# 2025-03-14 hydevcode
# 2025-05-10 kurisaW Fixed file existence, cache, and comment time issues
# 2025-05-11 kurisaW Fixed missing unique files creation and cache logic
# 2025-07-14 kurisaW Merge same tag with different paths, remove Path display from CI comment
# Script Function Description: Assign PR reviews based on the MAINTAINERS list.
name: Auto Review Assistant
on:
pull_request_target:
branches: [ master ]
types: [opened, synchronize, reopened]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
assign-reviewers:
runs-on: ubuntu-22.04
if: github.repository_owner == 'RT-Thread'
permissions:
issues: read
pull-requests: write
contents: read
steps:
- name: Extract PR number
id: extract-pr
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT
- name: Checkout code
uses: actions/checkout@v4
with:
ref: master
sparse-checkout: MAINTAINERS
persist-credentials: false
- name: Get changed files
id: changed_files
run: |
# 通过 GitHub API 获取 PR 的变更文件列表(带重试机制和错误处理)
max_retries=3
retry_count=0
changed_files=""
api_response=""
echo "Fetching changed files for PR #${{ steps.extract-pr.outputs.PR_NUMBER }}..."
while [ $retry_count -lt $max_retries ]; do
api_response=$(curl -s \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files")
# 验证响应是否为有效JSON且包含文件数组
if jq -e 'if type=="array" then .[0].filename else empty end' <<<"$api_response" >/dev/null 2>&1; then
changed_files=$(jq -r '.[].filename' <<<"$api_response")
break
else
echo "Retry $((retry_count+1)): API response not ready or invalid format"
echo "API Response: $api_response"
sleep 5
((retry_count++))
fi
done
if [ -z "$changed_files" ]; then
echo "Error: Failed to get changed files after $max_retries attempts"
echo "Final API Response: $api_response"
exit 1
fi
echo "$changed_files" > changed_files.txt
echo "Successfully fetched $(wc -l < changed_files.txt) changed files"
# 以下是原有的评论处理逻辑(保持不变)
existing_comment=$(curl -s \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
# Check if response is valid JSON
if jq -e . >/dev/null 2>&1 <<<"$existing_comment"; then
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- Auto Review Assistant Comment -->"))) | {body: .body} | @base64' <<< "$existing_comment")
else
existing_comment=""
echo "Warning: Invalid JSON response from GitHub API for comments"
echo "Response: $existing_comment"
fi
comment_body=""
if [[ ! -z "$existing_comment" ]]; then
comment_body=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .body | sed -nE 's/.*Last Updated: ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} CST).*/\1/p')
comment_time=$(TZ='Asia/Shanghai' date -d "$comment_body" +%s)
echo "CACHE_TIMESTAMP=${comment_time}" >> $GITHUB_OUTPUT # 统一使用这个变量名
echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
else
comment_time=""
echo "CACHE_TIMESTAMP=" >> $GITHUB_OUTPUT
echo "COMMENT_TIME=" >> $GITHUB_OUTPUT
fi
echo "Debug - CACHE_TIMESTAMP: $comment_time"
- name: Parse MAINTAINERS file
id: parse_maintainer
run: |
set -euo pipefail
awk '
BEGIN{ tag=""; paths=""; owners="" }
/^tag:/ {
tag = substr($0, index($0, $2));
paths=""; owners=""
}
/^path:/ {
path = substr($0, index($0, $2))
gsub(/^[ \t]+|[ \t]+$/, "", path)
paths = (paths == "" ? path : paths "|" path)
}
/^owners:/ {
owners = substr($0, index($0, $2))
n = split(owners, parts, /[()]/)
github_ids=""
for (i=2; i<=n; i+=2) {
id=parts[i]
gsub(/^[ \t@]+|[ \t]+$/, "", id)
if(id != "") github_ids=github_ids "@" id " "
}
print tag "|" paths "|" github_ids
tag=""; paths=""; owners=""
}
' MAINTAINERS > tag_data.csv
- name: Generate reviewers list and tag-file mapping
id: generate_reviewers
run: |
rm -f triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt tag_files_map.json tag_reviewers_map.txt
touch triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt
# 1. 读取 tag_data.csv,建立 tag -> [paths], tag -> reviewers
declare -A tag_paths_map
declare -A tag_reviewers_map
while IFS='|' read -r tag paths reviewers; do
IFS='|' read -ra path_arr <<< "$paths"
for p in "${path_arr[@]}"; do
tag_paths_map["$tag"]+="$p;"
done
# 合并 reviewers,去重,只保留合法格式
existing_reviewers="${tag_reviewers_map["$tag"]}"
all_reviewers="$existing_reviewers $reviewers"
# 只保留 @xxx 格式,去重
all_reviewers=$(echo "$all_reviewers" | grep -o '@[A-Za-z0-9_-]\+' | sort -u | tr '\n' ' ')
tag_reviewers_map["$tag"]="$all_reviewers"
done < tag_data.csv
# 2. 针对每个 tag,找出它所有 path 匹配的变更文件
declare -A tag_changedfiles_map
while IFS= read -r changed; do
for tag in "${!tag_paths_map[@]}"; do
IFS=';' read -ra tpaths <<< "${tag_paths_map[$tag]}"
for tpath in "${tpaths[@]}"; do
[[ -z "$tpath" ]] && continue
if [[ -f "$tpath" ]]; then
# 精确文件名
[[ "$changed" == "$tpath" ]] && tag_changedfiles_map["$tag"]+="$changed;"
else
# 目录前缀
[[ "$changed" == $tpath* ]] && tag_changedfiles_map["$tag"]+="$changed;"
fi
done
done
done < changed_files.txt
# 3. 输出合并后的 tag reviewers、tag、并去重
for tag in "${!tag_changedfiles_map[@]}"; do
reviewers="${tag_reviewers_map[$tag]}"
echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt
echo "$tag" >> triggered_tags.txt
done
# 生成去重的 unique_reviewers.txt 和 unique_tags.txt
sort -u triggered_reviewers.txt > unique_reviewers.txt
sort -u triggered_tags.txt > unique_tags.txt
# 4. 输出 tag_files_map.json,格式 { "tag1": ["file1","file2"], ... }
{
echo "{"
first_tag=1
for tag in "${!tag_changedfiles_map[@]}"; do
[[ $first_tag -eq 0 ]] && echo ","
echo -n " \"${tag}\": ["
IFS=';' read -ra files <<< "${tag_changedfiles_map[$tag]}"
file_list=""
for f in "${files[@]}"; do
[[ -z "$f" ]] && continue
[[ -n "$file_list" ]] && file_list+=", "
file_list+="\"$f\""
done
echo -n "$file_list"
echo -n "]"
first_tag=0
done
echo ""
echo "}"
} > tag_files_map.json
# 5. 保存聚合去重后的 reviewers 到 tag_reviewers_map.txt
{
for tag in "${!tag_reviewers_map[@]}"; do
echo "$tag|${tag_reviewers_map[$tag]}"
done
} > tag_reviewers_map.txt
# 6. 标记是否有 reviewer
if [[ -s unique_reviewers.txt ]]; then
echo "HAS_REVIEWERS=true" >> $GITHUB_OUTPUT
else
echo "HAS_REVIEWERS=false" >> $GITHUB_OUTPUT
fi
echo "=== Matched Tags ==="
cat unique_tags.txt
echo "=== Matched Reviewers ==="
cat unique_reviewers.txt
echo "=== Tag-ChangedFiles Map ==="
cat tag_files_map.json
- name: Restore Reviewers Cache
id: reviewers-cache-restore
if: ${{ steps.changed_files.outputs.CACHE_TIMESTAMP != '' }}
uses: actions/cache/restore@v4
with:
path: |
unique_tags_bak.txt
unique_reviewers_bak.txt
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.CACHE_TIMESTAMP }}-${{ github.run_id }}
restore-keys: |
${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.CACHE_TIMESTAMP }}-
${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-
- name: Get approval status
id: get_approval
run: |
current_time=$(TZ='Asia/Shanghai' date +"%Y-%m-%d %H:%M CST")
if [[ ! -s unique_reviewers.txt ]]; then
echo "No reviewers found, creating empty unique_reviewers.txt"
touch unique_reviewers.txt
fi
reviewers=$(cat unique_reviewers.txt | tr '\n' '|' | sed 's/|$//')
# 获取 PR 的所有评论
comments=$(curl -s \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
echo '#!/bin/bash' > approval_data.sh
echo 'declare -A approvals=()' >> approval_data.sh
# 使用 jq 解析包含 LGTM 的有效评论
jq -r --arg reviewers "$reviewers" '
.[] |
select(.user.login != "github-actions[bot]") | # 排除 bot 的评论
select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写)
.user.login as $user |
"@\($user)" as $mention |
select($mention | inside($reviewers)) | # 过滤有效审查者
"approvals[\"\($mention)\"]=\"\(.created_at)\"" # 记录审批时间
' <<< "$comments" >> approval_data.sh
# 加载审查数据并生成状态报告
chmod +x approval_data.sh
source ./approval_data.sh
jq -r --arg reviewers "$reviewers" '
.[] |
select(.user.login != "github-actions[bot]") | # 排除 bot 的评论
select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写)
.user.login as $user |
"@\($user)" as $mention |
select($mention | inside($reviewers)) | # 过滤有效审查者
"\($mention) \(.created_at)" # 输出审查者和时间
' <<< "$comments" >> approval_data.txt
notified_users=""
if [[ -f unique_reviewers_bak.txt ]]; then
notified_users=$(cat unique_reviewers_bak.txt | xargs)
else
notified_users=""
fi
{
echo "---"
echo "### 📊 Current Review Status (Last Updated: $current_time)"
while read -r reviewer; do
formatted_reviewers=""
for r in $reviewers; do
if [[ " ${notified_users[@]} " =~ " $reviewer " ]]; then
formatted_reviewers+="${reviewer#@}"
else
formatted_reviewers+="$reviewer"
fi
done
if [[ -n "${approvals[$reviewer]}" ]]; then
timestamp=$(TZ='Asia/Shanghai' date -d "${approvals[$reviewer]}" +"%Y-%m-%d %H:%M CST")
echo "- ✅ **$formatted_reviewers** Reviewed On $timestamp"
else
echo "- ⌛ **$formatted_reviewers** Pending Review"
fi
done < unique_reviewers.txt
} > review_status.md
echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT
- name: Generate review data (tag merge, no path in comment, changed files summary per tag)
id: generate_review
if: steps.generate_reviewers.outputs.HAS_REVIEWERS == 'true'
run: |
unique_tags=""
if [[ -s unique_tags.txt ]]; then
unique_tags=$(cat unique_tags.txt | xargs)
fi
unique_tags_bak=""
if [[ -f unique_tags_bak.txt ]]; then
unique_tags_bak=$(cat unique_tags_bak.txt | xargs)
fi
# 读取 tag->files 映射
declare -A tag_files_map
eval "$(jq -r 'to_entries[] | "tag_files_map[\"\(.key)\"]=\"\(.value | join(";"))\"" ' tag_files_map.json)"
# 读取 tag->reviewers(只读聚合去重后的结果)
declare -A tag_reviewers_map
while IFS='|' read -r tag reviewers; do
tag_reviewers_map["$tag"]="$reviewers"
done < tag_reviewers_map.txt
# 获取已通知的 reviewers
notified_users=""
if [[ -f unique_reviewers_bak.txt ]]; then
notified_users=$(cat unique_reviewers_bak.txt | xargs)
fi
current_time=$(TZ='Asia/Shanghai' date +"%Y-%m-%d %H:%M CST")
{
echo "<!-- Auto Review Assistant Comment -->"
echo "## 📌 Code Review Assignment"
echo ""
for tag in $unique_tags; do
reviewers="${tag_reviewers_map[$tag]}"
# 移除尾部空格并提取有效的@username格式
reviewers=$(echo "$reviewers" | sed 's/[[:space:]]*$//' | grep -o '@[A-Za-z0-9_-]\+' | sort -u | tr '\n' ' ')
# 格式化reviewers显示(仅对已通知用户去掉@)
formatted_reviewers=""
for reviewer in $reviewers; do
if [[ " ${notified_users[@]} " =~ " $reviewer " ]]; then
formatted_reviewers+="${reviewer#@} " # 已通知用户去掉@
else
formatted_reviewers+="$reviewer " # 未通知用户保留@
fi
done
echo "### 🏷️ Tag: $tag"
echo ""
echo "**Reviewers:** $formatted_reviewers" # 确保显示Reviewers
echo "<details>"
echo "<summary><b>Changed Files</b> (Click to expand)</summary>"
echo ""
IFS=';' read -ra files <<< "${tag_files_map[$tag]}"
for file in "${files[@]}"; do
[[ -z "$file" ]] && continue
echo "- $file"
done
echo ""
echo "</details>"
echo ""
done
# 插入审查状态
cat review_status.md
echo "---"
echo "### 📝 Review Instructions"
echo ""
echo "1. **维护者可以通过单击此处来刷新审查状态:** [🔄 刷新状态](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
echo " **Maintainers can refresh the review status by clicking here:** [🔄 Refresh Status](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
echo ""
echo "2. **确认审核通过后评论 \`LGTM/lgtm\`**"
echo " **Comment \`LGTM/lgtm\` after confirming approval**"
echo ""
echo "3. **PR合并前需至少一位维护者确认**"
echo " **PR must be confirmed by at least one maintainer before merging**"
echo ""
echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**"
echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**"
} > review_data.md
- name: Post/Update comment
id: post_comment
if: steps.generate_reviewers.outputs.HAS_REVIEWERS == 'true'
run: |
# 查找现有的 bot 评论
existing_comment=$(curl -s \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- Auto Review Assistant Comment -->"))) | {id: .id, body: .body} | @base64')
if [[ -n "$existing_comment" ]]; then
# 更新现有评论
comment_id=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .id)
echo "Updating existing comment $comment_id"
response=$(curl -s -X PATCH \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id")
else
# 创建新评论
echo "Creating new comment"
response=$(curl -s -X POST \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
fi
- name: Get Comment Time
id: get_comment_time
if: steps.generate_reviewers.outputs.HAS_REVIEWERS == 'true'
run: |
existing_comment=$(curl -s \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
# Check if response is valid JSON
if jq -e . >/dev/null 2>&1 <<<"$existing_comment"; then
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- Auto Review Assistant Comment -->"))) | {body: .body} | @base64' <<< "$existing_comment")
else
existing_comment=""
echo "Warning: Invalid JSON response from GitHub API"
echo "Response: $existing_comment"
fi
comment_body="${{ steps.get_approval.outputs.CURRENT_TIME }}"
comment_time=$(TZ='Asia/Shanghai' date -d "$comment_body" +%s)
echo "CACHE_TIMESTAMP=${comment_time}" >> $GITHUB_OUTPUT # 统一使用这个变量名
echo "Debug - Saving cache with timestamp: $comment_time"
mkdir -p $(dirname unique_reviewers_bak.txt)
if [[ -s unique_reviewers.txt ]]; then
cp unique_reviewers.txt unique_reviewers_bak.txt
else
touch unique_reviewers_bak.txt
fi
if [[ -s unique_tags.txt ]]; then
cp unique_tags.txt unique_tags_bak.txt
else
touch unique_tags_bak.txt
fi
- name: Save Reviewers Cache
id: reviewers-cache-save
if: steps.generate_reviewers.outputs.HAS_REVIEWERS == 'true'
continue-on-error: true
uses: actions/cache/save@v4
with:
path: |
unique_tags_bak.txt
unique_reviewers_bak.txt
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CACHE_TIMESTAMP }}-${{ github.run_id }}