There be tests ! And a massive code reorg to support the tests, And pep8 and pylint...
[margebot.git] / plugins / marge.py
diff --git a/plugins/marge.py b/plugins/marge.py
new file mode 100755 (executable)
index 0000000..968ef7a
--- /dev/null
@@ -0,0 +1,385 @@
+"""
+Margebot: A Errbot Plugin for Gitlab MR reminders
+"""
+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 import BotPlugin, botcmd, webhook
+from errcron.bot import CrontabMixin
+import gitlab
+
+
+def deltastr(any_delta):
+    """
+    Output a datetime delta in the format "x days, y hours, z minutes ago"
+    """
+    l_delta = []
+    days = any_delta.days
+    hours = any_delta.seconds // 3600
+    mins = (any_delta.seconds // 60) % 60
+
+    for (key, val) in [("day", days), ("hour", hours), ("minute", mins)]:
+        if val == 1:
+            l_delta.append("1 " + key)
+        elif val > 1:
+            l_delta.append("{} {}s".format(val, key))
+
+    if l_delta == []:
+        retval = "now"
+    else:
+        retval = ", ".join(l_delta) + " ago"
+    return retval
+
+
+class Marge(BotPlugin, CrontabMixin):
+    """
+    I remind you about merge requests
+
+    Use:
+       In gitlab:
+          Add a merge request webook of the form
+             'https://webookserver/margeboot/<rooms>'
+          to the projects you want tracked.  <rooms> should be a
+          comma-separated list of short room names (anything before the '@')
+          that you want notified.
+       In errbot:
+          Add <roomname>@domain to the CHATROOM_PRESENCE list in config.py for
+          rooms margebot should join
+    """
+
+    CRONTAB = [
+        # Set in config now: '0 11,17 * * * .crontab_hook'    # 7:00AM and 1:00PM EST warnings
+    ]
+
+    def __init__(self, *args, **kwargs):
+        self.git_host = None
+        self.chatroom_host = None
+        self.gitlab = None
+        self.soak_delta = None
+        super().__init__(*args, **kwargs)
+
+    def get_configuration_template(self):
+        """
+        GITLAB_HOST:      Host name of your gitlab server
+        GITLAB_ADMIN_TOKEN: PAT from an admin's https://${GIT_HOST}/profile/personal_access_tokens.
+        CHATROOM_HOST: Chatroom host.  Usually 'chatroom' + FQDN of Jabber server
+        CRONTAB: Schedule of automated merge request checks in '%M %H %d %m %w' format
+        VERIFY_SSL : True, False, or path to CA cert to verify cert
+        CRONTAB_SOAK_HOURS : Don't send out reminders about MRs opened less than this many hours
+        """
+        return {'GITLAB_HOST': 'gitlab.example.com',
+                'GITLAB_ADMIN_TOKEN': 'gitlab-admin-user-private-token',
+                'CHATROOM_HOST': 'conference.jabber.example.com',
+                'CRONTAB': '0 11,17 * * *',
+                'VERIFY_SSL': True,
+                'CRONTAB_SOAK_HOURS': 1}
+
+    def check_configuration(self, configuration):
+        """
+        Check that the plugin has been configured properly
+        """
+        super().check_configuration(configuration)
+
+    def activate(self):
+        """
+        Initialization done when the plugin is activated
+        """
+        if not self.config:
+            self.log.info('Margebot is not configured. Forbid activation')
+            return
+        self.git_host = self.config['GITLAB_HOST']
+        self.chatroom_host = self.config['CHATROOM_HOST']
+        Marge.CRONTAB = ['{} .crontab_hook'.format(self.config['CRONTAB'])]
+        gitlab_auth_token = self.config['GITLAB_ADMIN_TOKEN']
+        verify_ssl = self.config['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'])
+        super().activate()
+
+    def deactivate(self):
+        """
+        Anything that needs to be tore down when the plugin is deactivated goes here.
+        """
+        super().deactivate()
+
+    @webhook('/margebot/<rooms>/')
+    def gitlab_hook(self, request, rooms):
+        """
+        Webhook that listens on http://<server>:<port>/gitlab
+        """
+
+        self.log.info("webhook: request: {}, rooms: {}".format(request, rooms))
+        self.log.info("state: {}".format(request['object_attributes']['state']))
+
+        # verify it's a merge request
+        if request['object_kind'] != 'merge_request':
+            self.log.error('unexpecting object_kind: {}'.format(request['object_kind']))
+        elif 'opened' in request['object_attributes']['state']:
+
+            if request['object_attributes']['work_in_progress']:
+                wip = "WIP "
+            else:
+                wip = ""
+            url = request['project']['homepage']
+            title = request['object_attributes']['title']
+
+            author_id = request['object_attributes']['author_id']  # map this to user name ...
+            author = self.gitlab.getuser(author_id)
+            if author:
+                author_name = author['username']
+            else:
+                self.log.info("unexpected author_id {}".format(author_id))
+                author_name = author_id
+
+            target_project_id = request['object_attributes']['target_project_id']
+            iid = request['object_attributes']['iid']
+
+            msg_template = "New Review: {} has opened a new {}MR: \"{}\"\n{}/merge_requests/{}"
+            msg = msg_template.format(author_name, wip, title, url, iid)
+
+            for a_room in rooms.split(','):
+                if self.config:
+                    self.send(self.build_identifier(a_room + '@' + self.chatroom_host), msg)
+
+            if 'OPEN_MRS' not in self.keys():
+                empty_dict = {}
+                self['OPEN_MRS'] = empty_dict
+
+            open_mrs = self['OPEN_MRS']
+            self.log.info("webhook: Saving ({}, {}, {})".format(target_project_id, iid, rooms))
+            open_mrs[(target_project_id, id, rooms)] = True
+            self['OPEN_MRS'] = open_mrs
+
+        return "OK"
+
+    def mr_status_msg(self, a_mr, author=None):
+        """
+        Create the merge request status message
+        """
+        self.log.info("mr_status_msg: a_mr: {}".format(a_mr))
+
+        now = datetime.now(timezone.utc)
+        creation_time = parser.parse(a_mr['created_at'], tzinfos=tzutc)
+        self.log.info("times: {}, {}, {}".format(creation_time, self.soak_delta, now))
+        if creation_time + self.soak_delta > now:
+            project = a_mr['project']
+            iid = a_mr['iid']
+            soak_hours = self.config['CRONTAB_SOAK_HOURS']
+            info_template = "skipping: MR <{},{}> was opened less than {} hours ago"
+            info_msg = info_template.format(project, iid, soak_hours)
+            self.log.info(info_msg)
+            return None
+
+        str_open_since = deltastr(now - creation_time)
+
+        warning = ""
+        if a_mr['work_in_progress']:
+            warning = "still WIP"
+        elif a_mr['merge_status'] != 'can_be_merged':
+            warning = "there are merge conflicts"
+
+        if author:
+            authored = (a_mr['author']['id'] == author)
+        else:
+            authored = False
+
+        # getapprovals is only available in GitLab 8.9 EE or greater
+        # (not the open source CE version)
+        # approvals = self.gitlab.getapprovals(a_mr['id'])
+        # also_approved = ""
+        # for approved in approvals['approved_by']:
+        #    also_approved += "," + approved['user']['name']
+
+        upvotes = a_mr['upvotes']
+        msg = "{} (opened {})".format(a_mr['web_url'], str_open_since)
+        if upvotes >= 2:
+            msg += ": Has 2+ upvotes and could be merged in now"
+            if warning != "":
+                msg += " except {}.".format(warning)
+            else:
+                msg += "."
+
+        elif upvotes == 1:
+            if authored:
+                msg += ": Your MR is waiting for another upvote"
+            else:
+                msg += ": Waiting for another upvote"
+            if warning != "":
+                msg += " but {}.".format(warning)
+            else:
+                msg += "."
+
+        else:
+            if authored:
+                msg += ": Your MR has no upvotes"
+            else:
+                msg += ": No upvotes, please review"
+            if warning != "":
+                msg += " but {}.".format(warning)
+            else:
+                msg += "."
+
+        return (creation_time, msg)
+
+    def crontab_hook(self, polled_time):
+        """
+        Send a scheduled message to the rooms margebot is watching
+        about open MRs the room cares about.
+        """
+
+        self.log.info("crontab_hook triggered at {}".format(polled_time))
+
+        reminder_msg = {}  # Map of reminder_msg['roomname@domain'] = [(created_at,msg)]
+
+        # initialize the reminders
+        rooms = self.rooms()
+        for a_room in rooms:
+            reminder_msg[a_room.node] = []
+
+        msgs = ""
+        still_open_mrs = {}
+
+        # Let's walk through the MRs we've seen already:
+        open_mrs = self['OPEN_MRS']
+
+        for (project, iid, notify_rooms) in open_mrs:
+
+            # Lookup the MR from the project/iid
+            a_mr = self.gitlab.getmergerequest(project, iid)
+
+            self.log.info("a_mr: {} {} {} {}".format(project, iid, notify_rooms, a_mr['state']))
+
+            # If the MR is no longer open, skip to the next MR,
+            # and don't include this MR in the next check
+            if 'opened' not in a_mr['state']:
+                continue
+            else:
+                still_open_mrs[(project, iid, notify_rooms)] = True
+
+            msg_tuple = self.mr_status_msg(a_mr)
+            if msg_tuple is None:
+                continue
+
+            for a_room in notify_rooms.split(','):
+                reminder_msg[a_room].append(msg_tuple)
+
+        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"
+                    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)
+                    self.send(self.build_identifier(to_room), msg)
+
+        self['OPEN_MRS'] = still_open_mrs
+
+    @botcmd()
+    def reviews(self, msg, args):
+        """
+        Returns a list of MRs that are waiting for some luv.
+        Also returns a list of MRs that have had enough luv but aren't merged in yet.
+        """
+        # Sending directly to Margbot:  sender in the form sender@....
+        # Sending to a chatroom:  snder in the form room@rooms/sender
+
+        log_template = 'reviews: msg: {}, args: {}, \nmsg.frm: {}, \nmsg.msg.frm: {}, chatroom_host: {}'
+        self.log.info(log_template.format(msg, args, msg.frm.__dict__, dir(msg.frm), self.config['CHATROOM_HOST']))
+        self.log.info('reviews: bot mode: {}'.format(self._bot.mode))
+
+        if self._bot.mode == "xmpp":
+            if msg.frm.domain == self.config['CHATROOM_HOST']:
+                sender = msg.frm.resource
+            else:
+                sender = msg.frm.node
+        else:
+            sender = str(msg.frm).split('@')[0]
+
+        keys = self.keys()
+        if 'OPEN_MRS' not in keys:
+            self.log.error('OPEN_MRS not in {}'.format(keys))
+            return "No MRs to review"
+
+        sender_gitlab_id = None
+        sender_users = self.gitlab.getusers(search=(('username', sender)))
+        if not sender_users:
+            self.log.error('problem mapping {} to a gitlab user'.format(sender))
+            sender_gitlab_id = None
+        else:
+            sender_gitlab_id = sender_users[0]['id']
+
+        # Walk through the MRs we've seen already:
+        msg_list = []
+        msg = ""
+        still_open_mrs = {}
+        open_mrs = self['OPEN_MRS']
+        for (project, iid, notify_rooms) in open_mrs:
+
+            # Lookup the MR from the project/iid
+            a_mr = self.gitlab.getmergerequest(project, iid)
+
+            # If the MR is no longer open, skip to the next MR,
+            # and don't include this MR in the next check
+            if 'opened' not in a_mr['state']:
+                continue
+            else:
+                still_open_mrs[(project, iid, notify_rooms)] = True
+
+            msg_tuple = self.mr_status_msg(a_mr, author=sender_gitlab_id)
+            if msg_tuple 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)
+
+        self['OPEN_MRS'] = still_open_mrs
+
+        return response
+
+    # pragma pylint: disable=unused-argument
+    @botcmd()
+    def hello(self, msg, args):
+        """
+        A simple command to check if the bot is responding
+        """
+        return "Hi there"
+
+    @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)"
+
+    # pragma pylint: enable=unused-argument