mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-15 17:30:27 +08:00
81 lines
3.9 KiB
YAML
81 lines
3.9 KiB
YAML
name: PR Commit Check
|
||
|
||
on:
|
||
pull_request_target:
|
||
types: [opened, edited, ready_for_review, reopened, synchronize]
|
||
|
||
jobs:
|
||
check_commit_name_in_pr:
|
||
name: Check Commits in PR
|
||
if: ${{ !github.event.pull_request.merged && github.base_ref != 'master-v2' }}
|
||
runs-on: ubuntu-latest
|
||
steps:
|
||
- name: Check commits
|
||
uses: actions/github-script@v9
|
||
with:
|
||
script: |
|
||
const { data: commits } = await github.rest.pulls.listCommits({
|
||
owner: context.repo.owner,
|
||
repo: context.repo.repo,
|
||
pull_number: context.payload.pull_request.number,
|
||
per_page: 100
|
||
});
|
||
|
||
const regex = /^((build|chore|ci|docs?|feat!?|fix|perf|refactor|rft|style|test|i18n|typo|debug)[\:\.\(\,]|[Rr]evert|[Rr]elease|[Rr]eapply)/;
|
||
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), ${badTitleCommits.length} bad title(s), ${mergeCommits.length} merge commit(s)`);
|
||
|
||
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 (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
|
||
});
|
||
}
|
||
}
|