88# 2025-01-21 kurisaW Initial version
99# 2025-03-14 hydevcode
1010# 2025-05-10 kurisaW Fixed file existence, cache, and comment time issues
11+ # 2025-05-11 kurisaW Fixed missing unique files creation and cache logic
1112
1213# Script Function Description: Assign PR reviews based on the MAINTAINERS list.
1314
@@ -32,12 +33,14 @@ jobs:
3233 run : |
3334 PR_NUMBER=${{ github.event.pull_request.number }}
3435 echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT
36+
3537 - name : Checkout code
3638 uses : actions/checkout@v4
3739 with :
3840 ref : master
3941 sparse-checkout : MAINTAINERS
4042 persist-credentials : false
43+
4144 - name : Get changed files
4245 id : changed_files
4346 run : |
@@ -48,19 +51,17 @@ jobs:
4851 echo "$changed_files" | grep -v '^MAINTAINERS$' > changed_files.txt
4952
5053 existing_comment=$(curl -s \
51- "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
52- 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')
5356
5457 echo "=== Changed Files ==="
5558 cat changed_files.txt
5659 echo "====================="
5760
5861 comment_body=""
5962 if [[ ! -z "$existing_comment" ]]; then
60- 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')
61-
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')
6264 comment_time=$(date -d "$comment_body" +%s)
63-
6465 echo "${comment_body}"
6566 echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
6667 else
6970 fi
7071 echo "COMMENT_TIME=${comment_time}"
7172
72-
7373 - name : Parse MAINTAINERS file
7474 id : parse_maintainer
7575 run : |
9898 - name : Generate reviewers list
9999 id : generate_reviewers
100100 run : |
101- rm -f triggered_reviewers.txt triggered_tags.txt
102- touch triggered_reviewers.txt triggered_tags.txt
101+ rm -f triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt
102+ touch triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt
103103
104104 while IFS='|' read -r tag path reviewers; do
105105 # 转义路径中的正则特殊字符
@@ -113,10 +113,14 @@ jobs:
113113 fi
114114 done < tag_data.csv
115115
116+ # 生成去重的 unique_reviewers.txt 和 unique_tags.txt
117+ sort -u triggered_reviewers.txt > unique_reviewers.txt
118+ sort -u triggered_tags.txt > unique_tags.txt
119+
116120 echo "=== Matched Paths ==="
117- cat triggered_tags .txt
121+ cat unique_tags .txt
118122 echo "=== Matched Reviewers ==="
119- cat triggered_reviewers .txt
123+ cat unique_reviewers .txt
120124
121125 - name : Restore Reviewers Cache
122126 id : reviewers-cache-restore
@@ -127,11 +131,19 @@ jobs:
127131 unique_tags_bak.txt
128132 unique_reviewers_bak.txt
129133 key : ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.COMMENT_TIME }}
134+
130135 - name : Get approval status
131136 id : get_approval
132137 run : |
133138 current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
134- reviewers=$(cat unique_reviewers.txt | tr '\n' '|')
139+
140+ # 检查 unique_reviewers.txt 是否存在且非空
141+ if [[ ! -s unique_reviewers.txt ]]; then
142+ echo "No reviewers found, creating empty unique_reviewers.txt"
143+ touch unique_reviewers.txt
144+ fi
145+
146+ reviewers=$(cat unique_reviewers.txt | tr '\n' '|' | sed 's/|$//')
135147
136148 # 获取 PR 的所有评论
137149 comments=$(curl -s \
@@ -187,7 +199,6 @@ jobs:
187199
188200 if [[ -n "${approvals[$reviewer]}" ]]; then
189201 timestamp=$(date -d "${approvals[$reviewer]}" -u +"%Y-%m-%d %H:%M UTC")
190-
191202 echo "- ✅ **$formatted_reviewers** Reviewed On $timestamp"
192203 else
193204 echo "- ⌛ **$formatted_reviewers** Pending Review"
@@ -196,11 +207,14 @@ jobs:
196207 } > review_status.md
197208
198209 echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT
210+
199211 - name : Generate review data
200212 id : generate_review
201213 run : |
202214 unique_tags=""
203- unique_tags=$(cat unique_tags.txt | xargs)
215+ if [[ -s unique_tags.txt ]]; then
216+ unique_tags=$(cat unique_tags.txt | xargs)
217+ fi
204218 unique_tags_bak=""
205219 if [[ -f unique_tags_bak.txt ]]; then
206220 unique_tags_bak=$(cat unique_tags_bak.txt | xargs)
@@ -217,7 +231,6 @@ jobs:
217231
218232 current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
219233 {
220-
221234 # 生成审查分配信息
222235 echo "## 📌 Code Review Assignment"
223236 echo ""
@@ -264,6 +277,7 @@ jobs:
264277 echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**"
265278 echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**"
266279 } > review_data.md
280+
267281 - name : Post/Update comment
268282 id : post_comment
269283 run : |
@@ -289,22 +303,24 @@ jobs:
289303 -d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
290304 "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
291305 fi
306+
292307 - name : Get Comment Time
293308 id : get_comment_time
294309 run : |
295310 existing_comment=$(curl -s \
296- "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
297- jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
311+ "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
312+ jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
298313 comment_body="${{ steps.get_approval.outputs.CURRENT_TIME }}"
299314 comment_time=$(date -d "$comment_body" +%s)
300315 echo "CURRENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
301316 cp unique_reviewers.txt unique_reviewers_bak.txt
302317 cp unique_tags.txt unique_tags_bak.txt
303- - name : Restore Reviewers Save
318+
319+ - name : Save Reviewers Cache
304320 id : reviewers-cache-save
305321 uses : actions/cache/save@v4
306322 with :
307323 path : |
308324 unique_tags_bak.txt
309325 unique_reviewers_bak.txt
310- key : ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }}
326+ key : ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }}
0 commit comments