From 08591292aea3022c7375327d6d44ec22181775be Mon Sep 17 00:00:00 2001 From: Yossi Saadi Date: Sun, 8 Dec 2024 09:44:20 +0200 Subject: [PATCH] test: merge_group event --- src/action.test.mjs | 54 +++++++++++++++++++++++++++++++++++++++++++++ src/testUtils.mjs | 29 ++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/action.test.mjs b/src/action.test.mjs index 7b91e48..1a57f40 100644 --- a/src/action.test.mjs +++ b/src/action.test.mjs @@ -5,8 +5,10 @@ import { jest, describe, it } from '@jest/globals' import * as td from 'testdouble' import { buildResponseCommit, + createMergeGroupEventPayload, createPullRequestEventPayload, createPushEventPayload, + updateMergeGroupEnvVars, updatePullRequestEnvVars, updatePushEnvVars, } from './testUtils.mjs' @@ -952,4 +954,56 @@ describe('Commit Linter action', () => { td.verify(mockCore.setFailed(contains(incorrectCommit.message))) }) }) + + describe('when handling merge_group event', () => { + beforeEach(async () => { + cwd = await git.bootstrap('fixtures/conventional', process.cwd()) + td.when(mockCore.getInput('configFile')).thenReturn( + './commitlint.config.mjs', + ) + + td.replace(process, 'cwd', () => cwd) + td.replace(console, 'log') + }) + + it('should lint the squashed commit message successfully', async () => { + const mergeGroupData = { + head_sha: 'merge-group-head-sha', + head_commit: { + id: 'merge-group-head-sha', + message: 'feat: add new feature\n\nThis is a detailed description.', + tree_id: 'tree-sha', + }, + } + + await createMergeGroupEventPayload(cwd, mergeGroupData) + updateMergeGroupEnvVars(cwd) + + await runAction() + + td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true }) + td.verify(console.log('Lint free! 🎉')) + }) + + it('should fail if the squashed commit message has linting errors', async () => { + const mergeGroupData = { + head_sha: 'merge-group-head-sha', + head_commit: { + id: 'merge-group-head-sha', + message: 'bad commit message', + tree_id: 'tree-sha', + }, + } + + await createMergeGroupEventPayload(cwd, mergeGroupData) + updateMergeGroupEnvVars(cwd) + + await runAction() + + td.verify( + mockCore.setFailed(contains('You have commit messages with errors')), + ) + td.verify(mockCore.setFailed(contains('bad commit message'))) + }) + }) }) diff --git a/src/testUtils.mjs b/src/testUtils.mjs index 604bf2a..07426bf 100644 --- a/src/testUtils.mjs +++ b/src/testUtils.mjs @@ -77,3 +77,32 @@ export const buildResponseCommit = (sha, message) => ({ message, }, }) + +export const createMergeGroupEventPayload = async (cwd, mergeGroupData) => { + const payload = { + action: 'checks_requested', + merge_group: mergeGroupData, + repository: { + owner: { + login: 'wagoid', + }, + name: 'commitlint-github-action', + }, + } + + const eventPath = path.join(cwd, 'mergeGroupEventPayload.json') + + updateEnvVars({ + GITHUB_EVENT_PATH: eventPath, + GITHUB_EVENT_NAME: 'merge_group', + GITHUB_REPOSITORY: 'wagoid/commitlint-github-action', + }) + await writeFile(eventPath, JSON.stringify(payload), 'utf8') +} + +export const updateMergeGroupEnvVars = (cwd) => { + updateEnvVars({ + GITHUB_WORKSPACE: cwd, + GITHUB_EVENT_NAME: 'merge_group', + }) +}