From: Jude N Date: Fri, 24 Jan 2020 04:01:58 +0000 (-0500) Subject: Mattermost-specific branch X-Git-Url: https://pwan.org/git/?a=commitdiff_plain;h=71d5a45092b86eabd9d5d08362ed1434acd80787;p=margebot.git Mattermost-specific branch --- diff --git a/README.md b/README.md index 838c97f..cc317df 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,12 @@ Marge: I remind you about outstanding Gitlab merge requests +Mattermost Branch Notes + +- Install https://github.com/Vaelor/python-mattermost-driver +- Configure Errbot with the https://github.com/Vaelor/errbot-mattermost-backend +- Configure a regular, non-bot user in Mattermost. +- Create a channel in Mattermost where the user will reside + Development Notes: > python3 -m venv ./env diff --git a/plugins/marge.py b/plugins/marge.py index 0fe540f..5d3a3bd 100755 --- a/plugins/marge.py +++ b/plugins/marge.py @@ -163,7 +163,7 @@ class Marge(BotPlugin, CrontabMixin): if (target_project_id, iid, rooms) not in open_mrs: for a_room in rooms.split(','): if self.config: - self.send(self.build_identifier(a_room + '@' + self.chatroom_host), msg) + self.send(self.build_identifier('~' + a_room), msg) # No chatroom host for mattermost self.log.info("webhook: Saving ({}, {}, {})".format(target_project_id, iid, rooms)) open_mrs[(target_project_id, iid, rooms)] = True @@ -184,19 +184,8 @@ class Marge(BotPlugin, CrontabMixin): self.log.info("mr_status_msg: a_mr: {}".format(a_mr)) mr_attrs = a_mr.attributes - # Only weed out MRs less than the soak time for the crontab output (where author==None) now = datetime.now(timezone.utc) creation_time = parser.parse(mr_attrs['created_at'], tzinfos=[tzutc()]).astimezone(timezone.utc) - if not author: - self.log.info("times: {}, {}, {}".format(creation_time, self.soak_delta, now)) - if creation_time + self.soak_delta > now: - project_id = mr_attrs['target_project_id'] - mr_id = mr_attrs['id'] - soak_hours = self.config['CRONTAB_SOAK_HOURS'] - info_template = "skipping: MR <{},{}> was opened less than {} hours ago" - info_msg = info_template.format(project_id, mr_id, soak_hours) - self.log.info(info_msg) - return None str_open_since = deltastr(now - creation_time) @@ -264,8 +253,8 @@ class Marge(BotPlugin, CrontabMixin): # initialize the reminders rooms = self.rooms() for a_room in rooms: - self.log.info("poller: a_room.node: {}".format(a_room.node)) - reminder_msg[a_room.node] = [] + self.log.info("poller: a_room.name: {}".format(a_room.name)) # XMPP was a_room.node instead of a_room.name + reminder_msg[a_room.name] = [] still_open_mrs = {} @@ -315,7 +304,7 @@ class Marge(BotPlugin, CrontabMixin): for a_room, room_msg_list in reminder_msg.items(): if room_msg_list != []: if self.config: - to_room = a_room + '@' + self.config['CHATROOM_HOST'] + to_room = "~" + a_room # No CHATROOM_HOST in mattermost msg = tenv().get_template('reviews.md').render(msg_list=room_msg_list) self.send(self.build_identifier(to_room), msg) @@ -379,7 +368,7 @@ class Marge(BotPlugin, CrontabMixin): self.log.info('project: {}, id: {}, a_mr: {}'.format(project, id, a_mr)) - # If the MR is tagged 'never-close' or 'abandoned', ignore it + # If the MR is tagged 'never-close' or 'abondoned' ignore it if 'labels' in mr_attrs and ('never-close' in mr_attrs['labels'] or 'abandoned' in mr_attrs['labels']): continue @@ -431,6 +420,7 @@ class Marge(BotPlugin, CrontabMixin): msg = "Couldn't find {} hooks".format(repo) self.log.error("watchrepo: {}".format(msg)) return msg + for a_hook in hooks: self.log.info('a_hook: {}'.format(a_hook)) hook_attributes = a_hook.attributes @@ -442,9 +432,10 @@ class Marge(BotPlugin, CrontabMixin): url = "{}{}".format(self.webhook_url, rooms) # webhooks_url will end in '/' hook_updated = True if marge_hook: + old_rooms = marge_hook.attributes['url'].split(self.webhook_url, 1)[1] if old_rooms == rooms: - msg = "Already reporting {} MRs to the {} room(s)".format(repo, rooms) + msg = "Already reporting {} MRs to the {} room(s). Run !reviews to see the MR list.".format(repo, rooms) self.log.info('watchrepo: {}'.format(msg)) return msg try: @@ -484,16 +475,17 @@ class Marge(BotPlugin, CrontabMixin): new_open_mrs[(project_id, mr_id, old_rooms)] = True # pragma pylint: enable=simplifiable-if-statement open_mrs = new_open_mrs - - # If adding a new repo, check for existing opened MRs in the repo. - else: - for state in ['opened']: - a_project = self.gitlab.projects.get(target_project_id) - mr_list = a_project.mergerequests.list(state=state) - for an_mr in mr_list: - mr_count += 1 - mr_id = an_mr.attributes['iid'] - open_mrs[(target_project_id, mr_id, rooms)] = True + mr_count = len(new_open_mrs) + + # check for existing opened MRs in the repo. + # (add any MR that may have been opened while marge was down) + for state in ['opened']: + a_project = self.gitlab.projects.get(target_project_id) + mr_list = a_project.mergerequests.list(state=state) + for an_mr in mr_list: + mr_count += 1 + mr_id = an_mr.attributes['iid'] + open_mrs[(target_project_id, mr_id, rooms)] = True self['OPEN_MRS'] = open_mrs diff --git a/plugins/templates/reviews.md b/plugins/templates/reviews.md index 487b548..6809f76 100644 --- a/plugins/templates/reviews.md +++ b/plugins/templates/reviews.md @@ -12,7 +12,7 @@ {% else -%} {{welcome}}These MRs need some attention: {% for a_msg in msg_list|sort(attribute='creation_time') %} - - {{ a_msg['msg'] }} + - {{ a_msg['msg'] -}} {% endfor %} {% endif %} {%- if sender is not defined -%} diff --git a/tests/test_marge.py b/tests/test_marge.py index d96fc60..baa3ec4 100644 --- a/tests/test_marge.py +++ b/tests/test_marge.py @@ -284,7 +284,8 @@ class TestMarge: assert 'Already reporting group/new_repo MRs to the room1,room2,room3 room(s)' in pm def test_watchrepo_updating_roomlist(self, margebot): - with HTTMock(mock_projects_hooks_list_already_watching, + with HTTMock(mock_projects_mergerequests_list_unreviewed_review, + mock_projects_hooks_list_already_watching, mock_projects_hooks_get_already_watching, mock_projects_get): margebot.push_message('!watchrepo group/new_repo room4,room5,room6') @@ -473,8 +474,13 @@ class TestMarge: def test_crontab_hook(self, margebot_one_review, monkeypatch, mocker): plugin = margebot_one_review._bot.plugin_manager.get_plugin_obj_by_name('Marge') + # See comments from https://bradmontgomery.net/blog/how-world-do-you-mock-name-attribute/ def mock_rooms(): - return [mocker.MagicMock(node='room1'), mocker.MagicMock(node='room2')] + m1 = mocker.MagicMock() + m2 = mocker.MagicMock() + m1.name = 'room1' + m2.name = 'room2' + return [m1, m2] monkeypatch.setattr(plugin, 'rooms', mock_rooms) with HTTMock(mock_projects_get,