diff --git a/bears/vcs/git/GitCommitBear.py b/bears/vcs/git/GitCommitBear.py index 9279be4265..ae5f595708 100644 --- a/bears/vcs/git/GitCommitBear.py +++ b/bears/vcs/git/GitCommitBear.py @@ -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 @@ -175,6 +177,8 @@ def check_imperative(self, paragraph): def check_body(self, body, body_line_length: int=72, force_body: bool=False, + vcs_used: str='', + contains_url: bool=False, ignore_length_regex: typed_list(str)=()): """ Checks the given commit body. @@ -183,6 +187,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 vcs_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. @@ -197,6 +204,27 @@ def check_body(self, body, 'HEAD commit. Please add one.') return + if contains_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(vcs_used) != -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))