From 95fb305a44c9c5bc58fbb59273e1546ff6171b1e Mon Sep 17 00:00:00 2001 From: random-access7 Date: Tue, 3 Apr 2018 01:05:55 +0530 Subject: [PATCH] Added migrate_issue to labhub plugin Closes https://github.com/coala/corobo/issues/518 --- plugins/labhub.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/plugins/labhub.py b/plugins/labhub.py index 7616ba06..f74e140a 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -381,3 +381,90 @@ def pr_stats(self, msg, match): state=type(self).community_state(pr_count) ) yield reply + + @re_botcmd(pattern=r'^migrate\s+https://(github|gitlab)\.com/([^/]+)/([^/]+)/+issues/(\d+)\s+https://(github|gitlab)\.com/([^/]+)/([^/]+)/*$', # Ignore LineLengthBear, PyCodeStyleBear + # Ignore LineLengthBear, PyCodeStyleBear + re_cmd_name_help='migrate ', + flags=re.IGNORECASE) + def migrate_issue(self, msg, match): + """ + Migrate an issue from one repo + to another repo of coala + """ + org = match.group(2) + repo_name_orig = match.group(3) + issue_number = match.group(4) + user = msg.frm.nick + org2 = match.group(6) + try: + assert org == self.GH_ORG_NAME or org == self.GL_ORG_NAME + except AssertionError: + yield 'First repository not owned by our org.' + return + + try: + assert org2 == self.GH_ORG_NAME or org2 == self.GL_ORG_NAME + except AssertionError: + yield 'Second repository not owned by our org.' + return + + repo_name_final = match.group(7) + + if repo_name_orig in self.REPOS and repo_name_final in self.REPOS: + pass + else: + return tenv().get_template( + 'labhub/errors/no-repository.jinja2.md' + ).render( + target=user, + ) + + if self.TEAMS[self.GH_ORG_NAME + ' maintainers'].is_member(user): + pass + else: + return 'Error' + + try: + old_issue = self.REPOS[repo_name_orig].get_issue(int(issue_number)) + except KeyError: + return 'Issue doesn\'t exist.' + + if old_issue.is_closed(): + return 'Issue cannot be migrated as it has been closed already.' + else: + pass + + old_issue.add_labels('Invalid') + + new_issue_title = str(old_issue.title) + new_issue_description = str(old_issue.body).rstrip() + extra_msg = '\n\nMigrated issue from '+old_issue.html_url + extra_msg += ' by @'+user + new_issue = self.REPOS[repo_name_final].create_issue( + new_issue_title, new_issue_description + extra_msg) + + new_issue.add_labels(old_issue.labels) + + old_comments_iter = old_issue.iter_comments() + while(True): + try: + com = old_comments_iter.next() + auth = com.user + url = com.html_url + old_body = com.body + update_dt = com.updated_at + new_body = old_body.rstrip()+'\n\nOriginally written by @' + \ + auth + ' on ' + \ + str(update_dt) + new_body += ' and you can view it [here!](' + url + ')' + new_issue.create_comment(new_body) + except StopIteration: + break + + # Ignore LineLengthBear, PyCodeStyleBear + msg_to_old_repo = msg_to_old_repo = '\n\nIssue has been migrated to another [repository here!](' + \ + new_issue.html_url+') by @'+user+'.' + edited_body = str(old_issue.body).rstip()+msg_to_old_repo + old_issue.edit(body=edited_body) + old_issue.close() + return 'New issue created: {}'.format(new_issue.html_url)