Backporting production changes / updating tests
[margebot.git] / plugins / marge.py
index 0dbbffe..0fe540f 100755 (executable)
@@ -122,8 +122,8 @@ class Marge(BotPlugin, CrontabMixin):
         # 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']))
+        if request['event_type'] != 'merge_request':
+            self.log.error('unexpecting event_type: {}'.format(request['event_type']))
         elif 'opened' in request['object_attributes']['state']:
 
             if request['object_attributes']['work_in_progress']:
@@ -144,11 +144,11 @@ class Marge(BotPlugin, CrontabMixin):
             target_project_id = request['object_attributes']['target_project_id']
             iid = request['object_attributes']['iid']
 
-            # If the MR is tagged 'never-close' ignore it
+            # If the MR is tagged 'never-close' or 'abandoned' ignore it
             if 'labels' in request:
                 for a_label in request['labels']:
-                    if a_label['title'] == 'never-close':
-                        self.log.info("Skipping never-close notice for {} MR".format(url))
+                    if a_label['title'] in ['never-close', 'abandoned']:
+                        self.log.info("Skipping {} notice for {} MR".format(a_label['title'], url))
                         return "OK"
 
             msg_template = "Hi there ! {} has opened a new {}MR: \"{}\"\n{}/merge_requests/{}"
@@ -186,7 +186,7 @@ class Marge(BotPlugin, CrontabMixin):
 
         # 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(mr_attrs['created_at'], tzinfos=tzutc)
+        creation_time = parser.parse(mr_attrs['created_at'], tzinfos=[tzutc()]).astimezone(timezone.utc)
         if not author:
             self.log.info("times: {}, {}, {}".format(creation_time, self.soak_delta, now))
             if creation_time + self.soak_delta > now:
@@ -211,7 +211,7 @@ class Marge(BotPlugin, CrontabMixin):
         else:
             authored = False
 
-        msg = "{} (opened {})".format(mr_attrs['web_url'], str_open_since)
+        msg = "{} (opened by {} {})".format(mr_attrs['web_url'], mr_attrs['author']['username'], str_open_since)
         upvotes = mr_attrs['upvotes']
         if upvotes >= 2:
             msg += ": Has 2+ upvotes and could be merged in now"
@@ -286,8 +286,8 @@ class Marge(BotPlugin, CrontabMixin):
                 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 mr_attrs and 'never-close' in mr_attrs['labels']:
+            # If the MR is tagged 'never-close' or 'abandoned', ignore it
+            if 'labels' in mr_attrs and ('never-close' in mr_attrs['labels'] or 'abandoned' in mr_attrs['labels']):
                 continue
 
             self.log.info("a_mr: {} {} {} {}".format(project_id, mr_id, notify_rooms, mr_attrs['state']))
@@ -379,8 +379,8 @@ class Marge(BotPlugin, CrontabMixin):
 
             self.log.info('project: {}, id: {}, a_mr: {}'.format(project, id, a_mr))
 
-            # If the MR is tagged 'never-close' ignore it
-            if 'labels' in mr_attrs and 'never-close' in mr_attrs['labels']:
+            # If the MR is tagged 'never-close' or 'abandoned',  ignore it
+            if 'labels' in mr_attrs and ('never-close' in mr_attrs['labels'] or 'abandoned' in mr_attrs['labels']):
                 continue
 
             # If the MR is no longer open, skip to the next MR,
@@ -431,37 +431,34 @@ class Marge(BotPlugin, CrontabMixin):
             msg = "Couldn't find {} hooks".format(repo)
             self.log.error("watchrepo: {}".format(msg))
             return msg
-        else:
-            for a_hook in hooks:
-                self.log.info('a_hook: {}'.format(a_hook))
-                hook_attributes = a_hook.attributes
-                if hook_attributes['merge_requests_events'] and hook_attributes['url'].startswith(self.webhook_url):
-                    marge_hook = a_hook
-                    break
+        for a_hook in hooks:
+            self.log.info('a_hook: {}'.format(a_hook))
+            hook_attributes = a_hook.attributes
+            if hook_attributes['merge_requests_events'] and hook_attributes['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 '/'
         hook_updated = True
         if marge_hook:
-
             old_rooms = marge_hook.attributes['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:
-                try:
-                    s_action = "update"
-                    marge_hook.attributes['url'] = url
-                    marge_hook.save()
-                    s_watch_msg = "Updating room list for {} MRs from {} to {}".format(repo, old_rooms, rooms)
-                except Exception as exp:
-                    hook_updated = False
-                    self.log.error("watchrepo; update hook {}  raised exception {}".format(repo, exp))
+            try:
+                s_action = "update"
+                marge_hook.attributes['url'] = url
+                marge_hook.save()
+                s_watch_msg = "Updating room list for {} MRs from {} to {}".format(repo, old_rooms, rooms)
+            except Exception as exp:
+                hook_updated = False
+                self.log.error("watchrepo; update hook {}  raised exception {}".format(repo, exp))
         else:
             try:
                 s_action = "add"
-                project.hooks.create({'url': url, 'merge_request_events': 1, 'enable_ssl_verification': True})
+                project.hooks.create({'url': url, 'merge_requests_events': 1, 'enable_ssl_verification': True})
                 s_watch_msg = "Now watching for new MRs in the {} repo to the {} room(s)".format(repo, rooms)
             except Exception as exp:
                 hook_updated = False