Skip to content

Commit a31f4b5

Browse files
committed
feat!: use github event payload and API to list commits resolves #456
That way we guarantee we're linting the same commits that appear on github. BREAKING CHANGE: "firstParent" option has been removed
1 parent 2be323b commit a31f4b5

8 files changed

+219
-334
lines changed

README.md

-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ If the config file doesn't exist, [config-conventional](https://github.com/conve
3838

3939
Details on the configuration file can be found on [the commitlint website](https://commitlint.js.org/#/reference-configuration).
4040

41-
### `firstParent`
42-
43-
When set to true, we follow only the first parent commit when seeing a merge commit.
44-
45-
This helps to ignore errors in commits that were already present in your default branch (e.g. `master`) before adding conventional commit checks. More info in [git-log docs](https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent).
46-
47-
Default: `true`
48-
4941
### `failOnWarnings`
5042

5143
Whether you want to fail on warnings or not.

action.yml

+5-11
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,26 @@ description: Lints Pull Request commit messages with commitlint
33
author: Wagner Santos
44
inputs:
55
configFile:
6-
description: Commitlint config file. If the file doesn't exist, config-conventional settings will be
6+
description:
7+
Commitlint config file. If the file doesn't exist, config-conventional settings will be
78
loaded as a fallback.
89
default: ./commitlint.config.js
910
required: false
10-
firstParent:
11-
description: >
12-
When set to true, we follow only the first parent commit when seeing a merge commit.
13-
More info in git-log docs
14-
https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent
15-
default: "true"
16-
required: false
1711
failOnWarnings:
1812
description: Whether you want to fail on warnings or not
19-
default: "false"
13+
default: 'false'
2014
required: false
2115
failOnErrors:
2216
description: Whether you want to fail on errors or not
23-
default: "true"
17+
default: 'true'
2418
required: true
2519
helpURL:
2620
description: Link to a page explaining your commit message convention
2721
default: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
2822
required: false
2923
commitDepth:
3024
description: When set to a valid Integer value - X, considers only the latest X number of commits.
31-
default: ""
25+
default: ''
3226
required: false
3327
token:
3428
description: >

package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"conventional-changelog-conventionalcommits": "^4.6.3",
3939
"conventional-changelog-lint-config-canonical": "^1.0.0",
4040
"dargs": "^8.1.0",
41-
"execa": "^5.1.1",
4241
"lerna": "^5.1.4"
4342
},
4443
"devDependencies": {

src/action.js

+16-52
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import { context as eventContext, getOctokit } from '@actions/github'
55
import lint from '@commitlint/lint'
66
import { format } from '@commitlint/format'
77
import load from '@commitlint/load'
8-
import gitCommits from './gitCommits'
98
import generateOutputs from './generateOutputs'
109

1110
const pullRequestEvent = 'pull_request'
1211
const pullRequestTargetEvent = 'pull_request_target'
1312
const pullRequestEvents = [pullRequestEvent, pullRequestTargetEvent]
1413

15-
const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
14+
const { GITHUB_EVENT_NAME } = process.env
1615

1716
const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
1817

@@ -23,34 +22,18 @@ const getCommitDepth = () => {
2322
return Number.isNaN(commitDepth) ? null : Math.max(commitDepth, 0)
2423
}
2524

26-
const pushEventHasOnlyOneCommit = (from) => {
27-
const gitEmptySha = '0000000000000000000000000000000000000000'
25+
const getPushEventCommits = () => {
26+
const mappedCommits = eventContext.payload.commits.map((commit) => ({
27+
message: commit.message,
28+
hash: commit.id,
29+
}))
2830

29-
return from === gitEmptySha
31+
return mappedCommits
3032
}
3133

32-
const getRangeForPushEvent = () => {
33-
let from = eventContext.payload.before
34-
const to = GITHUB_SHA
35-
36-
if (eventContext.payload.forced) {
37-
// When a commit is forced, "before" field from the push event data may point to a commit that doesn't exist
38-
console.warn(
39-
'Commit was forced, checking only the latest commit from push instead of a range of commit messages',
40-
)
41-
from = null
42-
}
43-
44-
if (pushEventHasOnlyOneCommit(from)) {
45-
from = null
46-
}
47-
48-
return [from, to]
49-
}
50-
51-
const getRangeForEvent = async () => {
34+
const getEventCommits = async () => {
5235
if (!pullRequestEvents.includes(GITHUB_EVENT_NAME))
53-
return getRangeForPushEvent()
36+
return getPushEventCommits()
5437

5538
const octokit = getOctokit(getInput('token'))
5639
const { owner, repo, number } = eventContext.issue
@@ -60,30 +43,11 @@ const getRangeForEvent = async () => {
6043
pull_number: number,
6144
per_page: 100,
6245
})
63-
const commitShas = commits.map((commit) => commit.sha)
64-
const [from] = commitShas
65-
const to = commitShas[commitShas.length - 1]
66-
// 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
67-
const fromParent = `${from}^1`
68-
69-
return [fromParent, to]
70-
}
71-
72-
function getHistoryCommits(from, to) {
73-
const options = {
74-
from,
75-
to,
76-
}
77-
78-
if (getInput('firstParent') === 'true') {
79-
options.firstParent = true
80-
}
81-
82-
if (!from) {
83-
options.maxCount = 1
84-
}
8546

86-
return gitCommits(options)
47+
return commits.map((commit) => ({
48+
message: commit.commit.message,
49+
hash: commit.sha,
50+
}))
8751
}
8852

8953
function getOptsFromConfig(config) {
@@ -125,8 +89,8 @@ const handleOnlyWarnings = (formattedResults) => {
12589
}
12690
}
12791

128-
const showLintResults = async ([from, to]) => {
129-
let commits = await getHistoryCommits(from, to)
92+
const showLintResults = async (eventCommits) => {
93+
let commits = eventCommits
13094
const commitDepth = getCommitDepth()
13195
if (commitDepth) {
13296
commits = commits?.slice(0, commitDepth)
@@ -164,7 +128,7 @@ const exitWithMessage = (message) => (error) => {
164128
}
165129

166130
const commitLinterAction = () =>
167-
getRangeForEvent()
131+
getEventCommits()
168132
.catch(
169133
exitWithMessage("error trying to get list of pull request's commits"),
170134
)

0 commit comments

Comments
 (0)