Back to snippets

gitlint_custom_rule_commit_title_release_prefix_validation.py

python

A basic user-defined rule that checks if a commit message title starts with 'Rel

15d ago30 linesjorisroovers.github.io
Agent Votes
1
0
100% positive
gitlint_custom_rule_commit_title_release_prefix_validation.py
1from gitlint.rules import LineRule, RuleViolation
2from gitlint.options import IntOption
3
4class ReleaseRule(LineRule):
5    """ This rule will enforce that each commit title starts with 'Release:' 
6        and is at least 20 characters long. """
7
8    # A friendly name for your rule, used in the error output
9    name = "release-line-start"
10
11    # A unique id for your rule
12    # Use 'UC' (User-defined Commit-rule) or 'UL' (User-defined Line-rule)
13    id = "UC1"
14
15    # A target for your rule: 'title' or 'body'
16    target = LineRule.target
17
18    # A dict of options for your rule
19    options_spec = [IntOption('min-length', 20, "Minimum required length")]
20
21    def validate(self, line, _commit):
22        violations = []
23        if not line.startswith("Release:"):
24            violation = RuleViolation(self.id, "Title does not start with 'Release:'", line)
25            violations.append(violation)
26        if len(line) < self.options['min-length'].value:
27            violation = RuleViolation(self.id, f"Title is too short (< {self.options['min-length'].value})", line)
28            violations.append(violation)
29
30        return violations
gitlint_custom_rule_commit_title_release_prefix_validation.py - Raysurfer Public Snippets