Skip to content

Commit 2a262e2

Browse files
committed
modify script
1 parent 3436aa6 commit 2a262e2

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#
2+
# Copyright (c) 2006-2025, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2025-01-21 kurisaW Initial version
9+
#
10+
11+
# Script Function Description: Assign PR reviews based on the MAINTAINERS list.
12+
13+
name: Auto Review Assistant
14+
15+
on:
16+
pull_request_target:
17+
types: [opened, synchronize, reopened]
18+
workflow_dispatch:
19+
issue_comment:
20+
types: [created]
21+
22+
jobs:
23+
assign-reviewers:
24+
runs-on: ubuntu-22.04
25+
permissions:
26+
issues: write
27+
pull-requests: write
28+
contents: read
29+
env:
30+
SUPER_SECRET: ${{ secrets.ACTION_TOKEN_AUTO_REVIEW }}
31+
steps:
32+
- name: Checkout code
33+
uses: actions/checkout@v3
34+
35+
- name: Get changed files
36+
id: changed_files
37+
run: |
38+
# 通过 GitHub API 获取 PR 的变更文件列表
39+
changed_files=$(curl -s \
40+
-H "Authorization: Bearer ${{ github.token }}" \
41+
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" | \
42+
jq -r '.[].filename') # 使用 jq 提取文件名
43+
44+
echo "$changed_files" | grep -v '^MAINTAINERS$' > changed_files.txt
45+
46+
- name: Parse MAINTAINERS file
47+
id: parse_maintainer
48+
run: |
49+
# 使用 AWK 解析 MAINTAINERS 文件格式:
50+
# 提取 tag(标签)、path(路径)和 owners(维护者 GitHub ID)
51+
awk '
52+
/^tag:/ {
53+
tag = substr($0, index($0, $2)) # 提取标签内容
54+
}
55+
/^path:/ {
56+
path = substr($0, index($0, $2)) # 提取路径内容
57+
}
58+
/^owners:/ {
59+
owners = substr($0, index($0, $2)) # 提取维护者信息
60+
split(owners, parts, /[()]/) # 拆分出 GitHub ID(括号内内容)
61+
github_ids = ""
62+
for (i=2; i<=length(parts); i+=2) {
63+
github_ids = github_ids "@" parts[i] " " # 拼接为 @user 格式
64+
}
65+
print tag "|" path "|" github_ids
66+
}
67+
' MAINTAINERS > tag_data.csv
68+
69+
- name: Generate reviewers list
70+
id: generate_reviewers
71+
run: |
72+
# 根据变更文件路径匹配维护者规则
73+
rm -f triggered_reviewers.txt
74+
while IFS='|' read -r tag path reviewers; do
75+
# 使用正则匹配路径(支持子目录)
76+
if grep -qE "^$path(/|$)" changed_files.txt; then
77+
echo "$reviewers" | tr ' ' '\n' >> triggered_reviewers.txt
78+
fi
79+
done < tag_data.csv
80+
# 去重处理
81+
awk 'NF && !seen[$0]++' triggered_reviewers.txt > unique_reviewers.txt
82+
83+
- name: Get approval status
84+
id: get_approval
85+
run: |
86+
current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
87+
reviewers=$(cat unique_reviewers.txt | tr '\n' '|')
88+
89+
# 获取 PR 的所有评论
90+
comments=$(curl -s \
91+
-H "Authorization: Bearer ${{ github.token }}" \
92+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments")
93+
94+
echo '#!/bin/bash' > approval_data.sh
95+
echo 'declare -A approvals=()' >> approval_data.sh
96+
97+
# 使用 jq 解析包含 LGTM 的有效评论
98+
jq -r --arg reviewers "$reviewers" '
99+
.[] |
100+
select(.user.login != "github-actions[bot]") | # 排除 bot 的评论
101+
select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写)
102+
.user.login as $user |
103+
"@\($user)" as $mention |
104+
select($mention | inside($reviewers)) | # 过滤有效审查者
105+
"approvals[\"\($mention)\"]=\"\(.created_at)\"" # 记录审批时间
106+
' <<< "$comments" >> approval_data.sh
107+
108+
# 加载审查数据并生成状态报告
109+
chmod +x approval_data.sh
110+
source ./approval_data.sh
111+
112+
{
113+
echo "---"
114+
echo "### 📊 Current Review Status (Last Updated: $current_time)"
115+
while read -r reviewer; do
116+
if [[ -n "${approvals[$reviewer]}" ]]; then
117+
timestamp=$(date -d "${approvals[$reviewer]}" -u +"%Y-%m-%d %H:%M UTC")
118+
echo "- ✅ **$reviewer** Reviewed On $timestamp"
119+
else
120+
echo "- ⌛ **$reviewer** Pending Review"
121+
fi
122+
done < unique_reviewers.txt
123+
} > review_status.md
124+
125+
- name: Generate review data
126+
id: generate_review
127+
run: |
128+
current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
129+
{
130+
# 生成审查分配信息
131+
echo "## 📌 Code Review Assignment"
132+
echo ""
133+
134+
while IFS='|' read -r tag path reviewers; do
135+
if grep -qE "^$path(/|$)" changed_files.txt; then
136+
echo "### 🏷️ Tag: $tag"
137+
echo "**Path:** \`$path\` "
138+
echo "**Reviewers:** $reviewers "
139+
echo "<details>"
140+
echo "<summary><b>Changed Files</b> (Click to expand)</summary>"
141+
echo ""
142+
grep -E "^$path(/|$)" changed_files.txt | sed 's/^/- /' # 列出匹配的变更文件
143+
echo ""
144+
echo "</details>"
145+
echo ""
146+
fi
147+
done < tag_data.csv
148+
# 插入审查状态
149+
cat review_status.md
150+
151+
echo "---"
152+
echo "### 📝 Review Instructions"
153+
echo ""
154+
echo "1. **维护者可以通过单击此处来刷新审查状态:** [🔄 刷新状态](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
155+
echo " **Maintainers can refresh the review status by clicking here:** [🔄 Refresh Status](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
156+
echo ""
157+
echo "2. **确认审核通过后评论 \`LGTM/lgtm\`**"
158+
echo " **Comment \`LGTM/lgtm\` after confirming approval**"
159+
echo ""
160+
echo "3. **PR合并前需至少一位维护者确认**"
161+
echo " **PR must be confirmed by at least one maintainer before merging**"
162+
echo ""
163+
echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**"
164+
echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**"
165+
} > review_data.md
166+
167+
- name: Post/Update comment
168+
id: post_comment
169+
run: |
170+
# 查找现有的 bot 评论
171+
existing_comment=$(curl -s \
172+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
173+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | \
174+
jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64')
175+
176+
if [[ -n "$existing_comment" ]]; then
177+
# 更新现有评论
178+
comment_id=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .id)
179+
echo "Updating existing comment $comment_id"
180+
response=$(curl -s -X PATCH \
181+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
182+
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
183+
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id")
184+
else
185+
# 创建新评论
186+
echo "Creating new comment"
187+
response=$(curl -s -X POST \
188+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
189+
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
190+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments")
191+
fi

MAINTAINERS

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# List of maintainers of the RT-Thread open-source community
2+
#
3+
# This file manages the maintainers and their associated sections in the repository.
4+
# Developers should update this file according to project needs.
5+
# The following are explanations of each field and guidelines for adding new maintainer entries.
6+
#
7+
# When adding new entries, please follow the format:
8+
#
9+
# 1. **tag** - Assign a unique tag to each entry for identifying the code module or functionality.
10+
# - The tag should be concise and descriptive, such as `workflow`, `libc`...
11+
# - **Rule for Adding**: Use a new tag when adding a new functionality or module to ensure it clearly describes the area of responsibility.
12+
#
13+
# 2. **path** - Specify the directory or file path that the maintainer is responsible for.
14+
# - The path must be relative to the repository's root directory and can refer to either a single file or a folder.
15+
# - If the maintainer is responsible for all files in a directory, use the directory path; if it's for a specific file, provide the full file path.
16+
# - **Rule for Adding**: Ensure that the path correctly points to the relevant code location. Please note that a tag should correspond to only one path. Currently, multiple paths are not supported.
17+
#
18+
# 3. **owners** - List the maintainers responsible for the section, including their GitHub usernames and contact information.
19+
# - The owners should be listed as a comma-separated list if there are multiple maintainers.
20+
# - Format: `Name(GitHub username)<email address>`.
21+
# - **Rule for Adding**: Ensure that the listed GitHub usernames are correct, and the maintainers are aware of their responsibilities and duties.
22+
#
23+
# Example: How to Add a Maintainer Entry
24+
#
25+
# The following is a template for adding new entries in the MAINTAINER file:
26+
#
27+
# tag: <module-name>
28+
# path: <file-or-directory-path>
29+
# owners: <maintainer1>, <maintainer2>, ...
30+
#
31+
# When adding entries, please follow these guidelines:
32+
# - Ensure the `tag` is unique and descriptive.
33+
# - Ensure the `path` points to the correct location in the repository.
34+
# - Ensure the `owners` are accurate and that all new maintainers are aware of their responsibilities.
35+
#
36+
# Example Entry:
37+
# tag: example-module
38+
# path: example/module/path
39+
# owners: John Doe(johndoe)<[email protected]>, Jane Smith(janesmith)<[email protected]>
40+
41+
# Note:
42+
# - Each entry includes a `tag` that identifies the module or functionality, a `path` that points to the relevant code location, and `owners` who are the maintainers for that part of the codebase.
43+
# - If there are multiple entries, each entry should be separated by a blank line. Within a single entry, there is no need to insert blank lines between the tag, path, and owners.
44+
45+
# Below are existing maintainer entries, divided by module:
46+
47+
tag: workflow
48+
path: .github
49+
owners: supper thomas(supperthomas)<[email protected]>
50+
51+
tag: stm32f407-rt-spark
52+
path: bsp/stm32/stm32f407-rt-spark
53+
owners: Bingru Zhang(Rbb666)<[email protected]>, Yuqiang Wang(kurisaW)<[email protected]>
54+
55+
tag: libc
56+
path: components/libc
57+
owners: Meco Jianting Man(mysterywolf)<[email protected]>

0 commit comments

Comments
 (0)