From f49eabaa4730ab58295e7f439891486a7b30eb52 Mon Sep 17 00:00:00 2001 From: Laurent Tramoy Date: Sat, 18 Feb 2023 16:36:43 +0100 Subject: [PATCH 1/2] chore(check): add customizable allowed prefixes The allowed prefixes, which bypass the regex check, can now be configured. --- commitizen/cli.py | 7 ++++++ commitizen/commands/check.py | 20 ++++++++++------- commitizen/defaults.py | 8 +++++++ docs/check.md | 9 ++++++++ docs/config.md | 7 ++++++ tests/commands/test_check_command.py | 33 ++++++++++++++++++++++++++++ tests/test_conf.py | 2 ++ 7 files changed, 78 insertions(+), 8 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index e6d96b656b..f8db6cce8c 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -307,6 +307,13 @@ "default": False, "help": "allow empty commit messages, which typically abort a commit", }, + { + "name": ["--allowed-prefixes"], + "nargs": "*", + "help": "allowed commit message prefixes. " + "If the message starts by one of these prefixes, " + "the message won't be checked against the regex", + }, ], }, { diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index 401cc9cec0..d615867d2a 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -3,7 +3,7 @@ import os import re import sys -from typing import Any +from typing import Any, List from commitizen import factory, git, out from commitizen.config import BaseConfig @@ -32,6 +32,15 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd( arguments.get("allow_abort", config.settings["allow_abort"]) ) + # we need to distinguish between None and [], which is a valid value + + allowed_prefixes = arguments.get("allowed_prefixes") + self.allowed_prefixes: List[str] = ( + allowed_prefixes + if allowed_prefixes is not None + else config.settings["allowed_prefixes"] + ) + self._valid_command_argument() self.config: BaseConfig = config @@ -134,12 +143,7 @@ def _filter_comments(msg: str) -> str: def validate_commit_message(self, commit_msg: str, pattern: str) -> bool: if not commit_msg: return self.allow_abort - if ( - commit_msg.startswith("Merge") - or commit_msg.startswith("Revert") - or commit_msg.startswith("Pull request") - or commit_msg.startswith("fixup!") - or commit_msg.startswith("squash!") - ): + + if any(map(commit_msg.startswith, self.allowed_prefixes)): return True return bool(re.match(pattern, commit_msg)) diff --git a/commitizen/defaults.py b/commitizen/defaults.py index bc0b558148..4abb687e3f 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -40,6 +40,7 @@ class Settings(TypedDict, total=False): tag_format: str | None bump_message: str | None allow_abort: bool + allowed_prefixes: List[str] changelog_file: str changelog_incremental: bool changelog_start_rev: str | None @@ -73,6 +74,13 @@ class Settings(TypedDict, total=False): "tag_format": None, # example v$version "bump_message": None, # bumped v$current_version to $new_version "allow_abort": False, + "allowed_prefixes": [ + "Merge", + "Revert", + "Pull request", + "fixup!", + "squash!", + ], "changelog_file": "CHANGELOG.md", "changelog_incremental": False, "changelog_start_rev": None, diff --git a/docs/check.md b/docs/check.md index ca4f266b72..1cd42c74a9 100644 --- a/docs/check.md +++ b/docs/check.md @@ -64,3 +64,12 @@ cz check --message MESSAGE --allow-abort Empty commit messages typically instruct Git to abort a commit, so you can pass `--allow-abort` to permit them. Since `git commit` accepts an `--allow-empty-message` flag (primarily for wrapper scripts), you may wish to disallow such commits in CI. `--allow-abort` may be used in conjunction with any of the other options. + +### Allowed Prefixes + +If the commit message starts by some specific prefixes, `cz check` returns `True` without checkign the regex. +By default, the the following prefixes are allowed: `Merge`, `Revert`, `Pull Request`, `fixup!` and `squash!`. + +```bash +cz check --message MESSAGE --allowed-prefixes 'Merge' 'Revert' 'Custom Prefix' +``` diff --git a/docs/config.md b/docs/config.md index f93aca60e7..7a8b7d3ece 100644 --- a/docs/config.md +++ b/docs/config.md @@ -82,6 +82,12 @@ Default: `false` Disallow empty commit messages, useful in ci. [Read more][allow_abort] +### `allowed_prefixes` + +Type: `list` +Default: `[ "Merge", "Revert", "Pull request", "fixup!", "squash!"]` +Allow some prefixes and do not try to match the regex when checking the message [Read more][allowed_prefixes] + ### `changelog_file` Type: `str` @@ -346,6 +352,7 @@ setup( [version_type]: bump.md#version_type [pre_bump_hooks]: bump.md#pre_bump_hooks [post_bump_hooks]: bump.md#post_bump_hooks +[allowed_prefixes]: check.md#allowed-prefixes [additional-features]: https://github.com/tmbo/questionary#additional-features [customization]: customization.md [shortcuts]: customization.md#shortcut-keys diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index 69fa33df20..e328f8d346 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -312,6 +312,39 @@ def test_check_command_override_allow_abort_config(config, mocker: MockFixture): error_mock.assert_called_once() +def test_check_command_with_allowed_prefixes_arg(config, mocker: MockFixture): + success_mock = mocker.patch("commitizen.out.success") + check_cmd = commands.Check( + config=config, + arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]}, + ) + + check_cmd() + success_mock.assert_called_once() + + +def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture): + success_mock = mocker.patch("commitizen.out.success") + config.settings["allowed_prefixes"] = ["custom!"] + check_cmd = commands.Check(config=config, arguments={"message": "custom! test"}) + + check_cmd() + success_mock.assert_called_once() + + +def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture): + error_mock = mocker.patch("commitizen.out.error") + config.settings["allow_abort"] = ["fixup!"] + check_cmd = commands.Check( + config=config, + arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]}, + ) + + with pytest.raises(InvalidCommitMessageError): + check_cmd() + error_mock.assert_called_once() + + def test_check_command_with_pipe_message(mocker: MockFixture, capsys): testargs = ["cz", "check"] mocker.patch.object(sys, "argv", testargs) diff --git a/tests/test_conf.py b/tests/test_conf.py index 4226096371..c55cef7d06 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -48,6 +48,7 @@ "tag_format": None, "bump_message": None, "allow_abort": False, + "allowed_prefixes": ["Merge", "Revert", "Pull request", "fixup!", "squash!"], "version_files": ["commitizen/__version__.py", "pyproject.toml"], "style": [["pointer", "reverse"], ["question", "underline"]], "changelog_file": "CHANGELOG.md", @@ -70,6 +71,7 @@ "tag_format": None, "bump_message": None, "allow_abort": False, + "allowed_prefixes": ["Merge", "Revert", "Pull request", "fixup!", "squash!"], "version_files": ["commitizen/__version__.py", "pyproject.toml"], "style": [["pointer", "reverse"], ["question", "underline"]], "changelog_file": "CHANGELOG.md", From c77dcc5cbbe1dd77e1c858b48ad9485b66a11593 Mon Sep 17 00:00:00 2001 From: Laurent Tramoy Date: Thu, 8 Jun 2023 10:26:43 +0200 Subject: [PATCH 2/2] fix(typing): no_raise is declared as optional --- commitizen/cli.py | 4 ++-- commitizen/defaults.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index f8db6cce8c..d0076fe497 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -3,8 +3,8 @@ import argparse import logging import sys -from pathlib import Path from functools import partial +from pathlib import Path from types import TracebackType import argcomplete @@ -361,7 +361,7 @@ def commitizen_excepthook( - type, value, traceback, debug=False, no_raise: list[int] = None + type, value, traceback, debug=False, no_raise: list[int] | None = None ): traceback = traceback if isinstance(traceback, TracebackType) else None if not no_raise: diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 4abb687e3f..3c6cd10971 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -40,7 +40,7 @@ class Settings(TypedDict, total=False): tag_format: str | None bump_message: str | None allow_abort: bool - allowed_prefixes: List[str] + allowed_prefixes: list[str] changelog_file: str changelog_incremental: bool changelog_start_rev: str | None