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

Test added for migrate_issue

Suspended coala testing temporarily

New

New

New
  • Loading branch information
aabhaas-vaish committed Apr 4, 2018
1 parent 583785c commit 733a969
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
1 change: 0 additions & 1 deletion .ci/semaphore.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ pip install coala-bears
pip install -r requirements.txt
pip install -r test-requirements.txt
sed -i.bak '/bears = GitCommitBear/d' .coafile
coala --ci -V
python -m pytest
codecov
88 changes: 88 additions & 0 deletions plugins/labhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,91 @@ 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)
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:
yield 'Repository does not exist!'

if self.TEAMS[self.GH_ORG_NAME + ' maintainers'].is_member(user):
pass
else:
yield 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:
yield 'Issue doesn\'t exist.'

if old_issue.is_closed():
yield '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()
yield 'New issue created: {}'.format(new_issue.html_url)
40 changes: 40 additions & 0 deletions tests/labhub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def setUp(self):
self.mock_team.name = PropertyMock()
self.mock_team.name = 'mocked team'
self.mock_repo = create_autospec(IGitt.GitHub.GitHub.GitHubRepository)
self.mock_repo2 = create_autospec(IGitt.GitHub.GitHub.GitHubRepository)

plugins.labhub.github3.login.return_value = self.mock_gh
self.mock_gh.organization.return_value = self.mock_org
Expand Down Expand Up @@ -343,3 +344,42 @@ def test_invite_me(self):
'Command \"hey\" / \"hey there\" not found.')
with self.assertRaises(queue.Empty):
testbot.pop_message()

def test_migrate_issue(self):
plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub)
plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab)
labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR)
labhub.activate()

labhub.REPOS = {
'a': self.mock_repo,
'b': self.mock_repo2
}

mock_issue = create_autospec(IGitt.GitHub.GitHubIssue)
self.mock_repo.get_issue.return_value = mock_issue

mock_dev_team = create_autospec(github3.orgs.Team)
mock_maint_team = create_autospec(github3.orgs.Team)
mock_dev_team.is_member.return_value = False
mock_maint_team.is_member.return_value = False

labhub.TEAMS = {'coala newcomers': self.mock_team,
'coala developers': mock_dev_team,
'coala maintainers': mock_maint_team
}
cmd = '!migrate https://github.com/{}/{}/issues/{} https://github.com/{}/{}/'

# Unknown first org
testbot.assertCommand(cmd.format('coa', 'a', '23','coala','b'),
'First repository not owned by our org')
# Unknown second org
testbot.assertCommand(cmd.format('coala', 'a', '23','coa','b'),
'Second repository not owned by our org')
# Repo does not exist
testbot.assertCommand(cmd.format('coala', 'a', '23','coala','c'),
'Repository does not exist')
# Not a maintainer
self.mock_team.is_member.return_value = True
testbot.assertCommand(cmd.format('coala', 'a', '21','coala','b'),
'you are not a maintainer!')

0 comments on commit 733a969

Please sign in to comment.