Skip to content

Commit e6b4f2e

Browse files
authored
Merge pull request #595 from Sakethtadimeti/master
feat: adds commitDepth as new input param
2 parents 861b4d9 + 1e32deb commit e6b4f2e

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ Link to a page explaining your commit message convention.
5858

5959
default: `https://github.com/conventional-changelog/commitlint/#what-is-commitlint`
6060

61+
### `commitDepth`
62+
63+
When set to a valid Integer value - X, considers only the latest X number of commits.
64+
65+
default: `null` (Equivalent to linting all commits)
66+
6167
### `token`
6268

6369
Personal access token (PAT) used to interact with the GitHub API.

action.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ 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
@@ -12,16 +13,20 @@ inputs:
1213
When set to true, we follow only the first parent commit when seeing a merge commit.
1314
More info in git-log docs
1415
https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent
15-
default: "true"
16+
default: 'true'
1617
required: false
1718
failOnWarnings:
1819
description: Whether you want to fail on warnings or not
19-
default: "false"
20+
default: 'false'
2021
required: false
2122
helpURL:
2223
description: Link to a page explaining your commit message convention
2324
default: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
2425
required: false
26+
commitDepth:
27+
description: When set to a valid Integer value - X, considers only the latest X number of commits.
28+
default: ''
29+
required: false
2530
token:
2631
description: >
2732
Personal access token (PAT) used to interact with the GitHub API. By default, the

src/action.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
1616

1717
const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
1818

19+
const getCommitDepth = () => {
20+
const commitDepthString = getInput('commitDepth')
21+
if (!commitDepthString?.trim()) return null
22+
const commitDepth = parseInt(commitDepthString, 10)
23+
return Number.isNaN(commitDepth) ? null : Math.max(commitDepth, 0)
24+
}
25+
1926
const pushEventHasOnlyOneCommit = (from) => {
2027
const gitEmptySha = '0000000000000000000000000000000000000000'
2128

@@ -118,7 +125,11 @@ const handleOnlyWarnings = (formattedResults) => {
118125
}
119126

120127
const showLintResults = async ([from, to]) => {
121-
const commits = await getHistoryCommits(from, to)
128+
let commits = await getHistoryCommits(from, to)
129+
const commitDepth = getCommitDepth()
130+
if (commitDepth) {
131+
commits = commits?.slice(0, commitDepth)
132+
}
122133
const config = existsSync(configPath)
123134
? await load({}, { file: configPath })
124135
: await load({ extends: ['@commitlint/config-conventional'] })
@@ -130,7 +141,6 @@ const showLintResults = async ([from, to]) => {
130141
})),
131142
)
132143
const formattedResults = formatErrors(lintedCommits, { config })
133-
134144
generateOutputs(lintedCommits)
135145

136146
if (hasOnlyWarnings(lintedCommits)) {

src/action.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,41 @@ describe('Commit Linter action', () => {
605605
td.verify(core.setFailed(contains(' https://example.org')))
606606
})
607607
})
608+
609+
describe('when commitDepth is provided in the config', () => {
610+
beforeEach(async () => {
611+
cwd = await git.bootstrap('fixtures/conventional')
612+
await gitEmptyCommit(cwd, 'message from before push')
613+
await gitEmptyCommit(cwd, 'incorrect message within commit depth')
614+
await gitEmptyCommit(cwd, 'chore: correct message 2')
615+
const [before, , to] = await getCommitHashes(cwd)
616+
await createPushEventPayload(cwd, { before, to })
617+
updatePushEnvVars(cwd, to)
618+
td.replace(process, 'cwd', () => cwd)
619+
td.replace(console, 'log')
620+
})
621+
it('should pass when only considering messages defined by commitDepth', async () => {
622+
td.when(core.getInput('commitDepth')).thenReturn('1')
623+
await runAction()
624+
625+
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
626+
td.verify(console.log('Lint free! 🎉'))
627+
})
628+
it('should fail when older commits have lint errors', async () => {
629+
td.when(core.getInput('commitDepth')).thenReturn('2')
630+
await runAction()
631+
632+
td.verify(
633+
core.setFailed(contains('incorrect message within commit depth')),
634+
)
635+
})
636+
it('should consider all commits when an invalid commit depth is passed in config', async () => {
637+
td.when(core.getInput('commitDepth')).thenReturn('xzy')
638+
await runAction()
639+
640+
td.verify(
641+
core.setFailed(contains('incorrect message within commit depth')),
642+
)
643+
})
644+
})
608645
})

0 commit comments

Comments
 (0)