Merge branch 'master' of ssh://pwan.org/var/www/git/projects/margebot
[margebot.git] / plugins / marge.py
index 968ef7a..0cbe525 100755 (executable)
@@ -1,14 +1,60 @@
 """
 Margebot: A Errbot Plugin for Gitlab MR reminders
 """
+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 import BotPlugin, botcmd, webhook
+from errbot import BotPlugin, arg_botcmd, botcmd, re_botcmd, webhook
+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):
@@ -58,6 +104,7 @@ class Marge(BotPlugin, CrontabMixin):
         self.chatroom_host = None
         self.gitlab = None
         self.soak_delta = None
+        self.webhook_url = None
         super().__init__(*args, **kwargs)
 
     def get_configuration_template(self):
@@ -68,13 +115,15 @@ class Marge(BotPlugin, CrontabMixin):
         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
+        WEBHOOK_URL :  URL to use for defining MR integration in gitlab
         """
         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}
+                'CRONTAB_SOAK_HOURS': 1,
+                'WEBHOOK_URL': 'https://webhooks.example.com:3142/margebot/'}
 
     def check_configuration(self, configuration):
         """
@@ -94,10 +143,13 @@ 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']
-        self.gitlab = gitlab.Gitlab(self.git_host, gitlab_auth_token, verify_ssl=verify_ssl)
+        self.gitlab = MargeGitlab(self.git_host, gitlab_auth_token, verify_ssl=verify_ssl)
         self.activate_crontab()
 
         self.soak_delta = relativedelta(hours=self.config['CRONTAB_SOAK_HOURS'])
+        self.webhook_url = self.config['WEBHOOK_URL']
+        if self.webhook_url[-1] != '/':
+            self.webhook_url += '/'
         super().activate()
 
     def deactivate(self):
@@ -113,7 +165,7 @@ class Marge(BotPlugin, CrontabMixin):
         """
 
         self.log.info("webhook: request: {}, rooms: {}".format(request, rooms))
-        self.log.info("state: {}".format(request['object_attributes']['state']))
+        self.log.info("state: {}".format(request['object_attributes']['state']))
 
         # verify it's a merge request
         if request['object_kind'] != 'merge_request':
@@ -137,23 +189,25 @@ class Marge(BotPlugin, CrontabMixin):
 
             target_project_id = request['object_attributes']['target_project_id']
             iid = request['object_attributes']['iid']
+            mr_id = request['object_attributes']['id']
 
-            msg_template = "New Review: {} has opened a new {}MR: \"{}\"\n{}/merge_requests/{}"
+            msg_template = "Hi there ! {} 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
 
+            if (target_project_id, mr_id, 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.log.info("webhook: Saving ({}, {}, {})".format(target_project_id, mr_id, rooms))
+                open_mrs[(target_project_id, mr_id, rooms)] = True
+                self['OPEN_MRS'] = open_mrs
         return "OK"
 
     def mr_status_msg(self, a_mr, author=None):
@@ -162,17 +216,19 @@ class Marge(BotPlugin, CrontabMixin):
         """
         self.log.info("mr_status_msg: a_mr: {}".format(a_mr))
 
+        # 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(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
+        if not author:
+            self.log.info("times: {}, {}, {}".format(creation_time, self.soak_delta, now))
+            if creation_time + self.soak_delta > now:
+                project_id = a_mr['target_project_id']
+                mr_id = a_mr['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)
 
@@ -223,7 +279,7 @@ class Marge(BotPlugin, CrontabMixin):
             else:
                 msg += "."
 
-        return (creation_time, msg)
+        return {'creation_time': creation_time, 'msg': msg}
 
     def crontab_hook(self, polled_time):
         """
@@ -240,58 +296,50 @@ 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:
         open_mrs = self['OPEN_MRS']
 
-        for (project, iid, notify_rooms) in open_mrs:
+        for (project_id, mr_id, notify_rooms) in open_mrs:
+
+            # 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
 
-            # Lookup the MR from the project/iid
-            a_mr = self.gitlab.getmergerequest(project, iid)
+            # 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, iid, notify_rooms, a_mr['state']))
+            self.log.info("a_mr: {} {} {} {}".format(project_id, mr_id, 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
+                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)
-
-        self['OPEN_MRS'] = open_mrs
+                reminder_msg[a_room].append(msg_dict)
 
         # 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)
+                    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.
@@ -330,48 +378,140 @@ class Marge(BotPlugin, CrontabMixin):
         msg = ""
         still_open_mrs = {}
         open_mrs = self['OPEN_MRS']
-        for (project, iid, notify_rooms) in open_mrs:
+        for (project, mr_id, notify_rooms) in open_mrs:
+
+            # 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
 
-            # Lookup the MR from the project/iid
-            a_mr = self.gitlab.getmergerequest(project, iid)
+            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
             if 'opened' not in a_mr['state']:
                 continue
             else:
-                still_open_mrs[(project, iid, notify_rooms)] = True
+                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)
+            msg_list.append(msg_dict)
+
+        self['OPEN_MRS'] = still_open_mrs
+
+        return {'sender': sender, 'msg_list': msg_list}
 
-        if msg_list == []:
-            response = 'Hi {}: {}'.format(sender, 'I found no open MRs for you.')
+    @arg_botcmd('rooms', type=str, help="Comma-separated room list without @conference-room suffix")
+    @arg_botcmd('repo', type=str, help="repo to start watching for MRs in NAMESPACE/PROJECT_NAME format")
+    def watchrepo(self, msg, repo, rooms):
+        """
+        Add the margebot webhook to a repo, and prepopulate any open MRs in the repo with margebot
+        """
+        self.log.info("msg={}".format(msg))
+        self.log.info("repo={}".format(repo))
+        self.log.info("rooms={}".format(rooms))
+
+        # get the group/repo repo, error out if it doesn't exist
+        project = self.gitlab.getproject(repo)
+        if not project:
+            msg = "Couldn't find repo {}".format(repo)
+            self.log.info("watchrepo: {}".format(msg))
+            return msg
+
+        self.log.info('project: {}'.format(project))
+
+        target_project_id = project['id']
+
+        # Check is the project already includes the margebot hook
+        # If no hooks, will it return False or [] ?
+        marge_hook = None
+        hooks = self.gitlab.getprojecthooks(target_project_id)
+        self.log.error("hooks = {}".format(hooks))
+        if hooks is False:
+            msg = "Couldn't find {} hooks".format(repo)
+            self.log.error("watchrepo: {}".format(msg))
+            return msg
         else:
-            # sort by the creation time
-            sorted_msg_list = sorted(msg_list, key=lambda x: x[0])
+            for a_hook in hooks:
+                self.log.info('a_hook: {}'.format(a_hook))
+                if a_hook['merge_requests_events'] and a_hook['url'].startswith(self.webhook_url):
+                    marge_hook = a_hook
+                    break
+
+        # If so replace it (or error out ?)
+        url = "{}{}".format(self.webhook_url, rooms)  # webhooks_url will end in '/'
+        if marge_hook:
+
+            old_rooms = marge_hook['url'].split(self.webhook_url, 1)[1]
+            if old_rooms == rooms:
+                msg = "Already reporting {} MRs to the {} room(s)".format(repo, rooms)
+                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})
+                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)
+            s_action = "add"
+
+        if not hook_updated:
+            msg = "Couldn't {} hook: {}".format(s_action, repo)
+            self.log.error("watchrepo: {}".format(msg))
+            return msg
 
-            # extract the msgs from the tuple list
-            msgs = [x[1] for x in sorted_msg_list]
+        open_mrs = self['OPEN_MRS']
+        mr_count = 0
+        # get the open MRs in the repo
+
+        # If updating the room list, walk through the existing MR list
+        if s_action == "update":
+            new_open_mrs = {}
+            for (project_id, mr_id, old_rooms) in open_mrs:
+                # pragma pylint: disable=simplifiable-if-statement
+                if project_id == target_project_id:
+                    new_open_mrs[(project_id, mr_id, rooms)] = True
+                else:
+                    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/reopened MRs in the repo.
+        else:
+            for state in ['opened', 'reopened']:
+                page = 1
+                mr_list = self.gitlab.getmergerequests(target_project_id, page=page, per_page=100, state=state)
+                while (mr_list is not False) and (mr_list != []):
+                    for an_mr in mr_list:
+                        mr_count += 1
+                        self.log.info('watchrepo: an_mr WATS THE ID\n{}'.format(an_mr))
+                        mr_id = an_mr['id']
+                        open_mrs[(target_project_id, mr_id, rooms)] = True
+                    # Get the next page of MRs
+                    page += 1
+                    mr_list = self.gitlab.getmergerequests(target_project_id, page=page, per_page=100)
 
-            # join those msgs together.
-            msg = "\n".join(msgs)
-            response = 'Hi {}: These MRs need some attention:\n{}'.format(sender, msg)
+        self['OPEN_MRS'] = open_mrs
 
-        self['OPEN_MRS'] = still_open_mrs
+        if mr_count == 0:
+            mr_msg = "No open MRs were found in the repo."
+        elif mr_count == 1:
+            mr_msg = "1 open MR was found in the repo.  Run !reviews to see the updated MR list."
+        else:
+            mr_msg = "{} open MRs were found in the repo.  Run !reviews to see the updated MR list."
 
-        return response
+        return "{}\n{}".format(s_watch_msg, mr_msg)
 
     # 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):
@@ -382,4 +522,40 @@ class Marge(BotPlugin, CrontabMixin):
         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):
+        """
+        margebot is innocent.
+        """
+        yield u"(\u300D\uFF9F\uFF9B\uFF9F)\uFF63NOOOooooo say it ain't so."
+
+    @re_botcmd(pattern=r"good bot", prefixed=False, flags=re.IGNORECASE)
+    def best_bot(self, msg, match):
+        """
+        margebot is the best.
+        """
+        yield "Best bot"
+
+    @re_botcmd(pattern=r"magfest", prefixed=False, flags=re.IGNORECASE)
+    def margefest(self, msg, args):
+        """
+        margefest4ever
+        """
+        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):
+        """
+        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
+
+
     # pragma pylint: enable=unused-argument