| 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 |
|
| 30 |
name = "release-configuration-rule" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
id = "UCR1" |
| 35 |
|
| 36 |
|
| 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 |
|
| 43 |
|
| 44 |
if commit.message.title.startswith("Release"): |
| 45 |
|
| 46 |
|
| 47 |
config.ignore.append("body-trailing-whitespace") |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
config.set_rule_option("body-max-line-length", "line-length", 200) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
config.set_general_option("verbosity", 2) |
| 59 |
|
| 60 |
config.set_general_option("verbosity", self.options["custom-verbosity"].value) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
commit.message.body = [line for line in commit.message.body if not line.startswith("$")] |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
commit.my_property = "This is my property" |