forked from ad1992/clean-commit-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (44 loc) · 1.36 KB
/
index.js
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
const { getInput, setFailed, debug, error } = require('@actions/core');
const { getOctokit, context } = require('@actions/github');
const { BAD_KEYWORDS } = require('./constants');
function filterCommit(commit) {
commit = commit.toLowerCase();
const result = [];
for (const keyword of BAD_KEYWORDS) {
if (commit.indexOf(keyword) != -1) {
result.push(keyword);
}
}
return result;
}
async function verifyCommits(repoToken) {
const client = getOctokit(repoToken);
const { data: commits } = await client.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
debug(`There are ${commits.length} commits in this pull request`);
let badCommits = 0;
for (const commit of commits) {
const { message } = commit.commit;
const badKeywords = filterCommit(message);
if (badKeywords.length) {
error(`commit message "${message}" contains ${badKeywords.join()}`);
badCommits++;
}
}
if (badCommits) {
throw new Error(`${badCommits} bad commit(s) encountered. Please fix the above errors`);
}
}
async function main() {
try {
const repoToken = getInput('repo-token', { required: true });
debug('Received repo token');
await verifyCommits(repoToken);
} catch (e) {
setFailed(`Action failed with error: ${e.message}`);
}
}
main();