Debian Codegraph @

examples/my_configuration_rules.py

raw symbols SCIP
1 from gitlint.rules import ConfigurationRule
2 from gitlint.options import IntOption
3
4
5 """
6 Full details on user-defined rules: https://jorisroovers.com/gitlint/user_defined_rules
7
8 The ReleaseConfigurationRule class below is an example of a user-defined ConfigurationRule. Configuration rules are
9 gitlint rules that are applied once per commit and BEFORE any other rules are run. Configuration Rules are meant to
10 dynamically change gitlint's configuration and/or the commit that is about to be linted. A typically use-case for this
11 is modifying the behavior of gitlint's rules based on a commit contents.
12
13 Notes:
14 - Modifying the commit object DOES NOT modify the actual git commit message in the target repo, only gitlint's copy of
15 it.
16 - Modifying the config object only has effect on the commit that is being linted, subsequent commits will not
17 automatically inherit this configuration.
18 """
19
20
21 class ReleaseConfigurationRule(ConfigurationRule):
22 """
23 This rule will modify gitlint's behavior for Release Commits.
24
25 This example might not be the most realistic for a real-world scenario,
26 but is meant to give an overview of what's possible.
27 """
28
29 # A rule MUST have a human friendly name
30 name = "release-configuration-rule"
31
32 # A rule MUST have a *unique* id, we recommend starting with UCR
33 # (for User-defined Configuration-Rule), but this can really be anything.
34 id = "UCR1"
35
36 # A rule MAY have an option_spec if its behavior should be configurable.
37 options_spec = [IntOption("custom-verbosity", 2, "Gitlint verbosity for release commits")]
38
39 def apply(self, config, commit):
40 self.log.debug("ReleaseConfigurationRule: This will be visible when running `gitlint --debug`")
41
42 # If the commit title starts with 'Release', we want to modify
43 # how all subsequent rules interpret that commit
44 if commit.message.title.startswith("Release"):
45 # If your Release commit messages are auto-generated, the
46 # body might contain trailing whitespace. Let's ignore that
47 config.ignore.append("body-trailing-whitespace")
48
49 # Similarly, the body lines might exceed 80 chars,
50 # let's set gitlint's limit to 200
51 # To set rule options use:
52 # config.set_rule_option(<rule-name>, <rule-option>, <value>)
53 config.set_rule_option("body-max-line-length", "line-length", 200)
54
55 # For kicks, let's set gitlint's verbosity to 2
56 # To set general options use
57 # config.set_general_option(<general-option>, <value>)
58 config.set_general_option("verbosity", 2)
59 # Wwe can also use custom options to make this configurable
60 config.set_general_option("verbosity", self.options["custom-verbosity"].value)
61
62 # Strip any lines starting with $ from the commit message
63 # (this only affects how gitlint sees your commit message, it does
64 # NOT modify your actual commit in git)
65 commit.message.body = [line for line in commit.message.body if not line.startswith("$")]
66
67 # You can add any extra properties you want to the commit object, these will be available later on
68 # in all rules.
69 commit.my_property = "This is my property"