From 624945381bc1ad25d7df26e7fe07ebfe98e763f1 Mon Sep 17 00:00:00 2001 From: Wagner Santos <7467450+wagoid@users.noreply.github.com> Date: Sun, 23 Jul 2023 07:31:18 -0300 Subject: [PATCH] fix: make sure action passes when event doesn't have commits fixes #746 --- src/action.js | 15 +++++++++++---- src/action.test.js | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/action.js b/src/action.js index bc21e990..dfe4fe6c 100644 --- a/src/action.js +++ b/src/action.js @@ -31,10 +31,7 @@ const getPushEventCommits = () => { return mappedCommits } -const getEventCommits = async () => { - if (!pullRequestEvents.includes(GITHUB_EVENT_NAME)) - return getPushEventCommits() - +const getPullRequestEventCommits = async () => { const octokit = getOctokit(getInput('token')) const { owner, repo, number } = eventContext.issue const { data: commits } = await octokit.rest.pulls.listCommits({ @@ -50,6 +47,16 @@ const getEventCommits = async () => { })) } +const getEventCommits = async () => { + if (pullRequestEvents.includes(GITHUB_EVENT_NAME)) { + return getPullRequestEventCommits() + } + if (eventContext.payload.commits) { + return getPushEventCommits() + } + return [] +} + function getOptsFromConfig(config) { return { parserOpts: diff --git a/src/action.test.js b/src/action.test.js index 8b96c405..afe9108f 100644 --- a/src/action.test.js +++ b/src/action.test.js @@ -281,6 +281,20 @@ describe('Commit Linter action', () => { ) }) + it('should pass when commits are not available', async () => { + td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js') + cwd = await git.bootstrap('fixtures/conventional') + await createPushEventPayload(cwd, {}) + updatePushEnvVars(cwd) + td.replace(process, 'cwd', () => cwd) + td.replace(console, 'log') + + await runAction() + + td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true }) + td.verify(console.log('Lint free! 🎉')) + }) + describe.each(['pull_request', 'pull_request_target'])( 'when there are multiple commits failing in the %s event', (eventName) => {