Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LabHub: Use activate and configuration templates #577

Merged
merged 2 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,12 @@
'LabHub:*': {'allowprivate': False}}

AUTOINSTALL_DEPS = True

DEFAULT_CONFIG = {
'LabHub': {
'GH_TOKEN': os.environ.get('GH_TOKEN'),
'GL_TOKEN': os.environ.get('GL_TOKEN'),
'GH_ORG_NAME': os.environ.get('GH_ORG_NAME', 'coala'),
'GL_ORG_NAME': os.environ.get('GL_ORG_NAME', 'coala'),
},
}
4 changes: 0 additions & 4 deletions plugins/constants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import os

API_DOCS = 'http://api.coala.io/en/latest'
USER_DOCS = 'http://docs.coala.io/en/latest'

GH_ORG_NAME = os.environ.get('GH_ORG_NAME', 'coala')
GL_ORG_NAME = os.environ.get('GL_ORG_NAME', 'coala')

MAX_MSG_LEN = 1000
MAX_LINES = 20
35 changes: 23 additions & 12 deletions plugins/labhub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import os
import re
import time

Expand All @@ -10,8 +9,8 @@
from errbot.templating import tenv

from functools import wraps
from plugins import constants
from utils.backends import message_link
from utils.mixin import DefaultConfigMixin


def members_only(func):
Expand All @@ -33,18 +32,22 @@ def wrapper(*args, **kwargs):
return wrapper


class LabHub(BotPlugin):
class LabHub(DefaultConfigMixin, BotPlugin):
"""GitHub and GitLab utilities""" # Ignore QuotesBear

GH_ORG_NAME = constants.GH_ORG_NAME
GL_ORG_NAME = constants.GL_ORG_NAME
CONFIG_TEMPLATE = {
'GH_ORG_NAME': None,
'GH_TOKEN': None,
'GL_ORG_NAME': None,
'GL_TOKEN': None,
}

def __init__(self, bot, name=None):
super().__init__(bot, name)
def activate(self):
super().activate()

teams = dict()
try:
gh = github3.login(token=os.environ.get('GH_TOKEN'))
gh = github3.login(token=self.config['GH_TOKEN'])
assert gh is not None
except AssertionError:
self.log.error('Cannot create github object, please check GH_TOKEN')
Expand All @@ -55,8 +58,8 @@ def __init__(self, bot, name=None):

self._teams = teams

self.IGH = GitHub(GitHubToken(os.environ.get('GH_TOKEN')))
self.IGL = GitLab(GitLabPrivateToken(os.environ.get('GL_TOKEN')))
self.IGH = GitHub(GitHubToken(self.config['GH_TOKEN']))
self.IGL = GitLab(GitLabPrivateToken(self.config['GL_TOKEN']))

self.REPOS = dict()

Expand All @@ -65,7 +68,7 @@ def __init__(self, bot, name=None):
filter(lambda x: (x.full_name.split('/')[0] ==
self.GH_ORG_NAME),
self.IGH.write_repositories)}
except RuntimeError:
except RuntimeError: # pragma: no cover, for logging
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should be able to use requests-mock to break IGitt so it gives a RuntimeError , but ... not critical ... this is OK for now

self.log.exception('Something went wrong in fetching github repos.')
else:
self.REPOS.update(self.gh_repos)
Expand All @@ -82,6 +85,14 @@ def __init__(self, bot, name=None):

self.hello_world_users = set()

@property
def GH_ORG_NAME(self):
return self.config['GH_ORG_NAME']

@property
def GL_ORG_NAME(self):
return self.config['GL_ORG_NAME']

@property
def TEAMS(self):
return self._teams
Expand Down Expand Up @@ -368,7 +379,7 @@ def newcomer_issue_check(user, iss):
search_query = 'user:coala assignee:{} ' \
'label:difficulty/newcomer'.format(user)
result = GitHub.raw_search(GitHubToken(
os.environ.get('GH_TOKEN')), search_query)
self.config['GH_TOKEN']), search_query)
return not (sum(1 for _ in result) >= 1)
else:
return True
Expand Down
6 changes: 5 additions & 1 deletion tests/corobo_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ def load_plugin_templates(self, klass):
self.plug_files[klass.__name__] = plug_info
add_plugin_templates_path(plug_info)

def load_plugin(self, plugin_name: str, mock_dict=False):
def load_plugin(self,
plugin_name: str,
mock_dict=False,
plugin_config=None):
"""Load plugin manually"""
klass = self.klasses[plugin_name]
plugin = klass(self.bot, plugin_name)
plugin.configure(plugin_config)
self.plugins[plugin_name] = plugin
self.bot.plugin_manager.plugins[plugin_name] = plugin
plug_file = self.plug_files[plugin_name]
Expand Down
23 changes: 18 additions & 5 deletions tests/labhub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ def setUp(self):
},
'_teams': self.teams,
}
self.labhub = self.load_plugin('LabHub', self.global_mocks)
configs = {
'GH_TOKEN': None,
'GL_TOKEN': None,
'GH_ORG_NAME': 'coala',
'GL_ORG_NAME': 'coala',
}
self.labhub = self.load_plugin('LabHub', self.global_mocks, configs)

def test_invite_cmd(self):
mock_team_newcomers = create_autospec(github3.orgs.Team)
Expand All @@ -45,8 +51,6 @@ def test_invite_cmd(self):
self.inject_mocks('LabHub', mock_dict)
testbot = self

plugins.labhub.os.environ['GH_TOKEN'] = 'patched?'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note #572 (comment)

You probably need a separate test method for this.
And that is not a newcomer problem --it is a test coverage problem which you need to sort out in your project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current minor coverage error might be related to this


self.assertEqual(self.labhub.TEAMS, self.teams)

mock_dict['is_room_member'].return_value = False
Expand Down Expand Up @@ -304,18 +308,18 @@ def test_assign_cmd(self):

# no assignee, newcomer, difficulty medium
mock_dict = {
'GH_ORG_NAME': 'not-coala',
'TEAMS': {
'not-coala newcomers': self.mock_team,
'not-coala developers': mock_dev_team,
'not-coala maintainers': mock_maint_team,
},
}
self.inject_mocks('LabHub', mock_dict)
self.labhub.config['GH_ORG_NAME'] = 'not-coala'

testbot.assertCommand(cmd.format('coala', 'a', '23'),
'assigned')
mock_dict['GH_ORG_NAME'] = 'coala'
self.labhub.config['GH_ORG_NAME'] = 'coala'
mock_dict['TEAMS'] = self.teams
self.inject_mocks('LabHub', mock_dict)

Expand Down Expand Up @@ -472,3 +476,12 @@ def test_alive(self):
'!pr stats 3hours',
'You need to be a member of this organization '
'to use this command.', timeout=100)

def test_invalid_token(self):
plugins.labhub.github3.login.return_value = None
with self.assertLogs() as cm:
self.labhub.activate()
self.assertIn(
'ERROR:errbot.plugins.LabHub:Cannot create github object,'
' please check GH_TOKEN',
cm.output)