Checkpoint: 4/9/2019
authorJude N <juden@pwan.org>
Wed, 10 Apr 2019 02:36:21 +0000 (22:36 -0400)
committerJude N <juden@pwan.org>
Wed, 10 Apr 2019 02:36:21 +0000 (22:36 -0400)
.gitignore
plugins/marge.py
setup.cfg
setup.py
tests/test_marge.py

index a0b845a..45c865f 100644 (file)
@@ -8,5 +8,5 @@ env
 htmlcov
 plugins/__pycache__
 MANIFEST
 htmlcov
 plugins/__pycache__
 MANIFEST
-Margebot.egg-info
+Marge.egg-info
 *~
 *~
index 0cbe525..2764209 100755 (executable)
@@ -3,7 +3,6 @@ Margebot: A Errbot Plugin for Gitlab MR reminders
 """
 import re
 from datetime import datetime, timezone
 """
 import re
 from datetime import datetime, timezone
-from time import sleep
 from dateutil import parser
 from dateutil.tz import tzutc
 from dateutil.relativedelta import relativedelta
 from dateutil import parser
 from dateutil.tz import tzutc
 from dateutil.relativedelta import relativedelta
@@ -11,50 +10,6 @@ from errbot import BotPlugin, arg_botcmd, botcmd, re_botcmd, webhook
 from errbot.templating import tenv
 from errcron.bot import CrontabMixin
 import gitlab
 from errbot.templating import tenv
 from errcron.bot import CrontabMixin
 import gitlab
-import requests
-
-class MargeGitlab(gitlab.Gitlab):
-    """
-    Subclass gitlab.Gitlab so extra_data args can be added
-    to the addprojecthook() and editprojecthook() methods
-    """
-
-    def __init__(self, host, token="", oauth_token="", verify_ssl=True):
-        super().__init__(host, token, oauth_token, verify_ssl)
-
-    def addprojecthook_extra(self, project_id, url, push=False, issues=False, merge_requests=False, tag_push=False, extra_data=None):
-        """
-        A copy parent addprojecthook with an extra_data field
-        """
-        data = {"id": project_id, "url": url}
-        if extra_data:
-            for ed_key, ed_value in extra_data.items():
-                data[ed_key] = ed_value
-        data['push_events'] = int(bool(push))
-        data['issues_events'] = int(bool(issues))
-        data['merge_requests_events'] = int(bool(merge_requests))
-        data['tag_push_events'] = int(bool(tag_push))
-        request = requests.post("{0}/{1}/hooks".format(self.projects_url, project_id),
-                                headers=self.headers, data=data, verify=self.verify_ssl)
-        if request.status_code == 201:
-            return request.json()
-        return False
-
-    def editprojecthook_extra(self, project_id, hook_id, url, push=False, issues=False, merge_requests=False, tag_push=False, extra_data=None):
-        """
-        A copy of the parent editprojecthook with an extra_data field
-        """
-        data = {"id": project_id, "hook_id": hook_id, "url": url}
-        if extra_data:
-            for ed_key, ed_value in extra_data.items():
-                data[ed_key] = ed_value
-        data['push_events'] = int(bool(push))
-        data['issues_events'] = int(bool(issues))
-        data['merge_requests_events'] = int(bool(merge_requests))
-        data['tag_push_events'] = int(bool(tag_push))
-        request = requests.put("{0}/{1}/hooks/{2}".format(self.projects_url, project_id, hook_id),
-                               headers=self.headers, data=data, verify=self.verify_ssl)
-        return request.status_code == 200
 
 
 def deltastr(any_delta):
 
 
 def deltastr(any_delta):
@@ -143,7 +98,7 @@ class Marge(BotPlugin, CrontabMixin):
         Marge.CRONTAB = ['{} .crontab_hook'.format(self.config['CRONTAB'])]
         gitlab_auth_token = self.config['GITLAB_ADMIN_TOKEN']
         verify_ssl = self.config['VERIFY_SSL']
         Marge.CRONTAB = ['{} .crontab_hook'.format(self.config['CRONTAB'])]
         gitlab_auth_token = self.config['GITLAB_ADMIN_TOKEN']
         verify_ssl = self.config['VERIFY_SSL']
-        self.gitlab = MargeGitlab(self.git_host, gitlab_auth_token, verify_ssl=verify_ssl)
+        self.gitlab = gitlab.Gitlab(self.git_host, gitlab_auth_token, verify_ssl=verify_ssl)
         self.activate_crontab()
 
         self.soak_delta = relativedelta(hours=self.config['CRONTAB_SOAK_HOURS'])
         self.activate_crontab()
 
         self.soak_delta = relativedelta(hours=self.config['CRONTAB_SOAK_HOURS'])
@@ -441,7 +396,7 @@ class Marge(BotPlugin, CrontabMixin):
             return msg
         else:
             for a_hook in hooks:
             return msg
         else:
             for a_hook in hooks:
-                self.log.info('a_hook: {}'.format(a_hook))
+                self.log.info('a_hook: {} {}'.format(a_hook, self.webhook_url))
                 if a_hook['merge_requests_events'] and a_hook['url'].startswith(self.webhook_url):
                     marge_hook = a_hook
                     break
                 if a_hook['merge_requests_events'] and a_hook['url'].startswith(self.webhook_url):
                     marge_hook = a_hook
                     break
@@ -456,12 +411,12 @@ class Marge(BotPlugin, CrontabMixin):
                 self.log.info('watchrepo: {}'.format(msg))
                 return msg
             else:
                 self.log.info('watchrepo: {}'.format(msg))
                 return msg
             else:
-                hook_updated = self.gitlab.editprojecthook_extra(target_project_id, marge_hook['id'], url, merge_requests=True, extra_data={'enable_ssl_verification': False})
+                hook_updated = self.gitlab.editprojecthook(target_project_id, marge_hook['id'], url, merge_requests=True)
                 s_watch_msg = "Updating room list for {} MRs from {} to {}".format(repo, old_rooms, rooms)
                 s_action = "update"
         else:
                 s_watch_msg = "Updating room list for {} MRs from {} to {}".format(repo, old_rooms, rooms)
                 s_action = "update"
         else:
-            hook_updated = self.gitlab.addprojecthook_extra(target_project_id, url, merge_requests=True, extra_data={'enable_ssl_verification': False})
-            s_watch_msg = "Now watching for new MRs in the {} repo to the {} roomi(s)".format(repo, rooms)
+            hook_updated = self.gitlab.addprojecthook(target_project_id, url, merge_requests=True)
+            s_watch_msg = "Now watching for new MRs in the {} repo to the {} room(s)".format(repo, rooms)
             s_action = "add"
 
         if not hook_updated:
             s_action = "add"
 
         if not hook_updated:
@@ -513,15 +468,6 @@ class Marge(BotPlugin, CrontabMixin):
 
     # pragma pylint: disable=unused-argument
 
 
     # pragma pylint: disable=unused-argument
 
-    @botcmd()
-    def xyzzy(self, msg, args):
-        """
-        Don't call this command...
-        """
-        yield "/me whispers \"All open MRs have been merged into master.\""
-        sleep(5)
-        yield "(just kidding)"
-
     @re_botcmd(pattern=r"I blame marge(bot)?", prefixed=False, flags=re.IGNORECASE)
     def dont_blame_margebot(self, msg, match):
         """
     @re_botcmd(pattern=r"I blame marge(bot)?", prefixed=False, flags=re.IGNORECASE)
     def dont_blame_margebot(self, msg, match):
         """
@@ -544,18 +490,17 @@ class Marge(BotPlugin, CrontabMixin):
         return "More like MargeFest, amirite ?"
 
     @re_botcmd(pattern=r"margebot sucks", prefixed=False, flags=re.IGNORECASE)
         return "More like MargeFest, amirite ?"
 
     @re_botcmd(pattern=r"margebot sucks", prefixed=False, flags=re.IGNORECASE)
-    def bring_it_up_with_the_steering_committee(self, msg, args):
+    def margebot_sucks(self, msg, args):
         """
         Bring it up with the committee
         """
         return "Bring it up with the Margebot steering committee."
 
     @re_botcmd(pattern=r".*", prefixed=True)
         """
         Bring it up with the committee
         """
         return "Bring it up with the Margebot steering committee."
 
     @re_botcmd(pattern=r".*", prefixed=True)
-    def catchall(self, msg,args):
-       """
-       Don't have the bot complain about unknown commands if the first word in a msg is its name
-       """
-       return
-
+    def catchall(self, msg, args):
+        """
+        Don't have the bot complain about unknown commands if the first word in a msg is its name
+        """
+        return
 
     # pragma pylint: enable=unused-argument
 
     # pragma pylint: enable=unused-argument
index 2103725..6eb7cd1 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -2,6 +2,6 @@
 test = pytest
 
 [tool:pytest]
 test = pytest
 
 [tool:pytest]
-addopts = --cov=plugins --cov-report term-missing --cov-report html --pep8 --pylint --pylint-rcfile=.pylintrc
+addopts = --cov=plugins --cov-report term-missing --cov-report html --pep8 --pylint --pylint-rcfile=.pylintrc --ff -x
 pep8ignore =
    *.py E501
\ No newline at end of file
 pep8ignore =
    *.py E501
\ No newline at end of file
index 06eac90..35f5db5 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -3,15 +3,13 @@ Setup.py
 """
 # pragma pylint: disable=invalid-name
 from distutils.errors import DistutilsSetupError
 """
 # pragma pylint: disable=invalid-name
 from distutils.errors import DistutilsSetupError
-from distutils.filelist import FileList
-import os
 from setuptools import setup
 import setuptools.command.sdist
 import setuptools.command.upload
 import setuptools.command.install
 
 from setuptools import setup
 import setuptools.command.sdist
 import setuptools.command.upload
 import setuptools.command.install
 
-# Errbot plugins aren't distributed as eggs so stub out the sdist, install and upload commands
 
 
+# Errbot plugins aren't distributed as eggs so stub out the sdist, install and upload commands
 class ErrbotSdistCommand(setuptools.command.sdist.sdist):
     """
     No sdist command for now.
 class ErrbotSdistCommand(setuptools.command.sdist.sdist):
     """
     No sdist command for now.
@@ -32,7 +30,7 @@ class ErrbotInstallCommand(setuptools.command.install.install):
     """
     Short circuit the install command
     """
     """
     Short circuit the install command
     """
-    def run(self): 
+    def run(self):
         raise DistutilsSetupError("No install command - copy the ./plugin files to a 'Marge' directory under your Errbot's plugins directory.")
 
 
         raise DistutilsSetupError("No install command - copy the ./plugin files to a 'Marge' directory under your Errbot's plugins directory.")
 
 
@@ -59,8 +57,8 @@ setup(
     tests_require=tests_required,
 
     cmdclass={
     tests_require=tests_required,
 
     cmdclass={
-        'install' : ErrbotInstallCommand,
-        'sdist' : ErrbotSdistCommand,
-        'upload' : ErrbotUploadCommand,
+        'install': ErrbotInstallCommand,
+        'sdist': ErrbotSdistCommand,
+        'upload': ErrbotUploadCommand,
     }
 )
     }
 )
index 8e9d450..2fc7429 100644 (file)
@@ -1,5 +1,5 @@
 """
 """
-Mrgebot tests
+Margebot tests
 """
 # pragma pylint: disable=invalid-name
 # pragma pylint: disable=missing-docstring
 """
 # pragma pylint: disable=invalid-name
 # pragma pylint: disable=missing-docstring
@@ -12,12 +12,14 @@ Mrgebot tests
 from datetime import datetime
 import logging
 import json
 from datetime import datetime
 import logging
 import json
+from queue import Empty
 
 
-import errbot
-from errbot.backends.test import testbot   # pylint: disable=unused-import
 import gitlab
 from dateutil.relativedelta import relativedelta
 import pytest
 import gitlab
 from dateutil.relativedelta import relativedelta
 import pytest
+import requests
+import errbot
+from errbot.backends.test import testbot   # pylint: disable=unused-import
 from plugins.marge import deltastr
 
 
 from plugins.marge import deltastr
 
 
@@ -31,9 +33,13 @@ class TestMarge(object):
     extra_plugin_dir = "./plugins/"
     loglevel = logging.INFO
 
     extra_plugin_dir = "./plugins/"
     loglevel = logging.INFO
 
+    @pytest.fixture(autouse=True)
+    def no_requests(self, monkeypatch):
+        monkeypatch.setattr(requests.sessions.Session, 'request', None)
+
     @pytest.fixture
     def margebot(self, testbot, monkeypatch, mocker):
     @pytest.fixture
     def margebot(self, testbot, monkeypatch, mocker):
-        testbot.push_message("!plugin config Marge {'CHATROOM_HOST': 'conference.test.com', 'GITLAB_HOST': 'gitlab.test.com', 'GITLAB_ADMIN_TOKEN': 'fake-token', 'CRONTAB':  '0 * * * *', 'VERIFY_SSL': True, 'CRONTAB_SOAK_HOURS': 1, 'WEBHOOK_URL': 'https://webhood.errbot.com:3142/margebot'}")
+        testbot.push_message("!plugin config Marge {'CHATROOM_HOST': 'conference.test.com', 'GITLAB_HOST': 'gitlab.test.com', 'GITLAB_ADMIN_TOKEN': 'fake-token', 'CRONTAB':  '0 * * * *', 'VERIFY_SSL': True, 'CRONTAB_SOAK_HOURS': 1, 'WEBHOOK_URL': 'https://webhook.errbot.com:3142/margebot'}")
         testbot.pop_message()
         testbot.push_message("!plugin config Webserver {'HOST': '0.0.0.0', 'PORT':3141, 'SSL': {'certificate': '', 'enabled': False, 'host': '0.0.0.0', 'key': '', 'port': 3142}}")
         testbot.pop_message()
         testbot.pop_message()
         testbot.push_message("!plugin config Webserver {'HOST': '0.0.0.0', 'PORT':3141, 'SSL': {'certificate': '', 'enabled': False, 'host': '0.0.0.0', 'key': '', 'port': 3142}}")
         testbot.pop_message()
@@ -138,13 +144,49 @@ class TestMarge(object):
                 return [{'user': 'user1', 'id': 3001}, {'user': 'user2', 'id': 3002}]
         monkeypatch.setattr(gitlab.Gitlab, 'getusers', mock_getusers)
 
                 return [{'user': 'user1', 'id': 3001}, {'user': 'user2', 'id': 3002}]
         monkeypatch.setattr(gitlab.Gitlab, 'getusers', mock_getusers)
 
+        def mock_getproject(self, repo):
+            return {
+                'id': 'projectid'
+            }
+        monkeypatch.setattr(gitlab.Gitlab, 'getproject', mock_getproject)
+
+        def mock_getprojecthooks(self, repo):
+            if repo == "(missing)":
+                return []
+            else:
+                return [{'id': 'hook_id', 'merge_requests_events': True, 'url': 'url'}]
+        monkeypatch.setattr(gitlab.Gitlab, 'getprojecthooks', mock_getprojecthooks)
+
         # Waiting on Gitlab 8.9 or greater
         # def mock_getapprovals(id):
         #      return []
         # monkeypatch.setattr(gitlab.Gitlab, 'getapprovals', mock_getapprovals)
 
         # Waiting on Gitlab 8.9 or greater
         # def mock_getapprovals(id):
         #      return []
         # monkeypatch.setattr(gitlab.Gitlab, 'getapprovals', mock_getapprovals)
 
+        def mock_getmergerequests(self, project, page, per_page, state=None):
+            return []
+        monkeypatch.setattr(gitlab.Gitlab, 'getmergerequests', mock_getmergerequests)
+
+        def mock_addprojecthook(self, project, url, **kwargs):
+            return True
+        monkeypatch.setattr(gitlab.Gitlab, 'addprojecthook', mock_addprojecthook)
+
+        def mock_editprojecthook(self, project, hook_id, url, **kwargs):
+            return True
+        monkeypatch.setattr(gitlab.Gitlab, 'editprojecthook', mock_editprojecthook)
         return gitlab.Gitlab
 
         return gitlab.Gitlab
 
+
+#    @pytest.fixture
+#    def MargeGitlab(self, monkeypatch):
+#        def mock_addprojecthook_extra(self, project, url, push=False, issues=False, merge_requests=False, tag_push=False, extra_data=None):
+#            return True
+#        monkeypatch.setattr('plugins.marge.MargeGitlab.addprojecthook_extra', mock_addprojecthook_extra)
+#
+#        def mock_editprojecthook_extra(self, project, hook_id, url, push=False, issues=False, merge_requests=False, tag_push=False, extra_data=None):
+#            return True
+#        monkeypatch.setattr('plugins.marge.MargeGitlab.editprojecthook_extra', mock_editprojecthook_extra)
+#        return marge.MargeGitlab
+
     @pytest.fixture
     def gitlab_no_reviews(self, gitlab, monkeypatch):
         def mock_getmergerequest(self, project, iid):
     @pytest.fixture
     def gitlab_no_reviews(self, gitlab, monkeypatch):
         def mock_getmergerequest(self, project, iid):
@@ -154,15 +196,32 @@ class TestMarge(object):
 
     @pytest.fixture
     def gitlab_one_review(self, gitlab, monkeypatch):
 
     @pytest.fixture
     def gitlab_one_review(self, gitlab, monkeypatch):
+        ret_mr = {'id': 'mr_id',
+                  'author': {'id': 2001},
+                  'created_at': 'Oct 29, 2017 2:37am',
+                  'merge_status': 'can_be_merged',
+                  'state': 'opened',
+                  'upvotes': 0,
+                  'web_url': 'http://gitlab.example.com/sample/mr/2001',
+                  'work_in_progress': False}
+
         def mock_getmergerequest(self, project, iid):
         def mock_getmergerequest(self, project, iid):
-            return {'author': {'id': 2001},
-                    'created_at': 'Oct 29, 2017 2:37am',
-                    'merge_status': 'can_be_merged',
-                    'state': 'opened',
-                    'upvotes': 0,
-                    'web_url': 'http://gitlab.example.com/sample/mr/2001',
-                    'work_in_progress': False}
+            return ret_mr
         monkeypatch.setattr(gitlab, 'getmergerequest', mock_getmergerequest)
         monkeypatch.setattr(gitlab, 'getmergerequest', mock_getmergerequest)
+
+        def mock_getmergerequests(self, project, page, per_page, state=None):
+            if state and state == 'opened':
+                return [ret_mr]
+            else:
+                return []
+        monkeypatch.setattr(gitlab, 'getmergerequests', mock_getmergerequests)
+        return gitlab
+
+    @pytest.fixture
+    def gitlab_already_watching(self, gitlab, monkeypatch):
+        def mock_getprojecthooks(self, project):
+            return [{'id': 'hook_id', 'merge_requests_events': True, 'url': 'https://webhook.errbot.com:3142/margebot/room1,room2,room3'}]
+        monkeypatch.setattr(gitlab, 'getprojecthooks', mock_getprojecthooks)
         return gitlab
 
     # ============================================================================
         return gitlab
 
     # ============================================================================
@@ -187,12 +246,11 @@ class TestMarge(object):
         help_message = margebot.pop_message()
         assert "Marge" in help_message
         assert '!reviews' in help_message
         help_message = margebot.pop_message()
         assert "Marge" in help_message
         assert '!reviews' in help_message
-        assert '!hello' in help_message
-        assert '!xyzzy' in help_message
+        assert '!watchrepo' in help_message
 
 
-    def test_hello(self, margebot):
-        margebot.push_message('!hello')
-        assert 'Hi there' in margebot.pop_message()
+    def test_good_bot(self, margebot):
+        margebot.push_message('good bot')
+        assert 'Best bot' in margebot.pop_message()
 
 #    def test_xyzzy(self, margebot):
 #        margebot.push_message('!xyzzy')
 
 #    def test_xyzzy(self, margebot):
 #        margebot.push_message('!xyzzy')
@@ -204,6 +262,29 @@ class TestMarge(object):
         margebot.push_message('!webstatus')
         assert 'margebot/<rooms>' in margebot.pop_message()
 
         margebot.push_message('!webstatus')
         assert 'margebot/<rooms>' in margebot.pop_message()
 
+    def test_watchrepo(self, margebot, gitlab):
+        margebot.push_message('!watchrepo group/new_repo room1,room2,room3')
+        pm = margebot.pop_message()
+        assert 'Now watching for new MRs in the group/new_repo repo to the room1,room2,room3 room(s)' in pm
+        assert 'No open MRs were found in the repo.' in pm
+
+    def test_watchrepo_already_watching_repo(self, margebot, gitlab_already_watching):
+        margebot.push_message('!watchrepo group/new_repo room1,room2,room3')
+        pm = margebot.pop_message()
+        assert 'Already reporting group/new_repo MRs to the room1,room2,room3 room(s)' in pm
+
+    def test_watchrepo_updating_roomlist(self, margebot, gitlab_already_watching):
+        margebot.push_message('!watchrepo group/new_repo room4,room5,room6')
+        pm = margebot.pop_message()
+        assert 'Updating room list for group/new_repo MRs from room1,room2,room3 to room4,room5,room6' in pm
+
+#    @pytest.mark.skip(reason='until I figure out how to mock MargeGitlab')
+    def test_watchrepo_existing_mr(self, margebot, gitlab_one_review):
+        margebot.push_message('!watchrepo sample/mr room1,room2,room3')
+        pm = margebot.pop_message()
+        assert 'Now watching for new MRs in the sample/mr repo to the room1,room2,room3 room(s)' in pm
+        assert '1 open MR was found in the repo.' in pm
+
     def test_gitlab_hook(self, margebot, gitlab):
         request = json.dumps({'object_kind': 'merge_request',
                               'object_attributes': {
     def test_gitlab_hook(self, margebot, gitlab):
         request = json.dumps({'object_kind': 'merge_request',
                               'object_attributes': {
@@ -325,3 +406,20 @@ class TestMarge(object):
         output = one_waiting_review.pop_message()
         assert 'These MRs need some attention' in output
         assert 'http://gitlab.example.com/sample/mr/2001 (opened' in output
         output = one_waiting_review.pop_message()
         assert 'These MRs need some attention' in output
         assert 'http://gitlab.example.com/sample/mr/2001 (opened' in output
+
+    def test_dont_blame_margebot(self, margebot):
+        margebot.push_message('I blame margebot')
+        assert "say it ain't so" in margebot.pop_message()
+
+    def test_margefest(self, margebot):
+        margebot.push_message('magfest')
+        assert 'MargeFest' in margebot.pop_message()
+
+    def test_margebot_sucks(self, margebot):
+        margebot.push_message('margebot sucks')
+        assert 'steering committee' in margebot.pop_message()
+
+    def test_margebot_unknown_command(self, margebot):
+        margebot.push_message('Margebot, alacazam')
+        with pytest.raises(Empty):
+            margebot.pop_message()