"""
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 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):
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'])
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
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:
- 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:
# 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):
"""
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)
- 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
"""
-Mrgebot tests
+Margebot tests
"""
# pragma pylint: disable=invalid-name
# pragma pylint: disable=missing-docstring
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 requests
+import errbot
+from errbot.backends.test import testbot # pylint: disable=unused-import
from plugins.marge import deltastr
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):
- 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()
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)
+ 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
+
+# @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_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):
- 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)
+
+ 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
# ============================================================================
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')
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': {
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()