From de513030467551ee03fb8827bd790967fd5818ab Mon Sep 17 00:00:00 2001 From: wagoid <7467450+wagoid@users.noreply.github.com> Date: Mon, 5 Apr 2021 09:08:06 -0300 Subject: [PATCH] feat: add support for pull_request_target event Previously, the action was treating pull_request_target event as a regular push, which was causing some issues. --- src/action.js | 5 +- src/action.test.js | 121 +++++++++++++++++++++++---------------------- src/testUtils.js | 6 ++- 3 files changed, 71 insertions(+), 61 deletions(-) diff --git a/src/action.js b/src/action.js index 43c5aaf..aa7e6b8 100644 --- a/src/action.js +++ b/src/action.js @@ -9,6 +9,8 @@ const gitCommits = require('./gitCommits') const generateOutputs = require('./generateOutputs') const pullRequestEvent = 'pull_request' +const pullRequestTargetEvent = 'pull_request_target' +const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent] const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env @@ -45,7 +47,8 @@ const getRangeForPushEvent = () => { } const getRangeForEvent = async () => { - if (GITHUB_EVENT_NAME !== pullRequestEvent) return getRangeForPushEvent() + if (!pullRequestEvents.includes(GITHUB_EVENT_NAME)) + return getRangeForPushEvent() const octokit = github.getOctokit(core.getInput('token')) const { owner, repo, number } = eventContext.issue diff --git a/src/action.test.js b/src/action.test.js index 685fe22..86a876d 100644 --- a/src/action.test.js +++ b/src/action.test.js @@ -261,75 +261,80 @@ describe('Commit Linter action', () => { td.verify(core.setFailed(contains('wrong commit from another branch'))) }) - describe('when there are multiple commits failing in the pull request', () => { - let expectedResultsOutput - const firstMessage = 'wrong message 1' - const secondMessage = 'wrong message 2' + describe.each(['pull_request', 'pull_request_target'])( + 'when there are multiple commits failing in the %s event', + (eventName) => { + let expectedResultsOutput + const firstMessage = 'wrong message 1' + const secondMessage = 'wrong message 2' - beforeEach(async () => { - cwd = await git.bootstrap('fixtures/conventional') - td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js') - await gitEmptyCommit(cwd, 'message from before push') - await gitEmptyCommit(cwd, firstMessage) - await gitEmptyCommit(cwd, secondMessage) - await createPullRequestEventPayload(cwd) - const [, first, to] = await getCommitHashes(cwd) - updatePullRequestEnvVars(cwd, to) - td.when( - listCommits({ - owner: 'wagoid', - repo: 'commitlint-github-action', - pull_number: '1', - }), - ).thenResolve({ - data: [first, to].map((sha) => ({ sha })), + beforeEach(async () => { + cwd = await git.bootstrap('fixtures/conventional') + td.when(core.getInput('configFile')).thenReturn( + './commitlint.config.js', + ) + await gitEmptyCommit(cwd, 'message from before push') + await gitEmptyCommit(cwd, firstMessage) + await gitEmptyCommit(cwd, secondMessage) + await createPullRequestEventPayload(cwd) + const [, first, to] = await getCommitHashes(cwd) + updatePullRequestEnvVars(cwd, to, { eventName }) + td.when( + listCommits({ + owner: 'wagoid', + repo: 'commitlint-github-action', + pull_number: '1', + }), + ).thenResolve({ + data: [first, to].map((sha) => ({ sha })), + }) + td.replace(process, 'cwd', () => cwd) + + expectedResultsOutput = [ + { + hash: to, + message: secondMessage, + valid: false, + errors: ['subject may not be empty', 'type may not be empty'], + warnings: [], + }, + { + hash: first, + message: firstMessage, + valid: false, + errors: ['subject may not be empty', 'type may not be empty'], + warnings: [], + }, + ] }) - td.replace(process, 'cwd', () => cwd) - expectedResultsOutput = [ - { - hash: to, - message: secondMessage, - valid: false, - errors: ['subject may not be empty', 'type may not be empty'], - warnings: [], - }, - { - hash: first, - message: firstMessage, - valid: false, - errors: ['subject may not be empty', 'type may not be empty'], - warnings: [], - }, - ] - }) + it('should NOT show errors for a message from before the push', async () => { + await runAction() - it('should NOT show errors for a message from before the push', async () => { - await runAction() - - td.verify(core.setFailed(contains('message from before push')), { - times: 0, + td.verify(core.setFailed(contains('message from before push')), { + times: 0, + }) }) - }) - it('should show errors for the first wrong message', async () => { - await runAction() + it('should show errors for the first wrong message', async () => { + await runAction() - td.verify(core.setFailed(contains(firstMessage))) - }) + td.verify(core.setFailed(contains(firstMessage))) + }) - it('should show errors for the second wrong message', async () => { - await runAction() + it('should show errors for the second wrong message', async () => { + await runAction() - td.verify(core.setFailed(contains(secondMessage))) - }) + td.verify(core.setFailed(contains(secondMessage))) + }) - it('should generate a JSON output of the errors', async () => { - await runAction() + it('should generate a JSON output of the errors', async () => { + await runAction() - td.verify(core.setOutput(resultsOutputId, expectedResultsOutput)) - }) - }) + td.verify(core.setOutput(resultsOutputId, expectedResultsOutput)) + }) + }, + ) describe('when it fails to fetch commits', () => { beforeEach(async () => { diff --git a/src/testUtils.js b/src/testUtils.js index 4ed4718..5403370 100644 --- a/src/testUtils.js +++ b/src/testUtils.js @@ -66,10 +66,12 @@ exports.createPullRequestEventPayload = async (cwd) => { await writeFile(eventPath, JSON.stringify(payload), 'utf8') } -exports.updatePullRequestEnvVars = (cwd, to) => { +exports.updatePullRequestEnvVars = (cwd, to, options = {}) => { + const { eventName = 'pull_request' } = options + updateEnvVars({ GITHUB_WORKSPACE: cwd, - GITHUB_EVENT_NAME: 'pull_request', + GITHUB_EVENT_NAME: eventName, GITHUB_SHA: to, }) }