Skip to content

Commit e8b7ab1

Browse files
committed
feat(changelog): add support for message_hook method
1 parent 141981b commit e8b7ab1

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

commitizen/changelog.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import re
3030
from collections import defaultdict
3131
from datetime import date
32-
from typing import Dict, Iterable, List, Optional
32+
from typing import Callable, Dict, Iterable, List, Optional
3333

3434
import pkg_resources
3535
from jinja2 import Template
@@ -73,6 +73,7 @@ def generate_tree_from_commits(
7373
changelog_pattern: str = defaults.bump_pattern,
7474
unreleased_version: Optional[str] = None,
7575
change_type_map: Optional[Dict[str, str]] = None,
76+
message_hook: Optional[Callable] = None,
7677
) -> Iterable[Dict]:
7778
pat = re.compile(changelog_pattern)
7879
map_pat = re.compile(commit_parser)
@@ -119,6 +120,8 @@ def generate_tree_from_commits(
119120

120121
if change_type_map:
121122
change_type = change_type_map.get(change_type, change_type)
123+
if message_hook:
124+
parsed_message = message_hook(parsed_message, commit)
122125
changes[change_type].append(parsed_message)
123126
if message_body:
124127
parsed_message_body: Dict = message_body.groupdict()

commitizen/commands/changelog.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os.path
22
from difflib import SequenceMatcher
33
from operator import itemgetter
4-
from typing import Dict, List, Optional
4+
from typing import Callable, Dict, List, Optional
55

66
from commitizen import changelog, factory, git, out
77
from commitizen.config import BaseConfig
@@ -64,6 +64,7 @@ def __call__(self):
6464
unreleased_version = self.unreleased_version
6565
changelog_meta: Dict = {}
6666
change_type_map: Optional[Dict] = self.change_type_map
67+
message_hook: Optional[Callable] = self.cz.message_hook
6768

6869
if not changelog_pattern or not commit_parser:
6970
out.error(
@@ -92,7 +93,8 @@ def __call__(self):
9293
commit_parser,
9394
changelog_pattern,
9495
unreleased_version,
95-
change_type_map,
96+
change_type_map=change_type_map,
97+
message_hook=message_hook,
9698
)
9799
changelog_out = changelog.render_changelog(tree)
98100

commitizen/cz/base.py

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

44
from prompt_toolkit.styles import Style, merge_styles
55

@@ -28,6 +28,7 @@ class BaseCommitizen(metaclass=ABCMeta):
2828
commit_parser: Optional[str] = r"(?P<message>.*)"
2929
changelog_pattern: Optional[str] = r".*"
3030
change_type_map: Optional[dict] = None
31+
message_hook: Optional[Callable] = None # (dict, GitCommit) -> dict
3132

3233
def __init__(self, config: BaseConfig):
3334
self.config = config

tests/test_changelog.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def test_render_changelog_tag_and_unreleased(gitcommits, tags):
703703
assert "## v1.1.1" in result
704704

705705

706-
def test_render_changelog_with_change_type(gitcommits, tags, changelog_content):
706+
def test_render_changelog_with_change_type(gitcommits, tags):
707707
new_title = ":some-emoji: feature"
708708
change_type_map = {"feat": new_title}
709709
parser = defaults.commit_parser
@@ -713,3 +713,17 @@ def test_render_changelog_with_change_type(gitcommits, tags, changelog_content):
713713
)
714714
result = changelog.render_changelog(tree)
715715
assert new_title in result
716+
717+
718+
def test_render_changelog_with_message_hook(gitcommits, tags):
719+
def message_hook(message: dict, _) -> dict:
720+
message["message"] = f"{message['message']} [link](github.com/232323232)"
721+
return message
722+
723+
parser = defaults.commit_parser
724+
changelog_pattern = defaults.bump_pattern
725+
tree = changelog.generate_tree_from_commits(
726+
gitcommits, tags, parser, changelog_pattern, message_hook=message_hook
727+
)
728+
result = changelog.render_changelog(tree)
729+
assert "[link](github.com/232323232)" in result

0 commit comments

Comments
 (0)