11
0
Fork 0
mirror of https://github.com/wagoid/commitlint-github-action.git synced 2026-03-31 14:56:58 +00:00

feat: upgrade to commitlint v19

BREAKING CHANGE: `commitlint.config.js` is not supported anymore, please use `.mjs` extension
This commit is contained in:
Wagner Santos 2024-03-28 11:38:50 -03:00
parent 9763196e10
commit 732f0ad7d5
24 changed files with 2431 additions and 13465 deletions

View file

@ -5,7 +5,7 @@ import { context as eventContext, getOctokit } from '@actions/github'
import lint from '@commitlint/lint'
import { format } from '@commitlint/format'
import load from '@commitlint/load'
import generateOutputs from './generateOutputs'
import generateOutputs from './generateOutputs.mjs'
const pullRequestEvent = 'pull_request'
const pullRequestTargetEvent = 'pull_request_target'
@ -102,6 +102,13 @@ const showLintResults = async (eventCommits) => {
if (commitDepth) {
commits = commits?.slice(0, commitDepth)
}
if (configPath?.endsWith('.js')) {
throw new Error(
'.js extension is not allowed for the `configFile`, please use .mjs instead',
)
}
const config = existsSync(configPath)
? await load({}, { file: configPath })
: await load({ extends: ['@commitlint/config-conventional'] })

View file

@ -1,14 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-env jest */
import { git } from '@commitlint/test'
import { describe } from '@jest/globals'
import td from 'testdouble'
import { jest, describe, it } from '@jest/globals'
import * as td from 'testdouble'
import {
updatePushEnvVars,
createPushEventPayload,
createPullRequestEventPayload,
updatePullRequestEnvVars,
buildResponseCommit,
} from './testUtils'
} from './testUtils.mjs'
const resultsOutputId = 'results'
@ -18,39 +19,44 @@ const {
const initialEnv = { ...process.env }
const listCommits = td.func('listCommits')
const mockListCommits = td.func('listCommits')
const runAction = async () => {
const github = await import('@actions/github')
const mockCore = td.object(['getInput', 'setFailed', 'setOutput'])
jest.unstable_mockModule('@actions/core', () => mockCore)
jest.unstable_mockModule('@actions/github', () => {
class MockOctokit {
constructor() {
this.rest = {
pulls: {
listCommits,
listCommits: mockListCommits,
},
}
}
}
td.replace(github, 'getOctokit', () => new MockOctokit())
return {
...jest.requireActual('@actions/github'),
getOctokit: () => new MockOctokit(),
}
})
const action = (await import('./action')).default
const runAction = async () => {
const action = (await import('./action.mjs')).default
return action()
}
describe('Commit Linter action', () => {
let core
let cwd
beforeEach(async () => {
core = await import('@actions/core')
td.replace(core, 'getInput')
td.replace(core, 'setFailed')
td.replace(core, 'setOutput')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
td.when(core.getInput('failOnWarnings')).thenReturn('false')
td.when(core.getInput('helpURL')).thenReturn(
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.mjs',
)
td.when(mockCore.getInput('failOnWarnings')).thenReturn('false')
td.when(mockCore.getInput('helpURL')).thenReturn(
'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
)
})
@ -62,8 +68,10 @@ describe('Commit Linter action', () => {
})
it('should use default config when config file does not exist', async () => {
td.when(core.getInput('configFile')).thenReturn('./not-existing-config.js')
cwd = await git.bootstrap('fixtures/conventional')
td.when(mockCore.getInput('configFile')).thenReturn(
'./not-existing-config.mjs',
)
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -77,9 +85,11 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setFailed(contains('You have commit messages with errors')))
td.verify(
core.setFailed(
mockCore.setFailed(contains('You have commit messages with errors')),
)
td.verify(
mockCore.setFailed(
contains(
'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
),
@ -87,8 +97,11 @@ describe('Commit Linter action', () => {
)
})
it('should fail for single push with incorrect message', async () => {
cwd = await git.bootstrap('fixtures/conventional')
it('should fail when using js extension', async () => {
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.js',
)
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -102,11 +115,31 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setFailed(contains('You have commit messages with errors')))
td.verify(mockCore.setFailed(contains('.js extension is not allowed')))
})
it('should fail for single push with incorrect message', async () => {
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
id: 'wrong-message',
message: 'wrong message',
},
],
})
updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd)
await runAction()
td.verify(
mockCore.setFailed(contains('You have commit messages with errors')),
)
})
it('should fail for push range with wrong messages', async () => {
cwd = await git.bootstrap('fixtures/conventional')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -123,13 +156,13 @@ describe('Commit Linter action', () => {
td.replace(process, 'cwd', () => cwd)
await runAction()
td.verify(core.setFailed(contains('wrong message 1')))
td.verify(core.setFailed(contains('wrong message 2')))
td.verify(mockCore.setFailed(contains('wrong message 1')))
td.verify(mockCore.setFailed(contains('wrong message 2')))
})
it('should pass for push range with wrong messages with failOnErrors set to false', async () => {
td.when(core.getInput('failOnErrors')).thenReturn('false')
cwd = await git.bootstrap('fixtures/conventional')
td.when(mockCore.getInput('failOnErrors')).thenReturn('false')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -148,15 +181,15 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log(contains('wrong message 1')))
td.verify(console.log(contains('wrong message 2')))
td.verify(console.log(contains('Passing despite errors ✅')))
})
it('should pass for push range with correct messages with failOnErrors set to false', async () => {
td.when(core.getInput('failOnErrors')).thenReturn('false')
cwd = await git.bootstrap('fixtures/conventional')
td.when(mockCore.getInput('failOnErrors')).thenReturn('false')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -175,12 +208,12 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log('Lint free! 🎉'))
})
it('should pass for push range with correct messages', async () => {
cwd = await git.bootstrap('fixtures/conventional')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -199,13 +232,15 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log('Lint free! 🎉'))
})
it('should fail for commit with scope that is not a lerna package', async () => {
cwd = await git.bootstrap('fixtures/lerna-scopes')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.yml')
cwd = await git.bootstrap('fixtures/lerna-scopes', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.yml',
)
await createPushEventPayload(cwd, {
commits: [
{
@ -219,13 +254,15 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(
core.setFailed(contains('chore(wrong): not including package scope')),
mockCore.setFailed(contains('chore(wrong): not including package scope')),
)
})
it('should pass for scope that is a lerna package', async () => {
cwd = await git.bootstrap('fixtures/lerna-scopes')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.yml')
cwd = await git.bootstrap('fixtures/lerna-scopes', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.yml',
)
await createPushEventPayload(cwd, {
commits: [
{
@ -244,8 +281,10 @@ describe('Commit Linter action', () => {
})
it("should fail for commit that doesn't comply with jira rules", async () => {
cwd = await git.bootstrap('fixtures/jira')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
cwd = await git.bootstrap('fixtures/jira', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.mjs',
)
await createPushEventPayload(cwd, {
commits: [
{
@ -260,30 +299,32 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(
core.setFailed(contains('ib-21212121212121: without jira ticket')),
mockCore.setFailed(contains('ib-21212121212121: without jira ticket')),
)
td.verify(
core.setFailed(
mockCore.setFailed(
contains(
'ib-21212121212121 taskId must not be longer than 9 characters',
),
),
)
td.verify(
core.setFailed(
mockCore.setFailed(
contains('ib-21212121212121 taskId must be uppercase case'),
),
)
td.verify(
core.setFailed(
mockCore.setFailed(
contains('ib-21212121212121 commitStatus must be uppercase case'),
),
)
})
it('should pass when commits are not available', async () => {
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
cwd = await git.bootstrap('fixtures/conventional')
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.mjs',
)
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {})
updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd)
@ -291,7 +332,7 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log('Lint free! 🎉'))
})
@ -306,14 +347,14 @@ describe('Commit Linter action', () => {
)
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
td.when(core.getInput('configFile')).thenReturn(
'./commitlint.config.js',
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.mjs',
)
await createPullRequestEventPayload(cwd)
updatePullRequestEnvVars(cwd, { eventName })
td.when(
listCommits({
mockListCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
pull_number: '1',
@ -345,7 +386,7 @@ describe('Commit Linter action', () => {
it('should NOT show errors for a message from before the push', async () => {
await runAction()
td.verify(core.setFailed(contains('message from before push')), {
td.verify(mockCore.setFailed(contains('message from before push')), {
times: 0,
})
})
@ -353,31 +394,33 @@ describe('Commit Linter action', () => {
it('should show errors for the first wrong message', async () => {
await runAction()
td.verify(core.setFailed(contains(firstCommit.commit.message)))
td.verify(mockCore.setFailed(contains(firstCommit.commit.message)))
})
it('should show errors for the second wrong message', async () => {
await runAction()
td.verify(core.setFailed(contains(secondCommit.commit.message)))
td.verify(mockCore.setFailed(contains(secondCommit.commit.message)))
})
it('should generate a JSON output of the errors', async () => {
await runAction()
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
td.verify(mockCore.setOutput(resultsOutputId, expectedResultsOutput))
})
},
)
describe('when it fails to fetch commits', () => {
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.mjs',
)
await createPullRequestEventPayload(cwd)
updatePullRequestEnvVars(cwd)
td.when(
listCommits({
mockListCommits({
owner: 'wagoid',
repo: 'commitlint-github-action',
pull_number: '1',
@ -391,7 +434,7 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(
core.setFailed(
mockCore.setFailed(
contains("error trying to get list of pull request's commits"),
),
)
@ -400,7 +443,7 @@ describe('Commit Linter action', () => {
it('should show the original error message', async () => {
await runAction()
td.verify(core.setFailed(contains('HttpError: Bad credentials')))
td.verify(mockCore.setFailed(contains('HttpError: Bad credentials')))
})
})
@ -411,7 +454,7 @@ describe('Commit Linter action', () => {
}
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, { commits: [commit] })
updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd)
@ -421,7 +464,7 @@ describe('Commit Linter action', () => {
it('should pass', async () => {
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
})
it('should show success message', async () => {
@ -443,7 +486,7 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
td.verify(mockCore.setOutput(resultsOutputId, expectedResultsOutput))
})
})
@ -460,7 +503,7 @@ describe('Commit Linter action', () => {
message:
'chore: correct message\nsome context without leading blank line',
}
cwd = await git.bootstrap('fixtures/conventional')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [commitWithWarning, correctCommit],
})
@ -490,33 +533,33 @@ describe('Commit Linter action', () => {
it('should pass and show that warnings exist', async () => {
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log(contains('You have commit messages with warnings')))
})
it('should show the results in an output', async () => {
await runAction()
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
td.verify(mockCore.setOutput(resultsOutputId, expectedResultsOutput))
})
describe('and failOnWarnings is set to true', () => {
beforeEach(() => {
td.when(core.getInput('failOnWarnings')).thenReturn('true')
td.when(mockCore.getInput('failOnWarnings')).thenReturn('true')
})
it('should fail', async () => {
await runAction()
td.verify(
core.setFailed(contains('You have commit messages with errors')),
mockCore.setFailed(contains('You have commit messages with errors')),
)
})
it('should show the results in an output', async () => {
await runAction()
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
td.verify(mockCore.setOutput(resultsOutputId, expectedResultsOutput))
})
})
})
@ -533,7 +576,7 @@ describe('Commit Linter action', () => {
}
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [wrongCommit, commitWithWarning],
})
@ -546,7 +589,7 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(
core.setFailed(contains('You have commit messages with errors')),
mockCore.setFailed(contains('You have commit messages with errors')),
)
})
@ -571,19 +614,19 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(core.setOutput(resultsOutputId, expectedResultsOutput))
td.verify(mockCore.setOutput(resultsOutputId, expectedResultsOutput))
})
describe('and failOnWarnings is set to true', () => {
beforeEach(() => {
td.when(core.getInput('failOnWarnings')).thenReturn('true')
td.when(mockCore.getInput('failOnWarnings')).thenReturn('true')
})
it('should fail', async () => {
await runAction()
td.verify(
core.setFailed(contains('You have commit messages with errors')),
mockCore.setFailed(contains('You have commit messages with errors')),
)
})
})
@ -591,7 +634,7 @@ describe('Commit Linter action', () => {
describe('when commit contains required signed-off-by message', () => {
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/signed-off-by')
cwd = await git.bootstrap('fixtures/signed-off-by', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -609,14 +652,14 @@ describe('Commit Linter action', () => {
it('should pass', async () => {
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log('Lint free! 🎉'))
})
})
describe('when a different helpUrl is provided in the config', () => {
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/custom-help-url')
cwd = await git.bootstrap('fixtures/custom-help-url', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{
@ -634,9 +677,9 @@ describe('Commit Linter action', () => {
await runAction()
td.verify(
core.setFailed(contains('You have commit messages with errors')),
mockCore.setFailed(contains('You have commit messages with errors')),
)
td.verify(core.setFailed(contains(' https://example.org')))
td.verify(mockCore.setFailed(contains(' https://example.org')))
})
})
@ -647,7 +690,7 @@ describe('Commit Linter action', () => {
}
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
commits: [
{ id: 'correct-commit', message: 'chore: correct message 2' },
@ -660,25 +703,25 @@ describe('Commit Linter action', () => {
})
it('should pass when only considering messages defined by commitDepth', async () => {
td.when(core.getInput('commitDepth')).thenReturn('1')
td.when(mockCore.getInput('commitDepth')).thenReturn('1')
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(mockCore.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log('Lint free! 🎉'))
})
it('should fail when older commits have lint errors', async () => {
td.when(core.getInput('commitDepth')).thenReturn('2')
td.when(mockCore.getInput('commitDepth')).thenReturn('2')
await runAction()
td.verify(core.setFailed(contains(incorrectCommit.message)))
td.verify(mockCore.setFailed(contains(incorrectCommit.message)))
})
it('should consider all commits when an invalid commit depth is passed in config', async () => {
td.when(core.getInput('commitDepth')).thenReturn('xzy')
td.when(mockCore.getInput('commitDepth')).thenReturn('xzy')
await runAction()
td.verify(core.setFailed(contains(incorrectCommit.message)))
td.verify(mockCore.setFailed(contains(incorrectCommit.message)))
})
})
})