Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions .github/workflows/auto-assign-reviewers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# 2025-01-21 kurisaW Initial version
# 2025-03-14 hydevcode
# 2025-05-10 kurisaW Fixed file existence, cache, and comment time issues
# 2025-05-11 kurisaW Fixed missing unique files creation and cache logic

# Script Function Description: Assign PR reviews based on the MAINTAINERS list.

Expand All @@ -32,12 +33,14 @@ jobs:
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT

- name: Checkout code
uses: actions/checkout@v4
with:
ref: master
sparse-checkout: MAINTAINERS
persist-credentials: false

- name: Get changed files
id: changed_files
run: |
Expand All @@ -48,19 +51,17 @@ jobs:
echo "$changed_files" | grep -v '^MAINTAINERS$' > changed_files.txt

existing_comment=$(curl -s \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')

echo "=== Changed Files ==="
cat changed_files.txt
echo "====================="

comment_body=""
if [[ ! -z "$existing_comment" ]]; then
comment_body=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .body|sed -nE 's/.*Last Updated: ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} UTC).*/\1/p')

comment_body=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .body | sed -nE 's/.*Last Updated: ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} UTC).*/\1/p')
comment_time=$(date -d "$comment_body" +%s)

echo "${comment_body}"
echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
else
Expand All @@ -69,7 +70,6 @@ jobs:
fi
echo "COMMENT_TIME=${comment_time}"


- name: Parse MAINTAINERS file
id: parse_maintainer
run: |
Expand Down Expand Up @@ -98,8 +98,8 @@ jobs:
- name: Generate reviewers list
id: generate_reviewers
run: |
rm -f triggered_reviewers.txt triggered_tags.txt
touch triggered_reviewers.txt triggered_tags.txt
rm -f triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt
touch triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt

while IFS='|' read -r tag path reviewers; do
# 转义路径中的正则特殊字符
Expand All @@ -113,10 +113,14 @@ jobs:
fi
done < tag_data.csv

# 生成去重的 unique_reviewers.txt 和 unique_tags.txt
sort -u triggered_reviewers.txt > unique_reviewers.txt
sort -u triggered_tags.txt > unique_tags.txt

echo "=== Matched Paths ==="
cat triggered_tags.txt
cat unique_tags.txt
echo "=== Matched Reviewers ==="
cat triggered_reviewers.txt
cat unique_reviewers.txt

- name: Restore Reviewers Cache
id: reviewers-cache-restore
Expand All @@ -127,11 +131,19 @@ jobs:
unique_tags_bak.txt
unique_reviewers_bak.txt
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.COMMENT_TIME }}

- name: Get approval status
id: get_approval
run: |
current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
reviewers=$(cat unique_reviewers.txt | tr '\n' '|')

# 检查 unique_reviewers.txt 是否存在且非空
if [[ ! -s unique_reviewers.txt ]]; then
echo "No reviewers found, creating empty unique_reviewers.txt"
touch unique_reviewers.txt
fi

reviewers=$(cat unique_reviewers.txt | tr '\n' '|' | sed 's/|$//')

# 获取 PR 的所有评论
comments=$(curl -s \
Expand Down Expand Up @@ -187,7 +199,6 @@ jobs:

if [[ -n "${approvals[$reviewer]}" ]]; then
timestamp=$(date -d "${approvals[$reviewer]}" -u +"%Y-%m-%d %H:%M UTC")

echo "- ✅ **$formatted_reviewers** Reviewed On $timestamp"
else
echo "- ⌛ **$formatted_reviewers** Pending Review"
Expand All @@ -196,11 +207,14 @@ jobs:
} > review_status.md

echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT

- name: Generate review data
id: generate_review
run: |
unique_tags=""
unique_tags=$(cat unique_tags.txt | xargs)
if [[ -s unique_tags.txt ]]; then
unique_tags=$(cat unique_tags.txt | xargs)
fi
unique_tags_bak=""
if [[ -f unique_tags_bak.txt ]]; then
unique_tags_bak=$(cat unique_tags_bak.txt | xargs)
Expand All @@ -217,7 +231,6 @@ jobs:

current_time=$(date -u +"%Y-%m-%d %H:%M UTC")
{

# 生成审查分配信息
echo "## 📌 Code Review Assignment"
echo ""
Expand Down Expand Up @@ -264,6 +277,7 @@ jobs:
echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**"
echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**"
} > review_data.md

- name: Post/Update comment
id: post_comment
run: |
Expand All @@ -289,22 +303,24 @@ jobs:
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
fi

- name: Get Comment Time
id: get_comment_time
run: |
existing_comment=$(curl -s \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64')
comment_body="${{ steps.get_approval.outputs.CURRENT_TIME }}"
comment_time=$(date -d "$comment_body" +%s)
echo "CURRENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
cp unique_reviewers.txt unique_reviewers_bak.txt
cp unique_tags.txt unique_tags_bak.txt
- name: Restore Reviewers Save

- name: Save Reviewers Cache
id: reviewers-cache-save
uses: actions/cache/save@v4
with:
path: |
unique_tags_bak.txt
unique_reviewers_bak.txt
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }}
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }}
Loading