Debian Codegraph @

qa/samples/user_rules/extra/extra_rules.py

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