Skip to content

Commit

Permalink
labhub: Allow missing username and msg link
Browse files Browse the repository at this point in the history
Some backends do not provide usernames or msg links.

Zulip and Slack both using msg.extra['url'], and a
patch is pending for the gitter backend to follow suite.
errbotio/err-backend-gitter#32

Fixes coala#392
Related to coala#255
  • Loading branch information
jayvdb committed Nov 29, 2017
1 parent f2d506e commit 4e3f359
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
21 changes: 16 additions & 5 deletions plugins/labhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,22 @@ def create_issue_cmd(self, msg, match):
repo_name = match.group(1)
iss_title = match.group(2)
iss_description = match.group(3) if match.group(3) is not None else ''
extra_msg = '\nOpened by @{username} at [{backend}]({msg_link})'.format(
username=msg.frm.nick,
backend=self.bot_config.BACKEND,
msg_link=message_link(self, msg)
)

extra_parts = []
if msg.frm.nick:
extra_parts.append('by @{}'.format(msg.frm.nick))
msg_link = message_link(self, msg)
if msg_link:
extra_parts.append(
'at [{backend}]({msg_link})'.format(
backend=self.bot_config.BACKEND,
msg_link=message_link(self, msg)
)
)
else:
extra_parts.append('on {}'.format(self.bot_config.BACKEND))

extra_msg = '\nOpened {}'.format(' '.join(extra_parts))

if repo_name in self.REPOS:
repo = self.REPOS[repo_name]
Expand Down
14 changes: 8 additions & 6 deletions tests/labhub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from IGitt.GitLab.GitLabMergeRequest import GitLabMergeRequest
from IGitt.GitHub.GitHubIssue import GitHubIssue

from errbot.backends.test import TestBot
from errbot.backends.test import Message, TestBot

import plugins.labhub
from plugins.labhub import LabHub
Expand Down Expand Up @@ -88,22 +88,24 @@ def test_create_issue_cmd(self):
plugins.labhub.GitHubToken.assert_called_with(None)
plugins.labhub.GitLabPrivateToken.assert_called_with(None)


labhub.REPOS = {'repository': self.mock_repo,
'repository.github.io': self.mock_repo}

testbot.assertCommand('!new issue repository this is the title\nbo\ndy',
'Here you go')

labhub.REPOS['repository'].create_issue.assert_called_once_with(
'this is the title', 'bo\ndy\nOpened by @None at [text]()'
'this is the title', 'bo\ndy\nOpened on text'
)

testbot.assertCommand('!new issue repository.github.io another title\nand body',
'Here you go')
msg = Message('!new issue repository.github.io another title\nand body',
'Here you go')
msg.extras['url'] = 'http://example.com'

testbot.bot.callback_message(msg)

labhub.REPOS['repository.github.io'].create_issue.assert_called_with(
'another title', 'and body\nOpened by @None at [text]()'
'another title', 'and body\nOpened at [text](http://example.com/)'
)

testbot.assertCommand('!new issue coala title', 'repository that does not exist')
Expand Down
8 changes: 2 additions & 6 deletions utils/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ def message_link(bot, msg):
if backend == 'gitter':
return 'https://gitter.im/{uri}?at={idd}'.format(uri=msg.frm.room.uri,
idd=msg.extras['id'])
elif backend == 'slack':
elif 'url' in msg.extras:
return msg.extras['url']
elif backend == 'telegram':
return ''
elif backend == 'text':
return ''
else:
raise NotImplementedError
return None

0 comments on commit 4e3f359

Please sign in to comment.