Skip to content

Commit 196099e

Browse files
authored
feat: Support 'issue_comment' event as trigger for actions (#754)
1 parent 3beeb24 commit 196099e

22 files changed

+203
-32
lines changed

__fixtures__/unit/helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ module.exports = {
182182
},
183183
requestReviewers: jest.fn().mockReturnValue(options.requestReviewers || 'request review success'),
184184
merge: jest.fn().mockReturnValue(options.merge || 'merged'),
185-
get: jest.fn()
185+
get: jest.fn().mockReturnValue({ data: { head: { ref: 'test', sha: 'sha1' } } })
186186
},
187187
paginate: jest.fn(async (fn, cb) => {
188188
return fn.then(cb)

__tests__/unit/actions/assign.test.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
const Assign = require('../../../lib/actions/assign')
22
const Helper = require('../../../__fixtures__/unit/helper')
33

4+
test.each([
5+
undefined,
6+
'pull_request',
7+
'issues',
8+
'issue_comment'
9+
])('check that assign is called for %s events', async (eventName) => {
10+
const settings = {
11+
assignees: []
12+
}
13+
14+
const assign = new Assign()
15+
const context = createMockContext(eventName)
16+
17+
await assign.afterValidate(context, settings)
18+
expect(context.octokit.issues.addAssignees.mock.calls.length).toBe(1)
19+
})
20+
421
test('check that assignees are added when afterValidate is called with proper parameter', async () => {
522
const settings = {
623
assignees: ['testuser1', 'testuser2']
724
}
825

9-
const comment = new Assign()
26+
const assign = new Assign()
1027
const context = createMockContext()
1128

12-
await comment.afterValidate(context, settings)
29+
await assign.afterValidate(context, settings)
1330
expect(context.octokit.issues.addAssignees.mock.calls.length).toBe(1)
1431
expect(context.octokit.issues.addAssignees.mock.calls[0][0].assignees[0]).toBe('testuser1')
1532
expect(context.octokit.issues.addAssignees.mock.calls[0][0].assignees[1]).toBe('testuser2')
@@ -20,10 +37,10 @@ test('check that creator is added when assignee is @author', async () => {
2037
assignees: ['@author']
2138
}
2239

23-
const comment = new Assign()
40+
const assign = new Assign()
2441
const context = createMockContext()
2542

26-
await comment.afterValidate(context, settings)
43+
await assign.afterValidate(context, settings)
2744
expect(context.octokit.issues.addAssignees.mock.calls.length).toBe(1)
2845
expect(context.octokit.issues.addAssignees.mock.calls[0][0].assignees[0]).toBe('creator')
2946
})
@@ -33,7 +50,7 @@ test('check only authorized users are added as assignee ', async () => {
3350
assignees: ['testuser1', 'testuser2']
3451
}
3552

36-
const comment = new Assign()
53+
const assign = new Assign()
3754
const context = createMockContext()
3855

3956
context.octokit.issues.checkUserCanBeAssigned = (input) => {
@@ -45,14 +62,14 @@ test('check only authorized users are added as assignee ', async () => {
4562
})
4663
}
4764

48-
await comment.afterValidate(context, settings)
65+
await assign.afterValidate(context, settings)
4966
expect(context.octokit.issues.addAssignees.mock.calls.length).toBe(1)
5067
expect(context.octokit.issues.addAssignees.mock.calls[0][0].assignees[0]).toBe('testuser1')
5168
expect(context.octokit.issues.addAssignees.mock.calls[0][0].assignees[1]).toBeUndefined()
5269
})
5370

54-
const createMockContext = () => {
55-
const context = Helper.mockContext()
71+
const createMockContext = (eventName = undefined) => {
72+
const context = Helper.mockContext({ eventName })
5673

5774
context.octokit.issues.addAssignees = jest.fn()
5875
return context

__tests__/unit/actions/checks.test.js

+33-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,43 @@ const MetaData = require('../../../lib/metaData')
22
const Checks = require('../../../lib/actions/checks')
33
const Helper = require('../../../__fixtures__/unit/helper')
44

5-
test('run', async () => {
5+
test.each([
6+
undefined,
7+
'pull_request',
8+
'pull_request_review',
9+
'issue_comment'
10+
])('that checks is called for %s events', async (eventName) => {
11+
const checks = new Checks()
12+
const context = createMockContext(eventName)
13+
const result = {}
14+
const settings = {
15+
payload: {},
16+
state: 'completed',
17+
status: 'success'
18+
}
19+
20+
const name = undefined
21+
22+
checks.checkRunResult = new Map()
23+
24+
checks.checkRunResult.set(name, {
25+
data: {
26+
id: '3'
27+
}
28+
})
29+
30+
await checks.afterValidate(context, settings, name, result)
31+
expect(context.octokit.checks.update.mock.calls.length).toBe(1)
32+
})
33+
34+
test('that run calls create api', async () => {
635
const checks = new Checks()
736
const context = createMockContext()
837
await checks.run({ context, payload: {} })
938
expect(context.octokit.checks.create.mock.calls.length).toBe(1)
1039
})
1140

12-
test('check that checks created when doPostAction is called with proper parameter', async () => {
41+
test('that checks created when doPostAction is called with proper parameter', async () => {
1342
const checks = new Checks()
1443
const context = createMockContext()
1544
const settings = { name: 'test' }
@@ -183,8 +212,8 @@ test('that correct name is used afterValidate payload', async () => {
183212
expect(MetaData.exists(output.text)).toBe(true)
184213
})
185214

186-
const createMockContext = () => {
187-
const context = Helper.mockContext()
215+
const createMockContext = (eventName = undefined) => {
216+
const context = Helper.mockContext({ eventName })
188217
context.payload.action = 'actionName'
189218
context.octokit.checks.create = jest.fn()
190219
context.octokit.checks.update = jest.fn()

__tests__/unit/actions/close.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
const Close = require('../../../lib/actions/close')
22
const Helper = require('../../../__fixtures__/unit/helper')
33

4+
test.each([
5+
undefined,
6+
'pull_request',
7+
'issues',
8+
'issue_comment',
9+
'schedule'
10+
])('check that close is called for %s events', async (eventName) => {
11+
const close = new Close()
12+
const context = Helper.mockContext({ eventName: eventName })
13+
const schedulerResult = {
14+
validationSuites: [{
15+
schedule: {
16+
issues: [{ number: 1, user: { login: 'scheduler' } }],
17+
pulls: []
18+
}
19+
}]
20+
}
21+
22+
await close.afterValidate(context, {}, '', schedulerResult)
23+
expect(context.octokit.issues.update.mock.calls.length).toBe(1)
24+
})
25+
426
test('check that issue is closed', async () => {
527
const close = new Close()
628
const context = Helper.mockContext()

__tests__/unit/actions/comment.test.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ const result = {
1515
}]
1616
}
1717

18+
test.each([
19+
undefined,
20+
'pull_request',
21+
'issues',
22+
'issue_comment',
23+
'schedule'
24+
])('check that comment is called for %s events', async (eventName) => {
25+
const comment = new Comment()
26+
const context = createMockContext([], eventName)
27+
const schedulerResult = { ...result }
28+
schedulerResult.validationSuites = [{
29+
schedule: {
30+
issues: [{ number: 1, user: { login: 'scheduler' } }],
31+
pulls: []
32+
}
33+
}]
34+
35+
await comment.afterValidate(context, settings, '', schedulerResult)
36+
expect(context.octokit.issues.createComment.mock.calls.length).toBe(1)
37+
})
38+
1839
test('check that comment created when afterValidate is called with proper parameter', async () => {
1940
const comment = new Comment()
2041
const context = createMockContext()
@@ -34,8 +55,7 @@ test('check that comment created when afterValidate is called with proper parame
3455

3556
test('that comment is created three times when result contain three issues found to be acted on', async () => {
3657
const comment = new Comment()
37-
const context = createMockContext([], 'repository')
38-
context.eventName = 'schedule'
58+
const context = createMockContext([], 'schedule', 'repository')
3959
const schedulerResult = { ...result }
4060
schedulerResult.validationSuites = [{
4161
schedule: {
@@ -236,8 +256,8 @@ test('error handling includes removing old error comments and creating new error
236256
expect(context.octokit.issues.createComment.mock.calls[0][0].body).toBe('creator , do something!')
237257
})
238258

239-
const createMockContext = (listComments, event = undefined) => {
240-
const context = Helper.mockContext({ listComments, event })
259+
const createMockContext = (listComments, eventName = undefined, event = undefined) => {
260+
const context = Helper.mockContext({ listComments, eventName, event })
241261

242262
context.octokit.issues.createComment = jest.fn()
243263
context.octokit.issues.deleteComment = jest.fn()

__tests__/unit/actions/labels.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@ const Labels = require('../../../lib/actions/labels')
22
const Helper = require('../../../__fixtures__/unit/helper')
33
const UnSupportedSettingError = require('../../../lib/errors/unSupportedSettingError')
44

5+
test.each([
6+
undefined,
7+
'pull_request',
8+
'issues',
9+
'issue_comment',
10+
'schedule'
11+
])('check that close is called for %s events', async (eventName) => {
12+
const labels = new Labels()
13+
const context = createMockContext([], eventName)
14+
const settings = {
15+
add: ['a label']
16+
}
17+
const schedulerResult = {
18+
validationSuites: [{
19+
schedule: {
20+
issues: [{ number: 1, user: { login: 'scheduler' } }],
21+
pulls: []
22+
}
23+
}]
24+
}
25+
26+
await labels.afterValidate(context, settings, '', schedulerResult)
27+
expect(context.octokit.issues.setLabels.mock.calls.length).toBe(1)
28+
})
29+
530
test('check that replace replaces existing labels', async () => {
631
const labels = new Labels()
732
const context = createMockContext(['drop_label'])

__tests__/unit/configuration/transformers/v2Config.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ test('pass, fail, error defaults will load when pull_request is mixed with other
9292
expect(transformed.mergeable[0].error).toEqual(constants.DEFAULT_PR_ERROR)
9393
})
9494

95+
test('pass, fail, error defaults will load when issue_comment event is specified.', () => {
96+
const config = `
97+
version: 2
98+
mergeable:
99+
- when: issue_comment.*
100+
validate:
101+
- do: lastComment
102+
must_exclude:
103+
regex: 'wip|work in progress'
104+
`
105+
const transformed = V2Config.transform(yaml.safeLoad(config))
106+
107+
expect(transformed.mergeable[0].pass).toEqual(constants.DEFAULT_PR_PASS)
108+
expect(transformed.mergeable[0].fail).toEqual(constants.DEFAULT_PR_FAIL)
109+
expect(transformed.mergeable[0].error).toEqual(constants.DEFAULT_PR_ERROR)
110+
})
111+
112+
test('pass, fail, error defaults will load when pull_request_review event is specified.', () => {
113+
const config = `
114+
version: 2
115+
mergeable:
116+
- when: pull_request_review.*
117+
validate:
118+
- do: payload
119+
review:
120+
state:
121+
must_exclude:
122+
regex: 'changes_requested'
123+
`
124+
const transformed = V2Config.transform(yaml.safeLoad(config))
125+
126+
expect(transformed.mergeable[0].pass).toEqual(constants.DEFAULT_PR_PASS)
127+
expect(transformed.mergeable[0].fail).toEqual(constants.DEFAULT_PR_FAIL)
128+
expect(transformed.mergeable[0].error).toEqual(constants.DEFAULT_PR_ERROR)
129+
})
130+
95131
test('only pass, fail defaults ignore recipes that are not for pull_requests', () => {
96132
const config = `
97133
version: 2

docs/actions/assign.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Assign
22
^^^^^^^^
33

4+
You can assign specific people to a pull request or issue.
5+
46
::
57

68
- do: assign
@@ -9,4 +11,4 @@ Assign
911
Supported Events:
1012
::
1113

12-
'pull_request.*', 'issues.*'
14+
'pull_request.*', 'issues.*', 'issue_comment.*'

docs/actions/check.rst renamed to docs/actions/checks.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
Check
1+
Checks
22
^^^^^^^^
33

4+
You can add check runs to a pull request to enforce that it can only be merged once all checks returned with success.
5+
46
.. note::
57
The logic for whether checks will be added by default is as follows:
68
1. If no action is provided in either pass, fail or error , add `checks` as default (to be backward compatible)
@@ -74,4 +76,4 @@ The `pull_request.closed` event is not supported since it does not have meaningf
7476

7577
::
7678

77-
'pull_request.assigned', 'pull_request.auto_merge_disabled', 'pull_request.auto_merge_enabled', 'pull_request.converted_to_draft', 'pull_request.demilestoned', 'pull_request.dequeued', 'pull_request.edited', 'pull_request.enqueued', 'pull_request.labeled', 'pull_request.locked', 'pull_request.milestoned', 'pull_request.opened', 'pull_request.push_synchronize', 'pull_request.ready_for_review', 'pull_request.reopened', 'pull_request.review_request_removed', 'pull_request.review_requested', 'pull_request.synchronize', 'pull_request.unassigned', 'pull_request.unlabeled', 'pull_request.unlocked', 'pull_request_review.dismissed', 'pull_request_review.edited', 'pull_request_review.submitted'
79+
'pull_request.assigned', 'pull_request.auto_merge_disabled', 'pull_request.auto_merge_enabled', 'pull_request.converted_to_draft', 'pull_request.demilestoned', 'pull_request.dequeued', 'pull_request.edited', 'pull_request.enqueued', 'pull_request.labeled', 'pull_request.locked', 'pull_request.milestoned', 'pull_request.opened', 'pull_request.push_synchronize', 'pull_request.ready_for_review', 'pull_request.reopened', 'pull_request.review_request_removed', 'pull_request.review_requested', 'pull_request.synchronize', 'pull_request.unassigned', 'pull_request.unlabeled', 'pull_request.unlocked', 'pull_request_review.dismissed', 'pull_request_review.edited', 'pull_request_review.submitted', 'issue_comment.*'

docs/actions/close.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
Close
22
^^^^^^^^
33

4+
You can close a pull request or issue.
5+
46
::
57

68
- do: close
79

810
Supported Events:
911
::
1012

11-
'schedule.repository', 'pull_request.*', 'issues.*'
13+
'schedule.repository', 'pull_request.*', 'issues.*', 'issue_comment.*'

docs/actions/comment.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Comment
22
^^^^^^^^
33

4+
You can add a comment to a pull request or issue.
5+
46
::
57

68
- do: comment
@@ -12,4 +14,4 @@ Comment
1214
Supported Events:
1315
::
1416

15-
'schedule.repository', 'pull_request.*', 'issues.*'
17+
'schedule.repository', 'pull_request.*', 'issues.*', 'issue_comment.*'

docs/actions/labels.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,4 @@ Note that the glob functionality is powered by the minimatch library. Please see
8383
Supported Events:
8484
::
8585

86-
'schedule.repository', 'pull_request.*', 'issues.*'
86+
'schedule.repository', 'pull_request.*', 'issues.*', 'issue_comment.*'

docs/actions/merge.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Merge
22
^^^^^^^^
33

4+
You can merge a pull request and specify the merge method used.
5+
46
::
57

68
- do: merge
@@ -14,4 +16,4 @@ Merge
1416
Supported Events:
1517
::
1618

17-
'pull_request.*', 'pull_request_review.*', 'status.*', 'check_suite.*'
19+
'pull_request.*', 'pull_request_review.*', 'status.*', 'check_suite.*', 'issue_comment.*'

docs/actions/request_review.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Request Review
22
^^^^^^^^^^^^^^^
33

4-
You can request specific reviews from specific reviewers, teams, or both
4+
You can request specific reviews from specific reviewers, teams, or both for a pull request.
55

66
::
77

docs/changelog.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CHANGELOG
22
=====================================
3+
| June 12, 2024: feat: Support `issue_comment` event as trigger for actions `#754 <https://github.com/mergeability/mergeable/pull/754>`_
34
| June 10, 2024: fix: Docker image not working `#753 <https://github.com/mergeability/mergeable/pull/753>`_
45
| June 10, 2024: feat: publish multi arch docker image to dockerhub `#751 <https://github.com/mergeability/mergeable/pull/751>`_
56
| April 29, 2024: fix: Always allow assigning author `#744 <https://github.com/mergeability/mergeable/pull/744>`_

0 commit comments

Comments
 (0)