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

fix: build the action with rollup

On v4, we used the default nodejs resolution logic to allow ES modules in dependencies.
This created a breaking change of forcing users of this action
to use the .cjs extension instead of .js in config files.

With this fix, we now bundle the action with rollup to allow ES modules
in dependencies, while keeping the support for .js config files.

With this change, the default config file was returned back to .js instead of
.cjs.

Fixes #194
This commit is contained in:
wagoid 2021-07-17 08:00:12 -03:00
parent 0cb522abbc
commit 2e578124a5
23 changed files with 15924 additions and 93 deletions

View file

@ -1,5 +1,5 @@
node_modules node_modules
commitlint.config.cjs commitlint.config.js
action.yml action.yml
.github .github
CHANGELOG.md CHANGELOG.md

View file

@ -18,12 +18,6 @@
"no-process-exit": "off", "no-process-exit": "off",
"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"
"import/extensions": [
"error",
{
"js": "always"
}
]
} }
} }

3
.gitignore vendored
View file

@ -59,3 +59,6 @@ typings/
# next.js build output # next.js build output
.next .next
# Dist files
dist

2
.nvmrc
View file

@ -1 +1 @@
16 16.5.0

View file

@ -5,7 +5,7 @@
"package-lock.json", "package-lock.json",
{ {
"filename": "action.yml", "filename": "action.yml",
"updater": "./.github/tasks/actionYamlUpdater.cjs" "updater": "./.github/tasks/actionYamlUpdater.js"
} }
], ],
"releaseCommitMessageFormat": "chore(release): publish {{currentTag}} [skip-ci]" "releaseCommitMessageFormat": "chore(release): publish {{currentTag}} [skip-ci]"

View file

@ -18,7 +18,6 @@ All notable changes to this project will be documented in this file. See [standa
* Node.js version used on the action updated from 12 to * Node.js version used on the action updated from 12 to
16 16
* Config files now need to be renamed from .js to .cjs
### Features ### Features

View file

@ -1,11 +1,23 @@
FROM node:16-alpine3.13 FROM node:16.5.0-alpine3.14 as build
COPY package*.json /
RUN npm ci --ignore-scripts
COPY . .
RUN npm run build
FROM node:16.5.0-alpine3.14
RUN apk --no-cache add git RUN apk --no-cache add git
COPY --from=build dist/run.js /run.js
COPY package*.json / COPY package*.json /
RUN npm ci --production --ignore-scripts RUN npm ci --production --ignore-scripts
COPY . . COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"] ENTRYPOINT ["/entrypoint.sh"]

View file

@ -32,7 +32,7 @@ You can supply these inputs to the `wagoid/commitlint-github-action@v3` step.
The path to your commitlint config file. The path to your commitlint config file.
Default: `commitlint.config.cjs` Default: `commitlint.config.js`
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. 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.

View file

@ -3,19 +3,20 @@ description: Lints Pull Request commit messages with commitlint
author: Wagner Santos author: Wagner Santos
inputs: inputs:
configFile: configFile:
description: Commitlint config file. If the file doesn't exist, config-conventional settings will be description:
Commitlint config file. If the file doesn't exist, config-conventional settings will be
loaded as a fallback. loaded as a fallback.
default: ./commitlint.config.cjs default: ./commitlint.config.js
required: false required: false
firstParent: firstParent:
description: > description: >
When set to true, we follow only the first parent commit when seeing a merge commit. More info When set to true, we follow only the first parent commit when seeing a merge commit. More info
in git-log docs https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent in git-log docs https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent
default: "true" default: 'true'
required: false required: false
failOnWarnings: failOnWarnings:
description: Whether you want to fail on warnings or not description: Whether you want to fail on warnings or not
default: "false" default: 'false'
required: false required: false
helpURL: helpURL:
description: Link to a page explaining your commit message convention description: Link to a page explaining your commit message convention

6
babel.config.json Normal file
View file

@ -0,0 +1,6 @@
{
"presets": [["@babel/preset-env"]],
"targets": {
"node": "16.5.0"
}
}

View file

@ -1,6 +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',
transform: {}, transform: {
'\\.[jt]sx?$': 'babel-jest',
},
transformIgnorePatterns: ['node_modules/(?!dargs)'],
} }

15855
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -3,17 +3,17 @@
"version": "4.0.2", "version": "4.0.2",
"description": "commitlint github action", "description": "commitlint github action",
"private": true, "private": true,
"exports": "./run.js", "main": "./dist/run.js",
"scripts": { "scripts": {
"postinstall": "husky install", "postinstall": "husky install",
"test": "NODE_PATH=./node_modules node --experimental-vm-modules node_modules/.bin/jest", "test": "NODE_PATH=./node_modules jest",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"lint": "eslint ." "lint": "eslint .",
"build": "rollup -c"
}, },
"engines": { "engines": {
"node": ">=16.0.0" "node": "16.5.0"
}, },
"type": "module",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/wagoid/commitlint-github-action.git" "url": "git+https://github.com/wagoid/commitlint-github-action.git"
@ -39,11 +39,17 @@
"lerna": "^4.0.0" "lerna": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7",
"@commitlint/cli": "^12.1.4", "@commitlint/cli": "^12.1.4",
"@commitlint/ensure": "^12.1.4", "@commitlint/ensure": "^12.1.4",
"@commitlint/test": "^9.0.1", "@commitlint/test": "^9.0.1",
"@commitlint/test-environment": "^9.0.1", "@commitlint/test-environment": "^9.0.1",
"@jest/globals": "^27.0.6", "@jest/globals": "^27.0.6",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^19.0.1",
"@rollup/plugin-node-resolve": "^13.0.1",
"babel-jest": "^27.0.6",
"commitlint-plugin-function-rules": "^1.3.2", "commitlint-plugin-function-rules": "^1.3.2",
"conventional-changelog-cli": "^2.1.1", "conventional-changelog-cli": "^2.1.1",
"eslint": "^7.29.0", "eslint": "^7.29.0",
@ -57,6 +63,7 @@
"jest": "^27.0.6", "jest": "^27.0.6",
"lint-staged": "^11.0.1", "lint-staged": "^11.0.1",
"prettier": "^2.3.2", "prettier": "^2.3.2",
"rollup": "^2.53.2",
"standard-version": "^9.3.1", "standard-version": "^9.3.1",
"testdouble": "^3.16.1", "testdouble": "^3.16.1",
"yaml": "^1.10.2" "yaml": "^1.10.2"

23
rollup.config.js Normal file
View file

@ -0,0 +1,23 @@
import path from 'path'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import { babel } from '@rollup/plugin-babel'
import pkg from './package.json'
export default {
input: 'run.js',
external: (depName) =>
depName.includes('node_modules') && !depName.includes('node_modules/dargs'),
output: [{ file: pkg.main, format: 'cjs' }],
plugins: [
babel({
babelHelpers: 'bundled',
configFile: path.resolve(__dirname, 'babel.config.json'),
exclude: ['node_modules/(?!dargs)'],
}),
nodeResolve({
preferBuiltins: true,
}),
commonjs(),
],
}

2
run.js
View file

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

View file

@ -1,15 +1,12 @@
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { resolve } from 'path' import { resolve } from 'path'
import core from '@actions/core' import { getInput, setFailed } from '@actions/core'
import github from '@actions/github' import { context as eventContext, getOctokit } from '@actions/github'
import lintModule from '@commitlint/lint' import lint from '@commitlint/lint'
import { format } from '@commitlint/format' import { format } from '@commitlint/format'
import loadModule from '@commitlint/load' import load from '@commitlint/load'
import gitCommits from './gitCommits.js' import gitCommits from './gitCommits'
import generateOutputs from './generateOutputs.js' import generateOutputs from './generateOutputs'
const load = loadModule.default
const lint = lintModule.default
const pullRequestEvent = 'pull_request' const pullRequestEvent = 'pull_request'
const pullRequestTargetEvent = 'pull_request_target' const pullRequestTargetEvent = 'pull_request_target'
@ -17,12 +14,7 @@ const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent]
const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
const configPath = resolve( const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
process.env.GITHUB_WORKSPACE,
core.getInput('configFile'),
)
const { context: eventContext } = github
const pushEventHasOnlyOneCommit = (from) => { const pushEventHasOnlyOneCommit = (from) => {
const gitEmptySha = '0000000000000000000000000000000000000000' const gitEmptySha = '0000000000000000000000000000000000000000'
@ -53,7 +45,7 @@ const getRangeForEvent = async () => {
if (!pullRequestEvents.includes(GITHUB_EVENT_NAME)) if (!pullRequestEvents.includes(GITHUB_EVENT_NAME))
return getRangeForPushEvent() return getRangeForPushEvent()
const octokit = github.getOctokit(core.getInput('token')) const octokit = getOctokit(getInput('token'))
const { owner, repo, number } = eventContext.issue const { owner, repo, number } = eventContext.issue
const { data: commits } = await octokit.pulls.listCommits({ const { data: commits } = await octokit.pulls.listCommits({
owner, owner,
@ -75,7 +67,7 @@ function getHistoryCommits(from, to) {
to, to,
} }
if (core.getInput('firstParent') === 'true') { if (getInput('firstParent') === 'true') {
options.firstParent = true options.firstParent = true
} }
@ -104,7 +96,7 @@ const formatErrors = (lintedCommits) =>
{ results: lintedCommits.map((commit) => commit.lintResult) }, { results: lintedCommits.map((commit) => commit.lintResult) },
{ {
color: true, color: true,
helpUrl: core.getInput('helpURL'), helpUrl: getInput('helpURL'),
}, },
) )
@ -113,13 +105,13 @@ const hasOnlyWarnings = (lintedCommits) =>
lintedCommits.every(({ lintResult }) => lintResult.valid) && lintedCommits.every(({ lintResult }) => lintResult.valid) &&
lintedCommits.some(({ lintResult }) => lintResult.warnings.length) lintedCommits.some(({ lintResult }) => lintResult.warnings.length)
const setFailed = (formattedResults) => { const setFailedAction = (formattedResults) => {
core.setFailed(`You have commit messages with errors\n\n${formattedResults}`) setFailed(`You have commit messages with errors\n\n${formattedResults}`)
} }
const handleOnlyWarnings = (formattedResults) => { const handleOnlyWarnings = (formattedResults) => {
if (core.getInput('failOnWarnings') === 'true') { if (getInput('failOnWarnings') === 'true') {
setFailed(formattedResults) setFailedAction(formattedResults)
} else { } else {
console.log(`You have commit messages with warnings\n\n${formattedResults}`) console.log(`You have commit messages with warnings\n\n${formattedResults}`)
} }
@ -144,14 +136,14 @@ const showLintResults = async ([from, to]) => {
if (hasOnlyWarnings(lintedCommits)) { if (hasOnlyWarnings(lintedCommits)) {
handleOnlyWarnings(formattedResults) handleOnlyWarnings(formattedResults)
} else if (formattedResults) { } else if (formattedResults) {
setFailed(formattedResults) setFailedAction(formattedResults)
} else { } else {
console.log('Lint free! 🎉') console.log('Lint free! 🎉')
} }
} }
const exitWithMessage = (message) => (error) => { const exitWithMessage = (message) => (error) => {
core.setFailed(`${message}\n${error.message}\n${error.stack}`) setFailedAction(`${message}\n${error.message}\n${error.stack}`)
} }
const commitLinterAction = () => const commitLinterAction = () =>

View file

@ -1,5 +1,4 @@
/* eslint-env jest */ /* eslint-env jest */
import { jest } from '@jest/globals'
import { git } from '@commitlint/test' import { git } from '@commitlint/test'
import execa from 'execa' import execa from 'execa'
import td from 'testdouble' import td from 'testdouble'
@ -10,7 +9,7 @@ import {
createPushEventPayload, createPushEventPayload,
createPullRequestEventPayload, createPullRequestEventPayload,
updatePullRequestEnvVars, updatePullRequestEnvVars,
} from './testUtils.js' } from './testUtils'
const resultsOutputId = 'results' const resultsOutputId = 'results'
@ -23,7 +22,7 @@ const initialEnv = { ...process.env }
const listCommits = td.func('listCommits') const listCommits = td.func('listCommits')
const runAction = async () => { const runAction = async () => {
const github = (await import('@actions/github')).default const github = await import('@actions/github')
class MockOctokit { class MockOctokit {
constructor() { constructor() {
this.pulls = { this.pulls = {
@ -34,7 +33,7 @@ const runAction = async () => {
td.replace(github, 'getOctokit', () => new MockOctokit()) td.replace(github, 'getOctokit', () => new MockOctokit())
const action = (await import('./action.js')).default const action = (await import('./action')).default
return action() return action()
} }
@ -44,11 +43,11 @@ describe('Commit Linter action', () => {
let cwd let cwd
beforeEach(async () => { beforeEach(async () => {
core = (await import('@actions/core')).default core = await import('@actions/core')
td.replace(core, 'getInput') td.replace(core, 'getInput')
td.replace(core, 'setFailed') td.replace(core, 'setFailed')
td.replace(core, 'setOutput') td.replace(core, 'setOutput')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.cjs') td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
td.when(core.getInput('firstParent')).thenReturn('true') td.when(core.getInput('firstParent')).thenReturn('true')
td.when(core.getInput('failOnWarnings')).thenReturn('false') td.when(core.getInput('failOnWarnings')).thenReturn('false')
td.when(core.getInput('helpURL')).thenReturn( td.when(core.getInput('helpURL')).thenReturn(
@ -194,7 +193,7 @@ 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') cwd = await git.bootstrap('fixtures/jira')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.cjs') td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
await gitEmptyCommit(cwd, 'ib-21212121212121: without jira ticket') await gitEmptyCommit(cwd, 'ib-21212121212121: without jira ticket')
const [to] = await getCommitHashes(cwd) const [to] = await getCommitHashes(cwd)
await createPushEventPayload(cwd, { to }) await createPushEventPayload(cwd, { to })
@ -273,7 +272,7 @@ describe('Commit Linter action', () => {
beforeEach(async () => { beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional') cwd = await git.bootstrap('fixtures/conventional')
td.when(core.getInput('configFile')).thenReturn( td.when(core.getInput('configFile')).thenReturn(
'./commitlint.config.cjs', './commitlint.config.js',
) )
await gitEmptyCommit(cwd, 'message from before push') await gitEmptyCommit(cwd, 'message from before push')
await gitEmptyCommit(cwd, firstMessage) await gitEmptyCommit(cwd, firstMessage)
@ -341,7 +340,7 @@ describe('Commit Linter action', () => {
describe('when it fails to fetch commits', () => { describe('when it fails to fetch commits', () => {
beforeEach(async () => { beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional') cwd = await git.bootstrap('fixtures/conventional')
td.when(core.getInput('configFile')).thenReturn('./commitlint.config.cjs') td.when(core.getInput('configFile')).thenReturn('./commitlint.config.js')
await gitEmptyCommit(cwd, 'commit message') await gitEmptyCommit(cwd, 'commit message')
await createPullRequestEventPayload(cwd) await createPullRequestEventPayload(cwd)
const [to] = await getCommitHashes(cwd) const [to] = await getCommitHashes(cwd)

View file

@ -1,4 +1,4 @@
import core from '@actions/core' import { setOutput } from '@actions/core'
const resultsOutputId = 'results' const resultsOutputId = 'results'
@ -18,7 +18,7 @@ const mapResultOutput = ({
const generateOutputs = (lintedCommits) => { const generateOutputs = (lintedCommits) => {
const resultsOutput = lintedCommits.map(mapResultOutput) const resultsOutput = lintedCommits.map(mapResultOutput)
core.setOutput(resultsOutputId, resultsOutput) setOutput(resultsOutputId, resultsOutput)
} }
export default generateOutputs export default generateOutputs