From a261480d2e0dc4369f4acd22893cee143eaa5295 Mon Sep 17 00:00:00 2001 From: kurisaw <2053731441@qq.com> Date: Mon, 12 May 2025 10:03:06 +0800 Subject: [PATCH] Fixed missing unique files creation and cache logic --- .github/workflows/auto-assign-reviewers.yml | 52 ++++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index 70dd3d43dcf..c4787925429 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -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. @@ -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: | @@ -48,8 +51,8 @@ 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 @@ -57,10 +60,8 @@ jobs: 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 @@ -69,7 +70,6 @@ jobs: fi echo "COMMENT_TIME=${comment_time}" - - name: Parse MAINTAINERS file id: parse_maintainer run: | @@ -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 # 转义路径中的正则特殊字符 @@ -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 @@ -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 \ @@ -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" @@ -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) @@ -217,7 +231,6 @@ jobs: current_time=$(date -u +"%Y-%m-%d %H:%M UTC") { - # 生成审查分配信息 echo "## 📌 Code Review Assignment" echo "" @@ -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: | @@ -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 }} \ No newline at end of file