diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index b9597f322c..6a57fdb867 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -37,7 +37,9 @@ def __init__(self, config: BaseConfig, arguments: dict): }, } self.cz = factory.commiter_factory(self.config) - self.changelog = arguments["changelog"] + self.changelog = arguments["changelog"] or self.config.settings.get( + "update_changelog_on_bump" + ) self.no_verify = arguments["no_verify"] self.check_consistency = arguments["check_consistency"] diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 97854453ea..4f63c99d91 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -13,6 +13,7 @@ "changelog_file": "CHANGELOG.md", "changelog_incremental": False, "changelog_start_rev": None, + "update_changelog_on_bump": False, } MAJOR = "MAJOR" diff --git a/docs/bump.md b/docs/bump.md index 6fb3618b8e..3ff2d6a286 100644 --- a/docs/bump.md +++ b/docs/bump.md @@ -216,6 +216,19 @@ Some examples bump_message = "release $current_version → $new_version [skip-ci]" ``` +--- + +### `update_changelog_on_bump` + +When set to `true` the changelog is always updated incrementally when running `cz bump`, so the user does not have to provide the `--changelog` flag every time. + +defaults to: `false` + +```toml +[tool.commitizen] +update_changelog_on_bump = true +``` + ## Custom bump Read the [customizing section](./customization.md). diff --git a/tests/commands/conftest.py b/tests/commands/conftest.py index cadf369f06..25bab04464 100644 --- a/tests/commands/conftest.py +++ b/tests/commands/conftest.py @@ -1,3 +1,5 @@ +import os + import pytest from commitizen import defaults @@ -9,3 +11,13 @@ def config(): _config = BaseConfig() _config.settings.update({"name": defaults.name}) return _config + + +@pytest.fixture() +def changelog_path() -> str: + return os.path.join(os.getcwd(), "CHANGELOG.md") + + +@pytest.fixture() +def config_path() -> str: + return os.path.join(os.getcwd(), "pyproject.toml") diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 73982a336d..ce19c136bc 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -301,3 +301,36 @@ def test_none_increment_should_not_call_git_tag(mocker, tmp_commitizen_project): # restore pop stashed git.tag = stashed_git_tag + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_with_changelog_arg(mocker, changelog_path): + create_file_and_commit("feat(user): new file") + testargs = ["cz", "bump", "--yes", "--changelog"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + tag_exists = git.tag_exist("0.2.0") + assert tag_exists is True + + with open(changelog_path, "r") as f: + out = f.read() + assert out.startswith("#") + assert "0.2.0" in out + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_bump_with_changelog_config(mocker, changelog_path, config_path): + create_file_and_commit("feat(user): new file") + with open(config_path, "a") as fp: + fp.write("update_changelog_on_bump = true\n") + + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + tag_exists = git.tag_exist("0.2.0") + assert tag_exists is True + + with open(changelog_path, "r") as f: + out = f.read() + assert out.startswith("#") + assert "0.2.0" in out diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index d52d157c78..aceaaf9d96 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1,4 +1,3 @@ -import os import sys from datetime import date @@ -15,16 +14,6 @@ from tests.utils import create_file_and_commit -@pytest.fixture() -def changelog_path() -> str: - return os.path.join(os.getcwd(), "CHANGELOG.md") - - -@pytest.fixture() -def config_path() -> str: - return os.path.join(os.getcwd(), "pyproject.toml") - - @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_on_empty_project(mocker): testargs = ["cz", "changelog", "--dry-run"] diff --git a/tests/test_conf.py b/tests/test_conf.py index ba621bd635..919fb46e78 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -34,6 +34,7 @@ "changelog_file": "CHANGELOG.md", "changelog_incremental": False, "changelog_start_rev": None, + "update_changelog_on_bump": False, } _new_settings = { @@ -46,6 +47,7 @@ "changelog_file": "CHANGELOG.md", "changelog_incremental": False, "changelog_start_rev": None, + "update_changelog_on_bump": False, } _read_settings = {