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

fix: rollback update of commitlint to support configs that use commonjs

This commit is contained in:
Wagner Santos 2024-03-28 16:29:46 -03:00
parent e15adf3661
commit 06143b235b
17 changed files with 3576 additions and 5435 deletions

View file

@ -19,10 +19,5 @@
"node/no-unpublished-require": "off", "node/no-unpublished-require": "off",
"node/no-unpublished-import": "off", "node/no-unpublished-import": "off",
"node/no-unsupported-features/es-syntax": "off" "node/no-unsupported-features/es-syntax": "off"
}, }
"ignorePatterns": [
".github/tasks/actionYamlUpdater.js",
"rollup.config.js",
"fixtures"
]
} }

View file

@ -3,10 +3,10 @@ const yaml = require('yaml')
const versionRegex = /\d+\.\d+\.\d+/ const versionRegex = /\d+\.\d+\.\d+/
exports.readVersion = (contents) => module.exports.readVersion = (contents) =>
yaml.parse(contents).runs.image.match(versionRegex)[0] yaml.parse(contents).runs.image.match(versionRegex)[0]
exports.writeVersion = (contents, version) => { module.exports.writeVersion = (contents, version) => {
const actionFile = yaml.parse(contents) const actionFile = yaml.parse(contents)
actionFile.runs.image = actionFile.runs.image.replace(versionRegex, version) actionFile.runs.image = actionFile.runs.image.replace(versionRegex, version)

View file

@ -53,11 +53,7 @@ jobs:
git config user.email 'github-action@users.noreply.github.com' git config user.email 'github-action@users.noreply.github.com'
git config user.name 'GitHub Action' git config user.name 'GitHub Action'
- name: Update versions and changelog - name: Update versions and changelog
run: | run: npx commit-and-tag-version
# remove `"type": "module"` from package.json since `commit-and-tag-version` doesn't support it
sed -i '/"type": "module",/c\' package.json
npx commit-and-tag-version
git checkout package.json
- name: Set VERSION env var - name: Set VERSION env var
run: | run: |
version=`node -p "require('./package.json').version"` version=`node -p "require('./package.json').version"`

View file

@ -1,4 +1,4 @@
export default { module.exports = {
'*.{ts,tsx,vue,css,less,scss,html,htm,md,markdown}': 'prettier --write', '*.{ts,tsx,vue,css,less,scss,html,htm,md,markdown}': 'prettier --write',
'*.{json,yml,yaml}': ['prettier --write', () => 'npm run test'], '*.{json,yml,yaml}': ['prettier --write', () => 'npm run test'],
'*.{js,jsx}': ['eslint --fix', () => 'npm run test'], '*.{js,jsx}': ['eslint --fix', () => 'npm run test'],

View file

@ -1,4 +1,4 @@
FROM node:20.9.0-alpine3.17 as build FROM node:16.14.2-alpine3.14 as build
COPY package*.json / COPY package*.json /
@ -12,7 +12,7 @@ FROM node:20.9.0-alpine3.17
RUN apk --no-cache add git RUN apk --no-cache add git
COPY --from=build dist/run.mjs /run.mjs COPY --from=build dist/run.js /run.js
COPY package*.json / COPY package*.json /

View file

@ -1,5 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable import/no-extraneous-dependencies */
import { maxLineLength } from '@commitlint/ensure' const { maxLineLength } = require('@commitlint/ensure')
const bodyMaxLineLength = 100 const bodyMaxLineLength = 100
@ -14,7 +14,7 @@ const validateBodyMaxLengthIgnoringDeps = (parsedCommit) => {
] ]
} }
export default { module.exports = {
extends: ['@commitlint/config-conventional'], extends: ['@commitlint/config-conventional'],
plugins: ['commitlint-plugin-function-rules'], plugins: ['commitlint-plugin-function-rules'],
rules: { rules: {

View file

@ -5,13 +5,11 @@ set -e
if [ -z "$NODE_PATH" ]; then if [ -z "$NODE_PATH" ]; then
export NODE_PATH=/node_modules export NODE_PATH=/node_modules
else else
export NODE_PATH="$NODE_PATH":/node_modules export NODE_PATH=$NODE_PATH:/node_modules
fi fi
# Since actions/checkout can be setup with a different user ID, we need to set the workspace as safe inside this action # Since actions/checkout can be setup with a different user ID, we need to set the workspace as safe inside this action
# Info about the vunlerability: https://github.blog/2022-04-12-git-security-vulnerability-announced/ # Info about the vunlerability: https://github.blog/2022-04-12-git-security-vulnerability-announced/
git config --global --add safe.directory "$GITHUB_WORKSPACE" git config --global --add safe.directory "$GITHUB_WORKSPACE"
export NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules" node /run.js
node /run.mjs

View file

@ -1,12 +1,10 @@
export default { module.exports = {
// Automatically clear mock calls and instances between every test // Automatically clear mock calls and instances between every test
// preset: 'rollup-jest',
clearMocks: true, clearMocks: true,
testEnvironment: '@commitlint/test-environment', testEnvironment: '@commitlint/test-environment',
testMatch: [
'**/__tests__/**/*.?(m)[jt]s?(x)',
'**/?(*.)+(spec|test).?(m)[tj]s?(x)',
],
transform: { transform: {
'\\.[jt]sx?$': 'babel-jest', '\\.[jt]sx?$': 'babel-jest',
}, },
transformIgnorePatterns: ['node_modules/(?!dargs)'],
} }

8711
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,18 +1,18 @@
{ {
"name": "commitlint-github-action", "name": "commitlint-github-action",
"version": "5.4.6", "version": "5.4.5",
"description": "commitlint github action", "description": "commitlint github action",
"private": true, "private": true,
"module": "./dist/run.mjs", "main": "./dist/run.js",
"scripts": { "scripts": {
"postinstall": "husky install", "postinstall": "husky install",
"test": "NODE_PATH=./node_modules NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest", "test": "NODE_PATH=./node_modules jest",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"lint": "eslint .", "lint": "eslint .",
"build": "rollup -c" "build": "rollup -c"
}, },
"engines": { "engines": {
"node": "20.9.0" "node": "^v16.14.2"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -24,16 +24,16 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/github": "^6.0.0", "@actions/github": "^6.0.0",
"@commitlint/config-angular": "^19.1.0", "@commitlint/config-angular": "^18.1.0",
"@commitlint/config-conventional": "^19.1.0", "@commitlint/config-conventional": "^18.1.0",
"@commitlint/config-lerna-scopes": "^19.0.0", "@commitlint/config-lerna-scopes": "^18.1.0",
"@commitlint/config-patternplate": "^19.1.0", "@commitlint/config-patternplate": "^18.1.0",
"@commitlint/ensure": "^19.0.3", "@commitlint/ensure": "^18.1.0",
"@commitlint/format": "^19.0.3", "@commitlint/format": "^18.1.0",
"@commitlint/lint": "^19.1.0", "@commitlint/lint": "^18.1.0",
"@commitlint/load": "^19.2.0", "@commitlint/load": "^18.2.0",
"commitlint-config-jira": "^1.6.4", "commitlint-config-jira": "^1.6.4",
"commitlint-plugin-function-rules": "^4.0.0", "commitlint-plugin-function-rules": "^2.0.2",
"commitlint-plugin-jira-rules": "^1.6.4", "commitlint-plugin-jira-rules": "^1.6.4",
"conventional-changelog-conventionalcommits": "^7.0.2", "conventional-changelog-conventionalcommits": "^7.0.2",
"conventional-changelog-lint-config-canonical": "^1.0.0", "conventional-changelog-lint-config-canonical": "^1.0.0",
@ -43,15 +43,15 @@
"devDependencies": { "devDependencies": {
"@babel/core": "^7.19.3", "@babel/core": "^7.19.3",
"@babel/preset-env": "^7.19.3", "@babel/preset-env": "^7.19.3",
"@commitlint/cli": "^19.2.1", "@commitlint/cli": "^18.2.0",
"@commitlint/test": "^9.0.1", "@commitlint/test": "^9.0.1",
"@commitlint/test-environment": "^9.0.1", "@commitlint/test-environment": "^9.0.1",
"@jest/globals": "^29.7.0", "@jest/globals": "^29.4.2",
"@rollup/plugin-babel": "^5.3.1", "@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.2", "@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^14.1.0", "@rollup/plugin-node-resolve": "^14.1.0",
"babel-jest": "^29.7.0", "babel-jest": "^29.4.3",
"commit-and-tag-version": "^12.2.0", "commit-and-tag-version": "^12.0.0",
"conventional-changelog-cli": "^4.1.0", "conventional-changelog-cli": "^4.1.0",
"eslint": "^8.34.0", "eslint": "^8.34.0",
"eslint-config-airbnb-base": "^15.0.0", "eslint-config-airbnb-base": "^15.0.0",
@ -61,11 +61,14 @@
"eslint-plugin-node": "^11.1.0", "eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1", "eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.3", "husky": "^8.0.3",
"jest": "^29.7.0", "jest": "^29.4.3",
"lint-staged": "^13.1.1", "lint-staged": "^13.1.1",
"prettier": "^2.8.4", "prettier": "^2.8.4",
"rollup": "^2.79.1", "rollup": "^2.79.1",
"testdouble": "^3.20.2", "testdouble": "^3.16.8",
"yaml": "^2.1.3" "yaml": "^2.1.3"
},
"overrides": {
"@commitlint/lint": "^18.1.0"
} }
} }

View file

@ -5,10 +5,10 @@ import { babel } from '@rollup/plugin-babel'
import pkg from './package.json' import pkg from './package.json'
export default { export default {
input: 'run.mjs', input: 'run.js',
external: (depName) => external: (depName) =>
depName.includes('node_modules') && !depName.includes('node_modules/dargs'), depName.includes('node_modules') && !depName.includes('node_modules/dargs'),
output: [{ file: pkg.module, format: 'es' }], output: [{ file: pkg.main, format: 'cjs' }],
plugins: [ plugins: [
babel({ babel({
babelHelpers: 'bundled', babelHelpers: 'bundled',

3
run.js Normal file
View file

@ -0,0 +1,3 @@
import action from './src/action'
action()

View file

@ -1,3 +0,0 @@
import action from './src/action.mjs'
action()

View file

@ -5,7 +5,7 @@ import { context as eventContext, getOctokit } from '@actions/github'
import lint from '@commitlint/lint' import lint from '@commitlint/lint'
import { format } from '@commitlint/format' import { format } from '@commitlint/format'
import load from '@commitlint/load' import load from '@commitlint/load'
import generateOutputs from './generateOutputs.mjs' import generateOutputs from './generateOutputs'
const pullRequestEvent = 'pull_request' const pullRequestEvent = 'pull_request'
const pullRequestTargetEvent = 'pull_request_target' const pullRequestTargetEvent = 'pull_request_target'

View file

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