-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run action post command on teardown to set reactions
In case the job was cancelled, or failed for other reasons, we still want to try and update the comment reaction to failed if possible. Change-type: patch Signed-off-by: Kyle Harding <[email protected]>
- Loading branch information
Showing
9 changed files
with
256 additions
and
3 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
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,85 @@ | ||
const core = require('@actions/core') | ||
const { PostProcess } = require('../src/post') | ||
|
||
jest.mock('@actions/core') | ||
|
||
describe('PostProcess', () => { | ||
let postProcess | ||
let mockGitHubClient | ||
let mockReactionManager | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
|
||
mockGitHubClient = { | ||
getAuthenticatedUser: jest.fn() | ||
} | ||
|
||
mockReactionManager = { | ||
setReaction: jest.fn(), | ||
reactions: { | ||
SUCCESS: 'rocket', | ||
FAILED: 'confused' | ||
} | ||
} | ||
|
||
postProcess = new PostProcess(mockGitHubClient, mockReactionManager) | ||
}) | ||
|
||
describe('run', () => { | ||
beforeEach(() => { | ||
core.getState.mockImplementation(key => { | ||
const states = { | ||
'comment-id': 'test-comment-id', | ||
'approved-by': 'test-approver' | ||
} | ||
return states[key] | ||
}) | ||
mockGitHubClient.getAuthenticatedUser.mockResolvedValue({ | ||
id: 'test-user-id' | ||
}) | ||
}) | ||
|
||
test('creates success reaction when approval is successful', async () => { | ||
await postProcess.run() | ||
|
||
expect(mockGitHubClient.getAuthenticatedUser).toHaveBeenCalled() | ||
expect(mockReactionManager.setReaction).toHaveBeenCalledWith( | ||
'test-comment-id', | ||
'test-user-id', | ||
mockReactionManager.reactions.SUCCESS | ||
) | ||
}) | ||
|
||
test('creates failed reaction when approval is not successful', async () => { | ||
core.getState.mockImplementation(key => { | ||
const states = { | ||
'comment-id': 'test-comment-id', | ||
'approved-by': '' | ||
} | ||
return states[key] | ||
}) | ||
|
||
await postProcess.run() | ||
|
||
expect(mockGitHubClient.getAuthenticatedUser).toHaveBeenCalled() | ||
expect(mockReactionManager.setReaction).toHaveBeenCalledWith( | ||
'test-comment-id', | ||
'test-user-id', | ||
mockReactionManager.reactions.FAILED | ||
) | ||
}) | ||
|
||
test('logs a warning when an error occurs', async () => { | ||
mockGitHubClient.getAuthenticatedUser.mockRejectedValue( | ||
new Error('Authentication failed') | ||
) | ||
|
||
await postProcess.run() | ||
|
||
expect(core.warning).toHaveBeenCalledWith( | ||
'Cleanup failed: Authentication failed' | ||
) | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -30,3 +30,5 @@ outputs: | |
runs: | ||
using: 'node20' | ||
main: dist/index.js | ||
post: dist/index.js | ||
post-if: 'always()' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,35 @@ | ||
const core = require('@actions/core') | ||
|
||
class PostProcess { | ||
constructor(gitHubClient, reactionManager) { | ||
this.gitHubClient = gitHubClient | ||
this.reactionManager = reactionManager | ||
} | ||
|
||
async run() { | ||
try { | ||
const commentId = core.getState('comment-id') | ||
const wasApproved = core.getState('approved-by') !== '' | ||
const tokenUser = await this.gitHubClient.getAuthenticatedUser() | ||
|
||
if (commentId && wasApproved) { | ||
await this.reactionManager.setReaction( | ||
commentId, | ||
tokenUser.id, | ||
this.reactionManager.reactions.SUCCESS | ||
) | ||
return | ||
} | ||
await this.reactionManager.setReaction( | ||
commentId, | ||
tokenUser.id, | ||
this.reactionManager.reactions.FAILED | ||
) | ||
} catch (error) { | ||
core.warning(`Cleanup failed: ${error.message}`) | ||
console.trace() | ||
} | ||
} | ||
} | ||
|
||
module.exports = { PostProcess } |