-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathaction.js
167 lines (140 loc) · 4.84 KB
/
action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { existsSync } from 'fs'
import { resolve } from 'path'
import { getInput, setFailed } from '@actions/core'
import { context as eventContext, getOctokit } from '@actions/github'
import lint from '@commitlint/lint'
import { format } from '@commitlint/format'
import load from '@commitlint/load'
import gitCommits from './gitCommits'
import generateOutputs from './generateOutputs'
const pullRequestEvent = 'pull_request'
const pullRequestTargetEvent = 'pull_request_target'
const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent]
const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
const getCommitDepth = () => {
const commitDepthString = getInput('commitDepth')
if (!commitDepthString?.trim()) return null
const commitDepth = parseInt(commitDepthString, 10)
return Number.isNaN(commitDepth) ? null : Math.max(commitDepth, 0)
}
const pushEventHasOnlyOneCommit = (from) => {
const gitEmptySha = '0000000000000000000000000000000000000000'
return from === gitEmptySha
}
const getRangeForPushEvent = () => {
let from = eventContext.payload.before
const to = GITHUB_SHA
if (eventContext.payload.forced) {
// When a commit is forced, "before" field from the push event data may point to a commit that doesn't exist
console.warn(
'Commit was forced, checking only the latest commit from push instead of a range of commit messages',
)
from = null
}
if (pushEventHasOnlyOneCommit(from)) {
from = null
}
return [from, to]
}
const getRangeForEvent = async () => {
if (!pullRequestEvents.includes(GITHUB_EVENT_NAME))
return getRangeForPushEvent()
const octokit = getOctokit(getInput('token'))
const { owner, repo, number } = eventContext.issue
const { data: commits } = await octokit.rest.pulls.listCommits({
owner,
repo,
pull_number: number,
})
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
const fromParent = `${from}^1`
return [fromParent, to]
}
function getHistoryCommits(from, to) {
const options = {
from,
to,
}
if (getInput('firstParent') === 'true') {
options.firstParent = true
}
if (!from) {
options.maxCount = 1
}
return gitCommits(options)
}
function getOptsFromConfig(config) {
return {
parserOpts:
config.parserPreset != null && config.parserPreset.parserOpts != null
? config.parserPreset.parserOpts
: {},
plugins: config.plugins != null ? config.plugins : {},
ignores: config.ignores != null ? config.ignores : [],
defaultIgnores:
config.defaultIgnores != null ? config.defaultIgnores : true,
}
}
const formatErrors = (lintedCommits, { config }) =>
format(
{ results: lintedCommits.map((commit) => commit.lintResult) },
{
color: true,
helpUrl: config.helpUrl || getInput('helpURL'),
},
)
const hasOnlyWarnings = (lintedCommits) =>
lintedCommits.length &&
lintedCommits.every(({ lintResult }) => lintResult.valid) &&
lintedCommits.some(({ lintResult }) => lintResult.warnings.length)
const setFailedAction = (formattedResults) => {
setFailed(`You have commit messages with errors\n\n${formattedResults}`)
}
const handleOnlyWarnings = (formattedResults) => {
if (getInput('failOnWarnings') === 'true') {
setFailedAction(formattedResults)
} else {
console.log(`You have commit messages with warnings\n\n${formattedResults}`)
}
}
const showLintResults = async ([from, to]) => {
let commits = await getHistoryCommits(from, to)
const commitDepth = getCommitDepth()
if (commitDepth) {
commits = commits?.slice(0, commitDepth)
}
const config = existsSync(configPath)
? await load({}, { file: configPath })
: await load({ extends: ['@commitlint/config-conventional'] })
const opts = getOptsFromConfig(config)
const lintedCommits = await Promise.all(
commits.map(async (commit) => ({
lintResult: await lint(commit.message, config.rules, opts),
hash: commit.hash,
})),
)
const formattedResults = formatErrors(lintedCommits, { config })
generateOutputs(lintedCommits)
if (hasOnlyWarnings(lintedCommits)) {
handleOnlyWarnings(formattedResults)
} else if (formattedResults) {
setFailedAction(formattedResults)
} else {
console.log('Lint free! 🎉')
}
}
const exitWithMessage = (message) => (error) => {
setFailedAction(`${message}\n${error.message}\n${error.stack}`)
}
const commitLinterAction = () =>
getRangeForEvent()
.catch(
exitWithMessage("error trying to get list of pull request's commits"),
)
.then(showLintResults)
.catch(exitWithMessage('error running commitlint'))
export default commitLinterAction