From 9eda6fcb70c66dd5dda175442d760f6ef64c1476 Mon Sep 17 00:00:00 2001 From: Makman2 Date: Mon, 2 Oct 2017 16:37:09 +0200 Subject: [PATCH 1/7] labhub/assign: Allow multiple issue formats ... to be defined inside code easily by using custom parser functions. --- plugins/labhub.py | 36 ++++++++++++++++++++++++++++-------- tests/labhub_test.py | 4 ++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/plugins/labhub.py b/plugins/labhub.py index 7a284918..4443c399 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -7,7 +7,7 @@ import github3 from IGitt.GitHub.GitHub import GitHub, GitHubToken from IGitt.GitLab.GitLab import GitLab, GitLabPrivateToken -from errbot import BotPlugin, re_botcmd +from errbot import BotPlugin, arg_botcmd, re_botcmd from plugins import constants @@ -242,14 +242,34 @@ def mark_cmd(self, msg, match): bot_prefix=self.bot_config.BOT_PREFIX) ) - @re_botcmd(pattern=r'^assign\s+https://(github|gitlab)\.com/([^/]+)/([^/]+/)+issues/(\d+)', # Ignore LineLengthBear, PyCodeStyleBear - re_cmd_name_help='assign ', - flags=re.IGNORECASE) - def assign_cmd(self, msg, match): + @arg_botcmd('issue_reference', type=str) + def assign(self, msg, issue_reference): """Assign to GitLab and GitHub issues.""" # Ignore QuotesBear - org = match.group(2) - repo_name = match.group(3)[:-1] - iss_number = match.group(4) + + # Complete URL to issue + def process_full_url(issue_reference): + rgx = r'https://(github|gitlab)\.com/([^/]+)/([^/]+/)+issues/(\d+)' + m = re.fullmatch(rgx, issue_reference, re.IGNORECASE) + + if m is None: + return None + + return m.group(2), m.group(3)[:-1], m.group(4) + + issue_processors = [ + process_full_url + ] + + for issue_processor in issue_processors: + issue_data = issue_processor(issue_reference) + + if issue_data is not None: + break + else: + yield 'Invalid issue.' + return + + org, repo_name, iss_number = issue_data user = msg.frm.nick diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 272939ca..6455fe8a 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -148,6 +148,10 @@ def test_assign_cmd(self): 'coala developers': mock_dev_team, 'coala maintainers': mock_maint_team} + # invalid issue format + testbot.assertCommand('!assign this-is-an-invalid-issue-xyz', + 'Invalid issue.') + cmd = '!assign https://github.com/{}/{}/issues/{}' # no assignee, not newcomer mock_issue.assignees = tuple() From 1595a3d68e2a839a110404595eec2d7f21a29a4e Mon Sep 17 00:00:00 2001 From: Makman2 Date: Mon, 2 Oct 2017 16:38:22 +0200 Subject: [PATCH 2/7] labhub/assign: Support short issue references ... of the form `coala/corobo#12`. --- plugins/labhub.py | 13 ++++++++++++- tests/labhub_test.py | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/plugins/labhub.py b/plugins/labhub.py index 4443c399..7419eea6 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -256,8 +256,19 @@ def process_full_url(issue_reference): return m.group(2), m.group(3)[:-1], m.group(4) + # Short issue reference (e.g. `coala/corobo#12`) + def process_short_ref(issue_reference): + rgx = r'(.+?)/(.+?)#(\d+)' + m = re.fullmatch(rgx, issue_reference, re.IGNORECASE) + + if m is None: + return None + + return m.group(1), m.group(2), m.group(3) + issue_processors = [ - process_full_url + process_full_url, + process_short_ref ] for issue_processor in issue_processors: diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 6455fe8a..8f1f42a7 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -152,6 +152,12 @@ def test_assign_cmd(self): testbot.assertCommand('!assign this-is-an-invalid-issue-xyz', 'Invalid issue.') + # test short issue reference + mock_issue.assignees = tuple() + self.mock_team.is_member.return_value = False + testbot.assertCommand('!assign coala/a#23', + "You've been assigned to the issue") + cmd = '!assign https://github.com/{}/{}/issues/{}' # no assignee, not newcomer mock_issue.assignees = tuple() From b67c4d56b3f47ce27fdaf328d546fdd1f43e4191 Mon Sep 17 00:00:00 2001 From: Makman2 Date: Mon, 2 Oct 2017 16:39:25 +0200 Subject: [PATCH 3/7] labhub/assign: Support super-short issue ref ... of the form `#1234`. Closes https://github.com/coala/corobo/issues/167 --- plugins/labhub.py | 22 +++++++++++++++++++++- tests/labhub_test.py | 6 ++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/plugins/labhub.py b/plugins/labhub.py index 7419eea6..89eb80b1 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -266,9 +266,29 @@ def process_short_ref(issue_reference): return m.group(1), m.group(2), m.group(3) + # Super short issue reference (e.g. `#1234`) + def process_super_short_ref(issue_reference): + issue_rgx = r'#(\d+)' + issue_reference_match = re.fullmatch(issue_rgx, issue_reference) + + if issue_reference_match is None: + return None + + roomname_rgx = r'(.+?)/(.+)' + roomname_match = re.fullmatch( + roomname_rgx, msg.frm.room.uri, re.IGNORECASE) + + if roomname_match is None: + return None + + return (roomname_match.group(1), + roomname_match.group(2), + issue_reference_match.group(1)) + issue_processors = [ process_full_url, - process_short_ref + process_short_ref, + process_super_short_ref ] for issue_processor in issue_processors: diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 8f1f42a7..16ff3fd7 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -158,6 +158,12 @@ def test_assign_cmd(self): testbot.assertCommand('!assign coala/a#23', "You've been assigned to the issue") + # test super-short issue reference + mock_issue.assignees = tuple() + self.mock_team.is_member.return_value = False + testbot.assertCommand('!assign #23', + "You've been assigned to the issue") + cmd = '!assign https://github.com/{}/{}/issues/{}' # no assignee, not newcomer mock_issue.assignees = tuple() From 99b8e91f0c9f18c5e52555531aaea62dcf0f0af8 Mon Sep 17 00:00:00 2001 From: Makman2 Date: Sun, 19 Nov 2017 22:19:13 +0100 Subject: [PATCH 4/7] plugins/labhub: Fix typo in function name --- plugins/labhub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/labhub.py b/plugins/labhub.py index 89eb80b1..e9d7c086 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -141,7 +141,7 @@ def callback_message(self, msg): @re_botcmd(pattern=r'(?:new|file) issue ([\w-]+?)(?: |\n)(.+?)(?:$|\n((?:.|\n)*))', # Ignore LineLengthBear, PyCodeStyleBear re_cmd_name_help='new issue repo-name title\n[description]', flags=re.IGNORECASE) - def create_issut_cmd(self, msg, match): + def create_issue_cmd(self, msg, match): """Create issues on GitHub and GitLab repositories.""" # Ignore QuotesBear, LineLengthBear, PyCodeStyleBear repo_name = match.group(1) iss_title = match.group(2) From 090dd04ce6a733d2d0146b3b1ecda84184648362 Mon Sep 17 00:00:00 2001 From: Makman2 Date: Sun, 19 Nov 2017 22:24:38 +0100 Subject: [PATCH 5/7] labhub_test: Remove unused imports --- tests/labhub_test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 16ff3fd7..1a90de4c 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -1,9 +1,7 @@ import logging -import os import queue -import time import unittest -from unittest.mock import Mock, MagicMock, create_autospec, PropertyMock, patch +from unittest.mock import MagicMock, create_autospec, PropertyMock, patch import github3 import IGitt @@ -13,7 +11,6 @@ from errbot.backends.test import TestBot import plugins.labhub -from plugins.labhub import LabHub from tests.helper import plugin_testbot From 86eec4713cd801e547b5f87528015cdbededb99b Mon Sep 17 00:00:00 2001 From: Makman2 Date: Mon, 20 Nov 2017 02:24:04 +0100 Subject: [PATCH 6/7] labhub_test: Add blank line to conform with PEP8 ... to separate the import section from code with 2 blank lines. --- tests/labhub_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 1a90de4c..c50d31dd 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -14,6 +14,7 @@ from tests.helper import plugin_testbot + class TestLabHub(unittest.TestCase): def setUp(self): From 244084b43d674764a8fa5dabaf0a95ab95836b0f Mon Sep 17 00:00:00 2001 From: Makman2 Date: Mon, 20 Nov 2017 02:24:58 +0100 Subject: [PATCH 7/7] wip --- plugins/labhub.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/labhub.py b/plugins/labhub.py index e9d7c086..d4590685 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -276,7 +276,7 @@ def process_super_short_ref(issue_reference): roomname_rgx = r'(.+?)/(.+)' roomname_match = re.fullmatch( - roomname_rgx, msg.frm.room.uri, re.IGNORECASE) + roomname_rgx, msg.to.name, re.IGNORECASE) if roomname_match is None: return None