How to use the gitlint.rules.CommitRule function in gitlint

To help you get started, we’ve selected a few gitlint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jorisroovers / gitlint / gitlint / rule_finder.py View on Github external
# Import the module
        try:
            importlib.import_module(module)

        except Exception as e:
            raise rules.UserRuleError(u"Error while importing extra-path module '{0}': {1}".format(module, ustr(e)))

        # Find all rule classes in the module. We do this my inspecting all members of the module and checking
        # 1) is it a class, if not, skip
        # 2) is the parent path the current module. If not, we are dealing with an imported class, skip
        # 3) is it a subclass of rule
        rule_classes.extend([clazz for _, clazz in inspect.getmembers(sys.modules[module])
                             if
                             inspect.isclass(clazz) and  # check isclass to ensure clazz.__module__ exists
                             clazz.__module__ == module and  # ignore imported classes
                             (issubclass(clazz, rules.LineRule) or issubclass(clazz, rules.CommitRule))])

        # validate that the rule classes are valid user-defined rules
        for rule_class in rule_classes:
            assert_valid_rule_class(rule_class)

    return rule_classes
github zulip / zulip-terminal / tools / gitlint-extra-rules.py View on Github external
from typing import Any, List, Optional

import gitlint
from gitlint.options import ListOption
from gitlint.rules import CommitRule, RuleViolation


class EndsWithDot(CommitRule):
    name = "title-doesn't-end-with-dot"
    id = "ZT1"

    def validate(self, commit: Any) -> Optional[List[RuleViolation]]:
        error = "Title does not end with a '.' character"
        if not commit.message.title.endswith("."):
            return [RuleViolation(self.id, error, line_nr=1)]


class AreaFormatting(CommitRule):
    name = "area-formatting"
    id = "ZT2"

    options_spec = [ListOption("exclusions", ["WIP"],
                               "Exclusions to area lower-case rule")]
github zephyrproject-rtos / zephyr / scripts / gitlint / zephyr_commit_rules.py View on Github external
# A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule).
    id = "UC6"

    # A rule MAY have an option_spec if its behavior should be configurable.
    options_spec = [IntOption('min-line-count', 2, "Minimum body line count excluding Signed-off-by")]

    def validate(self, commit):
        filtered = [x for x in commit.message.body if not x.lower().startswith("signed-off-by") and x != '']
        line_count = len(filtered)
        min_line_count = self.options['min-line-count'].value
        if line_count < min_line_count:
            message = "Body has no content, should at least have {} line.".format(min_line_count)
            return [RuleViolation(self.id, message, line_nr=1)]

class BodyMaxLineCount(CommitRule):
    # A rule MUST have a human friendly name
    name = "body-max-line-count"

    # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule).
    id = "UC1"

    # A rule MAY have an option_spec if its behavior should be configurable.
    options_spec = [IntOption('max-line-count', 3, "Maximum body line count")]

    def validate(self, commit):
        line_count = len(commit.message.body)
        max_line_count = self.options['max-line-count'].value
        if line_count > max_line_count:
            message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count)
            return [RuleViolation(self.id, message, line_nr=1)]
github jorisroovers / gitlint / examples / my_commit_rules.py View on Github external
# A rule MUST have a *unique* id, we recommend starting with UC (for User-defined Commit-rule).
    id = "UC1"

    # A rule MAY have an option_spec if its behavior should be configurable.
    options_spec = [IntOption('max-line-count', 3, "Maximum body line count")]

    def validate(self, commit):
        line_count = len(commit.message.body)
        max_line_count = self.options['max-line-count'].value
        if line_count > max_line_count:
            message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count)
            return [RuleViolation(self.id, message, line_nr=1)]


class SignedOffBy(CommitRule):
    """ This rule will enforce that each commit contains a "Signed-Off-By" line.
    We keep things simple here and just check whether the commit body contains a line that starts with "Signed-Off-By".
    """

    # A rule MUST have a human friendly name
    name = "body-requires-signed-off-by"

    # A rule MUST have a *unique* id, we recommend starting with UC (for User-defined Commit-rule).
    id = "UC2"

    def validate(self, commit):
        for line in commit.message.body:
            if line.startswith("Signed-Off-By"):
                return

        return [RuleViolation(self.id, "Body does not contain a 'Signed-Off-By' line", line_nr=1)]
github jorisroovers / gitlint / gitlint / contrib / rules / signedoff_by.py View on Github external
from gitlint.rules import CommitRule, RuleViolation


class SignedOffBy(CommitRule):
    """ This rule will enforce that each commit body contains a "Signed-Off-By" line.
    We keep things simple here and just check whether the commit body contains a line that starts with "Signed-Off-By".
    """

    name = "contrib-body-requires-signed-off-by"
    id = "CC1"

    def validate(self, commit):
        for line in commit.message.body:
            if line.startswith("Signed-Off-By"):
                return []

        return [RuleViolation(self.id, "Body does not contain a 'Signed-Off-By' line", line_nr=1)]
github jorisroovers / gitlint / examples / my_commit_rules.py View on Github external
"""
The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that
act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them
to the entire commit. This happens exactly once per commit.

A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on
an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks
that should only be done once per gitlint run.

While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if
that fits your needs.
"""


class BodyMaxLineCount(CommitRule):
    # A rule MUST have a human friendly name
    name = "body-max-line-count"

    # A rule MUST have a *unique* id, we recommend starting with UC (for User-defined Commit-rule).
    id = "UC1"

    # A rule MAY have an option_spec if its behavior should be configurable.
    options_spec = [IntOption('max-line-count', 3, "Maximum body line count")]

    def validate(self, commit):
        line_count = len(commit.message.body)
        max_line_count = self.options['max-line-count'].value
        if line_count > max_line_count:
            message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count)
            return [RuleViolation(self.id, message, line_nr=1)]
github jorisroovers / gitlint / gitlint / rules.py View on Github external
id = "B3"
    target = CommitMessageBody


class BodyFirstLineEmpty(CommitRule):
    name = "body-first-line-empty"
    id = "B4"

    def validate(self, commit):
        if len(commit.message.body) >= 1:
            first_line = commit.message.body[0]
            if first_line != "":
                return [RuleViolation(self.id, "Second line is not empty", first_line, 2)]


class BodyMinLength(CommitRule):
    name = "body-min-length"
    id = "B5"
    options_spec = [IntOption('min-length', 20, "Minimum body length")]

    def validate(self, commit):
        min_length = self.options['min-length'].value
        body_message_no_newline = "".join([line for line in commit.message.body if line is not None])
        actual_length = len(body_message_no_newline)
        if 0 < actual_length < min_length:
            violation_message = "Body message is too short ({0}<{1})".format(actual_length, min_length)
            return [RuleViolation(self.id, violation_message, body_message_no_newline, 3)]


class BodyMissing(CommitRule):
    name = "body-is-missing"
    id = "B6"
github zephyrproject-rtos / zephyr / scripts / gitlint / zephyr_commit_rules.py View on Github external
act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them
to the entire commit. This happens exactly once per commit.

A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on
an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks
that should only be done once per gitlint run.

While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if
that fits your needs.
"""

from gitlint.rules import CommitRule, RuleViolation, CommitMessageTitle, LineRule, CommitMessageBody
from gitlint.options import IntOption, StrOption
import re

class BodyMinLineCount(CommitRule):
    # A rule MUST have a human friendly name
    name = "body-min-line-count"

    # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule).
    id = "UC6"

    # A rule MAY have an option_spec if its behavior should be configurable.
    options_spec = [IntOption('min-line-count', 2, "Minimum body line count excluding Signed-off-by")]

    def validate(self, commit):
        filtered = [x for x in commit.message.body if not x.lower().startswith("signed-off-by") and x != '']
        line_count = len(filtered)
        min_line_count = self.options['min-line-count'].value
        if line_count < min_line_count:
            message = "Body has no content, should at least have {} line.".format(min_line_count)
            return [RuleViolation(self.id, message, line_nr=1)]