Skip to content

Commit 963a96b

Browse files
authored
Update auto-assign-reviewers.yml
1 parent 7daf597 commit 963a96b

File tree

1 file changed

+75
-100
lines changed

1 file changed

+75
-100
lines changed

.github/workflows/auto-assign-reviewers.yml

Lines changed: 75 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#
32
# Copyright (c) 2006-2025, RT-Thread Development Team
43
#
@@ -33,64 +32,59 @@ jobs:
3332
run: |
3433
PR_NUMBER=${{ github.event.pull_request.number }}
3534
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT
35+
3636
- name: Checkout code
3737
uses: actions/checkout@v4
3838
with:
3939
ref: master
4040
sparse-checkout: MAINTAINERS
4141
persist-credentials: false
42+
4243
- name: Get changed files
4344
id: changed_files
4445
run: |
45-
# 通过 GitHub API 获取 PR 的变更文件列表
46+
# 获取 PR 的变更文件列表
4647
changed_files=$(curl -s \
4748
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \
48-
jq -r '.[].filename') # 使用 jq 提取文件名
49+
jq -r '.[].filename')
4950
echo "$changed_files" | grep -v '^MAINTAINERS$' > changed_files.txt
50-
51+
52+
# 检查现有 bot 评论
5153
existing_comment=$(curl -s \
52-
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
53-
jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
54+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
55+
jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
5456
5557
echo "=== Changed Files ==="
5658
cat changed_files.txt
5759
echo "====================="
5860
59-
comment_body=""
60-
if [[ ! -z "$existing_comment" ]]; then
61-
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} UTC).*/\1/p')
62-
63-
comment_time=$(date -d "$comment_body" +%s)
64-
65-
echo "${comment_body}"
66-
echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
67-
else
68-
comment_time=""
69-
echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
61+
comment_time=""
62+
if [[ -n "$existing_comment" ]]; then
63+
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} UTC).*/\1/p')
64+
if [[ -n "$comment_body" ]]; then
65+
comment_time=$(date -d "$comment_body" +%s)
66+
fi
7067
fi
71-
echo "COMMENT_TIME=${comment_time}"
72-
68+
echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
7369
7470
- name: Parse MAINTAINERS file
7571
id: parse_maintainer
7672
run: |
77-
# 使用 AWK 解析 MAINTAINERS 文件格式:
78-
# 提取 tag(标签)、path(路径)和 owners(维护者 GitHub ID)
73+
# 解析 MAINTAINERS 文件,提取 tag、path 和 owners
7974
awk '
8075
/^tag:/ {
81-
tag = substr($0, index($0, $2)) # 提取标签内容
76+
tag = substr($0, index($0, $2))
8277
}
8378
/^path:/ {
84-
# 提取 path 字段并去除前后空格
8579
path = substr($0, index($0, $2))
86-
gsub(/^[ \t]+|[ \t]+$/, "", path) # 清理前后空格和制表符
80+
gsub(/^[ \t]+|[ \t]+$/, "", path)
8781
}
8882
/^owners:/ {
89-
owners = substr($0, index($0, $2)) # 提取维护者信息
90-
split(owners, parts, /[()]/) # 拆分出 GitHub ID(括号内内容)
83+
owners = substr($0, index($0, $2))
84+
split(owners, parts, /[()]/)
9185
github_ids = ""
9286
for (i=2; i<=length(parts); i+=2) {
93-
github_ids = github_ids "@" parts[i] " " # 拼接为 @user 格式
87+
github_ids = github_ids "@" parts[i] " "
9488
}
9589
print tag "|" path "|" github_ids
9690
}
@@ -101,165 +95,146 @@ jobs:
10195
run: |
10296
rm -f triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt
10397
touch triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt
104-
98+
10599
while IFS='|' read -r tag path reviewers; do
106100
# 转义路径中的正则特殊字符
107101
escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path")
108-
109-
# 使用增强型正则匹配路径及其所有子目录
102+
# 匹配路径及其子目录
110103
if grep -qE "^$escaped_path(/.*)*" changed_files.txt; then
111104
echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt
112105
echo "$tag" >> triggered_tags.txt
113106
echo "Matched: $path → $tag"
114107
fi
115108
done < tag_data.csv
116-
117-
# 从 triggered_reviewers.txt 生成 unique_reviewers.txt(去重)
109+
110+
# 生成去重后的审阅者和标签列表
118111
sort triggered_reviewers.txt | uniq > unique_reviewers.txt
119-
# 从 triggered_tags.txt 生成 unique_tags.txt(去重)
120112
sort triggered_tags.txt | uniq > unique_tags.txt
121-
113+
122114
echo "=== Matched Paths ==="
123115
cat unique_tags.txt
124116
echo "=== Matched Reviewers ==="
125117
cat unique_reviewers.txt
126118
127119
- name: Restore Reviewers Cache
128-
id: reviewers-cache-restore
120+
id: reviewers-cache-restore
129121
if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }}
130122
uses: actions/cache/restore@v4
131-
with:
123+
with:
132124
path: |
133125
unique_tags_bak.txt
134126
unique_reviewers_bak.txt
135127
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.COMMENT_TIME }}
128+
136129
- name: Get approval status
137130
id: get_approval
138131
run: |
139132
current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
140-
133+
141134
# 检查 unique_reviewers.txt 是否存在
142135
if [[ ! -f unique_reviewers.txt ]]; then
143136
echo "Error: unique_reviewers.txt not found. Skipping approval status generation."
144137
echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT
145138
exit 0
146139
fi
147-
140+
148141
reviewers=$(cat unique_reviewers.txt | tr '\n' '|')
149-
142+
150143
# 获取 PR 的所有评论
151144
comments=$(curl -s \
152145
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
153-
146+
154147
echo '#!/bin/bash' > approval_data.sh
155148
echo 'declare -A approvals=()' >> approval_data.sh
156-
157-
# 使用 jq 解析包含 LGTM 的有效评论
149+
150+
# 解析 LGTM 评论
158151
jq -r --arg reviewers "$reviewers" '
159-
.[] |
160-
select(.user.login != "github-actions[bot]") | # 排除 bot 的评论
161-
select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写)
152+
.[] |
153+
select(.user.login != "github-actions[bot]") |
154+
select(.body | test("^\\s*LGTM\\s*$"; "i")) |
162155
.user.login as $user |
163156
"@\($user)" as $mention |
164-
select($mention | inside($reviewers)) | # 过滤有效审查者
165-
"approvals[\"\($mention)\"]=\"\(.created_at)\"" # 记录审批时间
157+
($reviewers | split("\\|") | index($mention)) as $is_reviewer |
158+
if $is_reviewer != null then
159+
"approvals[\"\($mention)\"]=\"\(.created_at)\""
160+
else
161+
empty
162+
end
166163
' <<< "$comments" >> approval_data.sh
167-
168-
# 加载审查数据并生成状态报告
164+
165+
# 加载审批数据
169166
chmod +x approval_data.sh
170167
source ./approval_data.sh
171-
172-
jq -r --arg reviewers "$reviewers" '
173-
.[] |
174-
select(.user.login != "github-actions[bot]") | # 排除 bot 的评论
175-
select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写)
176-
.user.login as $user |
177-
"@\($user)" as $mention |
178-
select($mention | inside($reviewers)) | # 过滤有效审查者
179-
"\($mention) \(.created_at)" # 输出审查者和时间
180-
' <<< "$comments" >> approval_data.txt
181-
168+
169+
# 生成审批状态报告
182170
notified_users=""
183171
if [[ -f unique_reviewers_bak.txt ]]; then
184172
notified_users=$(cat unique_reviewers_bak.txt | xargs)
185-
else
186-
notified_users=""
187173
fi
188-
174+
189175
{
190176
echo "---"
191177
echo "### 📊 Current Review Status (Last Updated: $current_time)"
192178
while read -r reviewer; do
193-
formatted_reviewers=""
194-
for r in $reviewers; do
195-
if [[ " ${notified_users[@]} " =~ " $reviewer " ]]; then
196-
formatted_reviewers+="${reviewer#@}"
197-
else
198-
formatted_reviewers+="$reviewer"
199-
fi
200-
done
201-
179+
display_name="${reviewer#@}"
202180
if [[ -n "${approvals[$reviewer]}" ]]; then
203181
timestamp=$(date -d "${approvals[$reviewer]}" -u +"%Y-%m-%d %H:%M UTC")
204-
echo "- ✅ **$formatted_reviewers** Reviewed On $timestamp"
182+
echo "- ✅ **$display_name** Reviewed On $timestamp"
205183
else
206-
echo "- ⌛ **$formatted_reviewers** Pending Review"
184+
echo "- ⌛ **$display_name** Pending Review"
207185
fi
208186
done < unique_reviewers.txt
209187
} > review_status.md
210-
188+
211189
echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT
212-
190+
213191
- name: Generate review data
214192
id: generate_review
215193
run: |
216-
unique_tags=""
217194
unique_tags=$(cat unique_tags.txt | xargs)
218195
unique_tags_bak=""
219196
if [[ -f unique_tags_bak.txt ]]; then
220197
unique_tags_bak=$(cat unique_tags_bak.txt | xargs)
221198
fi
222199
200+
# 确定新标签
223201
existing_tags=""
224202
for r in $unique_tags; do
225-
if [[ " ${unique_tags_bak[@]} " =~ " $r " ]]; then
226-
echo "$r 不存在于数组中"
227-
else
203+
if [[ ! " $unique_tags_bak " =~ " $r " ]]; then
228204
existing_tags+="$r "
229205
fi
230206
done
231207
232-
current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
233208
{
234-
235-
# 生成审查分配信息
236209
echo "## 📌 Code Review Assignment"
237210
echo ""
238211
239212
while IFS='|' read -r tag path reviewers; do
240213
if grep -qE "^$path(/|$)" changed_files.txt; then
241214
echo "### 🏷️ Tag: $tag"
242-
echo "**Path:** \`$path\` "
215+
echo "**Path:** \`$path\`"
243216
244-
if [[ " ${existing_tags[@]} " =~ " $tag " ]]; then
245-
echo "**Reviewers:** $reviewers "
217+
# 根据标签是否为新标签决定是否移除 @
218+
if [[ " $existing_tags " =~ " $tag " ]]; then
219+
echo "**Reviewers:** $reviewers"
246220
else
247221
formatted_reviewers=""
248222
for r in $reviewers; do
249223
formatted_reviewers+="${r#@} "
250224
done
251-
echo "**Reviewers:** $formatted_reviewers "
225+
echo "**Reviewers:** $formatted_reviewers"
252226
fi
253227
254228
echo "<details>"
255229
echo "<summary><b>Changed Files</b> (Click to expand)</summary>"
256230
echo ""
257-
grep -E "^$path(/|$)" changed_files.txt | sed 's/^/- /' # 列出匹配的变更文件
231+
grep -E "^$path(/|$)" changed_files.txt | sed 's/^/- /'
258232
echo ""
259233
echo "</details>"
260234
echo ""
261235
fi
262236
done < tag_data.csv
237+
263238
# 插入审查状态
264239
cat review_status.md
265240
@@ -278,43 +253,43 @@ jobs:
278253
echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**"
279254
echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**"
280255
} > review_data.md
256+
281257
- name: Post/Update comment
282258
id: post_comment
283259
run: |
284-
# 查找现有的 bot 评论
260+
# 查找现有 bot 评论
285261
existing_comment=$(curl -s \
286262
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
287263
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
288264
jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64')
289-
265+
290266
if [[ -n "$existing_comment" ]]; then
291267
# 更新现有评论
292268
comment_id=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .id)
293269
echo "Updating existing comment $comment_id"
294-
response=$(curl -s -X PATCH \
270+
curl -s -X PATCH \
295271
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
296272
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
297-
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id")
273+
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id"
298274
else
299275
# 创建新评论
300276
echo "Creating new comment"
301-
response=$(curl -s -X POST \
277+
curl -s -X POST \
302278
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
303279
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
304-
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
280+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments"
305281
fi
282+
306283
- name: Get Comment Time
307284
id: get_comment_time
308285
run: |
309-
existing_comment=$(curl -s \
310-
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
311-
jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
312-
comment_body="${{ steps.get_approval.outputs.CURRENT_TIME }}"
313-
comment_time=$(date -d "$comment_body" +%s)
286+
current_time="${{ steps.get_approval.outputs.CURRENT_TIME }}"
287+
comment_time=$(date -d "$current_time" +%s)
314288
echo "CURRENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
315289
cp unique_reviewers.txt unique_reviewers_bak.txt
316290
cp unique_tags.txt unique_tags_bak.txt
317-
- name: Restore Reviewers Save
291+
292+
- name: Save Reviewers Cache
318293
id: reviewers-cache-save
319294
uses: actions/cache/save@v4
320295
with:

0 commit comments

Comments
 (0)