From b0982e3b0a9fd9398bcfdf10c12df058bb8ffaf2 Mon Sep 17 00:00:00 2001 From: Sakethtadimeti Date: Sun, 20 Nov 2022 13:45:17 +0530 Subject: [PATCH 1/2] feat: adds commitDepth as new input param --- README.md | 10 ++++++++++ src/action.js | 14 ++++++++++++-- src/action.test.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8227b69a..711a856b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 ``` diff --git a/src/action.js b/src/action.js index 4f41a743..8d34009c 100644 --- a/src/action.js +++ b/src/action.js @@ -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' @@ -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'] }) @@ -130,7 +141,6 @@ const showLintResults = async ([from, to]) => { })), ) const formattedResults = formatErrors(lintedCommits, { config }) - generateOutputs(lintedCommits) if (hasOnlyWarnings(lintedCommits)) { diff --git a/src/action.test.js b/src/action.test.js index a8984ca6..7aef83bb 100644 --- a/src/action.test.js +++ b/src/action.test.js @@ -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')), + ) + }) + }) }) From 1e32deb6dd2fe3bad9b0a2810e7c4759946b3783 Mon Sep 17 00:00:00 2001 From: Sakethtadimeti Date: Thu, 24 Nov 2022 16:58:02 +0530 Subject: [PATCH 2/2] docs: removes commitDepth Info from readme --- README.md | 4 ---- action.yml | 11 ++++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 711a856b..1aa3e9ee 100644 --- a/README.md +++ b/README.md @@ -150,10 +150,6 @@ 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 ``` diff --git a/action.yml b/action.yml index 9b6435e5..c61a0ca0 100644 --- a/action.yml +++ b/action.yml @@ -3,7 +3,8 @@ description: Lints Pull Request commit messages with commitlint author: Wagner Santos inputs: configFile: - description: Commitlint config file. If the file doesn't exist, config-conventional settings will be + description: + Commitlint config file. If the file doesn't exist, config-conventional settings will be loaded as a fallback. default: ./commitlint.config.js required: false @@ -12,16 +13,20 @@ inputs: When set to true, we follow only the first parent commit when seeing a merge commit. More info in git-log docs https://git-scm.com/docs/git-log#Documentation/git-log.txt---first-parent - default: "true" + default: 'true' required: false failOnWarnings: description: Whether you want to fail on warnings or not - default: "false" + default: 'false' required: false helpURL: description: Link to a page explaining your commit message convention default: https://github.com/conventional-changelog/commitlint/#what-is-commitlint required: false + commitDepth: + description: When set to a valid Integer value - X, considers only the latest X number of commits. + default: '' + required: false token: description: > Personal access token (PAT) used to interact with the GitHub API. By default, the