Skip to content

Commit

Permalink
fix: stop trying to issue commands on failures
Browse files Browse the repository at this point in the history
Commands are disabled, so we should not use `core.setFailed` on errors anymore.

Closes #70
  • Loading branch information
wagoid committed Jan 28, 2021
1 parent 15aa3a7 commit 1128358
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
8 changes: 6 additions & 2 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ const hasOnlyWarnings = lintedCommits =>
lintedCommits.some(({ lintResult }) => lintResult.warnings.length)

const setFailed = formattedResults => {
core.setFailed(`You have commit messages with errors\n\n${formattedResults}`)
process.exitCode = 1

console.error(`You have commit messages with errors\n\n${formattedResults}`)
}

const handleOnlyWarnings = formattedResults => {
Expand Down Expand Up @@ -153,7 +155,9 @@ const showLintResults = async ([from, to]) => {
}

const exitWithMessage = message => error => {
core.setFailed(`${message}\n${error.message}\n${error.stack}`)
process.exitCode = 1

console.error(`${message}\n${error.message}\n${error.stack}`)
}

const commitLinterAction = () =>
Expand Down
59 changes: 29 additions & 30 deletions src/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ describe('Commit Linter action', () => {
beforeEach(() => {
core = require('@actions/core')
td.replace(core, 'getInput')
td.replace(core, 'setFailed')
td.replace(core, 'setOutput')
td.replace(console, 'log')
td.replace(console, 'error')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
td.when(core.getInput('firstParent')).thenReturn('true')
td.when(core.getInput('failOnWarnings')).thenReturn('false')
Expand All @@ -71,7 +72,8 @@ describe('Commit Linter action', () => {

await runAction()

td.verify(core.setFailed(contains('You have commit messages with errors')))
td.verify(console.error(contains('You have commit messages with errors')))
expect(process.exitCode).toBe(1)
})

it('should fail for single push with incorrect message', async () => {
Expand All @@ -84,7 +86,7 @@ describe('Commit Linter action', () => {

await runAction()

td.verify(core.setFailed(contains('You have commit messages with errors')))
td.verify(console.error(contains('You have commit messages with errors')))
})

it('should fail for push range with wrong messages', async () => {
Expand All @@ -99,8 +101,8 @@ describe('Commit Linter action', () => {

await runAction()

td.verify(core.setFailed(contains('wrong message 1')))
td.verify(core.setFailed(contains('wrong message 2')))
td.verify(console.error(contains('wrong message 1')))
td.verify(console.error(contains('wrong message 2')))
})

it('should pass for push range with correct messages', async () => {
Expand All @@ -116,7 +118,7 @@ describe('Commit Linter action', () => {

await runAction()

td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.error(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log('Lint free! 🎉'))
})

Expand All @@ -138,8 +140,8 @@ describe('Commit Linter action', () => {
'Commit was forced, checking only the latest commit from push instead of a range of commit messages',
),
)
td.verify(core.setFailed(contains('wrong message 1')), { times: 0 })
td.verify(core.setFailed(contains('wrong message 2')))
td.verify(console.error(contains('wrong message 1')), { times: 0 })
td.verify(console.error(contains('wrong message 2')))
})

it('should lint only last commit when "before" field is an empty sha', async () => {
Expand All @@ -155,8 +157,8 @@ describe('Commit Linter action', () => {

await runAction()

td.verify(core.setFailed(contains('wrong message 1')), { times: 0 })
td.verify(core.setFailed(contains('chore(WRONG): message 2')))
td.verify(console.error(contains('wrong message 1')), { times: 0 })
td.verify(console.error(contains('chore(WRONG): message 2')))
})

it('should fail for commit with scope that is not a lerna package', async () => {
Expand All @@ -171,7 +173,7 @@ describe('Commit Linter action', () => {
await runAction()

td.verify(
core.setFailed(contains('chore(wrong): not including package scope')),
console.error(contains('chore(wrong): not including package scope')),
)
})

Expand Down Expand Up @@ -201,23 +203,21 @@ describe('Commit Linter action', () => {

await runAction()

td.verify(console.error(contains('ib-21212121212121: without jira ticket')))
td.verify(
core.setFailed(contains('ib-21212121212121: without jira ticket')),
)
td.verify(
core.setFailed(
console.error(
contains(
'ib-21212121212121 taskId must not be loonger than 9 characters',
),
),
)
td.verify(
core.setFailed(
console.error(
contains('ib-21212121212121 taskId must be uppercase case'),
),
)
td.verify(
core.setFailed(
console.error(
contains('ib-21212121212121 commitStatus must be uppercase case'),
),
)
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('Commit Linter action', () => {

await runAction()

td.verify(core.setFailed(contains('wrong commit from another branch')))
td.verify(console.error(contains('wrong commit from another branch')))
})

describe('when there are multiple commits failing in the pull request', () => {
Expand Down Expand Up @@ -307,21 +307,21 @@ 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(console.error(contains('message from before push')), {
times: 0,
})
})

it('should show errors for the first wrong message', async () => {
await runAction()

td.verify(core.setFailed(contains(firstMessage)))
td.verify(console.error(contains(firstMessage)))
})

it('should show errors for the second wrong message', async () => {
await runAction()

td.verify(core.setFailed(contains(secondMessage)))
td.verify(console.error(contains(secondMessage)))
})

it('should generate a JSON output of the errors', async () => {
Expand Down Expand Up @@ -352,8 +352,9 @@ describe('Commit Linter action', () => {
it('should show an error message', async () => {
await runAction()

expect(process.exitCode).toBe(1)
td.verify(
core.setFailed(
console.error(
contains("error trying to get list of pull request's commits"),
),
)
Expand All @@ -362,7 +363,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(console.error(contains('HttpError: Bad credentials')))
})
})

Expand All @@ -383,7 +384,7 @@ describe('Commit Linter action', () => {
it('should pass', async () => {
await runAction()

td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.error(), { times: 0, ignoreExtraArgs: true })
})

it('should show success message', async () => {
Expand Down Expand Up @@ -448,7 +449,7 @@ 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(console.error(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log(contains('You have commit messages with warnings')))
})

Expand All @@ -467,7 +468,7 @@ describe('Commit Linter action', () => {
await runAction()

td.verify(
core.setFailed(contains('You have commit messages with errors')),
console.error(contains('You have commit messages with errors')),
)
})

Expand Down Expand Up @@ -503,9 +504,7 @@ describe('Commit Linter action', () => {
it('should fail', async () => {
await runAction()

td.verify(
core.setFailed(contains('You have commit messages with errors')),
)
td.verify(console.error(contains('You have commit messages with errors')))
})

it('should show the results in an output', async () => {
Expand Down Expand Up @@ -541,7 +540,7 @@ describe('Commit Linter action', () => {
await runAction()

td.verify(
core.setFailed(contains('You have commit messages with errors')),
console.error(contains('You have commit messages with errors')),
)
})
})
Expand Down

0 comments on commit 1128358

Please sign in to comment.