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 11, 2016
1 parent 02b72ab commit e1230f7
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 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,
should_contain_url: bool=False,
ignore_length_regex: typed_list(str)=()):
"""
Checks the given commit body.
Expand All @@ -197,6 +200,27 @@ def check_body(self, body,
'HEAD commit. Please add one.')
return

if should_contain_url:
urls = re.findall(r'(https?://\S+)', body)
has_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])
if netloc.find('github') != -1 and path.find('issues'):
has_good_url = True
except AssertionError:
pass

if not has_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

0 comments on commit e1230f7

Please sign in to comment.