Skip to content

feat(conventional_commits): add ability to overide settings from tool… #1570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions commitizen/cz/conventional_commits/conventional_commits.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os
from collections.abc import Iterable
from typing import TypedDict

from jinja2 import Template

from commitizen import defaults
from commitizen.config import BaseConfig
from commitizen.cz.base import BaseCommitizen
from commitizen.cz.utils import multiple_line_breaker, required_validator
from commitizen.question import CzQuestion
Expand Down Expand Up @@ -39,8 +43,31 @@ class ConventionalCommitsCz(BaseCommitizen):
}
changelog_pattern = defaults.BUMP_PATTERN

def questions(self) -> list[CzQuestion]:
return [
def __init__(self, config: BaseConfig) -> None:
super().__init__(config)

self.custom_settings: defaults.CzSettings = self.config.settings.get(
"customize", {}
)

if self.custom_settings:
for attr_name in [
"bump_pattern",
"bump_map",
"bump_map_major_version_zero",
"change_type_order",
"commit_parser",
"change_type_map",
]:
if value := self.custom_settings.get(attr_name):
setattr(self, attr_name, value)

self.changelog_pattern = (
self.custom_settings.get("changelog_pattern") or self.bump_pattern
)

def questions(self) -> Iterable[CzQuestion]:
return self.custom_settings.get("questions") or [
{
"type": "list",
"name": "prefix",
Expand Down Expand Up @@ -147,6 +174,9 @@ def questions(self) -> list[CzQuestion]:
]

def message(self, answers: ConventionalCommitsAnswers) -> str: # type: ignore[override]
if _message_template := self.custom_settings.get("message_template"):
return Template(_message_template).render(**answers)

prefix = answers["prefix"]
scope = answers["scope"]
subject = answers["subject"]
Expand All @@ -166,7 +196,7 @@ def message(self, answers: ConventionalCommitsAnswers) -> str: # type: ignore[o
return f"{prefix}{scope}: {subject}{body}{footer}"

def example(self) -> str:
return (
return self.custom_settings.get("example") or (
"fix: correct minor typos in code\n"
"\n"
"see the issue for details on the typos fixed\n"
Expand All @@ -175,7 +205,7 @@ def example(self) -> str:
)

def schema(self) -> str:
return (
return self.custom_settings.get("schema") or (
"<type>(<scope>): <subject>\n"
"<BLANK LINE>\n"
"<body>\n"
Expand All @@ -184,6 +214,8 @@ def schema(self) -> str:
)

def schema_pattern(self) -> str:
if schema_pattern := self.custom_settings.get("schema_pattern"):
return schema_pattern
change_types = (
"build",
"bump",
Expand All @@ -209,6 +241,11 @@ def schema_pattern(self) -> str:
)

def info(self) -> str:
if info_path := self.custom_settings.get("info_path"):
with open(info_path, encoding=self.config.settings["encoding"]) as f:
return f.read()
if info := self.custom_settings.get("info"):
return info
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "conventional_commits_info.txt")
with open(filepath, encoding=self.config.settings["encoding"]) as f:
Expand Down
10 changes: 1 addition & 9 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ We have two different ways to do so.

## 1. Customize in configuration file

The basic steps are:

1. Define your custom committing or bumping rules in the configuration file.
2. Declare `name = "cz_customize"` in your configuration file, or add `-n cz_customize` when running Commitizen.
Define your custom committing or bumping rules in the configuration file, changing behaviour of the default cz_conventional_commits commitizen.

Example:

```toml title="pyproject.toml"
[tool.commitizen]
name = "cz_customize"

[tool.commitizen.customize]
message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example = "feature: this feature enable customize through config file"
Expand Down Expand Up @@ -53,7 +47,6 @@ The equivalent example for a json config file:
```json title=".cz.json"
{
"commitizen": {
"name": "cz_customize",
"customize": {
"message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
"example": "feature: this feature enable customize through config file",
Expand Down Expand Up @@ -108,7 +101,6 @@ And the correspondent example for a yaml file:

```yaml title=".cz.yaml"
commitizen:
name: cz_customize
customize:
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example: 'feature: this feature enable customize through config file'
Expand Down
Loading
Loading