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:
Michael Shick 2020-05-21 18:26:10 -04:00 committed by GitHub
parent 096294b382
commit b7f4a4cd6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View file

@ -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

View file

@ -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'],