Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Feature/changelog stdout #359

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
merged 3 commits into from
Mar 8, 2021
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
6 changes: 6 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@
"help": "create annotated tag instead of lightweight one",
"action": "store_true",
},
{
"name": ["--changelog-to-stdout"],
"action": "store_true",
"default": False,
"help": "Output changelog to the stdout",
},
],
},
{
Expand Down
36 changes: 34 additions & 2 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, config: BaseConfig, arguments: dict):
self.changelog = arguments["changelog"] or self.config.settings.get(
"update_changelog_on_bump"
)
self.changelog_to_stdout = arguments["changelog_to_stdout"]
self.no_verify = arguments["no_verify"]
self.check_consistency = arguments["check_consistency"]

Expand Down Expand Up @@ -110,6 +111,11 @@ def __call__(self): # noqa: C901
else:
commits = git.get_commits(current_tag_version)

# If user specified changelog_to_stdout, they probably want the
# changelog to be generated as well, this is the most intuitive solution
if not self.changelog and self.changelog_to_stdout:
self.changelog = True

# No commits, there is no need to create an empty tag.
# Unless we previously had a prerelease.
if not commits and not current_version_instance.is_prerelease:
Expand Down Expand Up @@ -149,12 +155,20 @@ def __call__(self): # noqa: C901
)

# Report found information
out.write(
information = (
f"{message}\n"
f"tag to create: {new_tag_version}\n"
f"increment detected: {increment}\n"
)

if self.changelog_to_stdout:
# When the changelog goes to stdout, we want to send
# the bump information to stderr, this way the
# changelog output can be captured
out.diagnostic(information)
else:
out.write(information)

if increment is None and new_tag_version == current_tag_version:
raise NoneIncrementExit()

Expand All @@ -170,6 +184,19 @@ def __call__(self): # noqa: C901
)

if self.changelog:
if self.changelog_to_stdout:
changelog_cmd = Changelog(
self.config,
{
"unreleased_version": new_tag_version,
"incremental": True,
"dry_run": True,
},
)
try:
changelog_cmd()
except DryRunExit:
pass
changelog_cmd = Changelog(
self.config,
{
Expand All @@ -196,7 +223,12 @@ def __call__(self): # noqa: C901
)
if c.return_code != 0:
raise BumpTagFailedError(c.err)
out.success("Done!")

# TODO: For v3 output this only as diagnostic and remove this if
if self.changelog_to_stdout:
out.diagnostic("Done!")
else:
out.success("Done!")

def _get_commit_args(self):
commit_args = ["-a"]
Expand Down
4 changes: 4 additions & 0 deletions commitizen/out.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ def success(value: str):
def info(value: str):
message = colored(value, "blue")
line(message)


def diagnostic(value: str):
line(value, file=sys.stderr)
22 changes: 20 additions & 2 deletions docs/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ However, it will still update `pyproject.toml` and `src/__version__.py`.
To fix it, you'll first `git checkout .` to reset to the status before trying to bump and update
the version in `setup.py` to `1.21.0`


### `--local-version`

Bump the local portion of the version.
Expand All @@ -161,6 +160,25 @@ If `--local-version` is used, it will bump only the local version `0.1.0` and ke

If `--annotated-tag` is used, commitizen will create annotated tags. Also available via configuration, in `pyproject.toml` or `.cz.toml`.

### `--changelog-to-stdout`

If `--changelog-to-stdout` is used, the incremental changelog generated by the bump
will be sent to the stdout, and any other message generated by the bump will be
sent to stderr.

If `--changelog` is not used with this command, it is still smart enough to
understand that the user wants to create a changelog. It is recommened to be
explicit and use `--changelog` (or the setting `update_changelog_on_bump`).

This command is useful to "transport" the newly created changelog.
It can be sent to an auditing system, or to create a Github Release.

Example:

```bash
cz bump --changelog --changelog-to-stdout > body.md
```

## Configuration

### `tag_format`
Expand Down Expand Up @@ -198,7 +216,7 @@ Supported variables:

---

### `version_files` *
### `version_files` \*

It is used to identify the files which should be updated with the new version.
It is also possible to provide a pattern for each file, separated by colons (`:`).
Expand Down
18 changes: 18 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,21 @@ def test_prevent_prerelease_when_no_increment_detected(
"[NO_COMMITS_FOUND]\n" "No commits found to generate a pre-release."
)
assert expected_error_message in str(excinfo.value)


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_with_changelog_to_stdout_arg(mocker, capsys, changelog_path):
create_file_and_commit("feat(user): this should appear in stdout")
testargs = ["cz", "bump", "--yes", "--changelog-to-stdout"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
out, _ = capsys.readouterr()

assert "this should appear in stdout" in out
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
16 changes: 12 additions & 4 deletions tests/commands/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ def test_no_existing_pre_commit_conifg(_, default_choice, tmpdir, config):
if "json" in default_choice:
assert json.load(file) == EXPECTED_DICT_CONFIG
elif "yaml" in default_choice:
assert yaml.load(file) == EXPECTED_DICT_CONFIG
assert (
yaml.load(file, Loader=yaml.FullLoader) == EXPECTED_DICT_CONFIG
)
else:
config_data = file.read()
assert config_data == expected_config
Expand All @@ -136,7 +138,9 @@ def test_empty_pre_commit_config(_, default_choice, tmpdir, config):
if "json" in default_choice:
assert json.load(file) == EXPECTED_DICT_CONFIG
elif "yaml" in default_choice:
assert yaml.load(file) == EXPECTED_DICT_CONFIG
assert (
yaml.load(file, Loader=yaml.FullLoader) == EXPECTED_DICT_CONFIG
)
else:
config_data = file.read()
assert config_data == expected_config
Expand All @@ -162,7 +166,9 @@ def test_pre_commit_config_without_cz_hook(_, default_choice, tmpdir, config):
if "json" in default_choice:
assert json.load(file) == EXPECTED_DICT_CONFIG
elif "yaml" in default_choice:
assert yaml.load(file) == EXPECTED_DICT_CONFIG
assert (
yaml.load(file, Loader=yaml.FullLoader) == EXPECTED_DICT_CONFIG
)
else:
config_data = file.read()
assert config_data == expected_config
Expand All @@ -184,7 +190,9 @@ def test_cz_hook_exists_in_pre_commit_config(_, default_choice, tmpdir, config):
if "json" in default_choice:
assert json.load(file) == EXPECTED_DICT_CONFIG
elif "yaml" in default_choice:
assert yaml.load(file) == EXPECTED_DICT_CONFIG
assert (
yaml.load(file, Loader=yaml.FullLoader) == EXPECTED_DICT_CONFIG
)
else:
config_data = file.read()
assert config_data == expected_config
Expand Down