Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regex for commit title that skips body checks, if matches #57

Closed
wants to merge 2 commits into from
Closed

Conversation

SteffenKockel
Copy link

We needed a possibility to exclude release commits from our rigorous gitlint-checks.

This pull requests has the following to offer

  • An option to specify a regular expression and, if it matches on the commit messages title, skip further body checks for the given commit.
  • An option to set the loglevel from commandline. This behavior was splitted between -d and -v[vvv]

Copy link
Contributor

@bdrung bdrung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new test case would be nice.

@@ -44,6 +44,8 @@

# [body-min-length]
# min-length=5
# ignore-body-checks-regex=''

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary empty line

@@ -79,14 +81,14 @@ def __eq__(self, other):
return equal

def __str__(self):
return sstr(self) # pragma: no cover
return self.__unicode__() # pragma: no cover
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use self.__unicode__()? Does this work with Python 2 too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this works also with python 2 (Tested on python 2.7.14)

gitlint/rules.py Outdated
@@ -243,14 +245,23 @@ def validate(self, commit):
if first_line != "":
return [RuleViolation(self.id, "Second line is not empty", first_line, 2)]


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore the newline. Otherwise pep8 will complain.

gitlint/rules.py Outdated

def validate(self, commit):
min_length = self.options['min-length'].value
regex = self.options['ignore-body-checks-regex'].value
regex_valid = True if (self.options['ignore-body-checks-regex'].value) else False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regex_valid is not needed (see below).

gitlint/rules.py Outdated
regex = self.options['ignore-body-checks-regex'].value
regex_valid = True if (self.options['ignore-body-checks-regex'].value) else False

if regex_valid and re.search(regex, commit.message.title):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can directly check for regex:

         if regex and re.search(regex, commit.message.title): 

gitlint/rules.py Outdated

def validate(self, commit):
regex = self.options['ignore-body-checks-regex'].value
regex_valid = True if (self.options['ignore-body-checks-regex'].value) else False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

gitlint/rules.py Outdated
options_spec = [BoolOption('ignore-merge-commits', True, "Ignore merge commits")]
# This needs to come from the real options
options_spec = [BoolOption('ignore-merge-commits', True, "Ignore merge commits"),
StrOption('ignore-body-checks-regex', '^Release', 'Ignore body checks for tiles matching regex')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default of ignore-body-checks-regex should be empty.

gitlint/rules.py Outdated
if regex_valid and re.search(regex, commit.message.title):
logmsg = "Skipping %s (%s), because ignore-body-checks-regex matches." % (self.id, self.name)
LOG.info(logmsg)
return []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent, just return nothing.

@@ -77,12 +77,12 @@ def test_body_min_length(self):
commit = self.gitcommit("Title\n\nThis is the second body line\n")

violations = rule.validate(commit)
self.assertIsNone(violations)
self.assertIn(violations, [[],None])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not needed if you return None.


# assert no error - no body
commit = self.gitcommit(u"Tïtle\n")
violations = rule.validate(commit)
self.assertIsNone(violations)
self.assertIn(violations, [[], None])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not needed if you return None.

@SteffenKockel SteffenKockel changed the title Dear Maintainer Add regex for commit title that skips body checks, if matches Mar 5, 2018
gitlint/cli.py Outdated
@@ -122,14 +122,15 @@ def stdin_has_data():
help="Verbosity, more v's for more verbose output (e.g.: -v, -vv, -vvv). [default: -vvv]", )
@click.option('-s', '--silent', help="Silent mode (no output). Takes precedence over -v, -vv, -vvv.", is_flag=True)
@click.option('-d', '--debug', help="Enable debugging output.", is_flag=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--debug should be either removed or mapped to loglevel=debug.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks even more tests and is not what we want to achieve here. Rejected.

commit = self.gitcommit(u"Release foo-bar-baz 0.12.3")
violations = rule.validate(commit)
self.assertIsNone(violations)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test should go into a separate test method.

Copy link
Author

@SteffenKockel SteffenKockel Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefit of separate test cases is that it easier to spot which part breaks and it prevents hiding failures (the test will abort on the first assertion error).

commit = self.gitcommit(u"Release foo-bar-baz 0.12.3")
violations = rule.validate(commit)
self.assertIsNone(violations)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test should go into a separate test method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

Steffen Kockel added 2 commits March 6, 2018 17:07
Some commits don't need a body. This adds an option (B5.ignore-body-checks-regex) to
specify a regex. If this regex matches commit.message.title, further body checks are
skipped.

refs #39774

Signed-off-by: Steffen Kockel <steffen.kockel@profitbricks.com>
Signed-off-by: Steffen Kockel <steffen.kockel@profitbricks.com>
@jorisroovers
Copy link
Owner

It doesn't entirely sit right with me is that this is being implemented as an option for specific rules.

IMO the solution should be generic.

More something along the lines of:

[ignore.title-match]
regex=^Release(.*)
ignore=T1,body-min-length,B6
# Note that like with general ignore, ignore accepts both names and ids

[ignore.body-line-match]
regex=^Hotfix
ignore=all

# This can be added later
[ignore.meta-author-match]
regex=bossman@company.com
ignore=all

There's several hours of work involved with implementing this I think (and like usually, the majority of time spent on tests and docs).

Interested to hear your thoughts.

Let me know if you intend on working on this, if not, I can probably look at it at some point (today, next month, later this year, really depends on when I find/make time).

@jorisroovers jorisroovers added the enhancement User-facing feature enhancements label Mar 30, 2018
@jorisroovers
Copy link
Owner

Put some more thought into this, I think I've got a better solution.

We can introduce a new type of rule IgnoreRule that are ran before other rules and when they match, gitlint will stop running other rules for that particular commit.

The nice thing about this approach is that it's generic and that other users can easily extend it by building their own custom IgnoreRules - similarly to the existing functionality for building user-defined rules.

Gitlint would then just implement a couple of built-in ignore-rules that are set to ignore nothing by default, but can be configured like so:

[ignore-by-title]
regex="^Release(.*)"

[ignore-by-body-line]
regex="^Hotfix(.*)"

I'll work on this :-)

@jorisroovers jorisroovers self-assigned this Mar 30, 2018
jorisroovers pushed a commit that referenced this pull request Mar 30, 2018
Very much WIP, still need to write/fix tests, docs, etc.

This is part of #54 and #57.
jorisroovers pushed a commit that referenced this pull request Mar 30, 2018
Added a new IgnoreByTitle rule that is based on a new more generic rule
construct called ConfigurationRule.

ConfigurationRules can modify gitlint configuration (and therefore
behavior) on a per commit basis.

Added some tests and docs, more docs and integration tests to follow in
additional commits.

This is part of #54 and #57.
@jorisroovers
Copy link
Owner

This was implemented as part of 7d44795 and 52dc67e.

Will be released fairly soon as part of v0.10.0. Closing this out.

@SteffenKockel
Copy link
Author

This was actually my first contact with this code. Thanks for incorporating the requested feature. No bad feelings here. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement User-facing feature enhancements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants