-
Notifications
You must be signed in to change notification settings - Fork 116
/
commit.js
108 lines (93 loc) · 4.33 KB
/
commit.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
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
106
107
108
const { Validator } = require('./validator')
const _ = require('lodash')
const mustInclude = require('./options_processor/options/must_include')
const constructOutput = require('./options_processor/options/lib/constructOutput')
const consolidateResult = require('./options_processor/options/lib/consolidateResults')
const MESSAGE_NOT_FOUND_ERROR = 'Failed to run the \'commit\' validator because \'message\' option is not found. Please check README for more information about configuration'
const REGEX_NOT_FOUND_ERROR = 'Failed to run the test because \'regex\' is not provided for \'message\' option. Please check README for more information about configuration'
const DEFAULT_FAIL_MESSAGE = 'Some or all of your commit messages doesn\'t meet the criteria'
const DEFAULT_FAIL_AUTHOR_EMAIL_MESSAGE = 'Some or all of your commit author emails doesn\'t meet the criteria'
const DEFAULT_FAIL_COMMITTER_EMAIL_MESSAGE = 'Some or all of your commit committer emails doesn\'t meet the criteria'
const DEFAULT_SUCCESS_MESSAGE = 'Your commit messages met the specified criteria'
class Commit extends Validator {
constructor () {
super('commit')
this.supportedEvents = [
'pull_request.*',
'pull_request_review.*'
]
this.supportedSettings = {
jira: {
regex: 'string',
regex_flag: 'string',
message: 'string'
},
message: {
regex: 'string',
regex_flag: 'string',
message: 'string',
message_type: 'string',
skip_merge: 'boolean',
oldest_only: 'boolean',
newest_only: 'boolean',
single_commit_only: 'boolean'
}
}
}
async validate (context, validationSettings) {
if (_.isUndefined(validationSettings.message)) throw Error(MESSAGE_NOT_FOUND_ERROR)
if (_.isUndefined(validationSettings.message.regex)) throw Error(REGEX_NOT_FOUND_ERROR)
const messageSettings = validationSettings.message
const oldestCommitOnly = _.isUndefined(messageSettings.oldest_only) ? false : messageSettings.oldest_only
const newestCommitOnly = _.isUndefined(messageSettings.newest_only) ? false : messageSettings.newest_only
const skipMerge = _.isUndefined(messageSettings.skip_merge) ? true : messageSettings.skip_merge
const singleCommitOnly = _.isUndefined(messageSettings.single_commit_only) ? false : messageSettings.single_commit_only
const messageType = _.isUndefined(messageSettings.message_type) ? '' : messageSettings.message_type
const validatorContext = { name: 'commit' }
const commits = await this.githubAPI.listCommits(context, this.getPayload(context).number)
let orderedCommits = _.orderBy(commits, ['date'], ['asc'])
if (singleCommitOnly && orderedCommits.length !== 1) {
return consolidateResult([constructOutput(
validatorContext,
orderedCommits.map(commit => commit.message),
validationSettings,
{
status: 'pass',
description: 'Since there are more than one commits, Skipping validation'
}
)], validatorContext)
}
if (skipMerge) {
orderedCommits = orderedCommits.filter(commit => !commit.message.includes('Merge branch'))
}
if (oldestCommitOnly) {
orderedCommits = [orderedCommits[0]]
}
if (newestCommitOnly) {
orderedCommits = [orderedCommits[orderedCommits.length - 1]]
}
let commitMessages = orderedCommits.map(commit => commit.message)
let failMessage = DEFAULT_FAIL_MESSAGE
if (messageType === 'author_email') {
commitMessages = orderedCommits.map(commit => commit.author.email)
failMessage = DEFAULT_FAIL_AUTHOR_EMAIL_MESSAGE
} else if (messageType === 'committer_email') {
commitMessages = orderedCommits.map(commit => commit.committer.email)
failMessage = DEFAULT_FAIL_COMMITTER_EMAIL_MESSAGE
}
const result = await mustInclude.process(validatorContext, commitMessages, {
must_include: {
all: true,
regex: messageSettings.regex,
regex_flag: messageSettings.regex_flag,
message: messageSettings.message ? messageSettings.message : failMessage
}
})
if (result.status === 'pass') {
result.description = DEFAULT_SUCCESS_MESSAGE
}
const output = [constructOutput(validatorContext, commitMessages, validationSettings, result)]
return consolidateResult(output, validatorContext)
}
}
module.exports = Commit