Skip to content

Commit

Permalink
Added migrate_issue to labhub plugin
Browse files Browse the repository at this point in the history
Closes coala#518
  • Loading branch information
aabhaas-vaish committed Apr 3, 2018
1 parent 583785c commit 19a1c78
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions plugins/labhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,95 @@ 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 <complete-issue-URL-to-be-copied-from> <complete-repo-URL-to-be-copied-into>',
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)
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,
)

user = msg.frm.nick
if self.TEAMS[self.GH_ORG_NAME + ' maintainers'].is_member(user):
pass
else:
return tenv().get_template(
'labhub/errors/not-maintainer.jinja2.md'
).render(
action='migrate issues',
target=user,
)

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)

0 comments on commit 19a1c78

Please sign in to comment.