-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add last comment validator (#668)
* feat: Add last comment validator * Update changelog Co-authored-by: Cuong Pham <[email protected]>
- Loading branch information
1 parent
7b8f60f
commit 3c3fe59
Showing
8 changed files
with
189 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const LastComment = require('../../../lib/validators/lastComment') | ||
const Helper = require('../../../__fixtures__/unit/helper') | ||
|
||
test('validate returns correctly', async () => { | ||
const lastComment = new LastComment() | ||
|
||
const settings = { | ||
do: 'lastComment', | ||
must_exclude: { | ||
regex: 'exclude this' | ||
} | ||
} | ||
|
||
let results = await lastComment.processValidate(createMockContext(['exclude this']), settings) | ||
expect(results.status).toBe('fail') | ||
|
||
results = await lastComment.processValidate(createMockContext(['a', 'b']), settings) | ||
expect(results.status).toBe('pass') | ||
}) | ||
|
||
test('fail gracefully if invalid regex', async () => { | ||
const lastComment = new LastComment() | ||
|
||
const settings = { | ||
do: 'lastComment', | ||
must_exclude: { | ||
regex: '@#$@#$@#$' | ||
} | ||
} | ||
|
||
const validation = await lastComment.processValidate(createMockContext('WIP'), settings) | ||
expect(validation.status).toBe('pass') | ||
}) | ||
|
||
test('description is correct', async () => { | ||
const lastComment = new LastComment() | ||
|
||
const settings = { | ||
do: 'lastComment', | ||
must_exclude: { | ||
regex: 'Work in Progress' | ||
} | ||
} | ||
|
||
let validation = await lastComment.processValidate(createMockContext('Work in Progress'), settings) | ||
|
||
expect(validation.status).toBe('fail') | ||
expect(validation.validations[0].description).toBe('lastComment does not exclude "Work in Progress"') | ||
|
||
validation = await lastComment.processValidate(createMockContext('Just lastComment'), settings) | ||
expect(validation.validations[0].description).toBe("lastComment must exclude 'Work in Progress'") | ||
}) | ||
|
||
test('mergeable is true if must_include is the last comment', async () => { | ||
const lastComment = new LastComment() | ||
|
||
const settings = { | ||
do: 'lastComment', | ||
must_include: { | ||
regex: 'xyz' | ||
} | ||
} | ||
|
||
let validation = await lastComment.processValidate(createMockContext(['abc', 'experimental', 'xyz']), settings) | ||
expect(validation.status).toBe('pass') | ||
|
||
validation = await lastComment.processValidate(createMockContext(['Some lastComment', 'xyz', '456']), settings) | ||
expect(validation.status).toBe('fail') | ||
}) | ||
|
||
test('mergeable is false if must_exclude is one of the lastComment', async () => { | ||
const lastComment = new LastComment() | ||
|
||
const settings = { | ||
do: 'lastComment', | ||
must_exclude: { | ||
regex: 'xyz' | ||
} | ||
} | ||
|
||
let validation = await lastComment.processValidate(createMockContext(['abc', 'experimental', 'xyz']), settings) | ||
expect(validation.status).toBe('fail') | ||
|
||
validation = await lastComment.processValidate(createMockContext(['Some lastComment', 'xyz', '456']), settings) | ||
expect(validation.status).toBe('pass') | ||
}) | ||
|
||
test('complex Logic test', async () => { | ||
const lastComment = new LastComment() | ||
|
||
const settings = { | ||
do: 'lastComment', | ||
or: [{ | ||
and: [{ | ||
must_include: { | ||
regex: 'release note: yes', | ||
message: 'Please include release note: yes' | ||
} | ||
}, { | ||
must_include: { | ||
regex: 'lang\\/core|lang\\/c\\+\\+|lang\\/c#', | ||
message: 'Please include a language comment' | ||
} | ||
}] | ||
}, { | ||
must_include: { | ||
regex: 'release note: no', | ||
message: 'Please include release note: no' | ||
} | ||
}] | ||
} | ||
|
||
let validation = await lastComment.processValidate(createMockContext(['experimental', 'xyz', 'release note: no']), settings) | ||
expect(validation.status).toBe('pass') | ||
|
||
validation = await lastComment.processValidate(createMockContext(['123', '456', 'release note: yes']), settings) | ||
expect(validation.status).toBe('fail') | ||
expect(validation.validations[0].description).toBe('((Please include a language comment) ***OR*** Please include release note: no)') | ||
|
||
validation = await lastComment.processValidate(createMockContext(['456', 'lang/core']), settings) | ||
expect(validation.validations[0].description).toBe('((Please include release note: yes) ***OR*** Please include release note: no)') | ||
}) | ||
|
||
function createMockContext (comments) { | ||
return Helper.mockContext({ listComments: Array.isArray(comments) ? comments.map(comment => ({ body: comment })) : [{ body: comments }] }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Last Comment | ||
^^^^^^^^^^ | ||
|
||
:: | ||
|
||
- do: lastComment // check if the last comment contains only the word 'merge' | ||
must_include: | ||
regex: '^merge$' | ||
|
||
Supported Events: | ||
:: | ||
|
||
'pull_request.*', 'pull_request_review.*', 'issues.*', 'issue_comment.*' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { Validator } = require('./validator') | ||
|
||
class LastComment extends Validator { | ||
constructor () { | ||
super('lastComment') | ||
this.supportedEvents = [ | ||
'pull_request.*', | ||
'pull_request_review.*', | ||
'issues.*', | ||
'issue_comment.*' | ||
] | ||
this.supportedSettings = { | ||
must_include: { | ||
regex: ['string', 'array'], | ||
regex_flag: 'string', | ||
message: 'string' | ||
}, | ||
must_exclude: { | ||
regex: ['string', 'array'], | ||
regex_flag: 'string', | ||
message: 'string' | ||
} | ||
} | ||
} | ||
|
||
async validate (context, validationSettings) { | ||
const comments = (await this.githubAPI.listComments(context, this.getPayload(context).number)).data | ||
|
||
return this.processOptions( | ||
validationSettings, | ||
comments.length ? comments[comments.length - 1].body : '' | ||
) | ||
} | ||
} | ||
|
||
module.exports = LastComment |