From 0759748e4a5095c3eded70c9fe1f676dcbd7ddb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=93=E4=B8=B6=E6=A2=A6=E4=B8=B6=E4=BB=81?= <74444214+Daydreamer114@users.noreply.github.com> Date: Sun, 17 May 2026 19:07:32 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E4=BC=98=E5=8C=96=20PR=20commit=20?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E8=AF=84=E8=AE=BA=E9=80=BB=E8=BE=91=20(#1667?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Opus 4.7 --- .github/workflows/pr-checker.yml | 81 ++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/.github/workflows/pr-checker.yml b/.github/workflows/pr-checker.yml index c89da23efb..304cde3d7c 100644 --- a/.github/workflows/pr-checker.yml +++ b/.github/workflows/pr-checker.yml @@ -10,24 +10,6 @@ jobs: if: ${{ !github.event.pull_request.merged && github.base_ref != 'master-v2' }} runs-on: ubuntu-latest steps: - - name: Clean up previous comment - uses: actions/github-script@v9 - with: - script: | - const { data: comments } = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.payload.pull_request.number - }); - - const previousComment = comments.find(comment => comment.user.login === 'github-actions[bot]' && comment.body.includes('invalid commit(s)')); - if (previousComment) { - await github.rest.issues.deleteComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: previousComment.id - }); - } - name: Check commits uses: actions/github-script@v9 with: @@ -40,20 +22,59 @@ jobs: }); const regex = /^((build|chore|ci|docs?|feat!?|fix|perf|refactor|rft|style|test|i18n|typo|debug)[\:\.\(\,]|[Rr]evert|[Rr]elease|[Rr]eapply)/; - const invalidCommits = commits.filter(commit => !regex.test(commit.commit.message) || commit.parents.length > 1); + const badTitleCommits = commits.filter(commit => !regex.test(commit.commit.message) && commit.parents.length <= 1); + const mergeCommits = commits.filter(commit => commit.parents.length > 1); + const allInvalid = [...badTitleCommits, ...mergeCommits]; - console.log(`Checked ${commits.length} commit(s)`); + console.log(`Checked ${commits.length} commit(s), ${badTitleCommits.length} bad title(s), ${mergeCommits.length} merge commit(s)`); - if (invalidCommits.length > 0) { - const invalidCommitNames = invalidCommits.map(commit => commit.commit.message); - const invalidCommitInfoList = invalidCommits.map(commit => `- ${commit.commit.message.split("\n")[0]} [\`${commit.sha.substring(0, 7)}\`](${commit.html_url})`).join("\n"); + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number + }); - github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.payload.pull_request.number, - body: `## ⚠️ Found ${invalidCommits.length} invalid commit(s):\n\n${invalidCommitInfoList}\n\n---\nPlease follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) format, and **DO NOT** use merge commits.\n请遵循 [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) 格式,以及**不要**使用Merge Commit(修改 Commit Message 无法绕过检测)。` - }); + const previousComment = comments.find(comment => comment.user.login === 'github-actions[bot]' && comment.body.includes('invalid commit(s)')); - core.setFailed(`Found ${invalidCommits.length} invalid commit(s):\n${invalidCommitNames.join("\n-------------------\n")}`); + if (allInvalid.length > 0) { + const formatList = (list) => list.map(commit => `- ${commit.commit.message.split("\n")[0]} [\`${commit.sha.substring(0, 7)}\`](${commit.html_url})`).join("\n"); + + let bodySections = []; + if (badTitleCommits.length > 0) { + bodySections.push(`### 不合规提交名 | Invalid Commit Title(s):\n\n${formatList(badTitleCommits)}`); + } + if (mergeCommits.length > 0) { + bodySections.push(`### 不应使用 Merge Commit | Merge Commit(s) Detected:\n\n${formatList(mergeCommits)}`); + } + + const newBody = `## ⚠️ 发现 ${allInvalid.length} 个不合规提交 | Found ${allInvalid.length} invalid commit(s):\n\n${bodySections.join("\n\n")}\n\n---\n请遵循 [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) 格式,以及**不要**使用Merge Commit(修改 Commit Message 无法绕过检测)。\nPlease follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) format, and **DO NOT** use merge commits.`; + + if (previousComment && previousComment.body === newBody) { + console.log("Invalid commits unchanged, skipping comment update"); + } else { + if (previousComment) { + await github.rest.issues.deleteComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: previousComment.id + }); + } + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + body: newBody + }); + } + + const allInvalidNames = allInvalid.map(commit => commit.commit.message); + core.setFailed(`Found ${allInvalid.length} invalid commit(s):\n${allInvalidNames.join("\n-------------------\n")}`); + } else { + if (previousComment) { + await github.rest.issues.deleteComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: previousComment.id + }); + } }