@@ -21,28 +21,38 @@ const outputTitleFail = constants.output_title_fail;
21
21
22
22
/**
23
23
* Commit messages and PR title Validator
24
- * @param {Object } app
25
- * @param {Object } context
26
- * @param {Object } configuration
27
- * @param {Boolean } updateCheckRunFlag
24
+ * @param {Object } app Probot app object
25
+ * @param {Object } context Github event context
26
+ * @param {Object } configuration Contains data (i.e regex for PR and Commits) from config.yml file
27
+ * @param {Boolean } updateCheckRunFlag Update existing check run
28
+ * @param {Boolean } createCheckRunFlag Create existing check run
28
29
*/
29
- module . exports . commitAndTitleValidator = async ( app , context , configuration , updateCheckRunFlag ) => {
30
+ module . exports . commitAndTitleValidator = async ( app , context , configuration , updateCheckRunFlag , createCheckRunFlag ) => {
30
31
try {
31
32
let { prTitleRegex, commitTitleRegex } = regexExtractor ( configuration ) ;
32
33
let { owner, repository, pullRequestTitle, pullNumber } = prDetailsExtractor ( context ) ;
33
- // find commits
34
+ /**
35
+ * Find all commits for a pull request
36
+ */
34
37
let commits = await listCommitsOfPullRequest ( context , owner , repository , pullNumber ) ;
35
- if ( updateCheckRunFlag ) {
38
+ /**
39
+ * If `PR title` is not present in `context`
40
+ */
41
+ if ( ! pullRequestTitle ) {
36
42
/**
37
- * In case of check re-run and check suite re-run or Re-run all checks, get pull request data
43
+ * In case of new check suite requested, check re-run and check suite re-run or Re-run all checks, get
44
+ * pull request data
38
45
*/
39
46
const pullRequestDetails = await getPullRequest ( context , owner , repository , pullNumber ) ;
40
- if ( pullRequestDetails && pullRequestDetails . data && pullRequestDetails . data . title ) {
47
+ /**
48
+ * Get PR title
49
+ */
50
+ if ( pullRequestDetails && pullRequestDetails . data && pullRequestDetails . data . title ) {
41
51
pullRequestTitle = pullRequestDetails . data . title ;
42
52
}
43
53
}
44
54
let result = checkMessagesFormat ( pullRequestTitle , commits . data , prTitleRegex , commitTitleRegex ) ;
45
- await createOrUpdateCheckRun ( context , owner , repository , result , updateCheckRunFlag ) ;
55
+ await createOrUpdateCheckRun ( context , owner , repository , result , updateCheckRunFlag , createCheckRunFlag ) ;
46
56
} catch ( error ) {
47
57
app . log ( error ) ;
48
58
}
@@ -70,7 +80,9 @@ function checkMessagesFormat(pullRequestTitle, commits, prTitleRegex, commitMsgR
70
80
} ;
71
81
checkPrTitle ( pullRequestTitle , prTitleRegex , flags ) ;
72
82
if ( commits && Array . isArray ( commits ) && commits . length ) {
73
- // check all commit messages
83
+ /**
84
+ * Check all commit messages
85
+ */
74
86
checkCommitMessages ( commits , commitIds , commitMsgRegex , mergeCommitRegex , flags ) ;
75
87
result = concludeCheckRunParams ( prTitleRegex , commitMsgRegex , commitIds , flags ) ;
76
88
}
@@ -87,12 +99,16 @@ function checkMessagesFormat(pullRequestTitle, commits, prTitleRegex, commitMsgR
87
99
* @param {Object } flags
88
100
*/
89
101
function checkPrTitle ( pullRequestTitle , prTitleRegex , flags ) {
90
- // pull request title format
102
+ /**
103
+ * Check pull request title format
104
+ */
91
105
if ( checkRegex ( pullRequestTitle , prTitleRegex ) ) {
92
106
flags . pullReqTitleStatus = true ;
93
107
flags . pullReqTitleStatusMsg = messages . valid_pull_request_message ;
94
108
} else {
95
- // invalid pull Request title
109
+ /**
110
+ * Invalid pull Request title
111
+ */
96
112
flags . pullReqTitleStatus = false ;
97
113
flags . pullReqTitleStatusMsg = messages . invalid_pull_request_message ;
98
114
}
@@ -107,7 +123,9 @@ function checkPrTitle(pullRequestTitle, prTitleRegex, flags) {
107
123
* @param {Object } flags
108
124
*/
109
125
function checkCommitMessages ( commits , commitIds , commitMsgRegex , mergeCommitRegex , flags ) {
110
- // check all commit messages
126
+ /**
127
+ * Check all commit messages
128
+ */
111
129
for ( let index = 0 ; index < commits . length ; index ++ ) {
112
130
const element = commits [ index ] ;
113
131
const commitMessage = element . commit . message ;
@@ -157,19 +175,27 @@ function concludeCheckRunParams(prTitleRegex, commitMsgRegex, commitIds, flags)
157
175
} ;
158
176
let status = checkRunStatusCompleted ;
159
177
if ( ! prTitleRegex && ! commitMsgRegex ) {
160
- // pull request and commit message configration regex not set
178
+ /**
179
+ * Pull request and commit message configration regex not set
180
+ */
161
181
output . title = `${ messages . pr_and_commit_message_configuration_not_set } ` ;
162
182
output . summary = `${ messages . pr_and_commit_message_configuration_not_set } <br/>` ;
163
183
} else if ( ! commitMsgRegex ) {
164
- // commit message configration regex not set
184
+ /**
185
+ * Commit message configration regex not set
186
+ */
165
187
output . title = `${ messages . commit_message_configuration_not_set } ` ;
166
188
output . summary = `${ flags . pullReqTitleStatusMsg } <br/>${ messages . commit_message_configuration_not_set } <br/>` ;
167
189
} else if ( ! prTitleRegex ) {
168
- // pull request configration regex not set
190
+ /**
191
+ * Pull request configration regex not set
192
+ */
169
193
output . title = `${ messages . pr_configuration_not_set } ` ;
170
194
output . summary = `${ messages . pr_configuration_not_set } <br/>${ flags . commitMsgStatusMsg } <br/>${ flags . invalidCommits } <br/>` ;
171
195
}
172
- // set invalid commit messages and count
196
+ /**
197
+ * Set invalid commit messages and count
198
+ */
173
199
if ( flags . invalidCommitsCount && flags . invalidCommitsCount >= constants . INVALID_COMMIT_LIMIT ) {
174
200
output . summary += `${ flags . invalidCommitsCount } ${ flags . otherInvalidCommitMessages } ` ;
175
201
}
@@ -194,29 +220,29 @@ function prDetailsExtractor(context) {
194
220
pullRequestTitle : '' ,
195
221
pullNumber : 0
196
222
} ;
197
- if ( context . payload ) {
223
+ if ( context . payload ) {
198
224
/**
199
225
* Extract repository details
200
226
*/
201
- if ( context . payload . repository ) {
202
- if ( context . payload . repository . owner && context . payload . repository . owner . login ) {
227
+ if ( context . payload . repository ) {
228
+ if ( context . payload . repository . owner && context . payload . repository . owner . login ) {
203
229
result . owner = context . payload . repository . owner . login ;
204
230
}
205
- if ( context . payload . repository . name ) {
231
+ if ( context . payload . repository . name ) {
206
232
result . repository = context . payload . repository . name ;
207
233
}
208
234
}
209
235
/**
210
- * Extract pr title and pull number
236
+ * Extract PR title and pull number
211
237
*/
212
- if ( context . payload . pull_request && context . payload . pull_request . title ) {
238
+ if ( context . payload . pull_request && context . payload . pull_request . title ) {
213
239
result . pullRequestTitle = context . payload . pull_request . title ;
214
240
}
215
- if ( context . payload . number ) {
241
+ if ( context . payload . number ) {
216
242
result . pullNumber = context . payload . number ;
217
- } else if ( context . payload . check_run && context . payload . check_run . check_suite && context . payload . check_run . check_suite . pull_requests && context . payload . check_run . check_suite . pull_requests . length && context . payload . check_run . check_suite . pull_requests [ 0 ] . number ) {
243
+ } else if ( context . payload . check_run && context . payload . check_run . check_suite && context . payload . check_run . check_suite . pull_requests && context . payload . check_run . check_suite . pull_requests . length && context . payload . check_run . check_suite . pull_requests [ 0 ] . number ) {
218
244
result . pullNumber = context . payload . check_run . check_suite . pull_requests [ 0 ] . number ;
219
- } else if ( context . payload . check_suite && context . payload . check_suite . pull_requests . length && context . payload . check_suite . pull_requests [ 0 ] . number ) {
245
+ } else if ( context . payload . check_suite && context . payload . check_suite . pull_requests . length && context . payload . check_suite . pull_requests [ 0 ] . number ) {
220
246
result . pullNumber = context . payload . check_suite . pull_requests [ 0 ] . number ;
221
247
}
222
248
}
@@ -232,7 +258,7 @@ function regexExtractor(configuration) {
232
258
prTitleRegex : '' ,
233
259
commitTitleRegex : ''
234
260
} ;
235
- result . prTitleRegex = ( configuration && configuration . PR_TITLE_REGEX ) ? configuration . PR_TITLE_REGEX : '' ;
261
+ result . prTitleRegex = ( configuration && configuration . PR_TITLE_REGEX ) ? configuration . PR_TITLE_REGEX : '' ;
236
262
result . commitTitleRegex = ( configuration && configuration . COMMIT_MESSAGE_REGEX ) ? configuration . COMMIT_MESSAGE_REGEX : '' ;
237
263
return result ;
238
264
}
@@ -243,32 +269,37 @@ function regexExtractor(configuration) {
243
269
* @param {String } owner
244
270
* @param {String } repository
245
271
* @param {Object } result
246
- * @param {boolean } updateCheckRunFlag
272
+ * @param {boolean } updateCheckRunFlag
273
+ * @param {boolean } createCheckRunFlag
247
274
*/
248
- async function createOrUpdateCheckRun ( context , owner , repository , result , updateCheckRunFlag ) {
275
+ async function createOrUpdateCheckRun ( context , owner , repository , result , updateCheckRunFlag , createCheckRunFlag ) {
249
276
if ( result && result . commitIds && Array . isArray ( result . commitIds ) && result . commitIds . length ) {
250
277
for ( let index = 0 ; index < result . commitIds . length ; index ++ ) {
251
278
const commitId = result . commitIds [ index ] ;
252
- if ( updateCheckRunFlag ) {
279
+ if ( updateCheckRunFlag ) {
253
280
/**
254
281
* Update existing check run
255
282
*/
256
283
const checkRunId = context . payload . check_run . id ;
257
284
await updateCheckRun ( context , owner , repository , commitId , result . status , result . conclusion , result . output , checkRunId ) ;
258
- } else {
285
+ } else if ( createCheckRunFlag ) {
259
286
/**
260
287
* Create check new run
261
288
*/
262
289
/**
263
290
* check if checkSuite exists or not for the commit
264
291
*/
265
- let checkSuiteList = await listCheckSuite ( context , owner , repository , commitId ) ;
266
- if ( ! checkSuiteList || ( checkSuiteList && checkSuiteList . data && checkSuiteList . data . total_count && checkSuiteList . data . total_count === 0 ) ) {
267
- // create check suite for a particular commit
268
- await createCheckSuite ( context , owner , repository , commitId ) ;
269
- }
270
- // create check run
271
- await createCheckRun ( context , owner , repository , commitId , result . status , result . checkRunName , result . conclusion , result . output ) ;
292
+ let checkSuiteList = await listCheckSuite ( context , owner , repository , commitId ) ;
293
+ if ( ! checkSuiteList || ( checkSuiteList && checkSuiteList . data && checkSuiteList . data . total_count && checkSuiteList . data . total_count === 0 ) ) {
294
+ /**
295
+ * create check suite for a particular commit
296
+ */
297
+ await createCheckSuite ( context , owner , repository , commitId ) ;
298
+ }
299
+ /**
300
+ * create check run
301
+ */
302
+ await createCheckRun ( context , owner , repository , commitId , result . status , result . checkRunName , result . conclusion , result . output ) ;
272
303
}
273
304
}
274
305
}
0 commit comments