77# Date Author Notes
88# 2025-01-21 kurisaW Initial version
99# 2025-03-14 hydevcode
10+ # 2025-05-10 kurisaW Fixed file existence, cache, and comment time issues
1011
1112# Script Function Description: Assign PR reviews based on the MAINTAINERS list.
1213
5051 "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
5152 jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
5253
54+ echo "=== Changed Files ==="
55+ cat changed_files.txt
56+ echo "====================="
57+
5358 comment_body=""
5459 if [[ ! -z "$existing_comment" ]]; then
5560 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')
6368 echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
6469 fi
6570 echo "COMMENT_TIME=${comment_time}"
71+
72+
6673 - name : Parse MAINTAINERS file
6774 id : parse_maintainer
6875 run : |
7279 /^tag:/ {
7380 tag = substr($0, index($0, $2)) # 提取标签内容
7481 }
75- /^path:/ {
76- path = substr($0, index($0, $2)) # 提取路径内容
82+ /^path:/ {
83+ # 提取 path 字段并去除前后空格
84+ path = substr($0, index($0, $2))
85+ gsub(/^[ \t]+|[ \t]+$/, "", path) # 清理前后空格和制表符
7786 }
7887 /^owners:/ {
7988 owners = substr($0, index($0, $2)) # 提取维护者信息
@@ -85,21 +94,30 @@ jobs:
8594 print tag "|" path "|" github_ids
8695 }
8796 ' MAINTAINERS > tag_data.csv
97+
8898 - name : Generate reviewers list
8999 id : generate_reviewers
90100 run : |
91- # 根据变更文件路径匹配维护者规则
92- rm -f triggered_reviewers.txt
93- rm -f triggered_tags.txt
101+ rm -f triggered_reviewers.txt triggered_tags.txt
102+ touch triggered_reviewers.txt triggered_tags .txt
103+
94104 while IFS='|' read -r tag path reviewers; do
95- # 使用正则匹配路径(支持子目录)
96- if grep -qE "^$path(/|$)" changed_files.txt; then
97- echo "$reviewers" | tr ' ' '\n' >> triggered_reviewers.txt
98- echo "$tag" | tr ' ' '\n' >> triggered_tags.txt
105+ # 转义路径中的正则特殊字符
106+ escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path")
107+
108+ # 使用增强型正则匹配路径及其所有子目录
109+ if grep -qE "^$escaped_path(/.*)*" changed_files.txt; then
110+ echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt
111+ echo "$tag" >> triggered_tags.txt
112+ echo "Matched: $path → $tag"
99113 fi
100114 done < tag_data.csv
101- awk 'NF && !seen[$0]++' triggered_reviewers.txt > unique_reviewers.txt
102- awk 'NF && !seen[$0]++' triggered_tags.txt > unique_tags.txt
115+
116+ echo "=== Matched Paths ==="
117+ cat triggered_tags.txt
118+ echo "=== Matched Reviewers ==="
119+ cat triggered_reviewers.txt
120+
103121 - name : Restore Reviewers Cache
104122 id : reviewers-cache-restore
105123 if : ${{ steps.changed_files.outputs.COMMENT_TIME != '' }}
@@ -289,4 +307,4 @@ jobs:
289307 path : |
290308 unique_tags_bak.txt
291309 unique_reviewers_bak.txt
292- key : ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }}
310+ key : ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }}
0 commit comments