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

Merge pull request #95 from wagoid/style/add-eslint-config

style: add eslint to the project
This commit is contained in:
Wagner Santos 2021-02-18 15:13:13 -03:00 committed by GitHub
commit bd679b42e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 2380 additions and 37 deletions

View file

@ -7,3 +7,4 @@ coverage
fixtures
src/action.test.js
src/testUtils.js
.eslintrc.json

10
.eslintrc.json Normal file
View 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"
}
}

View file

@ -2,8 +2,8 @@ name: CI
on: [push]
jobs:
test:
name: Test
sanity-checks:
name: Sanity Checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
@ -19,9 +19,10 @@ jobs:
restore-keys: |
${{ runner.os }}-node-
- run: npm install
- run: npm run lint
- run: npm test -- --ci --coverage
release:
needs: test
needs: sanity-checks
runs-on: ubuntu-latest
if: "github.ref == 'refs/heads/master' && !contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
env:

View file

@ -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']],
}

View file

@ -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

File diff suppressed because it is too large Load diff

View file

@ -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",

View file

@ -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}`)
}

View file

@ -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)

View file

@ -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)

View file

@ -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 {

View file

@ -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: {