From b7f4a4cd6ba14808d1d2ef0c5a46e04a0874fe6f Mon Sep 17 00:00:00 2001 From: Michael Shick Date: Thu, 21 May 2020 18:26:10 -0400 Subject: [PATCH] fix: check for data before testing for length (#12) * fix: check for data before testing for length * fix: add test for no issues --- __tests__/add-pr-comment.test.ts | 36 +++++++++++++++++++++++++++++--- add-pr-comment.ts | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/__tests__/add-pr-comment.test.ts b/__tests__/add-pr-comment.test.ts index a2da821..0922036 100644 --- a/__tests__/add-pr-comment.test.ts +++ b/__tests__/add-pr-comment.test.ts @@ -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 diff --git a/add-pr-comment.ts b/add-pr-comment.ts index 1cf927a..7bf980d 100644 --- a/add-pr-comment.ts +++ b/add-pr-comment.ts @@ -38,7 +38,7 @@ const listCommitPulls = async (params: ListCommitPullsParams): Promise - commitPullsList.data.length ? commitPullsList.data[0].number : null + commitPullsList.data && commitPullsList.data.length ? commitPullsList.data[0].number : null const isMessagePresent = ( message: AddPrCommentInputs['message'],