+ # -----------------------------------------------------------
+ # webhook maintenance commands
+
+ @arg_botcmd('rooms', type=str)
+ @arg_botcmd('repo', type=str)
+ def watchrepo(self, msg, repo, rooms):
+ """
+ Add the margebot webhook to a repo, and prepopulate any open MRs in the repo with margebot
+ args: repo: gitlab repo name in the 'NAMESPACE/PROJECT_NAME' format
+ rooms: comma separates list of rooms to notify when the webhook triggers
+ """
+ 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)
+ if not hooks:
+ 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))
+ 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
+
+ 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 not mr_list 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)
+
+ self['OPEN_MRS'] = 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 "{}\n{}".format(s_watch_msg, mr_msg)
+