mirror of
https://github.com/mshick/add-pr-comment.git
synced 2025-12-31 14:20:32 +11:00
fix: check for data before testing for length (#12)
* fix: check for data before testing for length * fix: add test for no issues
This commit is contained in:
parent
096294b382
commit
b7f4a4cd6b
2 changed files with 34 additions and 4 deletions
|
|
@ -8,6 +8,8 @@ import run from '../add-pr-comment'
|
|||
|
||||
const repoFullName = 'foo/bar'
|
||||
const repoToken = '12345'
|
||||
const commitSha = 'abc123'
|
||||
const issueNumber = 1
|
||||
const simpleMessage = 'hello world'
|
||||
const multilineMessage = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString()
|
||||
const multilineMessageWindows = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString()
|
||||
|
|
@ -33,10 +35,12 @@ beforeEach(() => {
|
|||
}
|
||||
})
|
||||
|
||||
github.context.sha = commitSha
|
||||
|
||||
// https://developer.github.com/webhooks/event-payloads/#issues
|
||||
github.context.payload = {
|
||||
pull_request: {
|
||||
number: 1,
|
||||
number: issueNumber,
|
||||
},
|
||||
repository: {
|
||||
full_name: repoFullName,
|
||||
|
|
@ -45,7 +49,6 @@ beforeEach(() => {
|
|||
login: 'bar',
|
||||
},
|
||||
},
|
||||
sha: 'abc123',
|
||||
} as WebhookPayload
|
||||
})
|
||||
|
||||
|
|
@ -73,7 +76,7 @@ describe('add-pr-comment action', () => {
|
|||
})
|
||||
|
||||
nock('https://api.github.com')
|
||||
.post(`/repos/${repoFullName}/issues/1/comments`, ({body}) => body === simpleMessage)
|
||||
.post(`/repos/${repoFullName}/issues/${issueNumber}/comments`, ({body}) => body === simpleMessage)
|
||||
.reply(200, {
|
||||
url: 'https://github.com/#example',
|
||||
})
|
||||
|
|
@ -81,6 +84,33 @@ describe('add-pr-comment action', () => {
|
|||
await expect(run()).resolves.not.toThrow()
|
||||
})
|
||||
|
||||
it('safely exits when no issue can be found', async () => {
|
||||
inputs.message = simpleMessage
|
||||
inputs['repo-token'] = repoToken
|
||||
inputs['allow-repeats'] = 'true'
|
||||
|
||||
github.context.payload = {
|
||||
...github.context.payload,
|
||||
pull_request: {
|
||||
number: 0,
|
||||
},
|
||||
} as WebhookPayload
|
||||
|
||||
const originalSetOutput = core.setOutput
|
||||
|
||||
jest.spyOn(core, 'setOutput').mockImplementation((key: string, value: string): void => {
|
||||
if (key === 'comment-created') {
|
||||
expect(value).toBe('false')
|
||||
}
|
||||
|
||||
return originalSetOutput(key, value)
|
||||
})
|
||||
|
||||
nock('https://api.github.com').get(`/repos/${repoFullName}/commits/${commitSha}/pulls`).reply(200, [])
|
||||
|
||||
await run()
|
||||
})
|
||||
|
||||
it('identifies repeat messages and does not create a comment', async () => {
|
||||
inputs.message = simpleMessage
|
||||
inputs['repo-token'] = repoToken
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ const listCommitPulls = async (params: ListCommitPullsParams): Promise<ListCommi
|
|||
}
|
||||
|
||||
const getIssueNumberFromCommitPullsList = (commitPullsList: ListCommitPullsResponse): number | null =>
|
||||
commitPullsList.data.length ? commitPullsList.data[0].number : null
|
||||
commitPullsList.data && commitPullsList.data.length ? commitPullsList.data[0].number : null
|
||||
|
||||
const isMessagePresent = (
|
||||
message: AddPrCommentInputs['message'],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue