-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: appoint PR reviewers via action bot reviews
- Loading branch information
Showing
4 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"arduino": "@mysterywolf, @Rbb666, @kurisaW", | ||
"action": "@supperthomas", | ||
"Kernel": "@BernardXiong", | ||
"BSP: Cvitek": "@unicornx", | ||
"BSP: Renesas": "@Rbb666, @kurisaW" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
name: Auto Comment and Mention Reviewers Based on Labels | ||
|
||
on: | ||
pull_request: | ||
types: | ||
- labeled | ||
- synchronize | ||
- opened | ||
|
||
jobs: | ||
auto-comment: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Get PR labels | ||
id: pr_labels | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN_COMMENT }} | ||
run: | | ||
# 获取 PR 标签 | ||
LABELS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN_COMMENT }}" \ | ||
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels | jq -r '.[].name' | tr '\n' ' ') | ||
echo "Labels: $LABELS" | ||
echo "LABELS=$LABELS" >> $GITHUB_ENV | ||
- name: Load label-reviewers mapping from JSON file | ||
id: load-mapping | ||
run: | | ||
# 读取仓库中的标签与审核者映射文件 | ||
MAPPING_FILE=".github/label-reviewers.json" | ||
if [ ! -f "$MAPPING_FILE" ]; then | ||
echo "Mapping file not found!" | ||
exit 1 | ||
fi | ||
|
||
# 使用 jq 直接从 JSON 文件中读取并获取数据 | ||
LABELS_TO_REVIEWERS=$(cat $MAPPING_FILE) | ||
|
||
# 输出到文件供后续步骤使用 | ||
echo "$LABELS_TO_REVIEWERS" > labels_to_reviewers.json | ||
|
||
- name: Prepare reviewer mappings | ||
id: reviewer-mappings | ||
run: | | ||
# 获取 PR 标签并确定需要的审核者 | ||
REQUIRED_REVIEWERS=() | ||
# 遍历 PR 标签,并为每个标签添加相应的审核者 | ||
for label in $(echo "${{ env.LABELS }}" | tr " " "\n"); do | ||
# 使用 jq 从 JSON 文件中获取审核者 | ||
REVIEWERS=$(jq -r --arg label "$label" '.[$label] // empty' labels_to_reviewers.json) | ||
if [ -n "$REVIEWERS" ]; then | ||
# 将审核者添加到 REQUIRED_REVIEWERS 数组 | ||
for reviewer in $(echo $REVIEWERS | tr ',' '\n'); do | ||
if [ -n "$reviewer" ]; then # 检查是否为非空字符串 | ||
REQUIRED_REVIEWERS+=("$reviewer") | ||
fi | ||
done | ||
fi | ||
done | ||
# 使用 bash 数组去重 | ||
UNIQUE_REVIEWERS=($(echo "${REQUIRED_REVIEWERS[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) | ||
# 去除可能的空值并格式化审核者列表 | ||
REVIEWERS_LIST=$(echo "${UNIQUE_REVIEWERS[@]}" | tr -s '[:space:]' ' ' | sed 's/^ //;s/ $//') | ||
echo "REVIEWERS_LIST=$REVIEWERS_LIST" >> $GITHUB_ENV | ||
- name: Add comment and mention reviewers | ||
run: | | ||
REVIEWERS="${{ env.REVIEWERS_LIST }}" | ||
if [ -z "$REVIEWERS" ]; then | ||
echo "No reviewers to mention." | ||
else | ||
# 构建评论内容,避免空格和空审核者 | ||
COMMENT_BODY=$(jq -n --arg reviewers "$REVIEWERS" --arg labels "${{ env.LABELS }}" \ | ||
'{"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!"}') | ||
# 添加评论 | ||
RESPONSE=$(curl -s -w "%{http_code}" -o /tmp/response.json -X POST \ | ||
-H "Authorization: token ${{ secrets.GITHUB_TOKEN_COMMENT }}" \ | ||
-d "$COMMENT_BODY" \ | ||
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments) | ||
# 检查 API 调用是否成功 | ||
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1) | ||
if [ "$HTTP_STATUS" -ne 201 ]; then | ||
echo "Error posting comment. Response: $RESPONSE" | ||
exit 1 | ||
fi | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters