5
0
Fork 0
mirror of https://github.com/wagoid/commitlint-github-action.git synced 2025-11-07 08:06:54 +00:00

fix(action): fix bug with value, add tests

This commit is contained in:
Cooper 2023-04-05 11:23:02 -04:00
parent 83467da07b
commit 2640e4c8bf
No known key found for this signature in database
GPG key ID: DF6ECEF0888ECA4B
2 changed files with 45 additions and 7 deletions

View file

@ -1,6 +1,6 @@
import { existsSync } from 'fs'
import { resolve } from 'path'
import { getInput, setFailed } from '@actions/core'
import { getInput, setFailed, setOutput } from '@actions/core'
import { context as eventContext, getOctokit } from '@actions/github'
import lint from '@commitlint/lint'
import { format } from '@commitlint/format'
@ -16,7 +16,7 @@ const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
const failOnErrors = resolve(process.env.GITHUB_WORKSPACE, getInput('failOnErrors', { required: false }))
const failOnErrors = getInput('failOnErrors')
const getCommitDepth = () => {
const commitDepthString = getInput('commitDepth')
@ -147,12 +147,12 @@ const showLintResults = async ([from, to]) => {
if (hasOnlyWarnings(lintedCommits)) {
handleOnlyWarnings(formattedResults)
} else if (formattedResults && (failOnErrors == false)) {
} else if (formattedResults && failOnErrors === 'false') {
// https://github.com/actions/toolkit/tree/master/packages/core#exit-codes
// this would be a good place to implement the setNeutral() when it's eventually implimented.
// for now it can pass with a check mark.
console.log('Passing despite errors ✅')
console.log(`You have commit messages with errors\n\n${formattedResults}`)
setOutput(`You have commit messages with errors\n\n${formattedResults}`)
} else if (formattedResults) {
setFailedAction(formattedResults)
} else {
@ -164,7 +164,6 @@ const exitWithMessage = (message) => (error) => {
setFailedAction(`${message}\n${error.message}\n${error.stack}`)
}
const commitLinterAction = () =>
getRangeForEvent()
.catch(

View file

@ -110,10 +110,49 @@ describe('Commit Linter action', () => {
await runAction()
console.log()
td.verify(core.setFailed(contains('wrong message 1')))
td.verify(core.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')
await gitEmptyCommit(cwd, 'message from before push')
await gitEmptyCommit(cwd, 'wrong message 1')
await gitEmptyCommit(cwd, 'wrong message 2')
const [before, , to] = await getCommitHashes(cwd)
await createPushEventPayload(cwd, { before, to })
updatePushEnvVars(cwd, to)
td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log')
await runAction()
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log(contains('Passing despite errors ✅')))
td.verify(core.setOutput(contains('wrong message 1')))
td.verify(core.setOutput(contains('wrong message 2')))
})
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')
await gitEmptyCommit(cwd, 'message from before push')
await gitEmptyCommit(cwd, 'chore: correct message 1')
await gitEmptyCommit(cwd, 'chore: correct message 2')
const [before, , to] = await getCommitHashes(cwd)
await createPushEventPayload(cwd, { before, to })
updatePushEnvVars(cwd, to)
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! 🎉'))
})
it('should pass for push range with correct messages', async () => {
cwd = await git.bootstrap('fixtures/conventional')
await gitEmptyCommit(cwd, 'message from before push')