Skip to content

Commit 6a39a62

Browse files
committed
ci: appoint PR reviewers via action bot reviews
1 parent 3436aa6 commit 6a39a62

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

.github/label-reviewers.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"arduino": "@mysterywolf, @Rbb666, @kurisaW",
3+
"action": "@supperthomas",
4+
"Kernel": "@BernardXiong",
5+
"BSP: Cvitek": "@unicornx",
6+
"BSP: Renesas": "@Rbb666, @kurisaW"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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_COMMENT }}
22+
run: |
23+
# 获取 PR 标签
24+
LABELS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN_COMMENT }}" \
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_COMMENT }}" \
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

.github/workflows/bsp_buildings.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,6 @@ jobs:
508508
- name: Post failure comment
509509
if: failure()
510510
run: |
511-
curl -X POST -H "Authorization: token ${{ secrets.RTTHREAD_GITHUB_TOKEN }}" \
511+
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN_COMMENT }}" \
512512
-d '{"body":"@${{ github.actor }}, Thank you for your contribution, but there was an error with the action. Could you please help check the BSP compilation issue? Thank you."}' \
513513
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"

.github/workflows/manual_trigger_update_all.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ jobs:
140140
- name: Commit changes
141141
uses: devops-infra/action-commit-push@master
142142
with:
143-
github_token: "${{ secrets.RTTHREAD_GITHUB_TOKEN }}"
143+
github_token: "${{ secrets.GITHUB_TOKEN_COMMENT }}"
144144
commit_prefix: "[AUTO]"
145145
commit_message: "Updated dependencies"
146146
force: false
@@ -149,7 +149,7 @@ jobs:
149149
- name: Create Pull Request
150150
uses: devops-infra/action-pull-request@master
151151
with:
152-
github_token: ${{ secrets.RTTHREAD_GITHUB_TOKEN }}
152+
github_token: ${{ secrets.GITHUB_TOKEN_COMMENT }}
153153
title: "[action] Automated PR for updates"
154154
body: "This PR was created automatically by GitHub Actions."
155155
source_branch: updated_deps

0 commit comments

Comments
 (0)