From a85f3954a8fe112640c2c35da3228be29b17c97c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 11:49:10 +0200 Subject: Adding upstream version 0.18.0. Signed-off-by: Daniel Baumann --- qa/samples/user_rules/extra/extra_rules.py | 72 ++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 qa/samples/user_rules/extra/extra_rules.py (limited to 'qa/samples/user_rules/extra/extra_rules.py') diff --git a/qa/samples/user_rules/extra/extra_rules.py b/qa/samples/user_rules/extra/extra_rules.py new file mode 100644 index 0000000..cad531b --- /dev/null +++ b/qa/samples/user_rules/extra/extra_rules.py @@ -0,0 +1,72 @@ +from gitlint.rules import CommitRule, RuleViolation, ConfigurationRule +from gitlint.options import IntOption, StrOption, ListOption + + +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[u'int-öption'].value}", line_nr=1), + RuleViolation(self.id, f"str-öption: {self.options[u'str-öption'].value}", line_nr=1), + RuleViolation(self.id, f"list-öption: {self.options[u'list-öption'].value}", line_nr=1), + ] + + return violations -- cgit v1.2.3