-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlint_rules.py
More file actions
34 lines (25 loc) · 1.1 KB
/
gitlint_rules.py
File metadata and controls
34 lines (25 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Define custom gitlint rules for commit message validation."""
import re
from gitlint.rules import CommitMessageTitle, LineRule, RuleViolation
class GitmojiConventionalCommit(LineRule):
"""Enforces Gitmoji + Conventional Commits format."""
name = "gitmoji-conventional-commits"
id = "GCC1"
target = CommitMessageTitle
GITMOJI_PATTERN = r"^(:\w+:\s)"
CONVENTIONAL_PATTERN = (
r"(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)"
r"(\([a-z-]+\))?!?:\s.+"
)
RULE_REGEX = re.compile(f"{GITMOJI_PATTERN}?{CONVENTIONAL_PATTERN}")
def validate(self, line: str, _commit: CommitMessageTitle) -> list[RuleViolation]:
"""Validate commit message against Gitmoji + Conventional Commits format."""
violations = []
match = self.RULE_REGEX.match(line)
if not match:
msg = (
"Title does not follow Gitmoji + ConventionalCommits format "
"':gitmoji: type(optional-scope): description'"
)
violations.append(RuleViolation(self.id, msg, line))
return violations