Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inputs are not loaded directly to commitlint from github actions. Here are the steps:

  1. Add input in the inputs section of action.yml. (Already done)

  2. Load it as an env variable, at the end of the file at "Commitlint Action" step.

  3. Use the env variable at github_actions/action/run.py to add it as an argument. (you can take reference from INPUT_VERBOSE).

  4. Load the args to the config on src/commitlint/cli.py::main (reference config.verbose = args.verbose). Make sure to handle the optional case 🙂.

  5. Update the README.md regarding the GitHub Actions input.

description: Maximum length for commit message header.
default: "72"
required: false
token:
description: Token for fetching commits using Github API.
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/commitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/commitlint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from typing import Optional

from .constants import COMMIT_HEADER_MAX_LENGTH


class _CommitlintConfig:
"""
Expand All @@ -14,6 +16,7 @@ class _CommitlintConfig:

_verbose: bool = False
_quiet: bool = False
_max_header_length: int = COMMIT_HEADER_MAX_LENGTH

def __new__(cls) -> "_CommitlintConfig":
"""
Expand Down Expand Up @@ -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()

Expand Down
7 changes: 4 additions & 3 deletions src/commitlint/linter/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions src/commitlint/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This module provides constant messages used in the application for various scenarios.
"""

from .constants import COMMIT_HEADER_MAX_LENGTH, COMMIT_TYPES

Check failure on line 5 in src/commitlint/messages.py

View workflow job for this annotation

GitHub Actions / build (3.10)

Ruff (F401)

src/commitlint/messages.py:5:24: F401 `.constants.COMMIT_HEADER_MAX_LENGTH` imported but unused

VALIDATION_SUCCESSFUL = "Commit validation: successful!"
VALIDATION_FAILED = "Commit validation: failed!"
Expand All @@ -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)}."
Expand Down
14 changes: 11 additions & 3 deletions tests/fixtures/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_linter/test__linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading