-
Notifications
You must be signed in to change notification settings - Fork 119
63 lines (55 loc) · 2.71 KB
/
Copy pathcommit-convention.yml
File metadata and controls
63 lines (55 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
name: Check PR title follows commit convention
on:
pull_request:
types: [opened, edited, reopened]
merge_group:
jobs:
check-pr-title:
name: "PR title follows commit convention"
runs-on: ubuntu-latest
steps:
- name: Check PR title
uses: actions/github-script@v9
with:
script: |
let title;
if (context.eventName === 'merge_group') {
// The merge commit message's first line is the commit subject
title = context.payload.merge_group.head_commit.message.split('\n')[0];
} else {
title = context.payload.pull_request.title;
}
const types = ['feat', 'fix', 'doc', 'style', 'refactor', 'test', 'chore', 'perf'];
const typePattern = types.join('|');
const regex = new RegExp(`^(${typePattern}): .+$`);
const errors = [];
if (!regex.test(title)) {
errors.push(`PR title does not match the required format: \`<type>: <subject>\``);
errors.push(`Allowed types: ${types.join(', ')}`);
errors.push(`Example: \`fix: correct typo in documentation\``);
errors.push(`Example: \`feat: add RSS feed support\``);
} else {
// Extract subject (part after ": ")
const colonIndex = title.indexOf(': ');
const subject = title.substring(colonIndex + 2);
if (/^[A-Z]/.test(subject)) {
errors.push(`Subject must not start with a capital letter: \`${subject}\``);
}
if (subject.endsWith('.')) {
errors.push(`Subject must not end with a period: \`${subject}\``);
}
}
if (errors.length > 0) {
const message = [
'PR title does not follow the commit convention.',
'',
`Title: \`${title}\``,
'',
...errors.map(e => `- ${e}`),
'',
'Format: `<type>: <subject>`',
].join('\n');
core.setFailed(message);
} else {
core.info(`PR title is valid: ${title}`);
}