Skip to content

Commit

Permalink
feat: adds commitDepth as new input param
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakethtadimeti committed Nov 20, 2022
1 parent 861b4d9 commit b0982e3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ Link to a page explaining your commit message convention.

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

### `commitDepth`

When set to a valid Integer value - X, considers only the latest X number of commits.

default: `null` (Equivalent to linting all commits)

### `token`

Personal access token (PAT) used to interact with the GitHub API.
Expand Down Expand Up @@ -144,6 +150,10 @@ jobs:
# Run the commitlint action, considering its own dependencies and yours as well 🚀
# `github.workspace` is the path to your repository.
- uses: wagoid/commitlint-github-action@v5
# Optinally, add a commitDepth parameter
# This example would only consider the last 10 commits.
with:
commitDepth: 10
env:
NODE_PATH: ${{ github.workspace }}/node_modules
```
Expand Down
14 changes: 12 additions & 2 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ 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'

Expand Down Expand Up @@ -118,7 +125,11 @@ const handleOnlyWarnings = (formattedResults) => {
}

const showLintResults = async ([from, to]) => {
const commits = await getHistoryCommits(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'] })
Expand All @@ -130,7 +141,6 @@ const showLintResults = async ([from, to]) => {
})),
)
const formattedResults = formatErrors(lintedCommits, { config })

generateOutputs(lintedCommits)

if (hasOnlyWarnings(lintedCommits)) {
Expand Down
37 changes: 37 additions & 0 deletions src/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,4 +605,41 @@ describe('Commit Linter action', () => {
td.verify(core.setFailed(contains(' https://example.org')))
})
})

describe('when commitDepth is provided in the config', () => {
beforeEach(async () => {
cwd = await git.bootstrap('fixtures/conventional')
await gitEmptyCommit(cwd, 'message from before push')
await gitEmptyCommit(cwd, 'incorrect message within commit depth')
await gitEmptyCommit(cwd, 'chore: correct message 2')
const [before, , to] = await getCommitHashes(cwd)
await createPushEventPayload(cwd, { before, to })
updatePushEnvVars(cwd, to)
td.replace(process, 'cwd', () => cwd)
td.replace(console, 'log')
})
it('should pass when only considering messages defined by commitDepth', async () => {
td.when(core.getInput('commitDepth')).thenReturn('1')
await runAction()

td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
td.verify(console.log('Lint free! 🎉'))
})
it('should fail when older commits have lint errors', async () => {
td.when(core.getInput('commitDepth')).thenReturn('2')
await runAction()

td.verify(
core.setFailed(contains('incorrect message within commit depth')),
)
})
it('should consider all commits when an invalid commit depth is passed in config', async () => {
td.when(core.getInput('commitDepth')).thenReturn('xzy')
await runAction()

td.verify(
core.setFailed(contains('incorrect message within commit depth')),
)
})
})
})

0 comments on commit b0982e3

Please sign in to comment.