Skip to content

Commit fb73367

Browse files
committed
docs: add info about creating hooks
1 parent 87bebaf commit fb73367

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

docs/changelog.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,15 @@ Benefits:
124124
cz changelog --incremental
125125
```
126126
127-
## TODO
127+
## Hooks
128128
129-
- [ ] support for hooks: this would allow introduction of custom information in the commiter, like a github or jira url. Eventually we could build a `CzConventionalGithub`, which would add links to commits
130-
- [x] support for map: allow the usage of a `change_type` mapper, to convert from feat to feature for example.
129+
Supported hook methods:
130+
131+
- per parsed message: useful to add links
132+
- end of changelog generation: useful to send slack or chat message, or notify another department
133+
134+
Read more about hooks in the [customization page][customization]
131135
132136
[keepachangelog]: https://keepachangelog.com/
133137
[semver]: https://semver.org/
138+
[customization]: ./customization.md

docs/customization.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,52 @@ cz -n cz_strange bump
137137
The changelog generator should just work in a very basic manner without touching anything.
138138
You can customize it of course, and this are the variables you need to add to your custom `BaseCommitizen`.
139139

140-
| Parameter | Type | Required | Description |
141-
| ------------------- | --------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
142-
| `commit_parser` | `str` | NO | Regex which should provide the variables explained in the [changelog description][changelog-des] |
143-
| `changelog_pattern` | `str` | NO | Regex to validate the commits, this is useful to skip commits that don't meet your rulling standards like a Merge. Usually the same as bump_pattern |
144-
| `change_type_map` | `dict` | NO | Convert the title of the change type that will appear in the changelog, if a value is not found, the original will be provided |
145-
| `message_hook` | `method: (dict, git.GitCommit) -> dict` | NO | Customize with extra information your message output, like adding links, this function is executed per parsed commit. |
140+
| Parameter | Type | Required | Description |
141+
| ------------------- | ------------------------------------------------------------------------ | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
142+
| `commit_parser` | `str` | NO | Regex which should provide the variables explained in the [changelog description][changelog-des] |
143+
| `changelog_pattern` | `str` | NO | Regex to validate the commits, this is useful to skip commits that don't meet your rulling standards like a Merge. Usually the same as bump_pattern |
144+
| `change_type_map` | `dict` | NO | Convert the title of the change type that will appear in the changelog, if a value is not found, the original will be provided |
145+
| `message_hook` | `method: (dict, git.GitCommit) -> dict` | NO | Customize with extra information your message output, like adding links, this function is executed per parsed commit. |
146+
| `changelog_hook` | `method: (full_changelog: str, partial_changelog: Optional[str]) -> str` | NO | Receives the whole and partial (if used incremental) changelog. Useful to send slack messages or notify a compliance department. Must return the full_changelog |
147+
148+
```python
149+
from commitizen.cz.base import BaseCommitizen
150+
import chat
151+
import compliance
152+
153+
class StrangeCommitizen(BaseCommitizen):
154+
changelog_pattern = r"^(break|new|fix|hotfix)"
155+
commit_parser = r"^(?P<change_type>feat|fix|refactor|perf|BREAKING CHANGE)(?:\((?P<scope>[^()\r\n]*)\)|\()?(?P<breaking>!)?:\s(?P<message>.*)?"
156+
change_type_map = {
157+
"feat": "Features",
158+
"fix": "Bug Fixes",
159+
"refactor": "Code Refactor",
160+
"perf": "Performance improvements"
161+
}
162+
163+
def message_hook(self, parsed_message: dict, commit: git.GitCommit) -> dict:
164+
rev = commit.rev
165+
m = parsed_message["message"]
166+
parsed_message["message"] = f"{m} {rev}"
167+
return parsed_message
168+
169+
def changelog_hook(self, full_changelog: str, partial_changelog: Optional[str]) -> str:
170+
"""Executed at the end of the changelog generation
171+
172+
full_changelog: it's the output about to being written into the file
173+
partial_changelog: it's the new stuff, this is useful to send slack messages or
174+
similar
175+
176+
Return:
177+
the new updated full_changelog
178+
"""
179+
if partial_changelog:
180+
chat.room("#commiters").notify(partial_changelog)
181+
if full_changelog:
182+
compliance.send(full_changelog)
183+
full_changelog.replace(' fix ', ' **fix** ')
184+
return full_changelog
185+
```
146186

147187
[changelog-des]: ./changelog.md#description
148188

tests/commands/test_changelog_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_changlog_hook(mocker, config):
247247
create_file_and_commit("Merge into master")
248248

249249
changelog = Changelog(
250-
config, {"unreleased_version": None, "incremental": True, "dry_run": False,},
250+
config, {"unreleased_version": None, "incremental": True, "dry_run": False},
251251
)
252252
mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
253253
changelog()

0 commit comments

Comments
 (0)