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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
from gitlint.options import IntOption, ListOption, StrOption
from gitlint.rules import CommitRule, ConfigurationRule, RuleViolation
class GitContextRule(CommitRule):
"""Rule that tests whether we can correctly access certain gitcontext properties"""
name = "gïtcontext"
id = "UC1"
def validate(self, commit):
violations = [
RuleViolation(self.id, f"GitContext.current_branch: {commit.context.current_branch}", line_nr=1),
RuleViolation(self.id, f"GitContext.commentchar: {commit.context.commentchar}", line_nr=1),
]
return violations
class GitCommitRule(CommitRule):
"""Rule that tests whether we can correctly access certain commit properties"""
name = "gïtcommit"
id = "UC2"
def validate(self, commit):
violations = [
RuleViolation(self.id, f"GitCommit.branches: {commit.branches}", line_nr=1),
RuleViolation(self.id, f"GitCommit.custom_prop: {commit.custom_prop}", line_nr=1),
]
return violations
class GitlintConfigurationRule(ConfigurationRule):
"""Rule that tests whether we can correctly access the config as well as modify the commit message"""
name = "cönfigrule"
id = "UC3"
def apply(self, config, commit):
# We add a line to the commit message body that pulls a value from config, this proves we can modify the body
# and read the config contents
commit.message.body.append(f"{config.target} ") # trailing whitespace deliberate to trigger violation
# We set a custom property that we access in CommitRule, to prove we can add extra properties to the commit
commit.custom_prop = "foöbar"
# We also ignore some extra rules, proving that we can modify the config
config.ignore.append("B4")
class ConfigurableCommitRule(CommitRule):
"""Rule that tests that we can add configuration to user-defined rules"""
name = "configürable"
id = "UC4"
options_spec = [
IntOption("int-öption", 2, "int-öption description"),
StrOption("str-öption", "föo", "int-öption description"),
ListOption("list-öption", ["foo", "bar"], "list-öption description"),
]
def validate(self, _):
violations = [
RuleViolation(self.id, f"int-öption: {self.options['int-öption'].value}", line_nr=1),
RuleViolation(self.id, f"str-öption: {self.options['str-öption'].value}", line_nr=1),
RuleViolation(self.id, f"list-öption: {self.options['list-öption'].value}", line_nr=1),
]
return violations
|