Skip to content

Commit

Permalink
ci: appoint PR reviewers via action bot reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
kurisaW committed Jan 14, 2025
1 parent 3436aa6 commit 8d799f2
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/label-reviewers.json
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"
}
98 changes: 98 additions & 0 deletions .github/workflows/auto-assign-reviewers.yml
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.RTTHREAD_GITHUB_TOKEN }}
run: |
# 获取 PR 标签
LABELS=$(curl -s -H "Authorization: token ${{ secrets.RTTHREAD_GITHUB_TOKEN }}" \
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.RTTHREAD_GITHUB_TOKEN }}" \
-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

0 comments on commit 8d799f2

Please sign in to comment.