Skip to content

Commit b0982e3

Browse files
feat: adds commitDepth as new input param
1 parent 861b4d9 commit b0982e3

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

README.md

+10
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.
@@ -144,6 +150,10 @@ jobs:
144150
# Run the commitlint action, considering its own dependencies and yours as well 🚀
145151
# `github.workspace` is the path to your repository.
146152
- uses: wagoid/commitlint-github-action@v5
153+
# Optinally, add a commitDepth parameter
154+
# This example would only consider the last 10 commits.
155+
with:
156+
commitDepth: 10
147157
env:
148158
NODE_PATH: ${{ github.workspace }}/node_modules
149159
```

src/action.js

+12-2
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

+37
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)