Skip to content

Commit 87bebaf

Browse files
committed
feat(changelog): add support for changelog_hook when changelog finishes the generation
1 parent 221a8d7 commit 87bebaf

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

commitizen/commands/changelog.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def __call__(self):
6565
changelog_meta: Dict = {}
6666
change_type_map: Optional[Dict] = self.change_type_map
6767
message_hook: Optional[Callable] = self.cz.message_hook
68+
changelog_hook: Optional[Callable] = self.cz.changelog_hook
6869

6970
if not changelog_pattern or not commit_parser:
7071
out.error(
@@ -108,10 +109,14 @@ def __call__(self):
108109
lines = changelog_file.readlines()
109110

110111
with open(self.file_name, "w") as changelog_file:
112+
partial_changelog: Optional[str] = None
111113
if self.incremental:
112114
new_lines = changelog.incremental_build(
113115
changelog_out, lines, changelog_meta
114116
)
115-
changelog_file.writelines(new_lines)
116-
else:
117-
changelog_file.write(changelog_out)
117+
changelog_out = "".join(new_lines)
118+
partial_changelog = changelog_out
119+
120+
if changelog_hook:
121+
changelog_out = changelog_hook(changelog_out, partial_changelog)
122+
changelog_file.write(changelog_out)

commitizen/cz/base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from abc import ABCMeta, abstractmethod
2-
from typing import Callable, List, Optional, Tuple
2+
from typing import Callable, Dict, List, Optional, Tuple
33

44
from prompt_toolkit.styles import Style, merge_styles
55

6+
from commitizen import git
67
from commitizen.config.base_config import BaseConfig
78

89

@@ -27,8 +28,13 @@ class BaseCommitizen(metaclass=ABCMeta):
2728
# It can be modified per rule
2829
commit_parser: Optional[str] = r"(?P<message>.*)"
2930
changelog_pattern: Optional[str] = r".*"
30-
change_type_map: Optional[dict] = None
31-
message_hook: Optional[Callable] = None # (dict, GitCommit) -> dict
31+
change_type_map: Optional[Dict[str, str]] = None
32+
33+
# Executed per message parsed by the commitizen
34+
message_hook: Optional[Callable[[Dict, git.GitCommit], Dict]] = None
35+
36+
# Executed only at the end of the changelog generation
37+
changelog_hook: Optional[Callable[[str, Optional[str]], str]] = None
3238

3339
def __init__(self, config: BaseConfig):
3440
self.config = config

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ exclude =
3535
build,
3636
dist
3737
max-line-length = 88
38-
max-complexity = 10
38+
max-complexity = 11

tests/commands/test_changelog_command.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from commitizen import cli, git
8+
from commitizen.commands.changelog import Changelog
89
from tests.utils import create_file_and_commit
910

1011

@@ -234,3 +235,22 @@ def test_changlog_incremental_keep_a_changelog_sample(mocker, capsys):
234235
out
235236
== """# Changelog\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n\n## Unreleased \n\n### Feat\n\n- add more stuff\n- add new output\n\n### Fix\n\n- mama gotta work\n- output glitch\n\n## [1.0.0] - 2017-06-20\n### Added\n- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).\n- Version navigation.\n\n### Changed\n- Start using "changelog" over "change log" since it\'s the common usage.\n\n### Removed\n- Section about "changelog" vs "CHANGELOG".\n\n## [0.3.0] - 2015-12-03\n### Added\n- RU translation from [@aishek](https://github.com/aishek).\n"""
236237
)
238+
239+
240+
@pytest.mark.usefixtures("tmp_commitizen_project")
241+
def test_changlog_hook(mocker, config):
242+
changelog_hook_mock = mocker.Mock()
243+
changelog_hook_mock.return_value = "cool changelog hook"
244+
245+
create_file_and_commit("feat: new file")
246+
create_file_and_commit("refactor: is in changelog")
247+
create_file_and_commit("Merge into master")
248+
249+
changelog = Changelog(
250+
config, {"unreleased_version": None, "incremental": True, "dry_run": False,},
251+
)
252+
mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
253+
changelog()
254+
full_changelog = "\n## Unreleased \n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
255+
256+
changelog_hook_mock.assert_called_with(full_changelog, full_changelog)

0 commit comments

Comments
 (0)