Skip to content

Commit ed47420

Browse files
committed
ci: add pr check
1 parent 6d68769 commit ed47420

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: PR Title Check
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, edited, reopened, synchronize]
6+
7+
jobs:
8+
title-format:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
pull-requests: write
12+
issues: write
13+
14+
steps:
15+
- name: Validate PR title
16+
uses: actions/github-script@v7
17+
with:
18+
script: |
19+
const title = (context.payload.pull_request.title || "").trim();
20+
// allow only:
21+
// feat: xxx
22+
// feat(scope): xxx
23+
const pattern = /^(feat)(\([a-z0-9-]+\))?:\s.+$/i;
24+
const isValid = pattern.test(title);
25+
const isSameRepo =
26+
context.payload.pull_request.head.repo.full_name === context.payload.repository.full_name;
27+
28+
if (!isValid) {
29+
if (isSameRepo) {
30+
try {
31+
await github.rest.issues.createComment({
32+
owner: context.repo.owner,
33+
repo: context.repo.repo,
34+
issue_number: context.payload.pull_request.number,
35+
body: [
36+
"⚠️ PR title format check failed.",
37+
"Required formats:",
38+
"- `feat: xxx`",
39+
"- `feat(scope): xxx`",
40+
"Please update your PR title and push again."
41+
].join("\n")
42+
});
43+
} catch (e) {
44+
core.warning(`Failed to post PR title comment: ${e.message}`);
45+
}
46+
} else {
47+
core.warning("Fork PR: comment permission is restricted; skip posting review comment.");
48+
}
49+
}
50+
51+
if (!isValid) {
52+
core.setFailed("Invalid PR title. Expected format: feat: xxx or feat(scope): xxx.");
53+
}

0 commit comments

Comments
 (0)