Skip to content

Commit e187e3f

Browse files
feat: add max header as an optional settings
1 parent 7d51774 commit e187e3f

File tree

7 files changed

+56
-18
lines changed

7 files changed

+56
-18
lines changed

action.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
name: 'Conventional Commitlint'
2-
description: 'A GitHub Action to check conventional commit message'
1+
name: "Conventional Commitlint"
2+
description: "A GitHub Action to check conventional commit message"
33

44
inputs:
55
fail_on_error:
66
description: Whether to fail the workflow if commit messages don't follow conventions.
7-
default: 'true'
7+
default: "true"
88
required: false
99
verbose:
1010
description: Verbose output.
11-
default: 'false'
11+
default: "false"
12+
required: false
13+
max_header_length:
14+
description: Maximum length for commit message header.
15+
default: "72"
1216
required: false
1317
token:
1418
description: Token for fetching commits using Github API.
@@ -24,16 +28,16 @@ outputs:
2428
value: ${{ steps.commitlint.outputs.exit_code }}
2529

2630
branding:
27-
color: 'red'
28-
icon: 'git-commit'
31+
color: "red"
32+
icon: "git-commit"
2933

3034
runs:
31-
using: 'composite'
35+
using: "composite"
3236
steps:
3337
- name: Install Python
3438
uses: actions/setup-python@v5.1.0
3539
with:
36-
python-version: '3.10'
40+
python-version: "3.10"
3741

3842
- name: Commitlint Action
3943
id: commitlint

src/commitlint/cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def get_args() -> argparse.Namespace:
7474
help="Hide input from stdout",
7575
default=False,
7676
)
77+
# --max-header-length option is optional
78+
parser.add_argument(
79+
"--max-header-length",
80+
type=int,
81+
help="Set the maximum header length",
82+
)
7783

7884
output_group = parser.add_mutually_exclusive_group(required=False)
7985
# --quiet option is optional

src/commitlint/config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from typing import Optional
66

7+
from src.commitlint.constants import COMMIT_HEADER_MAX_LENGTH
8+
79

810
class _CommitlintConfig:
911
"""
@@ -14,6 +16,7 @@ class _CommitlintConfig:
1416

1517
_verbose: bool = False
1618
_quiet: bool = False
19+
_max_header_length: int = COMMIT_HEADER_MAX_LENGTH
1720

1821
def __new__(cls) -> "_CommitlintConfig":
1922
"""
@@ -70,6 +73,24 @@ def quiet(self, value: bool) -> None:
7073

7174
self._quiet = value
7275

76+
@property
77+
def max_header_length(self) -> int:
78+
"""
79+
Get the current max_header_length setting.
80+
Returns:
81+
int: The current max_header_length setting.
82+
"""
83+
return self._max_header_length
84+
85+
@max_header_length.setter
86+
def max_header_length(self, value: int) -> None:
87+
"""
88+
Set the max_header_length setting.
89+
Args:
90+
value (int): New value for max_header_length setting.
91+
"""
92+
self._max_header_length = value
93+
7394

7495
config = _CommitlintConfig()
7596

src/commitlint/linter/validators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from typing import List, Tuple, Type, Union
1010

1111
from .. import console
12-
from ..constants import COMMIT_HEADER_MAX_LENGTH, COMMIT_TYPES
12+
from ..config import config
13+
from ..constants import COMMIT_TYPES
1314
from ..messages import (
1415
COMMIT_TYPE_INVALID_ERROR,
1516
COMMIT_TYPE_MISSING_ERROR,
@@ -71,8 +72,8 @@ def validate(self) -> None:
7172
None
7273
"""
7374
header = self.commit_message.split("\n")[0]
74-
if len(header) > COMMIT_HEADER_MAX_LENGTH:
75-
self.add_error(HEADER_LENGTH_ERROR)
75+
if len(header) > config.max_header_length:
76+
self.add_error(HEADER_LENGTH_ERROR % config.max_header_length)
7677

7778

7879
class SimplePatternValidator(CommitValidator):

src/commitlint/messages.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
INCORRECT_FORMAT_ERROR = (
1111
"Commit message does not follow the Conventional Commits format."
1212
)
13-
HEADER_LENGTH_ERROR = (
14-
f"Header length cannot exceed {COMMIT_HEADER_MAX_LENGTH} characters."
15-
)
13+
HEADER_LENGTH_ERROR = "Header length cannot exceed %d characters."
1614
COMMIT_TYPE_MISSING_ERROR = "Type is missing."
1715
COMMIT_TYPE_INVALID_ERROR = (
1816
f"Invalid type '%s'. Type must be one of: {', '.join(COMMIT_TYPES)}."

tests/fixtures/linter.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,20 @@
5858
# incorrect format check
5959
("feat add new feature", False, [INCORRECT_FORMAT_ERROR]),
6060
# header length check
61-
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
62-
("feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1), False, [HEADER_LENGTH_ERROR]),
61+
(
62+
"feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1),
63+
False,
64+
[HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH],
65+
),
66+
(
67+
"feat: " + "a" * (COMMIT_HEADER_MAX_LENGTH - 1),
68+
False,
69+
[HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH],
70+
),
6371
(
6472
"Test " + "a" * (COMMIT_HEADER_MAX_LENGTH + 1),
6573
False,
66-
[HEADER_LENGTH_ERROR, INCORRECT_FORMAT_ERROR],
74+
[HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH, INCORRECT_FORMAT_ERROR],
6775
),
6876
# commit type check
6977
(": add new feature", False, [COMMIT_TYPE_MISSING_ERROR]),

tests/test_linter/test__linter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test__lint_commit_message__skip_detail_returns_header_length_error_message()
5151
commit_message = "Test " + "a" * (COMMIT_HEADER_MAX_LENGTH + 1)
5252
success, errors = lint_commit_message(commit_message, skip_detail=True)
5353
assert success is False
54-
assert errors == [HEADER_LENGTH_ERROR]
54+
assert errors == [HEADER_LENGTH_ERROR % COMMIT_HEADER_MAX_LENGTH]
5555

5656

5757
def test__lint_commit_message__skip_detail_returns_invalid_format_error_message():

0 commit comments

Comments
 (0)