Skip to content
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

fix(changelog): ensure changelog_message_builder_hook can access and modify change_type #1002

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
13 changes: 6 additions & 7 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,13 @@ def generate_tree_from_commits(
message = map_pat.match(commit.message)
if message:
parsed_message: dict = message.groupdict()
# change_type becomes optional by providing None
change_type = parsed_message.pop("change_type", None)

if change_type_map:
change_type = change_type_map.get(change_type, change_type)
if changelog_message_builder_hook:
parsed_message = changelog_message_builder_hook(parsed_message, commit)
if parsed_message:
change_type = parsed_message.pop("change_type", None)
if change_type_map:
change_type = change_type_map.get(change_type, change_type)
changes[change_type].append(parsed_message)

# Process body from commit message
Expand All @@ -177,14 +176,14 @@ def generate_tree_from_commits(
continue
parsed_message_body: dict = message_body.groupdict()

change_type = parsed_message_body.pop("change_type", None)
if change_type_map:
change_type = change_type_map.get(change_type, change_type)
if changelog_message_builder_hook:
parsed_message_body = changelog_message_builder_hook(
parsed_message_body, commit
)
if parsed_message_body:
change_type = parsed_message_body.pop("change_type", None)
if change_type_map:
change_type = change_type_map.get(change_type, change_type)
changes[change_type].append(parsed_message_body)

yield {"version": current_tag_name, "date": current_tag_date, "changes": changes}
Expand Down
31 changes: 31 additions & 0 deletions tests/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,37 @@ def changelog_message_builder_hook(message: dict, commit: git.GitCommit):
assert RE_HEADER.match(line), f"Line {no} should not be there: {line}"


def test_changelog_message_builder_hook_can_access_and_modify_change_type(
gitcommits, tags, any_changelog_format: ChangelogFormat
):
def changelog_message_builder_hook(message: dict, commit: git.GitCommit):
assert "change_type" in message
message["change_type"] = "overridden"
return message

parser = ConventionalCommitsCz.commit_parser
changelog_pattern = ConventionalCommitsCz.changelog_pattern
loader = ConventionalCommitsCz.template_loader
template = any_changelog_format.template
tree = changelog.generate_tree_from_commits(
gitcommits,
tags,
parser,
changelog_pattern,
changelog_message_builder_hook=changelog_message_builder_hook,
)
result = changelog.render_changelog(tree, loader, template)

RE_HEADER = re.compile(r"^### (?P<type>.+)$")
# There should be only "overridden" change type headers
for no, line in enumerate(result.splitlines()):
if (line := line.strip()) and (match := RE_HEADER.match(line)):
change_type = match.group("type")
assert (
change_type == "overridden"
), f"Line {no}: type {change_type} should have been overridden"


def test_get_smart_tag_range_returns_an_extra_for_a_range(tags):
start, end = (
tags[0],
Expand Down