Skip to content

Commit 73bc5d6

Browse files
committed
wrong feat!: use github event payload and API to list commits
That way we guarantee we're linting the same commits that appear on github. BREAKING CHANGE: "firstParent" option has been removed
1 parent 3db4b7d commit 73bc5d6

File tree

9 files changed

+238
-318
lines changed

9 files changed

+238
-318
lines changed

.github/workflows/commitlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
fetch-depth: 0
1111
- run: sed -i -E "s/(docker:.+)/Dockerfile/" ./action.yml
1212
- run: echo -n '' > .dockerignore
13+
- run: echo "${{toJSON(github.event)}}"
1314
- uses: actions/setup-node@v3
1415
with:
1516
node-version: '16.5.0'

README.md

Lines changed: 0 additions & 8 deletions
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

Lines changed: 5 additions & 11 deletions
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

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
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

Lines changed: 18 additions & 52 deletions
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,12 @@ 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-
}
46+
console.log('commits', JSON.stringify(commits, null, 2))
8547

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

8954
function getOptsFromConfig(config) {
@@ -125,8 +90,9 @@ const handleOnlyWarnings = (formattedResults) => {
12590
}
12691
}
12792

128-
const showLintResults = async ([from, to]) => {
129-
let commits = await getHistoryCommits(from, to)
93+
const showLintResults = async (eventCommits) => {
94+
// let commits = await getHistoryCommits(from, to)
95+
let commits = eventCommits
13096
const commitDepth = getCommitDepth()
13197
if (commitDepth) {
13298
commits = commits?.slice(0, commitDepth)
@@ -164,7 +130,7 @@ const exitWithMessage = (message) => (error) => {
164130
}
165131

166132
const commitLinterAction = () =>
167-
getRangeForEvent()
133+
getEventCommits()
168134
.catch(
169135
exitWithMessage("error trying to get list of pull request's commits"),
170136
)

0 commit comments

Comments
 (0)