Skip to content

Commit 746dd87

Browse files
committed
feat(cz/cz_customize): implement customizable cz
#54
1 parent df1d2d7 commit 746dd87

6 files changed

Lines changed: 102 additions & 2 deletions

File tree

commitizen/cz/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33

44
from commitizen.cz.conventional_commits import ConventionalCommitsCz
55
from commitizen.cz.jira import JiraSmartCz
6+
from commitizen.cz.customize import CustomizeCommitsCz
67

7-
registry = {"cz_conventional_commits": ConventionalCommitsCz, "cz_jira": JiraSmartCz}
8+
registry = {
9+
"cz_conventional_commits": ConventionalCommitsCz,
10+
"cz_jira": JiraSmartCz,
11+
"cz_customize": CustomizeCommitsCz,
12+
}
813
plugins = {
914
name: importlib.import_module(name).discover_this
1015
for finder, name, ispkg in pkgutil.iter_modules()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .customize import CustomizeCommitsCz # noqa
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from commitizen import defaults
2+
from commitizen.cz.base import BaseCommitizen
3+
4+
__all__ = ["CustomizeCommitsCz"]
5+
6+
7+
class CustomizeCommitsCz(BaseCommitizen):
8+
bump_pattern = defaults.bump_pattern
9+
bump_map = defaults.bump_map
10+
11+
def __init__(self, config: dict):
12+
super(CustomizeCommitsCz, self).__init__(config)
13+
self.custom_config = self.config.get("customize")
14+
15+
def questions(self) -> list:
16+
return self.custom_config.get("questions")
17+
18+
def message(self, answers: dict) -> str:
19+
message_template = self.custom_config.get("message_template")
20+
return message_template.format(**answers)
21+
22+
def example(self) -> str:
23+
return self.custom_config.get("example")
24+
25+
def schema(self) -> str:
26+
return self.custom_config.get("schema")
27+
28+
def info(self) -> str:
29+
# TODO
30+
raise NotImplementedError("Not Implemented yet")

commitizen/cz/customize/customize_info.txt

Whitespace-only changes.

scripts/test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export PREFIX="poetry run python -m "
21
if [ -d 'venv' ] ; then
32
export PREFIX="venv/bin/"
43
fi

tests/test_cz_customize.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import pytest
2+
from tomlkit import parse
3+
4+
from commitizen.cz.customize import CustomizeCommitsCz
5+
from commitizen.config import Config
6+
7+
8+
@pytest.fixture(scope="module")
9+
def config():
10+
_conf = Config()
11+
toml_str = """
12+
[tool.commitizen.customize]
13+
# message_template should follow the python string formatting spec
14+
message_template = "{change_type}: {message}"
15+
example = "feature: this feature eanable customize through config file"
16+
schema = "<type>: <body>"
17+
18+
[[tool.commitizen.customize.questions]]
19+
type = "list"
20+
name = "change_type"
21+
choices = ["feature", "bug fix"]
22+
message = "Select the type of change you are committing"
23+
24+
[[tool.commitizen.customize.questions]]
25+
type = "input"
26+
name = "message"
27+
message = "Body."
28+
"""
29+
_conf.update(parse(toml_str)["tool"]["commitizen"])
30+
return _conf.config
31+
32+
33+
def test_questions(config):
34+
cz = CustomizeCommitsCz(config)
35+
questions = cz.questions()
36+
expected_questions = [
37+
{
38+
"type": "list",
39+
"name": "change_type",
40+
"choices": ["feature", "bug fix"],
41+
"message": "Select the type of change you are committing",
42+
},
43+
{"type": "input", "name": "message", "message": "Body."},
44+
]
45+
assert list(questions) == expected_questions
46+
47+
48+
def test_answer(config):
49+
cz = CustomizeCommitsCz(config)
50+
answers = {
51+
"change_type": "feature",
52+
"message": "this feature eanable customize through config file",
53+
}
54+
message = cz.message(answers)
55+
assert message == "feature: this feature eanable customize through config file"
56+
57+
58+
def test_example(config):
59+
cz = CustomizeCommitsCz(config)
60+
assert "feature: this feature eanable customize through config file" in cz.example()
61+
62+
63+
def test_schema(config):
64+
cz = CustomizeCommitsCz(config)
65+
assert "<type>: <body>" in cz.schema()

0 commit comments

Comments
 (0)