Skip to content

Commit 12c3f31

Browse files
grahamharwoile
authored andcommitted
fix(changelog): Handle tag format without version pattern
1 parent b023711 commit 12c3f31

10 files changed

+333
-30
lines changed

commitizen/changelog.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ def get_version_tags(
9898
) -> list[GitTag]:
9999
valid_tags: list[GitTag] = []
100100
TAG_FORMAT_REGEXS = {
101-
"$version": str(scheme.parser.pattern),
101+
"$version": scheme.parser.pattern,
102102
"$major": r"(?P<major>\d+)",
103103
"$minor": r"(?P<minor>\d+)",
104104
"$patch": r"(?P<patch>\d+)",
105105
"$prerelease": r"(?P<prerelease>\w+\d+)?",
106106
"$devrelease": r"(?P<devrelease>\.dev\d+)?",
107-
"${version}": str(scheme.parser.pattern),
107+
"${version}": scheme.parser.pattern,
108108
"${major}": r"(?P<major>\d+)",
109109
"${minor}": r"(?P<minor>\d+)",
110110
"${patch}": r"(?P<patch>\d+)",

commitizen/changelog_formats/asciidoc.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ def parse_version_from_title(self, line: str) -> str | None:
1818
matches = list(re.finditer(self.version_parser, m.group("title")))
1919
if not matches:
2020
return None
21-
return matches[-1].group("version")
21+
if "version" in matches[-1].groupdict():
22+
return matches[-1].group("version")
23+
partial_matches = matches[-1].groupdict()
24+
try:
25+
partial_version = f"{partial_matches['major']}.{partial_matches['minor']}.{partial_matches['patch']}"
26+
except KeyError:
27+
return None
28+
29+
if partial_matches.get("prerelease"):
30+
partial_version += f"-{partial_matches['prerelease']}"
31+
if partial_matches.get("devrelease"):
32+
partial_version += f"{partial_matches['devrelease']}"
33+
return partial_version
2234

2335
def parse_title_level(self, line: str) -> int | None:
2436
m = self.RE_TITLE.match(line)

commitizen/changelog_formats/base.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import re
45
from abc import ABCMeta
56
from re import Pattern
67
from typing import IO, Any, ClassVar
@@ -25,30 +26,29 @@ def __init__(self, config: BaseConfig):
2526
# See: https://bugs.python.org/issue44807
2627
self.config = config
2728
self.encoding = self.config.settings["encoding"]
28-
self.tag_format = self.config.settings.get("tag_format")
29+
self.tag_format = self.config.settings["tag_format"]
2930

3031
@property
3132
def version_parser(self) -> Pattern:
33+
tag_regex: str = self.tag_format
3234
version_regex = get_version_scheme(self.config).parser.pattern
33-
if self.tag_format != "$version":
34-
TAG_FORMAT_REGEXS = {
35-
"$version": version_regex,
36-
"$major": "(?P<major>\d+)",
37-
"$minor": "(?P<minor>\d+)",
38-
"$patch": "(?P<patch>\d+)",
39-
"$prerelease": "(?P<prerelease>\w+\d+)?",
40-
"$devrelease": "(?P<devrelease>\.dev\d+)?",
41-
"${version}": version_regex,
42-
"${major}": "(?P<major>\d+)",
43-
"${minor}": "(?P<minor>\d+)",
44-
"${patch}": "(?P<patch>\d+)",
45-
"${prerelease}": "(?P<prerelease>\w+\d+)?",
46-
"${devrelease}": "(?P<devrelease>\.dev\d+)?",
47-
}
48-
version_regex = self.tag_format
49-
for pattern, regex in TAG_FORMAT_REGEXS.items():
50-
version_regex = version_regex.replace(pattern, regex)
51-
return rf"{version_regex}"
35+
TAG_FORMAT_REGEXS = {
36+
"$version": version_regex,
37+
"$major": r"(?P<major>\d+)",
38+
"$minor": r"(?P<minor>\d+)",
39+
"$patch": r"(?P<patch>\d+)",
40+
"$prerelease": r"(?P<prerelease>\w+\d+)?",
41+
"$devrelease": r"(?P<devrelease>\.dev\d+)?",
42+
"${version}": version_regex,
43+
"${major}": r"(?P<major>\d+)",
44+
"${minor}": r"(?P<minor>\d+)",
45+
"${patch}": r"(?P<patch>\d+)",
46+
"${prerelease}": r"(?P<prerelease>\w+\d+)?",
47+
"${devrelease}": r"(?P<devrelease>\.dev\d+)?",
48+
}
49+
for pattern, regex in TAG_FORMAT_REGEXS.items():
50+
tag_regex = tag_regex.replace(pattern, regex)
51+
return re.compile(tag_regex)
5252

5353
def get_metadata(self, filepath: str) -> Metadata:
5454
if not os.path.isfile(filepath):

commitizen/changelog_formats/markdown.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@ def parse_version_from_title(self, line: str) -> str | None:
1919
m = re.search(self.version_parser, m.group("title"))
2020
if not m:
2121
return None
22-
return m.group("version")
22+
if "version" in m.groupdict():
23+
return m.group("version")
24+
matches = m.groupdict()
25+
try:
26+
partial_version = (
27+
f"{matches['major']}.{matches['minor']}.{matches['patch']}"
28+
)
29+
except KeyError:
30+
return None
31+
32+
if matches.get("prerelease"):
33+
partial_version += f"-{matches['prerelease']}"
34+
if matches.get("devrelease"):
35+
partial_version += f"{matches['devrelease']}"
36+
return partial_version
2337

2438
def parse_title_level(self, line: str) -> int | None:
2539
m = self.RE_TITLE.match(line)

commitizen/changelog_formats/restructuredtext.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata:
4646
third = third.strip().lower()
4747
title: str | None = None
4848
kind: TitleKind | None = None
49-
5049
if self.is_overlined_title(first, second, third):
5150
title = second
5251
kind = (first[0], third[0])
@@ -67,10 +66,25 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata:
6766
# Try to find the latest release done
6867
m = re.search(self.version_parser, title)
6968
if m:
70-
version = m.group("version")
71-
meta.latest_version = version
72-
meta.latest_version_position = index
73-
break # there's no need for more info
69+
matches = m.groupdict()
70+
if "version" in matches:
71+
version = m.group("version")
72+
meta.latest_version = version
73+
meta.latest_version_position = index
74+
break # there's no need for more info
75+
try:
76+
partial_version = (
77+
f"{matches['major']}.{matches['minor']}.{matches['patch']}"
78+
)
79+
if matches.get("prerelease"):
80+
partial_version += f"-{matches['prerelease']}"
81+
if matches.get("devrelease"):
82+
partial_version += f"{matches['devrelease']}"
83+
meta.latest_version = partial_version
84+
meta.latest_version_position = index
85+
break
86+
except KeyError:
87+
pass
7488
if meta.unreleased_start is not None and meta.unreleased_end is None:
7589
meta.unreleased_end = (
7690
meta.latest_version_position if meta.latest_version else index + 1

commitizen/changelog_formats/textile.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ def parse_version_from_title(self, line: str) -> str | None:
1616
m = re.search(self.version_parser, line)
1717
if not m:
1818
return None
19-
return m.group("version")
19+
if "version" in m.groupdict():
20+
return m.group("version")
21+
matches = m.groupdict()
22+
try:
23+
partial_version = (
24+
f"{matches['major']}.{matches['minor']}.{matches['patch']}"
25+
)
26+
except KeyError:
27+
return None
28+
29+
if matches.get("prerelease"):
30+
partial_version += f"-{matches['prerelease']}"
31+
if matches.get("devrelease"):
32+
partial_version += f"{matches['devrelease']}"
33+
return partial_version
2034

2135
def parse_title_level(self, line: str) -> int | None:
2236
m = self.RE_TITLE.match(line)

tests/test_changelog_format_asciidoc.py

+61
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,42 @@
7272
unreleased_start=1,
7373
)
7474

75+
CHANGELOG_E = """
76+
= Changelog
77+
78+
All notable changes to this project will be documented in this file.
79+
80+
The format is based on https://keepachangelog.com/en/1.0.0/[Keep a Changelog],
81+
and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Versioning].
82+
83+
== [Unreleased]
84+
* Start using "changelog" over "change log" since it's the common usage.
85+
86+
== [{tag_formatted_version}] - 2017-06-20
87+
=== Added
88+
* New visual identity by https://github.com/tylerfortune8[@tylerfortune8].
89+
* Version navigation.
90+
""".strip()
91+
92+
EXPECTED_E = Metadata(
93+
latest_version="1.0.0",
94+
latest_version_position=10,
95+
unreleased_end=10,
96+
unreleased_start=7,
97+
)
98+
7599

76100
@pytest.fixture
77101
def format(config: BaseConfig) -> AsciiDoc:
78102
return AsciiDoc(config)
79103

80104

105+
@pytest.fixture
106+
def format_with_tags(config: BaseConfig, request) -> AsciiDoc:
107+
config.settings["tag_format"] = request.param
108+
return AsciiDoc(config)
109+
110+
81111
VERSIONS_EXAMPLES = [
82112
("== [1.0.0] - 2017-06-20", "1.0.0"),
83113
(
@@ -135,3 +165,34 @@ def test_get_matadata(
135165
changelog.write_text(content)
136166

137167
assert format.get_metadata(str(changelog)) == expected
168+
169+
170+
@pytest.mark.parametrize(
171+
"format_with_tags, tag_string, expected, ",
172+
(
173+
pytest.param("${version}-example", "1.0.0-example", "1.0.0"),
174+
pytest.param("${version}example", "1.0.0example", "1.0.0"),
175+
pytest.param("example${version}", "example1.0.0", "1.0.0"),
176+
pytest.param("example-${version}", "example-1.0.0", "1.0.0"),
177+
pytest.param("example-${major}-${minor}-${patch}", "example-1-0-0", "1.0.0"),
178+
pytest.param("example-${major}-${minor}", "example-1-0-0", None),
179+
pytest.param(
180+
"${major}-${minor}-${patch}-${prerelease}-example",
181+
"1-0-0-rc1-example",
182+
"1.0.0-rc1",
183+
),
184+
pytest.param(
185+
"${major}-${minor}-${patch}-${prerelease}${devrelease}-example",
186+
"1-0-0-a1.dev1-example",
187+
"1.0.0-a1.dev1",
188+
),
189+
),
190+
indirect=["format_with_tags"],
191+
)
192+
def test_get_metadata_custom_tag_format(
193+
tmp_path: Path, format_with_tags: AsciiDoc, tag_string: str, expected: Metadata
194+
):
195+
content = CHANGELOG_E.format(tag_formatted_version=tag_string)
196+
changelog = tmp_path / format_with_tags.default_changelog_file
197+
changelog.write_text(content)
198+
assert format_with_tags.get_metadata(str(changelog)).latest_version == expected

tests/test_changelog_format_markdown.py

+67
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,42 @@
7272
unreleased_start=1,
7373
)
7474

75+
CHANGELOG_E = """
76+
# Changelog
77+
78+
All notable changes to this project will be documented in this file.
79+
80+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
81+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
82+
83+
## [Unreleased]
84+
- Start using "changelog" over "change log" since it's the common usage.
85+
86+
## {tag_formatted_version} - 2017-06-20
87+
### Added
88+
- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).
89+
- Version navigation.
90+
""".strip()
91+
92+
EXPECTED_E = Metadata(
93+
latest_version="1.0.0",
94+
latest_version_position=10,
95+
unreleased_end=10,
96+
unreleased_start=7,
97+
)
98+
7599

76100
@pytest.fixture
77101
def format(config: BaseConfig) -> Markdown:
78102
return Markdown(config)
79103

80104

105+
@pytest.fixture
106+
def format_with_tags(config: BaseConfig, request) -> Markdown:
107+
config.settings["tag_format"] = request.param
108+
return Markdown(config)
109+
110+
81111
VERSIONS_EXAMPLES = [
82112
("## [1.0.0] - 2017-06-20", "1.0.0"),
83113
(
@@ -135,3 +165,40 @@ def test_get_matadata(
135165
changelog.write_text(content)
136166

137167
assert format.get_metadata(str(changelog)) == expected
168+
169+
170+
@pytest.mark.parametrize(
171+
"format_with_tags, tag_string, expected, ",
172+
(
173+
pytest.param("${version}-example", "1.0.0-example", "1.0.0"),
174+
pytest.param("${version}example", "1.0.0example", "1.0.0"),
175+
pytest.param("example${version}", "example1.0.0", "1.0.0"),
176+
pytest.param("example-${version}", "example-1.0.0", "1.0.0"),
177+
pytest.param("example-${major}-${minor}-${patch}", "example-1-0-0", "1.0.0"),
178+
pytest.param("example-${major}-${minor}", "example-1-0-0", None),
179+
pytest.param(
180+
"${major}-${minor}-${patch}-${prerelease}-example",
181+
"1-0-0-rc1-example",
182+
"1.0.0-rc1",
183+
),
184+
pytest.param(
185+
"${major}-${minor}-${patch}-${prerelease}-example",
186+
"1-0-0-a1-example",
187+
"1.0.0-a1",
188+
),
189+
pytest.param(
190+
"${major}-${minor}-${patch}-${prerelease}${devrelease}-example",
191+
"1-0-0-a1.dev1-example",
192+
"1.0.0-a1.dev1",
193+
),
194+
),
195+
indirect=["format_with_tags"],
196+
)
197+
def test_get_metadata_custom_tag_format(
198+
tmp_path: Path, format_with_tags: Markdown, tag_string: str, expected: Metadata
199+
):
200+
content = CHANGELOG_E.format(tag_formatted_version=tag_string)
201+
changelog = tmp_path / format_with_tags.default_changelog_file
202+
changelog.write_text(content)
203+
204+
assert format_with_tags.get_metadata(str(changelog)).latest_version == expected

0 commit comments

Comments
 (0)