Skip to content

Commit

Permalink
GitCommitBear: Refactor issue regexes
Browse files Browse the repository at this point in the history
Increased modularity by delegating the task of selecting issue
regex to a new class variable

Closes #2396
  • Loading branch information
virresh committed May 4, 2018
1 parent afe0be4 commit 9566efc
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions bears/vcs/git/GitCommitBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class GitCommitBear(GlobalBear):
LICENSE = 'AGPL-3.0'
ASCIINEMA_URL = 'https://asciinema.org/a/e146c9739ojhr8396wedsvf0d'
CAN_DETECT = {'Formatting'}
ISSUE_INFO = {
'github': {
'issue': r'(?:\w+/\w+)?#(\S+)',
'full issue': r'https?://github\S+/issues/(\S+)',
},
'gitlab': {
'issue': r'(?:\w+/\w+)?#(\S+)',
'full issue': r'https?://gitlab\S+/issues/(\S+)',
},
}
SUPPORTED_HOST_KEYWORD_REGEX = {
'github': (r'[Cc]lose[sd]?'
r'|[Rr]esolve[sd]?'
Expand Down Expand Up @@ -305,25 +315,24 @@ def check_issue_reference(self, body,
return

host = self.get_host_from_remotes()
if host not in self.SUPPORTED_HOST_KEYWORD_REGEX:
if (host not in self.SUPPORTED_HOST_KEYWORD_REGEX or
host not in self.ISSUE_INFO):
return

if body_close_issue_full_url:
self.issueType = 'full issue'
else:
self.issueType = 'issue'

if body_close_issue_on_last_line:
if body:
body = body.splitlines()[-1]
result_message = ('Body of HEAD commit does not contain any {} '
'reference in the last line.')
else:
result_message = ('Body of HEAD commit does not contain any {} '
'reference.')

if body_close_issue_full_url:
result_info = 'full issue'
issue_ref_regex = (
r'https?://{}\S+/issues/(\S+)'.format(re.escape(host)))
result_message = ('Body of HEAD commit does not contain any '
'{} reference in the last line.'
).format(self.issueType)
else:
result_info = 'issue'
issue_ref_regex = r'(?:\w+/\w+)?#(\S+)'
result_message = ('Body of HEAD commit does not contain any '
'{} reference.').format(self.issueType)

concat_regex = '|'.join(kw for kw in self.CONCATENATION_KEYWORDS)
compiled_joint_regex = re.compile(
Expand All @@ -346,10 +355,11 @@ def check_issue_reference(self, body,
matches = compiled_joint_regex.findall(body)

if body_enforce_issue_reference and len(matches) == 0:
yield Result(self, result_message.format(result_info))
yield Result(self, result_message)
return

compiled_issue_ref_regex = re.compile(issue_ref_regex)
compiled_issue_ref_regex = re.compile(
self.ISSUE_INFO[host][self.issueType])
compiled_issue_no_regex = re.compile(r'[1-9][0-9]*')
compiled_concat_regex = re.compile(
r'\s*(?:{})\s*'.format(concat_regex))
Expand All @@ -359,7 +369,8 @@ def check_issue_reference(self, body,
reference = compiled_issue_ref_regex.fullmatch(issue)
if not reference:
yield Result(self, 'Invalid {} reference: '
'{}'.format(result_info, issue))
'{}'.format(
self.issueType, issue))
elif not compiled_issue_no_regex.fullmatch(reference.group(1)):
yield Result(self, 'Invalid issue number: '
'{}'.format(issue))

0 comments on commit 9566efc

Please sign in to comment.