Skip to content

Commit 5759f4e

Browse files
committed
fix(changelog): changelog command does not find version tag with version_type=semver option
Signed-off-by: apkawa <apkawa@gmail.com>
1 parent 2f295cd commit 5759f4e

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

commitizen/changelog.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727

2828
import os
2929
import re
30+
import sys
31+
import typing
3032
from collections import OrderedDict, defaultdict
3133
from datetime import date
32-
from typing import Callable, Dict, Iterable, List, Optional, Tuple
34+
from typing import Callable, Dict, Iterable, List, Optional, Tuple, Type
3335

3436
from jinja2 import Environment, PackageLoader
3537

@@ -38,6 +40,12 @@
3840
from commitizen.exceptions import InvalidConfigurationError, NoCommitsFoundError
3941
from commitizen.git import GitCommit, GitTag
4042

43+
if sys.version_info >= (3, 8):
44+
from commitizen.version_types import VersionProtocol
45+
else:
46+
# workaround mypy issue for 3.7 python
47+
VersionProtocol = typing.Any
48+
4149

4250
def get_commit_tag(commit: GitCommit, tags: List[GitTag]) -> Optional[GitTag]:
4351
return next((tag for tag in tags if tag.rev == commit.rev), None)
@@ -286,7 +294,10 @@ def get_smart_tag_range(
286294

287295

288296
def get_oldest_and_newest_rev(
289-
tags: List[GitTag], version: str, tag_format: str
297+
tags: List[GitTag],
298+
version: str,
299+
tag_format: str,
300+
version_type_cls: Optional[Type[VersionProtocol]] = None,
290301
) -> Tuple[Optional[str], Optional[str]]:
291302
"""Find the tags for the given version.
292303
@@ -301,11 +312,15 @@ def get_oldest_and_newest_rev(
301312
except ValueError:
302313
newest = version
303314

304-
newest_tag = normalize_tag(newest, tag_format=tag_format)
315+
newest_tag = normalize_tag(
316+
newest, tag_format=tag_format, version_type_cls=version_type_cls
317+
)
305318

306319
oldest_tag = None
307320
if oldest:
308-
oldest_tag = normalize_tag(oldest, tag_format=tag_format)
321+
oldest_tag = normalize_tag(
322+
oldest, tag_format=tag_format, version_type_cls=version_type_cls
323+
)
309324

310325
tags_range = get_smart_tag_range(tags, newest=newest_tag, oldest=oldest_tag)
311326
if not tags_range:

commitizen/commands/changelog.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from operator import itemgetter
44
from typing import Callable, Dict, List, Optional
55

6-
from commitizen import bump, changelog, defaults, factory, git, out
6+
from commitizen import bump, changelog, defaults, factory, git, out, version_types
77
from commitizen.config import BaseConfig
88
from commitizen.exceptions import (
99
DryRunExit,
@@ -50,6 +50,9 @@ def __init__(self, config: BaseConfig, args):
5050
"tag_format"
5151
)
5252

53+
version_type = self.config.settings.get("version_type")
54+
self.version_type = version_type and version_types.types[version_type]
55+
5356
def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:
5457
"""Try to find the 'start_rev'.
5558
@@ -133,7 +136,9 @@ def __call__(self):
133136
latest_version = changelog_meta.get("latest_version")
134137
if latest_version:
135138
latest_tag_version: str = bump.normalize_tag(
136-
latest_version, tag_format=self.tag_format
139+
latest_version,
140+
tag_format=self.tag_format,
141+
version_type_cls=self.version_type,
137142
)
138143
start_rev = self._find_incremental_rev(latest_tag_version, tags)
139144

@@ -142,6 +147,7 @@ def __call__(self):
142147
tags,
143148
version=self.rev_range,
144149
tag_format=self.tag_format,
150+
version_type_cls=self.version_type,
145151
)
146152

147153
commits = git.get_commits(

tests/commands/test_changelog_command.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,3 +968,60 @@ def test_empty_commit_list(mocker):
968968
mocker.patch.object(sys, "argv", testargs)
969969
with pytest.raises(NoCommitsFoundError):
970970
cli.main()
971+
972+
973+
@pytest.mark.usefixtures("tmp_commitizen_project")
974+
@pytest.mark.freeze_time("2022-02-13")
975+
def test_changelog_prerelease_rev_with_use_version_type_semver(
976+
mocker: MockFixture, capsys, config_path, changelog_path, file_regression
977+
):
978+
mocker.patch("commitizen.git.GitTag.date", "2022-02-13")
979+
980+
with open(config_path, "a") as f:
981+
f.write('tag_format = "$version"\n' 'version_type = "semver"')
982+
983+
# create commit and tag
984+
create_file_and_commit("feat: new file")
985+
testargs = ["cz", "bump", "--yes"]
986+
mocker.patch.object(sys, "argv", testargs)
987+
cli.main()
988+
wait_for_tag()
989+
990+
create_file_and_commit("feat: after 0.2.0")
991+
create_file_and_commit("feat: another feature")
992+
993+
testargs = ["cz", "bump", "--yes", "--prerelease", "alpha"]
994+
mocker.patch.object(sys, "argv", testargs)
995+
cli.main()
996+
capsys.readouterr()
997+
wait_for_tag()
998+
999+
tag_exists = git.tag_exist("0.3.0-a0")
1000+
assert tag_exists is True
1001+
1002+
testargs = ["cz", "changelog", "0.3.0-a0", "--dry-run"]
1003+
mocker.patch.object(sys, "argv", testargs)
1004+
with pytest.raises(DryRunExit):
1005+
cli.main()
1006+
1007+
out, _ = capsys.readouterr()
1008+
1009+
file_regression.check(out, extension=".md")
1010+
1011+
testargs = ["cz", "bump", "--yes", "--prerelease", "alpha"]
1012+
mocker.patch.object(sys, "argv", testargs)
1013+
cli.main()
1014+
capsys.readouterr()
1015+
wait_for_tag()
1016+
1017+
tag_exists = git.tag_exist("0.3.0-a1")
1018+
assert tag_exists is True
1019+
1020+
testargs = ["cz", "changelog", "0.3.0-a1", "--dry-run"]
1021+
mocker.patch.object(sys, "argv", testargs)
1022+
with pytest.raises(DryRunExit):
1023+
cli.main()
1024+
1025+
out, _ = capsys.readouterr()
1026+
1027+
assert out == "## 0.3.0-a1 (2022-02-13)\n\n"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## 0.3.0-a0 (2022-02-13)
2+
3+
### Feat
4+
5+
- another feature
6+
- after 0.2.0
7+

0 commit comments

Comments
 (0)