Skip to content

Commit 577cc12

Browse files
authored
feat: Add baseRef filter (#596)
* feat: Add baseRef filter As a user of Mergeable i want to be able to execute validators only on PRs targeting a certain baseRef Document baseRef filter * fix: Remove rogue comma
1 parent f7f037b commit 577cc12

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

__fixtures__/unit/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module.exports = {
4747
full_name: options.baseRepo ? options.baseRepo : 'owner/test',
4848
private: (options.repoPrivate) ? options.repoPrivate : false
4949
},
50-
ref: 'baseRef',
50+
ref: options.baseRef ? options.baseRef : 'baseRef',
5151
sha: 'sha2'
5252
},
5353
head: {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const BaseRef = require('../../../lib/filters/baseRef')
2+
const Helper = require('../../../__fixtures__/unit/helper')
3+
4+
test('should fail with unexpected baseRef', async () => {
5+
const baseRef = new BaseRef()
6+
const settings = {
7+
do: 'baseRef',
8+
must_include: {
9+
regex: 'some-other-ref'
10+
}
11+
}
12+
const filter = await baseRef.processFilter(createMockContext('some-ref'), settings)
13+
expect(filter.status).toBe('fail')
14+
})
15+
16+
test('should pass with expected baseRef', async () => {
17+
const baseRef = new BaseRef()
18+
const settings = {
19+
do: 'baseRef',
20+
must_include: {
21+
regex: 'some-ref'
22+
}
23+
}
24+
const filter = await baseRef.processFilter(createMockContext('some-ref'), settings)
25+
expect(filter.status).toBe('pass')
26+
})
27+
28+
test('should fail with excluded baseRef', async () => {
29+
const baseRef = new BaseRef()
30+
const settings = {
31+
do: 'baseRef',
32+
must_exclude: {
33+
regex: 'some-ref'
34+
}
35+
}
36+
const filter = await baseRef.processFilter(createMockContext('some-ref'), settings)
37+
expect(filter.status).toBe('fail')
38+
})
39+
40+
test('should pass with excluded baseRef', async () => {
41+
const baseRef = new BaseRef()
42+
const settings = {
43+
do: 'baseRef',
44+
must_exclude: {
45+
regex: 'some-other-ref'
46+
}
47+
}
48+
const filter = await baseRef.processFilter(createMockContext('some-ref'), settings)
49+
expect(filter.status).toBe('pass')
50+
})
51+
52+
const createMockContext = (baseRef) => {
53+
return Helper.mockContext({ baseRef })
54+
}

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+
| November 12, 2021 : feat: Add baseRef filter `#596 <https://github.com/mergeability/mergeable/pull/596>`_
34
| October 19, 2021 : feat: Add validator approval option to exclude users `#594 <https://github.com/mergeability/mergeable/pull/594>`_
45
| October 12, 2021 : feat: Add boolean option for payload filter `#583 <https://github.com/mergeability/mergeable/pull/583>`_
56
| September 27, 2021 : fix: use node version 14.17.6 for the release action `#591 <https://github.com/mergeability/mergeable/pull/591>`_

docs/filters/baseRef.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
BaseRef
2+
^^^^^^^^^^^^^^
3+
4+
::
5+
6+
- do: baseRef
7+
must_include:
8+
regex: 'some-ref'
9+
message: 'Custom message...'
10+
# all of the message sub-option is optional
11+
12+
you can use ``and`` and ``or`` options to create more complex filters
13+
14+
::
15+
16+
- do: baseRef
17+
and:
18+
- must_exclude:
19+
regex: 'some-other-ref'
20+
message: 'Custom message...'
21+
or:
22+
- must_include:
23+
regex: 'some-ref'
24+
message: 'Custom message...'
25+
- must_include:
26+
regex: 'some-other-ref'
27+
message: 'Custom message...'
28+
29+
you can also nest ``and`` and ``or`` options
30+
31+
::
32+
33+
- do: baseRef
34+
and:
35+
- or:
36+
- must_include:
37+
regex: 'some-ref'
38+
message: 'Custom message...'
39+
- must_include:
40+
regex: 'some-other-ref'
41+
message: 'Custom message...'
42+
- must_exclude:
43+
regex: 'yet-another-ref'
44+
message: 'Custom message...'
45+
46+
Supported Events:
47+
::
48+
49+
'pull_request.*', 'pull_request_review.*'

lib/filters/baseRef.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { Filter } = require('./filter')
2+
3+
class BaseRef extends Filter {
4+
constructor () {
5+
super('baseRef')
6+
this.supportedEvents = [
7+
'pull_request.*',
8+
'pull_request_review.*'
9+
]
10+
this.supportedSettings = {
11+
must_include: {
12+
regex: 'string',
13+
regex_flag: 'string',
14+
message: 'string'
15+
},
16+
must_exclude: {
17+
regex: 'string',
18+
regex_flag: 'string',
19+
message: 'string'
20+
}
21+
}
22+
}
23+
24+
async filter (context, settings) {
25+
const payload = this.getPayload(context)
26+
return this.processOptions(context, payload.base.ref, settings)
27+
}
28+
}
29+
30+
module.exports = BaseRef

0 commit comments

Comments
 (0)