Syncing up with what's in production :-/
[margebot.git] / plugins / marge.py
index 968ef7a..42e42c3 100755 (executable)
@@ -137,23 +137,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 +164,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)
 
@@ -246,19 +250,19 @@ class Marge(BotPlugin, CrontabMixin):
         # 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/iid
-            a_mr = self.gitlab.getmergerequest(project, iid)
+            # Lookup the MR from the project/id
+            a_mr = self.gitlab.getmergerequest(project_id, mr_id)
 
-            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:
@@ -283,7 +287,7 @@ class Marge(BotPlugin, CrontabMixin):
                 room_msg = "\n".join(msgs)
 
                 if self.config:
-                    msg_template = "These MRs need some attention:{}\n"
+                    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)
@@ -330,17 +334,17 @@ 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/iid
-            a_mr = self.gitlab.getmergerequest(project, iid)
+            # Lookup the MR from the project/id
+            a_mr = self.gitlab.getmergerequest(project, mr_id)
 
             # 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: