From 78b3a112c5335810ee192b595250a816405842f2 Mon Sep 17 00:00:00 2001 From: Tomas Sebestik <33937168+tomassebestik@users.noreply.github.com> Date: Mon, 31 Jul 2023 12:45:14 +0200 Subject: [PATCH] fix(scope-capitalization): Update scope regex to be consistent with commitlint in DangerJS (#6) --- README.md | 2 +- conventional_precommit_linter/hook.py | 6 +++--- pyproject.toml | 2 +- tests/test_custom_args.py | 6 ++++++ tests/test_default_args.py | 6 ++++++ 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3622ec9..6fc4bda 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ Where: - ``: a descriptor of the performed change, e.g., `feat`, `fix`, `refactor`, etc. Use one of the specified types (either default or provided using the `--types` parameter). -- `` (optional): the scope or component that the commit pertains to. It should start with a lowercase letter. +- `` (optional): the scope or component that the commit pertains to. It should be written in lower case without whitespace, allowed special characters in `scope` are `_` `/` `.` `,` `*` `-` `.` - ``: a short, concise description of the change. It should not end with a period, and be between `subject_min_length` and `subject_max_length` characters long (as specified by script parameters). If the `--summary-uppercase` flag is used, then the summary must start with a uppercase letter. diff --git a/conventional_precommit_linter/hook.py b/conventional_precommit_linter/hook.py index c500afb..9e96366 100644 --- a/conventional_precommit_linter/hook.py +++ b/conventional_precommit_linter/hook.py @@ -9,7 +9,7 @@ ERROR_EMPTY_MESSAGE = 'Commit message seems to be empty.' ERROR_MISSING_COLON = "Missing colon after 'type' or 'scope'. Ensure the commit message has the format '<(scope/component)>: '." # noqa: E501 ERROR_TYPE = "Issue with 'type'. Ensure the type is one of [{}]." -ERROR_SCOPE_CAPITALIZATION = "Issue with 'scope'. Ensure the scope starts with a lowercase letter. Allowed special characters in `scope` are _ / . , * -" # noqa: E501 +ERROR_SCOPE_CAPITALIZATION = "Issue with 'scope'. Ensure the 'scope' is written in lower case without whitespace. Allowed special characters in 'scope' are _ / . , * -" # noqa: E501 ERROR_SUMMARY_LENGTH = "Issue with 'summary'. Ensure the summary is between {} and {} characters long." ERROR_SUMMARY_CAPITALIZATION = "Issue with 'summary'. Ensure the summary starts with an uppercase letter." ERROR_SUMMARY_PERIOD = "Issue with 'summary'. Ensure the summary does not end with a period." @@ -50,7 +50,7 @@ def raise_error(message: str, error: str, types: str, args: argparse.Namespace) commit message rules: - use one of the following types: [{types}] - - 'scope/component' is optional, but if used, it must start with a lowercase letter + - 'scope/component' is optional, but if used, must be written in lower case without whitespace - 'summary' must not end with a period - 'summary' must be between {args.subject_min_length} and {args.subject_max_length} characters long - 'body' is optional, but if used, lines must be no longer than {args.body_max_line_length} characters @@ -122,7 +122,7 @@ def parse_commit_message(args: argparse.Namespace, input_commit_message: str) -> raise_error(message_title, error, types, args) # If 'scope' is provided, check for valid 'scope' - REGEX_SCOPE = r'^[a-z][a-zA-Z0-9_/.,*-]*$' + REGEX_SCOPE = r'^[a-z0-9_/.,*-]*$' if commit_scope and not re.match(REGEX_SCOPE, commit_scope): raise_error(message_title, ERROR_SCOPE_CAPITALIZATION, types, args) diff --git a/pyproject.toml b/pyproject.toml index e8f91ef..adf4b2a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "conventional_precommit_linter" -version = "1.1.0" +version = "1.2.1" description = "A pre-commit hook that checks commit messages for Conventional Commits formatting." readme = "README.md" license = { file = "LICENSE" } diff --git a/tests/test_custom_args.py b/tests/test_custom_args.py index c098674..2709f33 100644 --- a/tests/test_custom_args.py +++ b/tests/test_custom_args.py @@ -114,6 +114,12 @@ False, ERROR_SCOPE_CAPITALIZATION, ), + ( + # Expected FAIL: uppercase in 'scope' + 'fix(dangerGH): Update token permissions - allow Danger to add comments to PR', + False, + ERROR_SCOPE_CAPITALIZATION, + ), ( # Expected FAIL: not allowed 'type' with scope and body 'delete(bt): Added new feature with change\n\nThis feature adds functionality', diff --git a/tests/test_default_args.py b/tests/test_default_args.py index 55a7451..7047ce2 100644 --- a/tests/test_default_args.py +++ b/tests/test_default_args.py @@ -106,6 +106,12 @@ False, ERROR_SCOPE_CAPITALIZATION, ), + ( + # Expected FAIL: uppercase in 'scope' + 'fix(dangerGH): Update token permissions - allow Danger to add comments to PR', + False, + ERROR_SCOPE_CAPITALIZATION, + ), ( # Expected FAIL: not allowed 'type' with scope and body 'delete(bt): Added new feature with change\n\nThis feature adds functionality',