Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix/major version zero bump map #726

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

Merged
woile merged 2 commits into master from fix/major-version-zero-bump-map
Apr 25, 2023
Merged
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
15 changes: 10 additions & 5 deletions commitizen/commands/bump.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import questionary
from packaging.version import InvalidVersion, Version

from commitizen import bump, cmd, defaults, factory, git, hooks, out, version_types
from commitizen import bump, cmd, factory, git, hooks, out, version_types
from commitizen.commands.changelog import Changelog
from commitizen.config import BaseConfig
from commitizen.exceptions import (
Expand Down Expand Up @@ -86,8 +86,16 @@ def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool
return is_initial

def find_increment(self, commits: List[git.GitCommit]) -> Optional[str]:
# Update the bump map to ensure major version doesn't increment.
is_major_version_zero: bool = self.bump_settings["major_version_zero"]
# self.cz.bump_map = defaults.bump_map_major_version_zero
bump_map = (
self.cz.bump_map_major_version_zero
if is_major_version_zero
else self.cz.bump_map
)
bump_pattern = self.cz.bump_pattern
bump_map = self.cz.bump_map

if not bump_map or not bump_pattern:
raise NoPatternMapError(
f"'{self.config.settings['name']}' rule does not support bump"
Expand Down Expand Up @@ -153,9 +161,6 @@ def __call__(self): # noqa: C901
f"--major-version-zero is meaningless for current version {current_version}"
)

# Update the bump map to ensure major version doesn't increment.
self.cz.bump_map = defaults.bump_map_major_version_zero

current_tag_version: str = bump.normalize_tag(
current_version,
tag_format=tag_format,
Expand Down
1 change: 1 addition & 0 deletions commitizen/cz/base.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class BaseCommitizen(metaclass=ABCMeta):
bump_pattern: Optional[str] = None
bump_map: Optional[Dict[str, str]] = None
bump_map_major_version_zero: Optional[Dict[str, str]] = None
default_style_config: List[Tuple[str, str]] = [
("qmark", "fg:#ff9d00 bold"),
("question", "bold"),
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def parse_subject(text):
class ConventionalCommitsCz(BaseCommitizen):
bump_pattern = defaults.bump_pattern
bump_map = defaults.bump_map
bump_map_major_version_zero = defaults.bump_map_major_version_zero
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not actually a default for every cz, should we move it out of defaults? I've been thinking for moving these kind of stuff from defaults for a long time but not yet have a chance to do so 🤦‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, many (not all) of those "defaults" are actually part of the conventional commit cz and we are probably creating confusion by having them there

Lee-W reacted with thumbs up emoji
commit_parser = defaults.commit_parser
version_parser = defaults.version_parser
change_type_map = {
Expand Down
7 changes: 7 additions & 0 deletions commitizen/cz/customize/customize.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class CustomizeCommitsCz(BaseCommitizen):
bump_pattern = defaults.bump_pattern
bump_map = defaults.bump_map
bump_map_major_version_zero = defaults.bump_map_major_version_zero
change_type_order = defaults.change_type_order

def __init__(self, config: BaseConfig):
Expand All @@ -34,6 +35,12 @@ def __init__(self, config: BaseConfig):
if custom_bump_map:
self.bump_map = custom_bump_map

custom_bump_map_major_version_zero = self.custom_settings.get(
"bump_map_major_version_zero"
)
if custom_bump_map_major_version_zero:
self.bump_map_major_version_zero = custom_bump_map_major_version_zero

custom_change_type_order = self.custom_settings.get("change_type_order")
if custom_change_type_order:
self.change_type_order = custom_change_type_order
Expand Down
1 change: 1 addition & 0 deletions commitizen/defaults.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class CzSettings(TypedDict, total=False):
bump_pattern: str
bump_map: "OrderedDict[str, str]"
bump_map_major_version_zero: "OrderedDict[str, str]"
change_type_order: List[str]

questions: Questions
Expand Down
44 changes: 44 additions & 0 deletions tests/commands/test_bump_command.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -980,3 +980,47 @@ def test_bump_command_prelease_version_type_check_old_tags(
for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
assert "0.2.0" in f.read()


@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.usefixtures("use_cz_semver")
@pytest.mark.parametrize(
"message, expected_tag",
[
("minor: add users", "0.2.0"),
("patch: bug affecting users", "0.1.1"),
("major: bug affecting users", "1.0.0"),
],
)
def test_bump_with_plugin(mocker: MockFixture, message: str, expected_tag: str):
create_file_and_commit(message)

testargs = ["cz", "--name", "cz_semver", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

tag_exists = git.tag_exist(expected_tag)
assert tag_exists is True


@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.usefixtures("use_cz_semver")
@pytest.mark.parametrize(
"message, expected_tag",
[
("minor: add users", "0.2.0"),
("patch: bug affecting users", "0.1.1"),
("major: bug affecting users", "0.2.0"),
],
)
def test_bump_with_major_version_zero_with_plugin(
mocker: MockFixture, message: str, expected_tag: str
):
create_file_and_commit(message)

testargs = ["cz", "--name", "cz_semver", "bump", "--yes", "--major-version-zero"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

tag_exists = git.tag_exist(expected_tag)
assert tag_exists is True
77 changes: 77 additions & 0 deletions tests/conftest.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from commitizen import cmd, defaults
from commitizen.config import BaseConfig
from commitizen.cz.base import BaseCommitizen
from commitizen.cz import registry
from tests.utils import create_file_and_commit

SIGNER = "GitHub Action"
Expand Down Expand Up @@ -122,3 +124,78 @@ def config():
@pytest.fixture()
def config_path() -> str:
return os.path.join(os.getcwd(), "pyproject.toml")


class SemverCommitizen(BaseCommitizen):
"""A minimal cz rules used to test changelog and bump.

Samples:
```
minor(users): add email to user
major: removed user profile
patch(deps): updated dependency for security
```
"""

bump_pattern = r"^(patch|minor|major)"
bump_map = {
"major": "MAJOR",
"minor": "MINOR",
"patch": "PATCH",
}
bump_map_major_version_zero = {
"major": "MINOR",
"minor": "MINOR",
"patch": "PATCH",
}
changelog_pattern = r"^(patch|minor|major)"
commit_parser = r"^(?P<change_type>patch|minor|major)(?:\((?P<scope>[^()\r\n]*)\)|\()?:?\s(?P<message>.+)" # noqa
change_type_map = {
"major": "Breaking Changes",
"minor": "Features",
"patch": "Bugs",
}

def questions(self) -> list:
return [
{
"type": "list",
"name": "prefix",
"message": "Select the type of change you are committing",
"choices": [
{
"value": "patch",
"name": "patch: a bug fix",
"key": "p",
},
{
"value": "minor",
"name": "minor: a new feature, non-breaking",
"key": "m",
},
{
"value": "major",
"name": "major: a breaking change",
"key": "b",
},
],
},
{
"type": "input",
"name": "subject",
"message": (
"Write a short and imperative summary of the code changes: (lower case and no period)\n"
),
},
]

def message(self, answers: dict) -> str:
prefix = answers["prefix"]
subject = answers.get("subject", "default message").trim()
return f"{prefix}: {subject}"


@pytest.fixture()
def use_cz_semver(mocker):
new_cz = {**registry, "cz_semver": SemverCommitizen}
mocker.patch.dict("commitizen.cz.registry", new_cz)

AltStyle によって変換されたページ (->オリジナル) /