From e187e3fbdfaf0740741ec5277e2866e403815a5e Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Date: Fri, 7 Nov 2025 22:21:16 -0400 Subject: [PATCH 1/2] feat: add max header as an optional settings --- action.yml | 20 ++++++++++++-------- src/commitlint/cli.py | 6 ++++++ src/commitlint/config.py | 21 +++++++++++++++++++++ src/commitlint/linter/validators.py | 7 ++++--- src/commitlint/messages.py | 4 +--- tests/fixtures/linter.py | 14 +++++++++++--- tests/test_linter/test__linter.py | 2 +- 7 files changed, 56 insertions(+), 18 deletions(-) diff --git a/action.yml b/action.yml index e397c54..edb7687 100644 --- a/action.yml +++ b/action.yml @@ -1,14 +1,18 @@ -name: 'Conventional Commitlint' -description: 'A GitHub Action to check conventional commit message' +name: "Conventional Commitlint" +description: "A GitHub Action to check conventional commit message" inputs: fail_on_error: description: Whether to fail the workflow if commit messages don't follow conventions. - default: 'true' + default: "true" required: false verbose: description: Verbose output. - default: 'false' + default: "false" + required: false + max_header_length: + description: Maximum length for commit message header. + default: "72" required: false token: description: Token for fetching commits using Github API. @@ -24,16 +28,16 @@ outputs: value: ${{ steps.commitlint.outputs.exit_code }} branding: - color: 'red' - icon: 'git-commit' + color: "red" + icon: "git-commit" runs: - using: 'composite' + using: "composite" steps: - name: Install Python uses: actions/setup-python@v5.1.0 with: - python-version: '3.10' + python-version: "3.10" - name: Commitlint Action id: commitlint diff --git a/src/commitlint/cli.py b/src/commitlint/cli.py index f34e6c8..3deef38 100644 --- a/src/commitlint/cli.py +++ b/src/commitlint/cli.py @@ -74,6 +74,12 @@ def get_args() -> argparse.Namespace: help="Hide input from stdout", default=False, ) + # --max-header-length option is optional + parser.add_argument( + "--max-header-length", + type=int, + help="Set the maximum header length", + ) output_group = parser.add_mutually_exclusive_group(required=False) # --quiet option is optional diff --git a/src/commitlint/config.py b/src/commitlint/config.py index f4ab9f4..49456e3 100644 --- a/src/commitlint/config.py +++ b/src/commitlint/config.py @@ -4,6 +4,8 @@ from typing import Optional +from src.commitlint.constants import COMMIT_HEADER_MAX_LENGTH + class _CommitlintConfig: """ @@ -14,6 +16,7 @@ class _CommitlintConfig: _verbose: bool = False _quiet: bool = False + _max_header_length: int = COMMIT_HEADER_MAX_LENGTH def __new__(cls) -> "_CommitlintConfig": """ @@ -70,6 +73,24 @@ def quiet(self, value: bool) -> None: self._quiet = value + @property + def max_header_length(self) -> int: + """ + Get the current max_header_length setting. + Returns: + int: The current max_header_length setting. + """ + return self._max_header_length + + @max_header_length.setter + def max_header_length(self, value: int) -> None: + """ + Set the max_header_length setting. + Args: + value (int): New value for max_header_length setting. + """ + self._max_header_length = value + config = _CommitlintConfig() diff --git a/src/commitlint/linter/validators.py b/src/commitlint/linter/validators.py index b7240f4..663306e 100644 --- a/src/commitlint/linter/validators.py +++ b/src/commitlint/linter/validators.py @@ -9,7 +9,8 @@ from typing import List, Tuple, Type, Union from .. import console -from ..constants import COMMIT_HEADER_MAX_LENGTH, COMMIT_TYPES +from ..config import config +from ..constants import COMMIT_TYPES from ..messages import ( COMMIT_TYPE_INVALID_ERROR, COMMIT_TYPE_MISSING_ERROR, @@ -71,8 +72,8 @@ def validate(self) -> None: None """ header = self.commit_message.split("\n")[0] - if len(header) > COMMIT_HEADER_MAX_LENGTH: - self.add_error(HEADER_LENGTH_ERROR) + if len(header) > config.max_header_length: + self.add_error(HEADER_LENGTH_ERROR % config.max_header_length) class SimplePatternValidator(CommitValidator): diff --git a/src/commitlint/messages.py b/src/commitlint/messages.py index c7cb4f2..56271d7 100644 --- a/src/commitlint/messages.py +++ b/src/commitlint/messages.py @@ -10,9 +10,7 @@ INCORRECT_FORMAT_ERROR = ( "Commit message does not follow the Conventional Commits format." ) -HEADER_LENGTH_ERROR = ( - f"Header length cannot exceed {COMMIT_HEADER_MAX_LENGTH} characters." -) +HEADER_LENGTH_ERROR = "Header length cannot exceed %d characters." COMMIT_TYPE_MISSING_ERROR = "Type is missing." COMMIT_TYPE_INVALID_ERROR = ( f"Invalid type '%s'. Type must be one of: {', '.join(COMMIT_TYPES)}." diff --git a/tests/fixtures/linter.py b/tests/fixtures/linter.py index 4e2bcee..8f23730 100644 --- a/tests/fixtures/linter.py +++ b/tests/fixtures/linter.py @@ -58,12 +58,20 @@ # incorrect format check ("feat add new feature", False, [INCORRECT_FORMAT_ERROR]), # header length check - ("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]), - ("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]), + ( + "feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), + False, + [HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH], + ), + ( + "feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), + False, + [HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH], + ), ( "Test " + "a" * (COMMIT_HEADER_MAX_LENGTH + 1), False, - [HEADER_LENGTH_ERROR, INCORRECT_FORMAT_ERROR], + [HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH, INCORRECT_FORMAT_ERROR], ), # commit type check (": add new feature", False, [COMMIT_TYPE_MISSING_ERROR]), diff --git a/tests/test_linter/test__linter.py b/tests/test_linter/test__linter.py index e994f64..79aa99f 100644 --- a/tests/test_linter/test__linter.py +++ b/tests/test_linter/test__linter.py @@ -51,7 +51,7 @@ def test__lint_commit_message__skip_detail_returns_header_length_error_message() commit_message = "Test " + "a" * (COMMIT_HEADER_MAX_LENGTH + 1) success, errors = lint_commit_message(commit_message, skip_detail=True) assert success is False - assert errors == [HEADER_LENGTH_ERROR] + assert errors == [HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH] def test__lint_commit_message__skip_detail_returns_invalid_format_error_message(): From c062d45e71c7e51f3f105ab3bc829d706015bf01 Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Souza Date: Sat, 8 Nov 2025 12:19:36 -0400 Subject: [PATCH 2/2] fix: update import statement for constants in config.py --- src/commitlint/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commitlint/config.py b/src/commitlint/config.py index 49456e3..880dde0 100644 --- a/src/commitlint/config.py +++ b/src/commitlint/config.py @@ -4,7 +4,7 @@ from typing import Optional -from src.commitlint.constants import COMMIT_HEADER_MAX_LENGTH +from .constants import COMMIT_HEADER_MAX_LENGTH class _CommitlintConfig: