2121jobs :
2222 assign-reviewers :
2323 runs-on : ubuntu-22.04
24- if : github.repository_owner == 'RT-Thread'
24+ if : github.repository_owner == 'RT-Thread' && github.event.pull_request.head.repo.full_name == github.repository
2525 permissions :
2626 issues : read
2727 pull-requests : write
@@ -32,30 +32,59 @@ jobs:
3232 run : |
3333 PR_NUMBER=${{ github.event.pull_request.number }}
3434 echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT
35+ echo "DEBUG: Extracted PR number: $PR_NUMBER" >&2
36+
3537 - name : Checkout code
3638 uses : actions/checkout@v4
3739 with :
3840 ref : master
3941 sparse-checkout : MAINTAINERS
4042 persist-credentials : false
43+
44+ - name : Verify MAINTAINERS file
45+ run : |
46+ if [[ ! -f $GITHUB_WORKSPACE/MAINTAINERS ]]; then
47+ echo "Error: MAINTAINERS file not found"
48+ exit 1
49+ fi
50+ echo "DEBUG: MAINTAINERS file verified" >&2
51+
4152 - name : Get changed files
4253 id : changed_files
4354 run : |
44- # Initialize changed_files.txt
4555 touch $GITHUB_WORKSPACE/changed_files.txt
4656
47- # Get changed files via GitHub API
48- changed_files=$(curl -s \
49- -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
50- "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \
51- jq -r '.[].filename')
52- echo "$changed_files" | grep -v '^MAINTAINERS$' > $GITHUB_WORKSPACE/changed_files.txt
57+ # Retry API call up to 3 times
58+ for attempt in {1..3}; do
59+ changed_files=$(curl -s -f \
60+ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
61+ "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \
62+ jq -r '.[].filename' || echo "")
63+ if [[ -n "$changed_files" ]]; then
64+ echo "$changed_files" | grep -v '^MAINTAINERS$' > $GITHUB_WORKSPACE/changed_files.txt
65+ break
66+ fi
67+ echo "Attempt $attempt failed to fetch changed files, retrying..."
68+ sleep 2
69+ done
70+
71+ if [[ -z "$changed_files" ]]; then
72+ echo "Error: Failed to fetch changed files after 3 attempts"
73+ exit 1
74+ fi
5375
5476 # Get existing bot comment
55- existing_comment=$(curl -s \
56- -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
57- "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
58- jq -r '.[] | select(.user.login == "github-actions[bot]") | .body | @base64')
77+ for attempt in {1..3}; do
78+ existing_comment=$(curl -s -f \
79+ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
80+ "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
81+ jq -r '.[] | select(.user.login == "github-actions[bot]") | .body | @base64' || echo "")
82+ if [[ -n "$existing_comment" || $attempt -eq 3 ]]; then
83+ break
84+ fi
85+ echo "Attempt $attempt failed to fetch comments, retrying..."
86+ sleep 2
87+ done
5988
6089 echo "=== Changed Files ==="
6190 cat $GITHUB_WORKSPACE/changed_files.txt
@@ -69,14 +98,13 @@ jobs:
6998 fi
7099 fi
71100 echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
72-
101+ echo "DEBUG: Comment time: $comment_time" >&2
102+
73103 - name : Parse MAINTAINERS file
74104 id : parse_maintainer
75105 run : |
76- # Initialize tag_data.csv
77106 touch $GITHUB_WORKSPACE/tag_data.csv
78107
79- # Parse MAINTAINERS file
80108 awk '
81109 /^tag:/ {
82110 tag = substr($0, index($0, $2))
@@ -95,41 +123,54 @@ jobs:
95123 print tag "|" path "|" github_ids
96124 }
97125 ' $GITHUB_WORKSPACE/MAINTAINERS > $GITHUB_WORKSPACE/tag_data.csv
98-
126+ echo "DEBUG: MAINTAINERS parsed, tag_data.csv created" >&2
127+
99128 - name : Generate reviewers list
100129 id : generate_reviewers
101130 run : |
102- # Initialize output files
103131 touch $GITHUB_WORKSPACE/triggered_reviewers.txt $GITHUB_WORKSPACE/triggered_tags.txt $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_tags.txt
104132
105133 while IFS='|' read -r tag path reviewers; do
106134 escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path")
107- if grep -qE "^$escaped_path(/.*)*" $GITHUB_WORKSPACE/changed_files.txt; then
135+ if [[ "$path" == */ ]]; then
136+ grep -qE "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt
137+ else
138+ grep -qE "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt
139+ fi
140+ if [[ $? -eq 0 ]]; then
108141 echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> $GITHUB_WORKSPACE/triggered_reviewers.txt
109142 echo "$tag" >> $GITHUB_WORKSPACE/triggered_tags.txt
110- echo "Matched: $path → $tag"
143+ echo "DEBUG: Matched: $path → $tag" >&2
111144 fi
112145 done < $GITHUB_WORKSPACE/tag_data.csv
113146
114- # Generate unique lists
115147 sort -u $GITHUB_WORKSPACE/triggered_reviewers.txt > $GITHUB_WORKSPACE/unique_reviewers.txt
116148 sort -u $GITHUB_WORKSPACE/triggered_tags.txt > $GITHUB_WORKSPACE/unique_tags.txt
117149
118150 echo "=== Matched Paths ==="
119151 cat $GITHUB_WORKSPACE/triggered_tags.txt
120152 echo "=== Matched Reviewers ==="
121153 cat $GITHUB_WORKSPACE/triggered_reviewers.txt
122-
154+
123155 - name : Restore Reviewers Cache
124156 id : reviewers-cache-restore
125- if : ${{ steps.changed_files.outputs.COMMENT_TIME != '' }}
157+ if : steps.changed_files.outputs.COMMENT_TIME != ''
126158 uses : actions/cache/restore@v4
127159 with :
128160 path : |
129161 $GITHUB_WORKSPACE/unique_tags_bak.txt
130162 $GITHUB_WORKSPACE/unique_reviewers_bak.txt
131163 key : ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.COMMENT_TIME }}
132-
164+
165+ - name : Validate Restored Cache
166+ if : steps.reviewers-cache-restore.outputs.cache-hit == 'true'
167+ run : |
168+ if [[ ! -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt || ! -f $GITHUB_WORKSPACE/unique_tags_bak.txt ]]; then
169+ echo "Error: Invalid cache data"
170+ exit 1
171+ fi
172+ echo "DEBUG: Cache validated" >&2
173+
133174 - name : Get approval status
134175 id : get_approval
135176 run : |
@@ -138,9 +179,21 @@ jobs:
138179
139180 reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers.txt | tr '\n' '|' | sed 's/|$//')
140181
141- comments=$(curl -s \
142- -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
143- "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
182+ for attempt in {1..3}; do
183+ comments=$(curl -s -f \
184+ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
185+ "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" || echo "")
186+ if [[ -n "$comments" || $attempt -eq 3 ]]; then
187+ break
188+ fi
189+ echo "Attempt $attempt failed to fetch comments, retrying..."
190+ sleep 2
191+ done
192+
193+ if [[ -z "$comments" ]]; then
194+ echo "Error: Failed to fetch comments after 3 attempts"
195+ exit 1
196+ fi
144197
145198 echo '#!/bin/bash' > $GITHUB_WORKSPACE/approval_data.sh
146199 echo 'declare -A approvals=()' >> $GITHUB_WORKSPACE/approval_data.sh
@@ -192,7 +245,8 @@ jobs:
192245 } > $GITHUB_WORKSPACE/review_status.md
193246
194247 echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT
195-
248+ echo "DEBUG: Approval status generated" >&2
249+
196250 - name : Generate review data
197251 id : generate_review
198252 run : |
@@ -217,7 +271,12 @@ jobs:
217271 echo ""
218272 while IFS='|' read -r tag path reviewers; do
219273 escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path")
220- if grep -qE "^$escaped_path(/|$)" $GITHUB_WORKSPACE/changed_files.txt; then
274+ if [[ "$path" == */ ]]; then
275+ grep -qE "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt
276+ else
277+ grep -qE "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt
278+ fi
279+ if [[ $? -eq 0 ]]; then
221280 echo "### 🏷️ Tag: $tag"
222281 echo "**Path:** \`$path\`"
223282 if [[ " $existing_tags " =~ " $tag " ]]; then
@@ -229,7 +288,11 @@ jobs:
229288 echo "<details>"
230289 echo "<summary><b>Changed Files</b> (Click to expand)</summary>"
231290 echo ""
232- grep -E "^$escaped_path(/|$)" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /'
291+ if [[ "$path" == */ ]]; then
292+ grep -E "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /'
293+ else
294+ grep -E "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /'
295+ fi
233296 echo ""
234297 echo "</details>"
235298 echo ""
@@ -251,14 +314,37 @@ jobs:
251314 echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**"
252315 echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**"
253316 } > $GITHUB_WORKSPACE/review_data.md
254-
317+ echo "DEBUG: Review data generated" >&2
318+
255319 - name : Post/Update comment
256320 id : post_comment
257321 run : |
258- existing_comment=$(curl -s \
259- -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
260- "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
261- jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64')
322+ for attempt in {1..3}; do
323+ existing_comment=$(curl -s -f \
324+ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
325+ "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
326+ jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64' || echo "")
327+ if [[ -n "$existing_comment" || $attempt -eq 3 ]]; then
328+ break
329+ fi
330+ echo "Attempt $attempt failed to fetch comments, retrying..."
331+ sleep 2
332+ done
333+
334+ new_reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers.txt | sort | tr '\n' ' ')
335+ old_reviewers=""
336+ if [[ -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt ]]; then
337+ old_reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers_bak.txt | sort | tr '\n' ' ')
338+ fi
339+
340+ if [[ "$new_reviewers" != "$old_reviewers" && -n "$existing_comment" ]]; then
341+ comment_id=$(echo "$existing_comment" | base64 -d | jq -r .id)
342+ echo "Reviewers changed, deleting old comment $comment_id"
343+ curl -s -X DELETE \
344+ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
345+ "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id"
346+ existing_comment=""
347+ fi
262348
263349 if [[ -n "$existing_comment" ]]; then
264350 comment_id=$(echo "$existing_comment" | base64 -d | jq -r .id)
@@ -274,15 +360,17 @@ jobs:
274360 -d "$(jq -n --arg body "$(cat $GITHUB_WORKSPACE/review_data.md)" '{body: $body}')" \
275361 "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments"
276362 fi
277-
363+ echo "DEBUG: Comment posted/updated" >&2
364+
278365 - name : Get Comment Time
279366 id : get_comment_time
280367 run : |
281368 current_time=$(date -u +"%s")
282369 echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT
283370 cp $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_reviewers_bak.txt
284371 cp $GITHUB_WORKSPACE/unique_tags.txt $GITHUB_WORKSPACE/unique_tags_bak.txt
285-
372+ echo "DEBUG: Comment time: $current_time" >&2
373+
286374 - name : Save Reviewers Cache
287375 id : reviewers-cache-save
288376 uses : actions/cache/save@v4
0 commit comments