Adding reviews.md template / Started fleshing out setup.py to handle errbot plugins
[margebot.git] / plugins / marge.py
index 42e42c3..7f66423 100755 (executable)
@@ -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