Skip to content

Commit

Permalink
GitCommitBear.py: Require URL in commit body
Browse files Browse the repository at this point in the history
This ensures that the git commit contains a URL
that relates to an issue.

Fixes coala#1112
  • Loading branch information
nkprince007 committed Dec 14, 2016
1 parent 02b72ab commit 86237fc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coafile
Expand Up @@ -29,6 +29,7 @@ bears = SpaceConsistencyBear

[commit]
bears = GitCommitBear
host_site = github
shortlog_trailing_period = False
shortlog_regex = ([^:]*|[^:]+: [A-Z0-9*].*)

Expand Down
33 changes: 33 additions & 0 deletions bears/vcs/git/GitCommitBear.py
Expand Up @@ -3,6 +3,8 @@
import shutil
import os

from urllib.parse import urlparse

from coalib.bears.GlobalBear import GlobalBear
from coalib.bears.requirements.PipRequirement import PipRequirement
from coala_utils.ContextManagers import change_directory
Expand Down Expand Up @@ -175,6 +177,7 @@ def check_imperative(self, paragraph):
def check_body(self, body,
body_line_length: int=72,
force_body: bool=False,
host_site: str='',
ignore_length_regex: typed_list(str)=()):
"""
Checks the given commit body.
Expand All @@ -183,6 +186,8 @@ def check_body(self, body,
:param body_line_length: The maximum line-length of the body. The
newline character at each line end does not
count to the length.
:param host_site: The website used for hosting the git
repository.
:param force_body: Whether a body shall exist or not.
:param ignore_length_regex: Lines matching each of the regular
expressions in this list will be ignored.
Expand All @@ -197,6 +202,34 @@ def check_body(self, body,
'HEAD commit. Please add one.')
return

# Git keywords for closing issues
# https://help.github.com/articles/closing-issues-via-commit-messages/
git_keys = """close closes closed
fix fixes fixed
resolve resolves resolved""".split()

body_string = '\n'.join(body).lower()
if host_site != '' and any(key in body_string for key in git_keys):
urls = re.findall(r'(https?://\S+)', body_string)
good_url = False
for url in urls:
pieces = urlparse(url)
netloc = pieces.netloc
scheme = pieces.scheme
path = pieces.path
try:
assert all([scheme, netloc, path])
except AssertionError:
pass
if netloc.find(host_site) != -1 and path.find('issues') != -1:
good_url = True

if not good_url:
yield Result(self, 'No issue related URL found in '
'the body at HEAD commit. '
'Please add one.')
return

ignore_regexes = [re.compile(regex) for regex in ignore_length_regex]
if any((len(line) > body_line_length and
not any(regex.search(line) for regex in ignore_regexes))
Expand Down
25 changes: 25 additions & 0 deletions tests/vcs/git/GitCommitBearTest.py
Expand Up @@ -267,6 +267,31 @@ def test_body_checks(self):
[])
self.assertTrue(self.msg_queue.empty())

# Commit has neither keywords nor issues
self.git_commit('Shortlog\n\n'
'This line is ok.\n'
'This line is by far too long (in this case).\n'
'This one too, blablablablablablablablabla.')
self.assertEqual(self.run_uut(host_site='github',), [])

# Host site chosen and has an issue
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fix https://github.com/user/repo/issues/someissue')
self.assertEqual(self.run_uut(host_site='github',), [])

# Host site chosen and has keywords but no url
self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
'Another line, blablablablablabla.\n'
'Fix #123')
self.assertEqual(self.run_uut(host_site='github',),
['No issue related URL found in '
'the body at HEAD commit. '
'Please add one.'])
self.assert_no_msgs()

def test_different_path(self):
no_git_dir = mkdtemp()
self.git_commit('Add a very long shortlog for a bad project history.')
Expand Down

0 comments on commit 86237fc

Please sign in to comment.