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 12, 2016
1 parent 02b72ab commit d56e6d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .coafile
Expand Up @@ -29,6 +29,8 @@ bears = SpaceConsistencyBear

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

Expand Down
32 changes: 31 additions & 1 deletion 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 @@ -50,7 +52,9 @@ def get_metadata(cls):
cls.get_shortlog_checks_metadata(),
cls.get_body_checks_metadata())

def run(self, allow_empty_commit_message: bool = False, **kwargs):
def run(self,
allow_empty_commit_message: bool = False,
**kwargs):
"""
Check the current git commit message at HEAD.
Expand Down Expand Up @@ -175,6 +179,8 @@ def check_imperative(self, paragraph):
def check_body(self, body,
body_line_length: int=72,
force_body: bool=False,
host_used: str='',
contains_url: bool=False,
ignore_length_regex: typed_list(str)=()):
"""
Checks the given commit body.
Expand All @@ -183,6 +189,9 @@ 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_used: The version control system used.
:param contains_url: Whether the body contains a issue related
URL or not.
: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 +206,27 @@ def check_body(self, body,
'HEAD commit. Please add one.')
return

if contains_url:
urls = re.findall(r'(https?://\S+)', '\n'.join(body))
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_used) != -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

0 comments on commit d56e6d6

Please sign in to comment.