Skip to content

Commit

Permalink
LabHub: Add unassign feature
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
meetmangukiya committed Jun 18, 2017
1 parent ce2a27a commit 5271c55
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
26 changes: 26 additions & 0 deletions plugins/labhub.py
Expand Up @@ -136,3 +136,29 @@ def create_issut_cmd(self, msg, match):
return ('Can\'t create an issue for a repository that does not '
'exist. Please ensure that the repository is available '
'and owned by coala.')

@re_botcmd(pattern=r'^unassign\s+https://(github|gitlab)\.com/([^/]+)/([^/]+)/issues/(\d+)', # Ignore LineLengthBear, PyCodeStyleBear
flags=re.IGNORECASE)
def unassign_cmd(self, msg, match):
"""Unassign from an issue.""" # Ignore QuotesBear
org = match.group(2)
repo_name = match.group(3)
issue_number = match.group(4)

user = msg.frm.nick

try:
assert org == self.GH_ORG_NAME or org == self.GL_ORG_NAME
except AssertionError:
return 'Repository not owned by our org.'

try:
iss = self.REPOS[repo_name].get_issue(int(issue_number))
except KeyError:
return 'Repository doesn\'t exist.'
else:
if user in iss.assignees:
iss.unassign(user)
return '@{}, you are unassigned now :+1:'.format(user)
else:
return 'You are not an assignee on the issue.'
Empty file added tests/__init__.py
Empty file.
41 changes: 34 additions & 7 deletions tests/labhub_test.py
Expand Up @@ -11,6 +11,8 @@
import plugins.labhub
from plugins.labhub import LabHub

from tests.helper import plugin_testbot

class TestLabHub(unittest.TestCase):

def setUp(self):
Expand All @@ -21,6 +23,7 @@ def setUp(self):
self.mock_team = create_autospec(github3.orgs.Team)
self.mock_team.name = PropertyMock()
self.mock_team.name = 'mocked team'
self.mock_repo = 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 All @@ -45,7 +48,7 @@ def test_invite_cmd(self):
plugins.labhub.os.environ['GH_TOKEN'] = 'patched?'
testbot.assertCommand('!invite meet to developers',
'@meet, you are a part of developers')
self.assertEqual(self.labhub.TEAMS, teams)
self.assertEqual(labhub.TEAMS, teams)
testbot.assertCommand('!invite meet to something',
'select from one of the')

Expand All @@ -69,16 +72,12 @@ def test_hello_world_callback(self):
self.mock_team.invite.assert_called_with(None)

def test_create_issue_cmd(self):
testbot = TestBot(loglevel=logging.ERROR)
testbot.start()
labhub = testbot.bot.plugin_manager.instanciateElement(plugins.labhub.LabHub)

labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR)
labhub.activate()
plugins.labhub.GitHub.assert_called_once_with(None)
plugins.labhub.GitLab.assert_called_once_with(None)

mock_repo = create_autospec(IGitt.GitHub.GitHub.GitHubRepository)
labhub.REPOS = {'repository': mock_repo}
labhub.REPOS = {'repository': self.mock_repo}

testbot.assertCommand('!new issue repository this is the title\nbo\ndy',
'Here you go')
Expand All @@ -88,3 +87,31 @@ def test_create_issue_cmd(self):
)

testbot.assertCommand('!new issue coala title', 'repository that does not exist')

def test_unassign_cmd(self):
labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR)

labhub.activate()
labhub.REPOS = {'name': self.mock_repo}

mock_iss = create_autospec(IGitt.GitHub.GitHubIssue)
self.mock_repo.get_issue.return_value = mock_iss
mock_iss.assignees = PropertyMock()
mock_iss.assignees = (None, )
mock_iss.unassign = MagicMock() #TODO Remove this after the PR(IGitt) is merged

testbot.assertCommand('!unassign https://github.com/coala/name/issues/23',
'you are unassigned now', timeout=10000)
self.mock_repo.get_issue.assert_called_with(23)
mock_iss.unassign.assert_called_once_with(None)

mock_iss.assignees = ('meetmangukiya', )
testbot.assertCommand('!unassign https://github.com/coala/name/issues/23',
'not an assignee on the issue')

testbot.assertCommand('!unassign https://github.com/coala/s/issues/52',
'Repository doesn\'t exist.')


testbot.assertCommand('!unassign https://gitlab.com/ala/am/issues/532',
'Repository not owned by our org.')

0 comments on commit 5271c55

Please sign in to comment.