Skip to content

Commit

Permalink
CommitBear.py: Check https use in issue references
Browse files Browse the repository at this point in the history
Check if commit message uses 'https' rather than 'http' to automatically
close the referenced issue for GitHub and GitLab. Error message now
includes host.

Closes coala#2683
Closes coala#2722
  • Loading branch information
samuelpiltch authored and ZamaSharik committed Nov 15, 2018
1 parent 1d23525 commit 3091131
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
14 changes: 7 additions & 7 deletions bears/vcs/CommitBear.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import abc
import logging
import nltk
Expand All @@ -25,11 +24,11 @@ class _CommitBear(GlobalBear):
ISSUE_INFO = {
'github': {
'issue': r'(?:\w+/\w+)?#(\d+)',
'full issue': r'https?://github\S+/issues/(\d+)',
'full issue': r'https://github\S+/issues/(\d+)',
},
'gitlab': {
'issue': r'(?:\w+/\w+)?#(\d+)',
'full issue': r'https?://gitlab\S+/issues/(\d+)',
'full issue': r'https://gitlab\S+/issues/(\d+)',
},
'bitbucket': {
'issue': r'#(\d+)',
Expand Down Expand Up @@ -386,8 +385,9 @@ def check_issue_reference(self, body,
for issue in re.split(compiled_concat_regex, match):
reference = compiled_issue_ref_regex.fullmatch(issue)
if not reference:
yield Result(self, 'Invalid {} reference: '
'{}'.format(self.issue_type, issue))
yield Result(self, 'Invalid {} {} reference: '
'{}'.format(host, self.issue_type,
issue))
elif not compiled_issue_no_regex.fullmatch(reference.group(1)):
yield Result(self, 'Invalid issue number: '
'{}'.format(issue))
yield Result(self, 'Invalid {} issue number: '
'{}'.format(host, issue))
27 changes: 14 additions & 13 deletions tests/vcs/git/GitCommitBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_check_issue_reference(self):
'Resolves https://bitbucket.org/user/repo/issues/1/')
self.assertEqual(self.run_uut(
body_close_issue=True),
['Invalid issue reference: '
['Invalid bitbucket issue reference: '
'https://bitbucket.org/user/repo/issues/1/'])

self.git_commit('Shortlog\n\n'
Expand Down Expand Up @@ -403,7 +403,7 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_enforce_issue_reference=True),
['Invalid issue reference: bug#1112'])
['Invalid bitbucket issue reference: bug#1112'])

self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
Expand All @@ -412,7 +412,8 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_enforce_issue_reference=True),
['Invalid issue reference: randomkeyword#1112'])
['Invalid bitbucket issue reference: '
'randomkeyword#1112'])

self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
Expand All @@ -431,7 +432,7 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_enforce_issue_reference=True),
['Invalid issue reference: bug'])
['Invalid bitbucket issue reference: bug'])

self.git_commit('Shortlog\n\n'
'First line, blablablablablabla.\n'
Expand All @@ -449,7 +450,7 @@ def test_check_issue_reference(self):
'Resolves https://bitbucket.org/user/repo/issues/1/')
self.assertEqual(self.run_uut(
body_close_issue=True),
['Invalid issue reference: '
['Invalid bitbucket issue reference: '
'https://bitbucket.org/user/repo/issues/1/'])

self.git_commit('Shortlog\n\n'
Expand Down Expand Up @@ -501,7 +502,7 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Invalid full issue reference: '
['Invalid github full issue reference: '
'https://github.com/user/repo.git'])
self.assert_no_msgs()

Expand All @@ -518,7 +519,7 @@ def test_check_issue_reference(self):
'Another line, blablablablablabla.\n'
'Fix #01112 and #111')
self.assertEqual(self.run_uut(body_close_issue=True,),
['Invalid issue number: #01112'])
['Invalid github issue number: #01112'])
self.assert_no_msgs()

# GitHub host with no full issue reference
Expand All @@ -529,7 +530,7 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Invalid full issue reference: #1112'])
['Invalid github full issue reference: #1112'])
self.assert_no_msgs()

# Invalid characters in issue number
Expand All @@ -540,7 +541,7 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Invalid full issue reference: #1112-3'])
['Invalid github full issue reference: #1112-3'])
self.assert_no_msgs()

# Adding GitLab remote for testing
Expand All @@ -566,7 +567,7 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Invalid full issue reference: '
['Invalid gitlab full issue reference: '
'https://gitlab.com/user/repo/issues/not_num'])
self.assert_no_msgs()

Expand All @@ -578,7 +579,7 @@ def test_check_issue_reference(self):
self.assertEqual(self.run_uut(
body_close_issue=True,
body_close_issue_full_url=True),
['Invalid full issue reference: '
['Invalid gitlab full issue reference: '
'www.google.com/issues/hehehe'])
self.assert_no_msgs()

Expand All @@ -588,7 +589,7 @@ def test_check_issue_reference(self):
'Another line, blablablablablabla.\n'
'Resolve #11 and close #notnum')
self.assertEqual(self.run_uut(body_close_issue=True,),
['Invalid issue reference: #notnum'])
['Invalid gitlab issue reference: #notnum'])
self.assert_no_msgs()

# Close issues in other repos
Expand All @@ -605,7 +606,7 @@ def test_check_issue_reference(self):
'Another line, blablablablablabla.\n'
'Fix #11 and close github#32')
self.assertEqual(self.run_uut(body_close_issue=True,),
['Invalid issue reference: github#32'])
['Invalid gitlab issue reference: github#32'])
self.assert_no_msgs()

# Last line enforce full URL
Expand Down
12 changes: 6 additions & 6 deletions tests/vcs/mercurial/HgCommitBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def test_check_issue_reference(self):
'Closes #01112')
self.assertEqual(self.run_uut(body_close_issue=True,
body_enforce_issue_reference=True),
['Invalid issue number: #01112'])
['Invalid bitbucket issue number: #01112'])

# Adding incompatible remote for testing
new_config_file = ('[paths]\n'
Expand Down Expand Up @@ -338,7 +338,7 @@ def test_check_issue_reference(self):
'Closes #01112')
self.assertEqual(self.run_uut(body_close_issue=True,
body_enforce_issue_reference=True),
['Invalid issue number: #01112'])
['Invalid bitbucket issue number: #01112'])

# No keywords and no issues
self.hg_commit('Shortlog\n\n'
Expand Down Expand Up @@ -384,7 +384,7 @@ def test_check_issue_reference(self):
'Another line, blablablablablabla.\n'
'Fix #01112 and #111')
self.assertEqual(self.run_uut(body_close_issue=True,),
['Invalid issue number: #01112'])
['Invalid bitbucket issue number: #01112'])
self.assert_no_msgs()

# Bitbucket host with no full issue reference
Expand All @@ -406,7 +406,7 @@ def test_check_issue_reference(self):
'Fix #1112-3')
self.assertEqual(self.run_uut(
body_close_issue=True),
['Invalid issue reference: #1112-3'])
['Invalid bitbucket issue reference: #1112-3'])
self.assert_no_msgs()

# One of the short references is broken
Expand All @@ -415,7 +415,7 @@ def test_check_issue_reference(self):
'Another line, blablablablablabla.\n'
'Resolve #11 and closes #notnum')
self.assertEqual(self.run_uut(body_close_issue=True),
['Invalid issue reference: #notnum'])
['Invalid bitbucket issue reference: #notnum'])
self.assert_no_msgs()

# Incorrect close issue other repo pattern
Expand All @@ -424,5 +424,5 @@ def test_check_issue_reference(self):
'Another line, blablablablablabla.\n'
'Fix #11 and close bitbucket#32')
self.assertEqual(self.run_uut(body_close_issue=True),
['Invalid issue reference: bitbucket#32'])
['Invalid bitbucket issue reference: bitbucket#32'])
self.assert_no_msgs()

0 comments on commit 3091131

Please sign in to comment.