Skip to content

Commit af176ce

Browse files
committed
fix(changelog): changelog command does not find version tag with version_type=semver option
Signed-off-by: apkawa <[email protected]>
1 parent 389b85b commit af176ce

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

commitizen/changelog.py

+19-4
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
from packaging.version import InvalidVersion, Version
@@ -39,6 +41,12 @@
3941
from commitizen.exceptions import InvalidConfigurationError, NoCommitsFoundError
4042
from commitizen.git import GitCommit, GitTag
4143

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

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

314322

315323
def get_oldest_and_newest_rev(
316-
tags: List[GitTag], version: str, tag_format: str
324+
tags: List[GitTag],
325+
version: str,
326+
tag_format: str,
327+
version_type_cls: Optional[Type[VersionProtocol]] = None,
317328
) -> Tuple[Optional[str], Optional[str]]:
318329
"""Find the tags for the given version.
319330
@@ -328,11 +339,15 @@ def get_oldest_and_newest_rev(
328339
except ValueError:
329340
newest = version
330341

331-
newest_tag = normalize_tag(newest, tag_format=tag_format)
342+
newest_tag = normalize_tag(
343+
newest, tag_format=tag_format, version_type_cls=version_type_cls
344+
)
332345

333346
oldest_tag = None
334347
if oldest:
335-
oldest_tag = normalize_tag(oldest, tag_format=tag_format)
348+
oldest_tag = normalize_tag(
349+
oldest, tag_format=tag_format, version_type_cls=version_type_cls
350+
)
336351

337352
tags_range = get_smart_tag_range(tags, newest=newest_tag, oldest=oldest_tag)
338353
if not tags_range:

commitizen/commands/changelog.py

+8-2
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,
@@ -53,6 +53,9 @@ def __init__(self, config: BaseConfig, args):
5353
"merge_prerelease"
5454
) or self.config.settings.get("changelog_merge_prerelease")
5555

56+
version_type = self.config.settings.get("version_type")
57+
self.version_type = version_type and version_types.types[version_type]
58+
5659
def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:
5760
"""Try to find the 'start_rev'.
5861
@@ -137,7 +140,9 @@ def __call__(self):
137140
latest_version = changelog_meta.get("latest_version")
138141
if latest_version:
139142
latest_tag_version: str = bump.normalize_tag(
140-
latest_version, tag_format=self.tag_format
143+
latest_version,
144+
tag_format=self.tag_format,
145+
version_type_cls=self.version_type,
141146
)
142147
start_rev = self._find_incremental_rev(latest_tag_version, tags)
143148

@@ -146,6 +151,7 @@ def __call__(self):
146151
tags,
147152
version=self.rev_range,
148153
tag_format=self.tag_format,
154+
version_type_cls=self.version_type,
149155
)
150156

151157
commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order")

tests/commands/test_changelog_command.py

+57
Original file line numberDiff line numberDiff line change
@@ -1152,3 +1152,60 @@ def test_empty_commit_list(mocker):
11521152
mocker.patch.object(sys, "argv", testargs)
11531153
with pytest.raises(NoCommitsFoundError):
11541154
cli.main()
1155+
1156+
1157+
@pytest.mark.usefixtures("tmp_commitizen_project")
1158+
@pytest.mark.freeze_time("2022-02-13")
1159+
def test_changelog_prerelease_rev_with_use_version_type_semver(
1160+
mocker: MockFixture, capsys, config_path, changelog_path, file_regression
1161+
):
1162+
mocker.patch("commitizen.git.GitTag.date", "2022-02-13")
1163+
1164+
with open(config_path, "a") as f:
1165+
f.write('tag_format = "$version"\n' 'version_type = "semver"')
1166+
1167+
# create commit and tag
1168+
create_file_and_commit("feat: new file")
1169+
testargs = ["cz", "bump", "--yes"]
1170+
mocker.patch.object(sys, "argv", testargs)
1171+
cli.main()
1172+
wait_for_tag()
1173+
1174+
create_file_and_commit("feat: after 0.2.0")
1175+
create_file_and_commit("feat: another feature")
1176+
1177+
testargs = ["cz", "bump", "--yes", "--prerelease", "alpha"]
1178+
mocker.patch.object(sys, "argv", testargs)
1179+
cli.main()
1180+
capsys.readouterr()
1181+
wait_for_tag()
1182+
1183+
tag_exists = git.tag_exist("0.3.0-a0")
1184+
assert tag_exists is True
1185+
1186+
testargs = ["cz", "changelog", "0.3.0-a0", "--dry-run"]
1187+
mocker.patch.object(sys, "argv", testargs)
1188+
with pytest.raises(DryRunExit):
1189+
cli.main()
1190+
1191+
out, _ = capsys.readouterr()
1192+
1193+
file_regression.check(out, extension=".md")
1194+
1195+
testargs = ["cz", "bump", "--yes", "--prerelease", "alpha"]
1196+
mocker.patch.object(sys, "argv", testargs)
1197+
cli.main()
1198+
capsys.readouterr()
1199+
wait_for_tag()
1200+
1201+
tag_exists = git.tag_exist("0.3.0-a1")
1202+
assert tag_exists is True
1203+
1204+
testargs = ["cz", "changelog", "0.3.0-a1", "--dry-run"]
1205+
mocker.patch.object(sys, "argv", testargs)
1206+
with pytest.raises(DryRunExit):
1207+
cli.main()
1208+
1209+
out, _ = capsys.readouterr()
1210+
1211+
assert out == "## 0.3.0-a1 (2022-02-13)\n\n"
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)