-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-message-validator.js
More file actions
105 lines (90 loc) · 3.34 KB
/
commit-message-validator.js
File metadata and controls
105 lines (90 loc) · 3.34 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const core = require('@actions/core');
const github = require('@actions/github');
function buildDefaultPattern() {
const mergeBranchPattern = 'Merge branch [\'"][^\'"]+[\'"] into [^\\s]+';
const revertPattern = 'Revert ".*"';
const types = [
'feat', 'fix', 'chore', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'revert',
].join('|');
// Scope allows lowercase/uppercase letters, digits, underscores, slashes, and hyphens
const conventionalPattern = `(?:(${types})(\\([a-zA-Z0-9_/\\-]+\\))?(!)?: .+)`;
return `^(${mergeBranchPattern}|${revertPattern}|${conventionalPattern})$`;
}
async function run() {
try {
const token = core.getInput('github-token', { required: true });
const defaultPattern = buildDefaultPattern();
const rawPattern = core.getInput('pattern') || defaultPattern;
let regexPattern;
try {
regexPattern = new RegExp(rawPattern);
} catch (e) {
core.setFailed(`Invalid regex pattern provided: ${e.message}`);
return;
}
const octokit = github.getOctokit(token);
const context = github.context;
const pullRequest = context.payload.pull_request;
if (!pullRequest) {
core.info('No pull request found. Skipping commit message validation.');
return;
}
const { data: commits } = await octokit.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullRequest.number,
});
if (commits.length === 0) {
core.info('No commits found in this pull request. Skipping validation.');
return;
}
let hasError = false;
const errorMessages = [];
commits.forEach((commit) => {
const message = commit.commit.message;
if (!message) {
core.warning(`Commit ${commit.sha.substring(0, 7)} has an empty message, skipping.`);
return;
}
const commitMessage = message.split('\n')[0].trim();
const sha = commit.sha.substring(0, 7);
if (!regexPattern.test(commitMessage)) {
// Escape backticks to avoid breaking markdown code spans in PR comments
const safeMessage = commitMessage.replace(/`/g, '\\`');
errorMessages.push(`- ❌ \`${sha}\`: "${safeMessage}"`);
hasError = true;
} else {
core.info(`✅ Commit ${sha} has a valid message: "${commitMessage}"`);
}
});
if (hasError) {
const errorSummary = [
'One or more commits have invalid message format.',
'',
errorMessages.join('\n'),
'',
'Please follow the Conventional Commits format: `<type>[optional scope]: <description>`',
'Example: `feat(auth): add login functionality`',
'',
'For more information, visit: https://www.conventionalcommits.org/',
].join('\n');
try {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body: errorSummary,
});
core.info('Commented on PR with error summary.');
} catch (commentError) {
core.warning(`Failed to post PR comment: ${commentError.message}`);
}
core.setFailed(errorSummary);
} else {
core.info('✅ All commit messages have valid format.');
}
} catch (error) {
core.setFailed(`Action failed with error: ${error.message}`);
}
}
run();