From 760d6bc6455de7538ad7e0d4fb288f7f588ede60 Mon Sep 17 00:00:00 2001 From: matteo_fiore Date: Tue, 5 Oct 2021 16:08:45 +0200 Subject: [PATCH 1/5] feat(customize.py): adding support for commit_parser, changelog_pattern, change_type_map --- commitizen/cz/customize/customize.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/commitizen/cz/customize/customize.py b/commitizen/cz/customize/customize.py index 7a6f03a825..994c21c714 100644 --- a/commitizen/cz/customize/customize.py +++ b/commitizen/cz/customize/customize.py @@ -38,6 +38,18 @@ def __init__(self, config: BaseConfig): if custom_change_type_order: self.change_type_order = custom_change_type_order + commit_parser = self.custom_settings.get("commit_parser") + if commit_parser: + self.commit_parser = commit_parser + + changelog_pattern = self.custom_settings.get("changelog_pattern") + if changelog_pattern: + self.changelog_pattern = changelog_pattern + + change_type_map = self.custom_settings.get("change_type_map") + if change_type_map: + self.change_type_map = change_type_map + def questions(self) -> Questions: return self.custom_settings.get("questions", [{}]) From d7127cd0ff033eb69cf5f93e516a6018612b8b48 Mon Sep 17 00:00:00 2001 From: matteo_fiore Date: Tue, 5 Oct 2021 16:11:23 +0200 Subject: [PATCH 2/5] test(test_cz_customize): adding tests for commit_parser, changelog_pattern, change_type_map --- tests/test_cz_customize.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_cz_customize.py b/tests/test_cz_customize.py index e919093e04..84e463cf96 100644 --- a/tests/test_cz_customize.py +++ b/tests/test_cz_customize.py @@ -10,6 +10,9 @@ example = "feature: this feature enable customize through config file" schema = ": " schema_pattern = "(feature|bug fix):(\\s.*)" + commit_parser = "^(?Pfeature|bug fix):\\s(?P.*)?" + changelog_pattern = "^(feature|bug fix)?(!)?" + change_type_map = {"feature" = "Feat", "bug fix" = "Fix"} bump_pattern = "^(break|new|fix|hotfix)" bump_map = {"break" = "MAJOR", "new" = "MINOR", "fix" = "PATCH", "hotfix" = "PATCH"} @@ -57,6 +60,9 @@ "fix": "PATCH", "hotfix": "PATCH" }, + "commit_parser": "^(?Pfeature|bug fix):\\s(?P.*)?", + "changelog_pattern": "^(feature|bug fix)?(!)?", + "change_type_map": {"feature": "Feat", "bug fix": "Fix"}, "change_type_order": ["perf", "BREAKING CHANGE", "feat", "fix", "refactor"], "info": "This is a customized cz.", "questions": [ @@ -363,3 +369,18 @@ def test_info_with_info_path(tmpdir, config_info): def test_info_without_info(config_without_info): cz = CustomizeCommitsCz(config_without_info) assert cz.info() is None + + +def test_commit_parser(config): + cz = CustomizeCommitsCz(config) + assert cz.commit_parser == "^(?Pfeature|bug fix):\\s(?P.*)?" + + +def test_changelog_pattern(config): + cz = CustomizeCommitsCz(config) + assert cz.changelog_pattern == "^(feature|bug fix)?(!)?" + + +def test_change_type_map(config): + cz = CustomizeCommitsCz(config) + assert cz.change_type_map == {"feature": "Feat", "bug fix": "Fix"} From cc70eb6e0958f50b1ccc8beb7efc226b5ad12f63 Mon Sep 17 00:00:00 2001 From: matteo_fiore Date: Tue, 5 Oct 2021 16:13:12 +0200 Subject: [PATCH 3/5] docs(customization): adding information on commit_parser, changelog_pattern, change_type_map customizable fields --- docs/customization.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/customization.md b/docs/customization.md index b165638e7c..92610f4585 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -26,6 +26,9 @@ info_path = "cz_customize_info.txt" info = """ This is customized info """ +commit_parser = "^(?Pfeature|bug fix):\\s(?P.*)?" +changelog_pattern = "^(feature|bug fix)?(!)?" +change_type_map = {"feature" = "Feat", "bug fix" = "Fix"} [[tool.commitizen.customize.questions]] type = "list" @@ -66,6 +69,9 @@ The equivalent example for a json config file: "change_type_order": ["BREAKING CHANGE", "feat", "fix", "refactor", "perf"], "info_path": "cz_customize_info.txt", "info": "This is customized info", + "commit_parser": "^(?Pfeature|bug fix):\\s(?P.*)?", + "changelog_pattern": "^(feature|bug fix)?(!)?", + "change_type_map": {"feature": "Feat", "bug fix": "Fix"}, "questions": [ { "type": "list", @@ -109,6 +115,11 @@ commitizen: schema: ": " schema_pattern: "(feature|bug fix):(\\s.*)" bump_pattern: "^(break|new|fix|hotfix)" + commit_parser: "^(?Pfeature|bug fix):\\s(?P.*)?", + changelog_pattern: "^(feature|bug fix)?(!)?", + change_type_map: + feature: Feat + bug fix: Fix bump_map: break: MAJOR new: MINOR @@ -148,8 +159,12 @@ commitizen: | `bump_map` | `dict` | `None` | (OPTIONAL) Dictionary mapping the extracted information to a `SemVer` increment type (`MAJOR`, `MINOR`, `PATCH`) | | `bump_pattern` | `str` | `None` | (OPTIONAL) Regex to extract information from commit (subject and body) | | `change_type_order` | `str` | `None` | (OPTIONAL) List of strings used to order the Changelog. All other types will be sorted alphabetically. Default is `["BREAKING CHANGE", "feat", "fix", "refactor", "perf"]` | +| `commit_parser` | `str` | `None` | (OPTIONAL) Regex to extract information used in creating changelog. [See more][changelog-spec] | +| `changelog_pattern` | `str` | `None` | (OPTIONAL) Regex to understand which commits to include in the changelog | +| `change_type_map` | `dict` | `None` | (OPTIONAL) Dictionary mapping the type of the commit to a changelog entry | [jinja2]: https://jinja.palletsprojects.com/en/2.10.x/ +[changelog-spec]: https://commitizen-tools.github.io/commitizen/changelog/ #### Detailed `questions` content From 437d8d957941bd54844445b96022bbf2af308359 Mon Sep 17 00:00:00 2001 From: matteo_fiore Date: Mon, 11 Oct 2021 10:24:38 +0200 Subject: [PATCH 4/5] test(test_changelog_command): adding test for changelog generated with customize commits --- commitizen/defaults.py | 5 +++- tests/commands/conftest.py | 32 +++++++++++++++++++++++- tests/commands/test_changelog_command.py | 22 ++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 3fa7c5256d..0b217bf7f8 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -1,6 +1,6 @@ import pathlib from collections import OrderedDict -from typing import Any, Iterable, List, MutableMapping, Optional, Tuple, Union +from typing import Any, Dict, Iterable, List, MutableMapping, Optional, Tuple, Union from typing_extensions import TypedDict @@ -20,6 +20,9 @@ class CzSettings(TypedDict, total=False): info_path: Union[str, pathlib.Path] info: str message_template: str + commit_parser: Optional[str] + changelog_pattern: Optional[str] + change_type_map: Optional[Dict[str, str]] class Settings(TypedDict, total=False): diff --git a/tests/commands/conftest.py b/tests/commands/conftest.py index 3447b97422..91931849b2 100644 --- a/tests/commands/conftest.py +++ b/tests/commands/conftest.py @@ -3,7 +3,7 @@ import pytest from commitizen import defaults -from commitizen.config import BaseConfig +from commitizen.config import BaseConfig, JsonConfig @pytest.fixture() @@ -13,6 +13,36 @@ def config(): return _config +@pytest.fixture() +def config_customize(): + json_string = r"""{ + "commitizen": { + "name": "cz_customize", + "version": "3.0.0", + "changelog_incremental": "true", + "customize": { + "message_template": "{{prefix}}({{scope}}): {{subject}}\n\n{{body}}{% if is_breaking_change %}\nBREAKING CHANGE: {{footer}}{% endif %}", + "schema": "(): \n\n\n\n(BREAKING CHANGE: