diff --git a/src/action.mjs b/src/action.mjs index 7d30200..49d7285 100644 --- a/src/action.mjs +++ b/src/action.mjs @@ -12,6 +12,7 @@ const pullRequestTargetEvent = 'pull_request_target' const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent] const { GITHUB_EVENT_NAME } = process.env +const FIRST_COMMIT_SHA = '0000000000000000000000000000000000000000' const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile')) @@ -26,6 +27,14 @@ const getPushEventCommits = async () => { const octokit = getOctokit(getInput('token')) const { owner, repo } = eventContext.issue const { before, after } = eventContext.payload + + if (before === FIRST_COMMIT_SHA) { + return eventContext.payload.commits.map((commit) => ({ + message: commit.message, + hash: commit.id, + })) + } + const { data: comparison } = await octokit.rest.repos.compareCommits({ owner, repo, diff --git a/src/action.test.mjs b/src/action.test.mjs index 0525d0f..7b91e48 100644 --- a/src/action.test.mjs +++ b/src/action.test.mjs @@ -4,11 +4,11 @@ import { git } from '@commitlint/test' import { jest, describe, it } from '@jest/globals' import * as td from 'testdouble' import { - updatePushEnvVars, - createPushEventPayload, - createPullRequestEventPayload, - updatePullRequestEnvVars, buildResponseCommit, + createPullRequestEventPayload, + createPushEventPayload, + updatePullRequestEnvVars, + updatePushEnvVars, } from './testUtils.mjs' const resultsOutputId = 'results' @@ -451,6 +451,37 @@ describe('Commit Linter action', () => { td.verify(console.log('Lint free! 🎉')) }) + it('should get commits from event for a first push', async () => { + const commit = { + id: '0000000000000000000000000000000000000000', + message: 'chore: correct message', + } + + cwd = await git.bootstrap('fixtures/conventional', process.cwd()) + await createPushEventPayload( + cwd, + { commits: [commit] }, + '0000000000000000000000000000000000000000', + ) + updatePushEnvVars(cwd) + td.when( + mockCompareCommits({ + owner: 'wagoid', + repo: 'commitlint-github-action', + head: 'aaaaa', + base: '0000000000000000000000000000000000000000', + per_page: 100, + }), + ).thenResolve({ + data: { commits: [buildResponseCommit(commit.id, commit.message)] }, + }) + td.replace(process, 'cwd', () => cwd) + td.replace(console, 'log') + + td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true }) + td.verify(mockCompareCommits(), { times: 0 }) + }) + describe.each(['pull_request', 'pull_request_target'])( 'when there are multiple commits failing in the %s event', (eventName) => {