Skip to content
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

feat(format): added scopes argument to specify valid scopes #102

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions conventional_pre_commit/format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from typing import List, Optional

CONVENTIONAL_TYPES = ["feat", "fix"]
DEFAULT_TYPES = [
Expand Down Expand Up @@ -26,8 +27,20 @@ def r_types(types):
return "|".join(types)


def r_scope(optional=True):
def _get_scope_pattern(scopes: Optional[List[str]] = None):
scopes_str = r_types(scopes)
escaped_delimiters = list(map(re.escape, [":", ","])) # type: ignore
mjspeck marked this conversation as resolved.
Show resolved Hide resolved
delimiters_pattern = r_types(escaped_delimiters)
return rf"\(\s*(?:{scopes_str})(?:\s*(?:{delimiters_pattern})\s*(?:{scopes_str}))*\s*\)"


def r_scope(optional=True, scopes: Optional[List[str]] = None):
"""Regex str for an optional (scope)."""

if scopes:
scopes_pattern = _get_scope_pattern(scopes)
return scopes_pattern

if optional:
return r"(\([\w \/:,-]+\))?"
else:
Expand Down Expand Up @@ -79,7 +92,7 @@ def conventional_types(types=[]):
return types


def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True, scopes: Optional[list[str]] = None):
mjspeck marked this conversation as resolved.
Show resolved Hide resolved
"""
Returns True if input matches Conventional Commits formatting
https://www.conventionalcommits.org
Expand All @@ -89,7 +102,7 @@ def is_conventional(input, types=DEFAULT_TYPES, optional_scope=True):
input = strip_verbose_diff(input)
input = strip_comments(input)
types = conventional_types(types)
pattern = f"^({r_types(types)}){r_scope(optional_scope)}{r_delim()}{r_subject()}{r_body()}"
pattern = f"^({r_types(types)}){r_scope(optional_scope, scopes=scopes)}{r_delim()}{r_subject()}{r_body()}"
regex = re.compile(pattern, re.MULTILINE)

result = regex.match(input)
Expand Down
3 changes: 2 additions & 1 deletion conventional_pre_commit/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def main(argv=[]):
action="store_true",
help="Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits.",
)
parser.add_argument("scopes", type=str, nargs="*", default=None, help="Optional list of scopes to support")
mjspeck marked this conversation as resolved.
Show resolved Hide resolved

if len(argv) < 1:
argv = sys.argv[1:]
Expand Down Expand Up @@ -56,7 +57,7 @@ def main(argv=[]):
if format.has_autosquash_prefix(message):
return RESULT_SUCCESS

if format.is_conventional(message, args.types, args.optional_scope):
if format.is_conventional(message, args.types, args.optional_scope, args.scopes):
return RESULT_SUCCESS
else:
print(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def test_r_scope__special_chars():
assert regex.match("(some,thing)")


def test_r_scope__scopes():
scopes_input = ["api", "client"]
result = format.r_scope(scopes=scopes_input)
regex = re.compile(result)
assert regex.match("(api)")
assert regex.match("(client)")
assert regex.match("(api, client)")
assert regex.match("(api: client)")
assert not regex.match("(test)")
assert not regex.match("(api; client)")


def test_r_delim():
result = format.r_delim()
regex = re.compile(result)
Expand Down
Loading