-
-
Couldn't load subscription status.
- Fork 302
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
base: master
Are you sure you want to change the base?
Changes from 13 commits
f66afbc
7bd16c3
67b5eb4
8dfe7b6
2dc3e78
8eaf37c
3596a06
784e3b4
904f200
33a06e9
ff7eee5
866101a
be8d7a9
e5805b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from abc import ABCMeta, abstractmethod | ||
| from collections.abc import Iterable | ||
| from typing import Any, Callable, Protocol | ||
| from typing import Any, Callable, NamedTuple, Protocol | ||
|
|
||
| from jinja2 import BaseLoader, PackageLoader | ||
| from prompt_toolkit.styles import Style, merge_styles | ||
|
|
@@ -24,6 +25,11 @@ def __call__( | |
| ) -> dict[str, Any]: ... | ||
|
|
||
|
|
||
| class ValidationResult(NamedTuple): | ||
| is_valid: bool | ||
| errors: list | ||
|
|
||
|
|
||
| class BaseCommitizen(metaclass=ABCMeta): | ||
| bump_pattern: str | None = None | ||
| bump_map: dict[str, str] | None = None | ||
|
|
@@ -41,7 +47,7 @@ class BaseCommitizen(metaclass=ABCMeta): | |
| ("disabled", "fg:#858585 italic"), | ||
| ] | ||
|
|
||
| # The whole subject will be parsed as message by default | ||
| # The whole subject will be parsed as a message by default | ||
| # This allows supporting changelog for any rule system. | ||
| # It can be modified per rule | ||
| commit_parser: str | None = r"(?P<message>.*)" | ||
|
|
@@ -96,6 +102,55 @@ def schema_pattern(self) -> str: | |
| """Regex matching the schema used for message validation.""" | ||
| raise NotImplementedError("Not Implemented yet") | ||
|
|
||
| def validate_commit_message( | ||
| self, | ||
| *, | ||
| commit_msg: str, | ||
| pattern: str | None, | ||
| allow_abort: bool, | ||
| allowed_prefixes: list[str], | ||
| max_msg_length: int, | ||
| ) -> ValidationResult: | ||
| """Validate commit message against the pattern.""" | ||
| if not commit_msg: | ||
| return ValidationResult(allow_abort, []) | ||
|
|
||
| if pattern is None: | ||
| return ValidationResult(True, []) | ||
|
|
||
| if any(map(commit_msg.startswith, allowed_prefixes)): | ||
| return ValidationResult(True, []) | ||
| if max_msg_length: | ||
| msg_len = len(commit_msg.partition("\n")[0].strip()) | ||
|
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. Do we want user perceived length? Unicode characters may take more than 1 char, if we want the user perceived length we'll need to measure "graphemes". For example, an emoji like 🎏 may take up to 4 chars. And languages outside english may have the same problem. What do you think @Lee-W ? 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. There are not many library options in python:
Tracking issue on cpython: |
||
| if msg_len > max_msg_length: | ||
| return ValidationResult( | ||
| False, | ||
| [ | ||
| f"The commit message subject is too long ({msg_len} > {max_msg_length} characters)." | ||
| ], | ||
| ) | ||
| return ValidationResult( | ||
| bool(re.match(pattern, commit_msg)), | ||
| [f"The commit message does not match the pattern: {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}"' | ||
| for commit, _ 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()}" | ||
| ) | ||
|
|
||
| def info(self) -> str: | ||
| """Information about the standardized commit message.""" | ||
| raise NotImplementedError("Not Implemented yet") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.