-
-
Notifications
You must be signed in to change notification settings - Fork 288
feat: add custom validation #1236
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
Open
benediktziegler
wants to merge
12
commits into
commitizen-tools:master
Choose a base branch
from
benediktziegler:feature/custom-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f66afbc
feat: add custom validation
benediktziegler 7bd16c3
test: add test to cover schema_pattern is None case
benediktziegler 67b5eb4
Merge branch 'master' into feature/custom-validation
benediktziegler 8dfe7b6
Merge branch 'commitizen-tools:master' into feature/custom-validation
benediktziegler 2dc3e78
Merge branch 'master' into feature/custom-validation
benediktziegler 8eaf37c
fix: fix method call
benediktziegler 3596a06
Merge branch 'commitizen-tools:master' into feature/custom-validation
benediktziegler 784e3b4
refactor: make arguments keyword only
benediktziegler 904f200
refactor: use namedtuple for return
benediktziegler 33a06e9
Merge branch 'master' into feature/custom-validation
benediktziegler ff7eee5
fix: fix type mismatch from merge
benediktziegler 866101a
Merge branch 'master' into feature/custom-validation
benediktziegler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
Customizing commitizen is not hard at all. | ||||||
from commitizen import BaseCommitizenCustomizing commitizen is not hard at all. | ||||||
We have two different ways to do so. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like a typo |
||||||
|
||||||
## 1. Customize in configuration file | ||||||
|
@@ -308,6 +308,72 @@ cz -n cz_strange bump | |||||
|
||||||
[convcomms]: https://github.com/commitizen-tools/commitizen/blob/master/commitizen/cz/conventional_commits/conventional_commits.py | ||||||
|
||||||
### Custom commit validation and error message | ||||||
|
||||||
The commit message validation can be customized by overriding the `validate_commit_message` and `format_error_message` | ||||||
methods from `BaseCommitizen`. This allows for a more detailed feedback to the user where the error originates from. | ||||||
|
||||||
```python | ||||||
import re | ||||||
|
||||||
from commitizen.cz.base import BaseCommitizen | ||||||
from commitizen import git | ||||||
|
||||||
|
||||||
class CustomValidationCz(BaseCommitizen): | ||||||
def validate_commit_message( | ||||||
self, | ||||||
commit_msg: str, | ||||||
pattern: str | None, | ||||||
allow_abort: bool, | ||||||
allowed_prefixes: list[str], | ||||||
max_msg_length: int, | ||||||
) -> tuple[bool, list]: | ||||||
"""Validate commit message against the pattern.""" | ||||||
if not commit_msg: | ||||||
return allow_abort, [] if allow_abort else [f"commit message is empty"] | ||||||
|
||||||
if pattern is None: | ||||||
return True, [] | ||||||
|
||||||
if any(map(commit_msg.startswith, allowed_prefixes)): | ||||||
return True, [] | ||||||
if max_msg_length: | ||||||
msg_len = len(commit_msg.partition("\n")[0].strip()) | ||||||
if msg_len > max_msg_length: | ||||||
return False, [ | ||||||
f"commit message is too long. Max length is {max_msg_length}" | ||||||
] | ||||||
pattern_match = re.match(pattern, commit_msg) | ||||||
if pattern_match: | ||||||
return True, [] | ||||||
else: | ||||||
# Perform additional validation of the commit message format | ||||||
# and add custom error messages as needed | ||||||
return False, ["commit message does not match the pattern"] | ||||||
|
||||||
def format_exception_message( | ||||||
self, ill_formated_commits: list[tuple[git.GitCommit, list]] | ||||||
) -> str: | ||||||
"""Format commit errors.""" | ||||||
displayed_msgs_content = "\n".join( | ||||||
[ | ||||||
( | ||||||
f'commit "{commit.rev}": "{commit.message}"' | ||||||
f"errors:\n" | ||||||
"\n".join((f"- {error}" for error in errors)) | ||||||
) | ||||||
for commit, errors in ill_formated_commits | ||||||
] | ||||||
) | ||||||
return ( | ||||||
"commit validation: failed!\n" | ||||||
"please enter a commit message in the commitizen format.\n" | ||||||
f"{displayed_msgs_content}\n" | ||||||
f"pattern: {self.schema_pattern()}" | ||||||
) | ||||||
``` | ||||||
|
||||||
### Custom changelog generator | ||||||
|
||||||
The changelog generator should just work in a very basic manner without touching anything. | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.