Skip to content

Commit 0d4b68e

Browse files
fengwei-tianeagrafshine2lay
authored
feat:Added user information for commit validator (#682)
* Enable commit validator to filter by using author/committer email information * update * update changelog * Updated documentation for commit validator * Update failMessage --------- Co-authored-by: Ethan Graf <egraf@rippling.com> Co-authored-by: Shine Lee <aungshine@gmail.com>
1 parent 903d5e8 commit 0d4b68e

File tree

6 files changed

+205
-8
lines changed

6 files changed

+205
-8
lines changed

__tests__/unit/github/api.test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,12 +697,21 @@ describe('listReviews', () => {
697697
describe('listCommits', () => {
698698
test('return correct data if no error', async () => {
699699
const date = Date.now()
700+
const author = {
701+
date,
702+
name: 'Monalisa Octocat',
703+
email: 'support@github.com'
704+
}
705+
const committer = {
706+
date,
707+
name: 'Valdis Ferdinand',
708+
email: 'valdis@github.com'
709+
}
700710
const commits = [
701711
{
702712
commit: {
703-
author: {
704-
date
705-
},
713+
author,
714+
committer,
706715
message: 'fix: this'
707716
}
708717
}
@@ -712,6 +721,8 @@ describe('listCommits', () => {
712721
expect(res.length).toEqual(1)
713722
expect(res[0].date).toEqual(date)
714723
expect(res[0].message).toEqual('fix: this')
724+
expect(res[0].author.email).toEqual('support@github.com')
725+
expect(res[0].committer.email).toEqual('valdis@github.com')
715726
})
716727

717728
test('that error are NOT re-thrown', async () => {

__tests__/unit/validators/commit.test.js

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,29 @@ test('validate returns correctly', async () => {
1414
{
1515
commit: {
1616
author: {
17-
date
17+
date,
18+
name: 'Monalisa Octocat',
19+
email: 'support@github.com'
20+
},
21+
committer: {
22+
date,
23+
name: 'Valdis Ferdinand',
24+
email: 'valdis@github.com'
1825
},
1926
message: 'fix: this'
2027
}
2128
},
2229
{
2330
commit: {
2431
author: {
25-
date: date + 1
32+
date: date + 1,
33+
name: 'Monalisa Octocat1',
34+
email: 'support1@github.com'
35+
},
36+
committer: {
37+
date: date + 1,
38+
name: 'Valdis Ferdinand1',
39+
email: 'valdis1@github.com'
2640
},
2741
message: 'feat: that'
2842
}
@@ -190,6 +204,164 @@ test('single_commit_only sub option', async () => {
190204
expect(validation.validations[0].description).toBe('Some or all of your commit messages doesn\'t meet the criteria')
191205
})
192206

207+
test('author_email option should be used', async () => {
208+
const commit = new Commit()
209+
const settings = {
210+
do: 'commit',
211+
message: {
212+
regex: '^[A-Za-z0-9._%+-]+@github.com$',
213+
message_type: 'author_email'
214+
}
215+
}
216+
const date = Date.now()
217+
const commits = [
218+
{
219+
commit: {
220+
author: {
221+
date,
222+
name: 'Monalisa Octocat',
223+
email: 'support@github.com'
224+
},
225+
committer: {
226+
date,
227+
name: 'Valdis Ferdinand',
228+
email: 'valdis@github.com'
229+
},
230+
message: 'fix: this'
231+
}
232+
},
233+
{
234+
commit: {
235+
author: {
236+
date: date + 1,
237+
name: 'Monalisa Octocat1',
238+
email: 'support1@github.com'
239+
},
240+
committer: {
241+
date: date + 1,
242+
name: 'Valdis Ferdinand1',
243+
email: 'valdis1@github.com'
244+
},
245+
message: 'feat: that'
246+
}
247+
}
248+
]
249+
250+
const validation = await commit.processValidate(createMockContext(commits), settings)
251+
expect(validation.status).toBe('pass')
252+
})
253+
254+
test('author_email option should match the regex', async () => {
255+
const commit = new Commit()
256+
const settings = {
257+
do: 'commit',
258+
message: {
259+
regex: '^[A-Za-z0-9._%+-]+@github.com$',
260+
message_type: 'author_email'
261+
}
262+
}
263+
const date = Date.now()
264+
const commits = [
265+
{
266+
commit: {
267+
author: {
268+
date: date + 1,
269+
name: 'Monalisa Octocat1',
270+
email: 'support1@github1.com'
271+
},
272+
committer: {
273+
date: date + 1,
274+
name: 'Valdis Ferdinand1',
275+
email: 'valdis1@github1.com'
276+
},
277+
message: 'feat: that'
278+
}
279+
}
280+
]
281+
282+
const validation = await commit.processValidate(createMockContext(commits), settings)
283+
expect(validation.status).toBe('fail')
284+
})
285+
286+
test('committer_email option should be used', async () => {
287+
const commit = new Commit()
288+
const settings = {
289+
do: 'commit',
290+
message: {
291+
regex: '^[A-Za-z0-9._%+-]+@github.com$',
292+
message_type: 'committer_email'
293+
}
294+
}
295+
const date = Date.now()
296+
const commits = [
297+
{
298+
commit: {
299+
author: {
300+
date,
301+
name: 'Monalisa Octocat',
302+
email: 'support@github.com'
303+
},
304+
committer: {
305+
date,
306+
name: 'Valdis Ferdinand',
307+
email: 'valdis@github.com'
308+
},
309+
message: 'fix: this'
310+
}
311+
},
312+
{
313+
commit: {
314+
author: {
315+
date: date + 1,
316+
name: 'Monalisa Octocat1',
317+
email: 'support1@github.com'
318+
},
319+
committer: {
320+
date: date + 1,
321+
name: 'Valdis Ferdinand1',
322+
email: 'valdis1@github.com'
323+
},
324+
message: 'feat: that'
325+
}
326+
}
327+
]
328+
329+
const validation = await commit.processValidate(createMockContext(commits), settings)
330+
expect(validation.status).toBe('pass')
331+
})
332+
333+
test('committer_email option should match the regex', async () => {
334+
const commit = new Commit()
335+
const settings = {
336+
do: 'commit',
337+
message: {
338+
regex: '^[A-Za-z0-9._%+-]+@github.com$',
339+
message_type: 'committer_email'
340+
}
341+
}
342+
const date = Date.now()
343+
const commits = [
344+
{
345+
commit: {
346+
author: {
347+
date: date + 1,
348+
name: 'Monalisa Octocat1',
349+
email: 'support1@github.com'
350+
},
351+
committer: {
352+
date: date + 1,
353+
name: 'Valdis Ferdinand1',
354+
email: 'valdis1@github1.com'
355+
},
356+
message: 'feat: that'
357+
}
358+
}
359+
]
360+
361+
const validation = await commit.processValidate(createMockContext(commits), settings)
362+
expect(validation.status).toBe('fail')
363+
})
364+
193365
const createMockContext = (commits) => {
194366
return Helper.mockContext({ commits })
195367
}

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CHANGELOG
22
=====================================
3+
| March 1, 2023: feat:Added user information for commit validator `#682 <https://github.com/mergeability/mergeable/pull/682>`_
34
| February 03, 2023: feat: Add team option to author filter `#696 <https://github.com/mergeability/mergeable/pull/696>`_
45
| February 3, 2023: chore: Update node version for release workflow `#699 <https://github.com/mergeability/mergeable/pull/699>`_
56
| February 3, 2023: feat: Add Not operator `#695 <https://github.com/mergeability/mergeable/pull/695>`_

docs/validators/commit.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Commit
1111
oldest_only: false # Optional, Default is false. Only check the regex against the oldest commit
1212
newest_only: false # Optional, Default is false. Only check the regex against the newest commit
1313
single_commit_only: false # Optional, Default is false. only process this validator if there is one commit
14+
message_type: '' # Optional, only check regex against the field specified. Default is '', which processes the 'message' field. Can also be set to 'author_email' or 'committer_email'
1415
jira:
1516
regex: '[A-Z][A-Z0-9]+-\d+'
1617
regex_flag: none

lib/github/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ class GithubAPI {
482482
context.octokit.pulls.listCommits.endpoint.merge(
483483
context.repo({ pull_number: pullNumber })
484484
),
485-
res => res.data.map(o => ({ message: o.commit.message, date: o.commit.author.date }))
485+
res => res.data.map(o => ({ message: o.commit.message, date: o.commit.author.date, author: o.commit.author, committer: o.commit.committer }))
486486
)
487487
} catch (err) {
488488
return checkCommonError(err, context, callFn)

lib/validators/commit.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const consolidateResult = require('./options_processor/options/lib/consolidateRe
77
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'
88
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'
99
const DEFAULT_FAIL_MESSAGE = 'Some or all of your commit messages doesn\'t meet the criteria'
10+
const DEFAULT_FAIL_AUTHOR_EMAIL_MESSAGE = 'Some or all of your commit author emails doesn\'t meet the criteria'
11+
const DEFAULT_FAIL_COMMITTER_EMAIL_MESSAGE = 'Some or all of your commit committer emails doesn\'t meet the criteria'
1012
const DEFAULT_SUCCESS_MESSAGE = 'Your commit messages met the specified criteria'
1113

1214
class Commit extends Validator {
@@ -26,6 +28,7 @@ class Commit extends Validator {
2628
regex: 'string',
2729
regex_flag: 'string',
2830
message: 'string',
31+
message_type: 'string',
2932
skip_merge: 'boolean',
3033
oldest_only: 'boolean',
3134
newest_only: 'boolean',
@@ -43,6 +46,7 @@ class Commit extends Validator {
4346
const newestCommitOnly = _.isUndefined(messageSettings.newest_only) ? false : messageSettings.newest_only
4447
const skipMerge = _.isUndefined(messageSettings.skip_merge) ? true : messageSettings.skip_merge
4548
const singleCommitOnly = _.isUndefined(messageSettings.single_commit_only) ? false : messageSettings.single_commit_only
49+
const messageType = _.isUndefined(messageSettings.message_type) ? '' : messageSettings.message_type
4650

4751
const validatorContext = { name: 'commit' }
4852

@@ -73,14 +77,22 @@ class Commit extends Validator {
7377
orderedCommits = [orderedCommits[orderedCommits.length - 1]]
7478
}
7579

76-
const commitMessages = orderedCommits.map(commit => commit.message)
80+
let commitMessages = orderedCommits.map(commit => commit.message)
81+
let failMessage = DEFAULT_FAIL_MESSAGE
82+
if (messageType === 'author_email') {
83+
commitMessages = orderedCommits.map(commit => commit.author.email)
84+
failMessage = DEFAULT_FAIL_AUTHOR_EMAIL_MESSAGE
85+
} else if (messageType === 'committer_email') {
86+
commitMessages = orderedCommits.map(commit => commit.committer.email)
87+
failMessage = DEFAULT_FAIL_COMMITTER_EMAIL_MESSAGE
88+
}
7789

7890
const result = await mustInclude.process(validatorContext, commitMessages, {
7991
must_include: {
8092
all: true,
8193
regex: messageSettings.regex,
8294
regex_flag: messageSettings.regex_flag,
83-
message: messageSettings.message ? messageSettings.message : DEFAULT_FAIL_MESSAGE
95+
message: messageSettings.message ? messageSettings.message : failMessage
8496
}
8597
})
8698

0 commit comments

Comments
 (0)