mirror of
https://github.com/wagoid/commitlint-github-action.git
synced 2025-11-07 16:06:56 +00:00
style: add eslint to the project
The config is based on airbnb shared config, with some rules that work better with the project.
This commit is contained in:
parent
300f6dc47c
commit
51913c302c
11 changed files with 2376 additions and 34 deletions
|
|
@ -7,3 +7,4 @@ coverage
|
|||
fixtures
|
||||
src/action.test.js
|
||||
src/testUtils.js
|
||||
.eslintrc.json
|
||||
|
|
|
|||
10
.eslintrc.json
Normal file
10
.eslintrc.json
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"extends": ["airbnb-base", "prettier", "plugin:node/recommended"],
|
||||
"plugins": ["prettier"],
|
||||
"rules": {
|
||||
"prettier/prettier": "error",
|
||||
"no-console": "off",
|
||||
"no-process-exit": "off",
|
||||
"node/no-unpublished-require": "off"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
module.exports = {
|
||||
'*.{ts,tsx,vue,css,less,scss,html,htm,md,markdown}': 'prettier --write',
|
||||
'*.{js,jsx,json,yml,yaml}': ['prettier --write', () => 'npm run test'],
|
||||
'*.{json,yml,yaml}': ['prettier --write', () => 'npm run test'],
|
||||
'*.{js,jsx}': ['eslint --fix', () => 'npm run test']],
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
/* eslint-disable import/no-extraneous-dependencies */
|
||||
const { maxLineLength } = require('@commitlint/ensure')
|
||||
|
||||
const bodyMaxLineLength = 100
|
||||
|
||||
const validateBodyMaxLengthIgnoringDeps = parsedCommit => {
|
||||
const validateBodyMaxLengthIgnoringDeps = (parsedCommit) => {
|
||||
const { type, scope, body } = parsedCommit
|
||||
const isDepsCommit =
|
||||
type === 'chore' && (scope === 'deps' || scope === 'deps-dev')
|
||||
|
|
|
|||
2322
package-lock.json
generated
2322
package-lock.json
generated
File diff suppressed because it is too large
Load diff
13
package.json
13
package.json
|
|
@ -6,7 +6,11 @@
|
|||
"main": "run.js",
|
||||
"scripts": {
|
||||
"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 --ignore-path .gitignore ."
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
@ -39,6 +43,13 @@
|
|||
"@commitlint/test-environment": "^9.0.1",
|
||||
"commitlint-plugin-function-rules": "^1.1.20",
|
||||
"conventional-changelog-cli": "^2.1.1",
|
||||
"eslint": "^7.20.0",
|
||||
"eslint-config-airbnb-base": "^14.2.1",
|
||||
"eslint-config-node": "^4.1.0",
|
||||
"eslint-config-prettier": "^7.2.0",
|
||||
"eslint-plugin-import": "^2.22.1",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-prettier": "^3.3.1",
|
||||
"husky": "^5.0.9",
|
||||
"jest": "^26.6.3",
|
||||
"lint-staged": "^10.5.4",
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ const configPath = resolve(
|
|||
|
||||
const { context: eventContext } = github
|
||||
|
||||
const pushEventHasOnlyOneCommit = from => {
|
||||
const pushEventHasOnlyOneCommit = (from) => {
|
||||
const gitEmptySha = '0000000000000000000000000000000000000000'
|
||||
|
||||
return from === gitEmptySha
|
||||
|
|
@ -54,7 +54,7 @@ const getRangeForEvent = async () => {
|
|||
repo,
|
||||
pull_number: number,
|
||||
})
|
||||
const commitShas = commits.map(commit => commit.sha)
|
||||
const commitShas = commits.map((commit) => commit.sha)
|
||||
const [from] = commitShas
|
||||
const to = commitShas[commitShas.length - 1]
|
||||
// Git revision range doesn't include the "from" field in "git log", so for "from" we use the parent commit of PR's first commit
|
||||
|
|
@ -93,25 +93,25 @@ function getOptsFromConfig(config) {
|
|||
}
|
||||
}
|
||||
|
||||
const formatErrors = lintedCommits =>
|
||||
const formatErrors = (lintedCommits) =>
|
||||
format(
|
||||
{ results: lintedCommits.map(commit => commit.lintResult) },
|
||||
{ results: lintedCommits.map((commit) => commit.lintResult) },
|
||||
{
|
||||
color: true,
|
||||
helpUrl: core.getInput('helpURL'),
|
||||
},
|
||||
)
|
||||
|
||||
const hasOnlyWarnings = lintedCommits =>
|
||||
const hasOnlyWarnings = (lintedCommits) =>
|
||||
lintedCommits.length &&
|
||||
lintedCommits.every(({ lintResult }) => lintResult.valid) &&
|
||||
lintedCommits.some(({ lintResult }) => lintResult.warnings.length)
|
||||
|
||||
const setFailed = formattedResults => {
|
||||
const setFailed = (formattedResults) => {
|
||||
core.setFailed(`You have commit messages with errors\n\n${formattedResults}`)
|
||||
}
|
||||
|
||||
const handleOnlyWarnings = formattedResults => {
|
||||
const handleOnlyWarnings = (formattedResults) => {
|
||||
if (core.getInput('failOnWarnings') === 'true') {
|
||||
setFailed(formattedResults)
|
||||
} else {
|
||||
|
|
@ -126,7 +126,7 @@ const showLintResults = async ([from, to]) => {
|
|||
: await load({ extends: ['@commitlint/config-conventional'] })
|
||||
const opts = getOptsFromConfig(config)
|
||||
const lintedCommits = await Promise.all(
|
||||
commits.map(async commit => ({
|
||||
commits.map(async (commit) => ({
|
||||
lintResult: await lint(commit.message, config.rules, opts),
|
||||
hash: commit.hash,
|
||||
})),
|
||||
|
|
@ -144,7 +144,7 @@ const showLintResults = async ([from, to]) => {
|
|||
}
|
||||
}
|
||||
|
||||
const exitWithMessage = message => error => {
|
||||
const exitWithMessage = (message) => (error) => {
|
||||
core.setFailed(`${message}\n${error.message}\n${error.stack}`)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
/* eslint-disable global-require */
|
||||
/* eslint-env jest */
|
||||
const { git } = require('@commitlint/test')
|
||||
const execa = require('execa')
|
||||
const td = require('testdouble')
|
||||
const {
|
||||
updateEnvVars,
|
||||
gitEmptyCommit,
|
||||
getCommitHashes,
|
||||
updatePushEnvVars,
|
||||
|
|
@ -147,7 +148,7 @@ describe('Commit Linter action', () => {
|
|||
await gitEmptyCommit(cwd, 'message from before push')
|
||||
await gitEmptyCommit(cwd, 'wrong message 1')
|
||||
await gitEmptyCommit(cwd, 'chore(WRONG): message 2')
|
||||
const [before, , to] = await getCommitHashes(cwd)
|
||||
const [, , to] = await getCommitHashes(cwd)
|
||||
await createPushEventPayload(cwd, { before: gitEmptySha, to })
|
||||
updatePushEnvVars(cwd, to)
|
||||
td.replace(process, 'cwd', () => cwd)
|
||||
|
|
@ -281,7 +282,7 @@ describe('Commit Linter action', () => {
|
|||
pull_number: '1',
|
||||
}),
|
||||
).thenResolve({
|
||||
data: [first, to].map(sha => ({ sha })),
|
||||
data: [first, to].map((sha) => ({ sha })),
|
||||
})
|
||||
td.replace(process, 'cwd', () => cwd)
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ const core = require('@actions/core')
|
|||
|
||||
const resultsOutputId = 'results'
|
||||
|
||||
const mapMessageValidation = item => item.message
|
||||
const mapMessageValidation = (item) => item.message
|
||||
|
||||
const mapResultOutput = ({
|
||||
hash,
|
||||
|
|
@ -15,7 +15,7 @@ const mapResultOutput = ({
|
|||
warnings: warnings.map(mapMessageValidation),
|
||||
})
|
||||
|
||||
const generateOutputs = lintedCommits => {
|
||||
const generateOutputs = (lintedCommits) => {
|
||||
const resultsOutput = lintedCommits.map(mapResultOutput)
|
||||
|
||||
core.setOutput(resultsOutputId, resultsOutput)
|
||||
|
|
|
|||
|
|
@ -7,12 +7,12 @@ const hashDelimiter = '--------->hash---------'
|
|||
|
||||
const format = `%H${hashDelimiter}%B%n${commitDelimiter}`
|
||||
|
||||
const buildGitArgs = gitOpts => {
|
||||
const buildGitArgs = (gitOpts) => {
|
||||
const { from, to, ...otherOpts } = gitOpts
|
||||
var formatArg = `--format=${format}`
|
||||
var fromToArg = [from, to].filter(Boolean).join('..')
|
||||
const formatArg = `--format=${format}`
|
||||
const fromToArg = [from, to].filter(Boolean).join('..')
|
||||
|
||||
var gitArgs = ['log', formatArg, fromToArg]
|
||||
const gitArgs = ['log', formatArg, fromToArg]
|
||||
|
||||
return gitArgs.concat(
|
||||
dargs(gitOpts, {
|
||||
|
|
@ -21,14 +21,14 @@ const buildGitArgs = gitOpts => {
|
|||
)
|
||||
}
|
||||
|
||||
const gitCommits = async gitOpts => {
|
||||
var args = buildGitArgs(gitOpts)
|
||||
const gitCommits = async (gitOpts) => {
|
||||
const args = buildGitArgs(gitOpts)
|
||||
|
||||
var { stdout } = await execa('git', args, {
|
||||
const { stdout } = await execa('git', args, {
|
||||
cwd: process.cwd(),
|
||||
})
|
||||
|
||||
const commits = stdout.split(`${commitDelimiter}\n`).map(messageItem => {
|
||||
const commits = stdout.split(`${commitDelimiter}\n`).map((messageItem) => {
|
||||
const [hash, message] = messageItem.split(hashDelimiter)
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -2,20 +2,21 @@ const path = require('path')
|
|||
const fs = require('fs')
|
||||
const { promisify } = require('util')
|
||||
const execa = require('execa')
|
||||
const td = require('testdouble')
|
||||
|
||||
const writeFile = promisify(fs.writeFile)
|
||||
|
||||
const updateEnvVars = (exports.updateEnvVars = envVars => {
|
||||
Object.keys(envVars).forEach(key => {
|
||||
const updateEnvVars = (envVars) => {
|
||||
Object.keys(envVars).forEach((key) => {
|
||||
process.env[key] = envVars[key]
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports.updateEnvVars = updateEnvVars
|
||||
|
||||
exports.gitEmptyCommit = (cwd, message) =>
|
||||
execa('git', ['commit', '--allow-empty', '-m', message], { cwd })
|
||||
|
||||
exports.getCommitHashes = async cwd => {
|
||||
exports.getCommitHashes = async (cwd) => {
|
||||
const { stdout } = await execa.command('git log --pretty=%H', { cwd })
|
||||
const hashes = stdout.split('\n').reverse()
|
||||
|
||||
|
|
@ -45,7 +46,7 @@ exports.createPushEventPayload = async (
|
|||
await writeFile(eventPath, JSON.stringify(payload), 'utf8')
|
||||
}
|
||||
|
||||
exports.createPullRequestEventPayload = async cwd => {
|
||||
exports.createPullRequestEventPayload = async (cwd) => {
|
||||
const payload = {
|
||||
number: '1',
|
||||
repository: {
|
||||
|
|
|
|||
Loading…
Reference in a new issue