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

Add changelog argument options #294

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 2 commits into from
Nov 4, 2020
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
8 changes: 6 additions & 2 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ def __init__(self, config: BaseConfig, args):
self.config: BaseConfig = config
self.cz = factory.commiter_factory(self.config)

self.start_rev = args.get("start_rev")
self.start_rev = args.get("start_rev") or self.config.settings.get(
"changelog_start_rev"
)
self.file_name = args.get("file_name") or self.config.settings.get(
"changelog_file"
)
self.incremental = args["incremental"]
self.incremental = args["incremental"] or self.config.settings.get(
"changelog_incremental"
)
self.dry_run = args["dry_run"]
self.unreleased_version = args["unreleased_version"]
self.change_type_map = (
Expand Down
2 changes: 2 additions & 0 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"tag_format": None, # example v$version
"bump_message": None, # bumped v$current_version to $new_version
"changelog_file": "CHANGELOG.md",
"changelog_incremental": False,
"changelog_start_rev": None,
}

MAJOR = "MAJOR"
Expand Down
24 changes: 24 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ cz changelog --file-name="CHANGES.md"

### `incremental`

This flag can be set in the `toml` file with the key `changelog_incremental` under `tools.commitizen`

Benefits:

- Build from latest version found in changelog, this is useful if you have a different changelog and want to use commitizen
Expand All @@ -124,6 +126,28 @@ Benefits:
cz changelog --incremental
```

```toml
[tools.commitizen]
# ...
changelog_incremental = true
```

### `start-rev`

This value can be set in the `toml` file with the key `changelog_start_rev` under `tools.commitizen`

Start from a given git rev to generate the changelog. Commits before that rev will not be considered. This is especially useful for long-running projects adopting conventional commits, where old commit messages might fail to be parsed for changelog generation.

```bash
cz changelog --start-rev="v0.2.0"
```

```toml
[tools.commitizen]
# ...
changelog_start_rev = "v0.2.0"
```

## Hooks

Supported hook methods:
Expand Down
50 changes: 50 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ 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"]
Expand Down Expand Up @@ -389,3 +394,48 @@ def test_breaking_change_content_v1(mocker, capsys):
"## Unreleased\n\n### Feat\n\n- **users**: email pattern corrected\n\n"
"### BREAKING CHANGE\n\n- migrate by renaming user to users\n\n"
)


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_config_flag_increment(mocker, changelog_path, config_path):

with open(config_path, "a") as f:
f.write("changelog_incremental = true\n")
with open(changelog_path, "a") as f:
f.write("\nnote: this should be persisted using increment\n")

create_file_and_commit("feat: add new output")

testargs = ["cz", "changelog"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

with open(changelog_path, "r") as f:
out = f.read()

assert "this should be persisted using increment" in out


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_config_start_rev_option(mocker, capsys, config_path):

# create commit and tag
create_file_and_commit("feat: new file")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
capsys.readouterr()

create_file_and_commit("feat: after 0.2.0")
create_file_and_commit("feat: after 0.2")

with open(config_path, "a") as f:
f.write('changelog_start_rev = "0.2.0"\n')

testargs = ["cz", "changelog", "--dry-run"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(DryRunExit):
cli.main()

out, _ = capsys.readouterr()
assert out == "## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n"
4 changes: 4 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"changelog_file": "CHANGELOG.md",
"changelog_incremental": False,
"changelog_start_rev": None,
}

_new_settings = {
Expand All @@ -42,6 +44,8 @@
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
"style": [["pointer", "reverse"], ["question", "underline"]],
"changelog_file": "CHANGELOG.md",
"changelog_incremental": False,
"changelog_start_rev": None,
}

_read_settings = {
Expand Down