mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-15 17:30:27 +08:00
ci: 优化 PR commit 检查评论逻辑 (#16671)
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
81
.github/workflows/pr-checker.yml
vendored
81
.github/workflows/pr-checker.yml
vendored
@@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user