Skip to content

Commit a0536a5

Browse files
authored
feat(ci): pull request title check (#2282)
1 parent 23ef0ec commit a0536a5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Pull Request Title Check
2+
on:
3+
pull_request:
4+
types: [opened, edited, synchronize]
5+
permissions:
6+
pull-requests: read
7+
jobs:
8+
pull-request-title-check:
9+
runs-on: buildjet-4vcpu-ubuntu-2204
10+
container:
11+
image: node:22
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
- name: Check pull request title
16+
run: |
17+
npx tsx ./scripts/pull-request-title-check.ts

scripts/pull-request-title-check.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import fs from 'node:fs';
2+
3+
const eventPath = process.env.GITHUB_EVENT_PATH;
4+
if (!eventPath) {
5+
throw new Error(
6+
'GITHUB_EVENT_PATH environment variable is not set. This script is only meant to run in during a Github workflow.',
7+
);
8+
}
9+
10+
const eventJson = JSON.parse(fs.readFileSync(eventPath, 'utf8'));
11+
const { title } = eventJson.pull_request;
12+
13+
const validTypeRegex =
14+
/^(feat|fix|chore|refactor)(\([a-zA-Z0-9-]+\))?:\s[a-z].*$/;
15+
16+
if (!validTypeRegex.test(title)) {
17+
console.error(
18+
`pull request title does not follow the required format.
19+
example: "type: description of the change"
20+
21+
- type: "feat", "fix", "chore", or "refactor"
22+
- first letter of the title after the 'type' needs to be lowercased`,
23+
);
24+
process.exit(1);
25+
}
26+
27+
console.info('pull request title is valid');

0 commit comments

Comments
 (0)