Skip to content

Commit a1f083e

Browse files
authored
adding comment when check fails (#53)
1 parent 6359a4d commit a1f083e

File tree

5 files changed

+121
-43
lines changed

5 files changed

+121
-43
lines changed

__tests__/main.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,22 @@ const createChecksMock = jest.fn().mockResolvedValue({
5555
data: {}
5656
})
5757

58+
const createCommentMock = jest.fn().mockResolvedValue({
59+
status: 200,
60+
headers: {},
61+
data: {}
62+
})
63+
5864
const getOctokitMock = jest
5965
.spyOn(github, 'getOctokit')
6066
.mockImplementation(() => {
6167
return {
6268
rest: {
6369
checks: {
6470
create: createChecksMock
71+
},
72+
issues: {
73+
createComment: createCommentMock
6574
}
6675
}
6776
}
@@ -176,16 +185,33 @@ describe('action', () => {
176185
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'result', 'Success')
177186
})
178187

188+
it('creates a comment when github checks API fails', async () => {
189+
// Set the action's inputs as return values from core.getInput()
190+
mockInput()
191+
github.context = {
192+
payload: { pull_request: { number: 123 } },
193+
issue: { number: 123 }
194+
}
195+
createChecksMock.mockRejectedValue(new Error('Failed to create check'))
196+
197+
await main.run()
198+
expect(runMock).toHaveReturned()
199+
200+
// Verify that comment was created
201+
expect(createCommentMock).toHaveBeenCalled()
202+
})
203+
179204
it('sets a failed status when github fails', async () => {
180205
// Set the action's inputs as return values from core.getInput()
181206
mockInput()
182207
createChecksMock.mockRejectedValue(new Error('Failed to create check'))
208+
createCommentMock.mockRejectedValue(new Error('Failed to create comment'))
183209

184210
await main.run()
185211
expect(runMock).toHaveReturned()
186212

187213
// Verify that all of the core library functions were called correctly
188-
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'Failed to create check')
214+
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'Failed to create comment')
189215
})
190216

191217
it('fails if no pylint file provided', async () => {

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

dist/index.js

Lines changed: 46 additions & 20 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/main.js

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,53 @@ async function run() {
5858

5959
const octokit = github.getOctokit(token)
6060

61-
const resp = await octokit.rest.checks.create({
62-
owner: repo_owner,
63-
repo: repo_name,
64-
name: 'pylint',
65-
head_sha: headSha,
66-
completed_at: new Date().toISOString(),
67-
conclusion,
68-
status: 'completed',
69-
output: {
70-
title:
71-
conclusion === 'success'
72-
? 'No issues have been found!'
73-
: 'Pylint has some suggestions!',
74-
summary:
75-
conclusion === 'success'
76-
? 'No issues have been found!'
77-
: `Pylint has some suggestions!'${trimmedWarning}'`,
78-
annotations
61+
try {
62+
const resp = await octokit.rest.checks.create({
63+
owner: repo_owner,
64+
repo: repo_name,
65+
name: 'pylint',
66+
head_sha: headSha,
67+
completed_at: new Date().toISOString(),
68+
conclusion,
69+
status: 'completed',
70+
output: {
71+
title:
72+
conclusion === 'success'
73+
? 'No issues have been found!'
74+
: 'Pylint has some suggestions!',
75+
summary:
76+
conclusion === 'success'
77+
? 'No issues have been found!'
78+
: `Pylint has some suggestions!'${trimmedWarning}'`,
79+
annotations
80+
}
81+
})
82+
core.debug(`response from checks create: ${resp.status}`)
83+
} catch (checkError) {
84+
core.error(`Error creating checks: ${checkError}`)
85+
core.debug('Trying to add a comment instead')
86+
87+
const comment = annotations
88+
.map(
89+
annotation =>
90+
`::${annotation.annotation_level} file=${annotation.path},line=${annotation.start_line},col=${annotation.start_column}::${annotation.message}`
91+
)
92+
.join('\n')
93+
94+
const issueNumber =
95+
github.context?.payload?.pull_request?.number ||
96+
github.context?.issue?.number
97+
if (issueNumber) {
98+
const commentResp = await octokit.rest.issues.createComment({
99+
owner: repo_owner,
100+
repo: repo_name,
101+
issue_number: issueNumber,
102+
body: comment
103+
})
104+
core.debug(`response from comment create: ${commentResp.status}`)
79105
}
80-
})
81-
core.debug(`response from checks create: ${resp.status}`)
106+
}
107+
82108
core.setOutput('result', 'Success')
83109
} catch (error) {
84110
// Fail the workflow run if an error occurs

0 commit comments

Comments
 (0)