From ea189b9619448d87c2333bed0559a166d6f53193 Mon Sep 17 00:00:00 2001 From: Jude N Date: Tue, 2 Jan 2018 09:40:17 -0500 Subject: [PATCH] Adding reviews.md template / Started fleshing out setup.py to handle errbot plugins --- .gitignore | 1 + MANIFEST.in | 6 +++ README.md | 4 +- plugins/marge.py | 79 +++++++++++++++++++----------------- plugins/templates/reviews.md | 21 ++++++++++ setup.py | 10 ++++- 6 files changed, 81 insertions(+), 40 deletions(-) create mode 100644 MANIFEST.in create mode 100644 plugins/templates/reviews.md diff --git a/.gitignore b/.gitignore index dc5d342..a0b845a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .coverage .eggs __pycache__ +build dist env htmlcov diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..9a92a1a --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,6 @@ +include plugins/* +include setup.cfg +include setup.py +include templates/* +include LICENCE +include README.md diff --git a/README.md b/README.md index effada8..fa3a9d9 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ Marge: I remind you about outstanding Gitlab merge requests Use: -* In gitlab: +* In Gitlab: * For each repo you want Margebot to watch, add a Merge Request triggered webhook in the form https://your.webhook.host/margebot/room1,room2 for haing Mrbebot send reminders to rooms room1 and room2. -* In errbot: +* In Errbot: * Update CHATROOM\_PRESENCE list in config.py so Margebot joins all the rooms you want it to join. diff --git a/plugins/marge.py b/plugins/marge.py index 42e42c3..7f66423 100755 --- a/plugins/marge.py +++ b/plugins/marge.py @@ -6,7 +6,8 @@ from time import sleep from dateutil import parser from dateutil.tz import tzutc from dateutil.relativedelta import relativedelta -from errbot import BotPlugin, botcmd, webhook +from errbot import BotPlugin, botcmd, re_botcmd, webhook +from errbot.templating import tenv from errcron.bot import CrontabMixin import gitlab @@ -227,7 +228,7 @@ class Marge(BotPlugin, CrontabMixin): else: msg += "." - return (creation_time, msg) + return {'creation_time': creation_time, 'msg': msg} def crontab_hook(self, polled_time): """ @@ -244,7 +245,6 @@ class Marge(BotPlugin, CrontabMixin): for a_room in rooms: reminder_msg[a_room.node] = [] - msgs = "" still_open_mrs = {} # Let's walk through the MRs we've seen already: @@ -254,6 +254,13 @@ class Marge(BotPlugin, CrontabMixin): # Lookup the MR from the project/id a_mr = self.gitlab.getmergerequest(project_id, mr_id) + if not a_mr: + self.log.debug("Couldn't find project: {}, id: {}".format(project_id, mr_id)) + continue + + # If the MR is tagged 'never-close' ignore it + if 'labels' in a_mr and 'never-close' in a_mr['labels']: + continue self.log.info("a_mr: {} {} {} {}".format(project_id, mr_id, notify_rooms, a_mr['state'])) @@ -264,38 +271,26 @@ class Marge(BotPlugin, CrontabMixin): else: still_open_mrs[(project_id, mr_id, notify_rooms)] = True - msg_tuple = self.mr_status_msg(a_mr) - if msg_tuple is None: + msg_dict = self.mr_status_msg(a_mr) + if msg_dict is None: continue for a_room in notify_rooms.split(','): - reminder_msg[a_room].append(msg_tuple) + reminder_msg[a_room].append(msg_dict) self['OPEN_MRS'] = open_mrs # Remind each of the rooms about open MRs for a_room, room_msg_list in reminder_msg.items(): if room_msg_list != []: - - # sort by the creation time - sorted_room_msg_list = sorted(room_msg_list, key=lambda x: x[0]) - - # extract the msgs from the tuple list - msgs = [x[1] for x in sorted_room_msg_list] - - # join those msgs together. - room_msg = "\n".join(msgs) - if self.config: - msg_template = "These MRs need some attention:\n{}\n" - msg_template += "You can get an updated list with the !reviews command." to_room = a_room + '@' + self.config['CHATROOM_HOST'] - msg = msg_template.format(room_msg) + msg = tenv().get_template('reviews.md').render(msg_list=room_msg_list) self.send(self.build_identifier(to_room), msg) self['OPEN_MRS'] = still_open_mrs - @botcmd() + @botcmd(template="reviews") def reviews(self, msg, args): """ Returns a list of MRs that are waiting for some luv. @@ -338,6 +333,15 @@ class Marge(BotPlugin, CrontabMixin): # Lookup the MR from the project/id a_mr = self.gitlab.getmergerequest(project, mr_id) + if not a_mr: + self.log.debug("Couldn't find project: {}, id: {}".format(project, id)) + continue + + self.log.info('project: {}, id: {}, a_mr: {}'.format(project, id, a_mr)) + + # If the MR is tagged 'never-close' ignore it + if 'labels' in a_mr and 'never-close' in a_mr['labels']: + continue # If the MR is no longer open, skip to the next MR, # and don't include this MR in the next check @@ -346,28 +350,15 @@ class Marge(BotPlugin, CrontabMixin): else: still_open_mrs[(project, mr_id, notify_rooms)] = True - msg_tuple = self.mr_status_msg(a_mr, author=sender_gitlab_id) - if msg_tuple is None: + msg_dict = self.mr_status_msg(a_mr, author=sender_gitlab_id) + if msg_dict is None: continue - msg_list.append(msg_tuple) - - if msg_list == []: - response = 'Hi {}: {}'.format(sender, 'I found no open MRs for you.') - else: - # sort by the creation time - sorted_msg_list = sorted(msg_list, key=lambda x: x[0]) - - # extract the msgs from the tuple list - msgs = [x[1] for x in sorted_msg_list] - - # join those msgs together. - msg = "\n".join(msgs) - response = 'Hi {}: These MRs need some attention:\n{}'.format(sender, msg) + msg_list.append(msg_dict) self['OPEN_MRS'] = still_open_mrs - return response + return {'sender': sender, 'msg_list': msg_list} # pragma pylint: disable=unused-argument @botcmd() @@ -386,4 +377,18 @@ class Marge(BotPlugin, CrontabMixin): sleep(5) yield "(just kidding)" + @re_botcmd(pattern=r"I blame [Mm]arge([Bb]ot)?") + def dont_blame_margebot(self, msg, match): + """ + margebot is innocent. + """ + yield "(」゚ロ゚)」NOOOooooo say it ain't so." + + @re_botcmd(pattern=r"good bot") + def best_bot(self, msg, match): + """ + margebot is the best. + """ + yield "Best bot" + # pragma pylint: enable=unused-argument diff --git a/plugins/templates/reviews.md b/plugins/templates/reviews.md new file mode 100644 index 0000000..487b548 --- /dev/null +++ b/plugins/templates/reviews.md @@ -0,0 +1,21 @@ +{#- reviews.md template -#} +{#- ------------------- -#} +{#- sender: Name of sender -#} +{#- msg_list: Unsorted list of {'creation_time','msg'} dicts -#} +{%- if sender is defined -%} +{%- set welcome = "Hi " ~ sender ~ ": " -%} +{%- else -%} +{%- set welcome = "" %} +{%- endif -%} +{% if not msg_list %} +{{ welcome }}I found no open MRs for you. +{% else -%} +{{welcome}}These MRs need some attention: +{% for a_msg in msg_list|sort(attribute='creation_time') %} + - {{ a_msg['msg'] }} +{% endfor %} +{% endif %} +{%- if sender is not defined -%} +You can get an updated list with the !reviews command. +{%- endif -%} + diff --git a/setup.py b/setup.py index e7f4764..7a5bb92 100644 --- a/setup.py +++ b/setup.py @@ -13,12 +13,20 @@ with open('test-requirements.txt') as f: setup( name='Margebot', version='1.0.0', - packages=['plugins', ], + # packages=['plugins'], + data_files=[('/opt/errbot/plugins/Marge', + ['./plugins/marge.plug', + './plugins/marge.py', + './plugins//templates/reviews.md', + 'README.md', + 'LICENCE'])], license='GPLv3', + description='A Errbot plugin for reminding you about outstanding Gitlab merge requests', long_description=open('README.md').read(), url='https://pwan.org/git/?p=margebot.git;a=summary', author='JudeN', author_email='margebot_spam@pwan.org', + include_package_data=True, install_requires=install_required, -- 2.39.2