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