Skip to content

Commit

Permalink
feat: upgrade to commitlint v19
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `commitlint.config.js` is not supported anymore, please use `.mjs` extension
  • Loading branch information
wagoid committed Mar 28, 2024
1 parent d2194f4 commit 3be1218
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules
commitlint.config.js
commitlint.config.mjs
action.yml
.github
CHANGELOG.md
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ jobs:
# 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
# bring back `"type": "module"`
sed -i 's/"private": true,/"private": true,\n "type": "module",/' package.json
git commit --amend --no-edit
- name: Set VERSION env var
run: |
version=`node -p "require('./package.json').version"`
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: wagoid/commitlint-github-action@v5
- uses: wagoid/commitlint-github-action@v6
```

Alternatively, you can run on other event types such as `on: [push]`. In that case the action will lint the push event's commit(s) instead of linting commits from a pull request. You can also combine `push` and `pull_request` together in the same workflow.

## Inputs

You can supply these inputs to the `wagoid/commitlint-github-action@v5` step.
You can supply these inputs to the `wagoid/commitlint-github-action@v6` step.

### `configFile`

The path to your commitlint config file.

Default: `commitlint.config.js`
Default: `commitlint.config.mjs`

If the config file doesn't exist, [config-conventional](https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional) settings will be loaded as a default fallback.

Details on the configuration file can be found on [the commitlint website](https://commitlint.js.org/#/reference-configuration).

Note: `commitlint.config.js` doesn't work with this action. If you use a JS config file, it's required to be an ES Module (`.mjs` extension)

### `failOnWarnings`

Whether you want to fail on warnings or not.
Expand Down Expand Up @@ -149,7 +151,7 @@ jobs:
- run: npm install
# Run the commitlint action, considering its own dependencies and yours as well 🚀
# `github.workspace` is the path to your repository.
- uses: wagoid/commitlint-github-action@v5
- uses: wagoid/commitlint-github-action@v6
env:
NODE_PATH: ${{ github.workspace }}/node_modules
```
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inputs:
configFile:
description: Commitlint config file. If the file doesn't exist, config-conventional settings will be
loaded as a fallback.
default: ./commitlint.config.js
default: ./commitlint.config.mjs
required: false
failOnWarnings:
description: Whether you want to fail on warnings or not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
export default {
extends: ['@commitlint/config-conventional'],
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
extends: ['@commitlint/config-conventional'],
helpUrl: 'https://example.org',
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
plugins: ['commitlint-plugin-jira-rules'],
extends: ['jira'],
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
extends: ['@commitlint/config-conventional'],
rules: {
'signed-off-by': [2, 'always', 'Signed-off-by:'],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "5.5.1",
"description": "commitlint github action",
"private": true,
"module": "./dist/run.mjs",
"type": "module",
"module": "./dist/run.mjs",
"scripts": {
"postinstall": "husky install",
"test": "NODE_PATH=./node_modules NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest",
Expand Down
7 changes: 7 additions & 0 deletions src/action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'] })
Expand Down
33 changes: 27 additions & 6 deletions src/action.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Commit Linter action', () => {

beforeEach(async () => {
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.js',
'./commitlint.config.mjs',
)
td.when(mockCore.getInput('failOnWarnings')).thenReturn('false')
td.when(mockCore.getInput('helpURL')).thenReturn(
Expand All @@ -69,7 +69,7 @@ describe('Commit Linter action', () => {

it('should use default config when config file does not exist', async () => {
td.when(mockCore.getInput('configFile')).thenReturn(
'./not-existing-config.js',
'./not-existing-config.mjs',
)
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {
Expand Down Expand Up @@ -97,6 +97,27 @@ describe('Commit Linter action', () => {
)
})

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: [
{
id: 'wrong-message',
message: 'wrong message',
},
],
})
updatePushEnvVars(cwd)
td.replace(process, 'cwd', () => cwd)

await runAction()

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, {
Expand Down Expand Up @@ -262,7 +283,7 @@ describe('Commit Linter action', () => {
it("should fail for commit that doesn't comply with jira rules", async () => {
cwd = await git.bootstrap('fixtures/jira', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.js',
'./commitlint.config.mjs',
)
await createPushEventPayload(cwd, {
commits: [
Expand Down Expand Up @@ -301,7 +322,7 @@ describe('Commit Linter action', () => {

it('should pass when commits are not available', async () => {
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.js',
'./commitlint.config.mjs',
)
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
await createPushEventPayload(cwd, {})
Expand All @@ -328,7 +349,7 @@ describe('Commit Linter action', () => {
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.js',
'./commitlint.config.mjs',
)
await createPullRequestEventPayload(cwd)
updatePullRequestEnvVars(cwd, { eventName })
Expand Down Expand Up @@ -394,7 +415,7 @@ describe('Commit Linter action', () => {
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional', process.cwd())
td.when(mockCore.getInput('configFile')).thenReturn(
'./commitlint.config.js',
'./commitlint.config.mjs',
)
await createPullRequestEventPayload(cwd)
updatePullRequestEnvVars(cwd)
Expand Down

0 comments on commit 3be1218

Please sign in to comment.