11
0
Fork 0
mirror of https://github.com/wagoid/commitlint-github-action.git synced 2026-03-31 06:46: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:
Wagner Santos 2021-02-15 06:51:15 -03:00
parent 300f6dc47c
commit 51913c302c
11 changed files with 2376 additions and 34 deletions

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