From a5ed888a07b1f375f116ab18a08ecaef42cd7823 Mon Sep 17 00:00:00 2001 From: Amit Levy Date: Wed, 3 Aug 2022 14:56:50 +0300 Subject: [PATCH] feat(ConventionalCommitsCz): allow to override defaults from config Before fixing tests and adding extra ones, I would like to received partial feedback about my changes to see if this aligns with the desired code changes by maintainers. As addressed on #535, when using customize commitizen, if we want to customize a small attribute, we need to redefine all commitizens options to make our custom class work. For example: ```diff diff --git a/cz.yaml b/cz.yaml index 2e847b0..302e961 100644 --- a/cz.yaml +++ b/cz.yaml @@ -1,17 +1,18 @@ commitizen: annotated_tag: true bump_message: 'bump: $current_version -> $new_version [skip ci]' - bump_pattern: '^(fix|feat|docs|style|refactor|test|build|ci)' - bump_map: - fix: PATCH - feat: PATCH - docs: PATCH - style: PATCH - refactor: PATCH - test: PATCH - build: PATCH - ci: PATCH - - name: cz_conventional_commits + name: cz_customize update_changelog_on_bump: true version: 0.11.0 + + customize: + bump_pattern: '^(fix|feat|docs|style|refactor|test|build|ci)' + bump_map: + fix: PATCH + feat: PATCH + docs: PATCH + style: PATCH + refactor: PATCH + test: PATCH + build: PATCH + ci: PATCH ``` making the following change on a repo would cause an unexpected behavior: ```python + bash -c cz commit Traceback (most recent call last): File "/home/amit/.local/bin/cz", line 8, in sys.exit(main()) File "/home/amit/.local/lib/python3.10/site-packages/commitizen/cli.py", line 382, in main args.func(conf, vars(args))() File "/home/amit/.local/lib/python3.10/site-packages/commitizen/commands/commit.py", line 74, in __call__ m = self.prompt_commit_questions() File "/home/amit/.local/lib/python3.10/site-packages/commitizen/commands/commit.py", line 49, in prompt_commit_questions for question in filter(lambda q: q["type"] == "list", questions): File "/home/amit/.local/lib/python3.10/site-packages/commitizen/commands/commit.py", line 49, in for question in filter(lambda q: q["type"] == "list", questions): KeyError: 'type' ``` From my best understanding, this error happens because I didn't defined question section in config, though I'm ok with using ConventionalCommitsCz default ones. This commit extends ConventionalCommitsCz to read from config and fallbacks to defaults if some are not provided. By adding this change, potentially customize commitizen can be deprecated. BREAKING CHANGES: CustomizeCommitsCz was removed. Closes #535. --- commitizen/cz/__init__.py | 2 - commitizen/cz/base.py | 55 +++++++------ .../conventional_commits.py | 58 +++++++------ commitizen/cz/customize/__init__.py | 1 - commitizen/cz/customize/customize.py | 81 ------------------- commitizen/cz/customize/customize_info.txt | 0 commitizen/defaults.py | 22 +++++ commitizen/exceptions.py | 6 -- 8 files changed, 84 insertions(+), 141 deletions(-) delete mode 100644 commitizen/cz/customize/__init__.py delete mode 100644 commitizen/cz/customize/customize.py delete mode 100644 commitizen/cz/customize/customize_info.txt diff --git a/commitizen/cz/__init__.py b/commitizen/cz/__init__.py index e14cb9f7c9..6f70d84829 100644 --- a/commitizen/cz/__init__.py +++ b/commitizen/cz/__init__.py @@ -5,7 +5,6 @@ from commitizen.cz.base import BaseCommitizen from commitizen.cz.conventional_commits import ConventionalCommitsCz -from commitizen.cz.customize import CustomizeCommitsCz from commitizen.cz.jira import JiraSmartCz @@ -33,7 +32,6 @@ def discover_plugins(path: Iterable[str] = None) -> Dict[str, Type[BaseCommitize registry: Dict[str, Type[BaseCommitizen]] = { "cz_conventional_commits": ConventionalCommitsCz, "cz_jira": JiraSmartCz, - "cz_customize": CustomizeCommitsCz, } registry.update(discover_plugins()) diff --git a/commitizen/cz/base.py b/commitizen/cz/base.py index a5abe35f16..ac847a9802 100644 --- a/commitizen/cz/base.py +++ b/commitizen/cz/base.py @@ -1,37 +1,14 @@ from abc import ABCMeta, abstractmethod -from typing import Callable, Dict, List, Optional, Tuple +from typing import Callable, Dict, List, Optional from prompt_toolkit.styles import Style, merge_styles -from commitizen import git +from commitizen import defaults, git from commitizen.config.base_config import BaseConfig from commitizen.defaults import Questions class BaseCommitizen(metaclass=ABCMeta): - bump_pattern: Optional[str] = None - bump_map: Optional[Dict[str, str]] = None - default_style_config: List[Tuple[str, str]] = [ - ("qmark", "fg:#ff9d00 bold"), - ("question", "bold"), - ("answer", "fg:#ff9d00 bold"), - ("pointer", "fg:#ff9d00 bold"), - ("highlighted", "fg:#ff9d00 bold"), - ("selected", "fg:#cc5454"), - ("separator", "fg:#cc5454"), - ("instruction", ""), - ("text", ""), - ("disabled", "fg:#858585 italic"), - ] - - # The whole subject will be parsed as message by default - # This allows supporting changelog for any rule system. - # It can be modified per rule - commit_parser: Optional[str] = r"(?P.*)" - changelog_pattern: Optional[str] = r".*" - change_type_map: Optional[Dict[str, str]] = None - change_type_order: Optional[List[str]] = None - # Executed per message parsed by the commitizen changelog_message_builder_hook: Optional[ Callable[[Dict, git.GitCommit], Dict] @@ -42,8 +19,30 @@ class BaseCommitizen(metaclass=ABCMeta): def __init__(self, config: BaseConfig): self.config = config - if not self.config.settings.get("style"): - self.config.settings.update({"style": BaseCommitizen.default_style_config}) + self.default_style_config: Optional[Dict[str, str]] = self.config.settings.get( + "default_style_config", defaults.default_style_config + ) + self.bump_pattern: Optional[str] = self.config.settings.get( + "bump_pattern", defaults.bump_pattern + ) + self.bump_map: Optional[Dict[str, str]] = self.config.settings.get( + "bump_map", defaults.bump_map + ) + self.change_type_order: Optional[List[str]] = self.config.settings.get( + "change_type_order", defaults.change_type_order + ) + self.change_type_map: Optional[Dict[str, str]] = self.config.settings.get( + "change_type_map", defaults.change_type_map + ) + self.commit_parser: Optional[str] = self.config.settings.get( + "commit_parser", defaults.commit_parser + ) + self.changelog_pattern: Optional[str] = self.config.settings.get( + "changelog_pattern", defaults.changelog_pattern + ) + self.version_parser = self.config.settings.get( + "version_parser", defaults.version_parser + ) @abstractmethod def questions(self) -> Questions: @@ -58,7 +57,7 @@ def style(self): return merge_styles( [ Style(BaseCommitizen.default_style_config), - Style(self.config.settings["style"]), + Style(self.default_style_config), ] ) diff --git a/commitizen/cz/conventional_commits/conventional_commits.py b/commitizen/cz/conventional_commits/conventional_commits.py index 7989a17122..d4a01b1e44 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -1,7 +1,11 @@ import os import re -from commitizen import defaults +try: + from jinja2 import Template +except ImportError: + from string import Template # type: ignore + from commitizen.cz.base import BaseCommitizen from commitizen.cz.utils import multiple_line_breaker, required_validator from commitizen.defaults import Questions @@ -28,18 +32,6 @@ def parse_subject(text): class ConventionalCommitsCz(BaseCommitizen): - bump_pattern = defaults.bump_pattern - bump_map = defaults.bump_map - commit_parser = defaults.commit_parser - version_parser = defaults.version_parser - change_type_map = { - "feat": "Feat", - "fix": "Fix", - "refactor": "Refactor", - "perf": "Perf", - } - changelog_pattern = defaults.bump_pattern - def questions(self) -> Questions: questions: Questions = [ { @@ -148,9 +140,21 @@ def questions(self) -> Questions: ), }, ] - return questions + + # TODO: How would filter functions would be handled from config? + return self.config.settings.get("questions", questions) def message(self, answers: dict) -> str: + custom_message = self.self.config.settings.get("message_template") + if custom_message: + message_template = Template( + self.self.config.settings.get("message_template", "") + ) + if getattr(Template, "substitute", None): + return message_template.substitute(**answers) # type: ignore + else: + return message_template.render(**answers) + prefix = answers["prefix"] scope = answers["scope"] subject = answers["subject"] @@ -172,39 +176,47 @@ def message(self, answers: dict) -> str: return message def example(self) -> str: - return ( + return self.config.settings.get( + "example", "fix: correct minor typos in code\n" "\n" "see the issue for details on the typos fixed\n" "\n" - "closes issue #12" + "closes issue #12", ) def schema(self) -> str: - return ( + return self.config.settings.get( + "schema", "(): \n" "\n" "\n" "\n" - "(BREAKING CHANGE: )