1
+ name : Auto Comment and Mention Reviewers Based on Labels
2
+
3
+ on :
4
+ pull_request :
5
+ types :
6
+ - labeled
7
+ - synchronize
8
+ - opened
9
+
10
+ jobs :
11
+ auto-comment :
12
+ runs-on : ubuntu-latest
13
+
14
+ steps :
15
+ - name : Check out code
16
+ uses : actions/checkout@v2
17
+
18
+ - name : Get PR labels
19
+ id : pr_labels
20
+ env :
21
+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN_REVIEWER }}
22
+ run : |
23
+ # 获取 PR 标签
24
+ LABELS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN_REVIEWER }}" \
25
+ https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels | jq -r '.[].name' | tr '\n' ' ')
26
+ echo "Labels: $LABELS"
27
+ echo "LABELS=$LABELS" >> $GITHUB_ENV
28
+
29
+ - name : Load label-reviewers mapping from JSON file
30
+ id : load-mapping
31
+ run : |
32
+ # 读取仓库中的标签与审核者映射文件
33
+ MAPPING_FILE=".github/label-reviewers.json"
34
+
35
+ if [ ! -f "$MAPPING_FILE" ]; then
36
+ echo "Mapping file not found!"
37
+ exit 1
38
+ fi
39
+
40
+ # 使用 jq 直接从 JSON 文件中读取并获取数据
41
+ LABELS_TO_REVIEWERS=$(cat $MAPPING_FILE)
42
+
43
+ # 输出到文件供后续步骤使用
44
+ echo "$LABELS_TO_REVIEWERS" > labels_to_reviewers.json
45
+
46
+ - name : Prepare reviewer mappings
47
+ id : reviewer-mappings
48
+ run : |
49
+ # 获取 PR 标签并确定需要的审核者
50
+ REQUIRED_REVIEWERS=()
51
+
52
+ # 遍历 PR 标签,并为每个标签添加相应的审核者
53
+ for label in $(echo "${{ env.LABELS }}" | tr " " "\n"); do
54
+ # 使用 jq 从 JSON 文件中获取审核者
55
+ REVIEWERS=$(jq -r --arg label "$label" '.[$label] // empty' labels_to_reviewers.json)
56
+
57
+ if [ -n "$REVIEWERS" ]; then
58
+ # 将审核者添加到 REQUIRED_REVIEWERS 数组
59
+ for reviewer in $(echo $REVIEWERS | tr ',' '\n'); do
60
+ if [ -n "$reviewer" ]; then # 检查是否为非空字符串
61
+ REQUIRED_REVIEWERS+=("$reviewer")
62
+ fi
63
+ done
64
+ fi
65
+ done
66
+
67
+ # 使用 bash 数组去重
68
+ UNIQUE_REVIEWERS=($(echo "${REQUIRED_REVIEWERS[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
69
+
70
+ # 去除可能的空值并格式化审核者列表
71
+ REVIEWERS_LIST=$(echo "${UNIQUE_REVIEWERS[@]}" | tr -s '[:space:]' ' ' | sed 's/^ //;s/ $//')
72
+
73
+ echo "REVIEWERS_LIST=$REVIEWERS_LIST" >> $GITHUB_ENV
74
+
75
+ - name : Add comment and mention reviewers
76
+ run : |
77
+ REVIEWERS="${{ env.REVIEWERS_LIST }}"
78
+
79
+ if [ -z "$REVIEWERS" ]; then
80
+ echo "No reviewers to mention."
81
+ else
82
+ # 构建评论内容,避免空格和空审核者
83
+ COMMENT_BODY=$(jq -n --arg reviewers "$REVIEWERS" --arg labels "${{ env.LABELS }}" \
84
+ '{"body": "Hey \($reviewers), could you please review this PR? 😊\n\nThe following labels were applied:\n- \($labels)\n\nPlease check the changes carefully, and let us know if you need any further details. Thank you for your time!"}')
85
+
86
+ # 添加评论
87
+ RESPONSE=$(curl -s -w "%{http_code}" -o /tmp/response.json -X POST \
88
+ -H "Authorization: token ${{ secrets.GITHUB_TOKEN_REVIEWER }}" \
89
+ -d "$COMMENT_BODY" \
90
+ https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments)
91
+
92
+ # 检查 API 调用是否成功
93
+ HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
94
+ if [ "$HTTP_STATUS" -ne 201 ]; then
95
+ echo "Error posting comment. Response: $RESPONSE"
96
+ exit 1
97
+ fi
98
+ fi
0 commit comments