Skip to content

Commit 6c9d281

Browse files
authored
Merge pull request #404 from github/unlock-on-merge-improvements
Unlock on Merge - Minor Improvements
2 parents 48eb06d + 61c696c commit 6c9d281

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

__tests__/functions/unlock-on-merge.test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {COLORS} from '../../src/functions/colors'
77

88
const setOutputMock = jest.spyOn(core, 'setOutput')
99
const infoMock = jest.spyOn(core, 'info')
10-
const setFailedMock = jest.spyOn(core, 'setFailed')
10+
const warningMock = jest.spyOn(core, 'warning')
1111
const debugMock = jest.spyOn(core, 'debug')
1212

1313
const environment_targets = 'production,development,staging'
@@ -16,7 +16,7 @@ var context
1616
var octokit
1717
beforeEach(() => {
1818
jest.clearAllMocks()
19-
jest.spyOn(core, 'setFailed').mockImplementation(() => {})
19+
jest.spyOn(core, 'warning').mockImplementation(() => {})
2020
jest.spyOn(core, 'setOutput').mockImplementation(() => {})
2121
jest.spyOn(core, 'info').mockImplementation(() => {})
2222
jest.spyOn(core, 'debug').mockImplementation(() => {})
@@ -148,7 +148,25 @@ test('fails due to the context not being a PR merge', async () => {
148148
expect(infoMock).toHaveBeenCalledWith(
149149
'event name: pull_request, action: opened, merged: false'
150150
)
151-
expect(setFailedMock).toHaveBeenCalledWith(
152-
'this workflow can only run in the context of a merged pull request'
151+
expect(warningMock).toHaveBeenCalledWith(
152+
`this workflow can only run in the context of a ${COLORS.highlight}merged${COLORS.reset} pull request`
153+
)
154+
})
155+
156+
test('fails due to the context being a PR closed event but not a merge', async () => {
157+
context.payload.action = 'closed'
158+
context.payload.pull_request.merged = false
159+
context.payload.eventName = 'pull_request'
160+
expect(
161+
await unlockOnMerge(octokit, context, environment_targets)
162+
).toStrictEqual(false)
163+
expect(warningMock).toHaveBeenCalledWith(
164+
`this workflow can only run in the context of a ${COLORS.highlight}merged${COLORS.reset} pull request`
165+
)
166+
expect(infoMock).toHaveBeenCalledWith(
167+
'event name: pull_request, action: closed, merged: false'
168+
)
169+
expect(infoMock).toHaveBeenCalledWith(
170+
'pull request was closed but not merged so this workflow will not run - OK'
153171
)
154172
})

dist/index.js

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions/unlock-on-merge.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ export async function unlockOnMerge(octokit, context, environment_targets) {
1818
context?.payload?.action !== 'closed' ||
1919
context?.payload?.pull_request?.merged !== true
2020
) {
21+
core.warning(
22+
`this workflow can only run in the context of a ${COLORS.highlight}merged${COLORS.reset} pull request`
23+
)
2124
core.info(
2225
`event name: ${context?.eventName}, action: ${context?.payload?.action}, merged: ${context?.payload?.pull_request?.merged}`
2326
)
24-
core.setFailed(
25-
'this workflow can only run in the context of a merged pull request'
26-
)
27+
28+
// many pull requests in a project will end up being closed without being merged, so we can just log this so its clear
29+
if (context?.payload?.action === 'closed') {
30+
core.info(
31+
`pull request was closed but not merged so this workflow will not run - OK`
32+
)
33+
}
34+
2735
return false
2836
}
2937

0 commit comments

Comments
 (0)