From e124770be091b354078ff57e3a697fcc063af4b8 Mon Sep 17 00:00:00 2001 From: Naveen Kumar Sangi Date: Sun, 11 Dec 2016 06:47:37 +0530 Subject: [PATCH] GitCommitBear.py: Require URL in commit message body This ensures that the git commit contains a URL that relates to an issue. Fixes https://github.com/coala/coala-bears/issues/1112 --- bears/vcs/git/GitCommitBear.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/bears/vcs/git/GitCommitBear.py b/bears/vcs/git/GitCommitBear.py index 9279be4265..6c8ad9fa11 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 @@ -197,6 +199,25 @@ def check_body(self, body, 'HEAD commit. Please add one.') return + urls = re.findall(r'(https|http?://\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))