Skip to content

Commit

Permalink
LabHub: Use activate and configuration templates
Browse files Browse the repository at this point in the history
__init__ is executed at load time and
and any issue in the plugin will lead
to it not showing up.
LifeCycle: __init__->configured->enable

Adapts to the `DefaultConfigMixin` and
migrated to using configuration templates.

Closes #554
Closes #382
  • Loading branch information
nvzard committed Aug 3, 2018
1 parent 7179622 commit e70b745
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
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': 'coala',
'GL_ORG_NAME': 'coala',
},
}
58 changes: 35 additions & 23 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,47 +32,54 @@ def wrapper(*args, **kwargs):
return wrapper


class LabHub(BotPlugin):
class LabHub(BotPlugin, DefaultConfigMixin):
"""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': 'org_name',
'GH_TOKEN': 'token',
'GL_ORG_NAME': 'org_name',
'GL_TOKEN': 'token',
}

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

def activate(self):
BotPlugin.activate(self)

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:
except AssertionError: # pragma: no cover, for logging
self.log.error('Cannot create github object, please check GH_TOKEN')
else:
self.GH3_ORG = gh.organization(self.GH_ORG_NAME)
self.GH3_ORG = gh.organization(self.config['GH_ORG_NAME'])
for team in self.GH3_ORG.teams():
teams[team.name] = team

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()

try:
self.gh_repos = {repo.full_name.split('/')[-1]: repo for repo in
filter(lambda x: (x.full_name.split('/')[0] ==
self.GH_ORG_NAME),
self.config['GH_ORG_NAME']),
self.IGH.write_repositories)}
except RuntimeError:
except RuntimeError: # pragma: no cover, for logging
self.log.exception('Something went wrong in fetching github repos.')
else:
self.REPOS.update(self.gh_repos)

try:
self.gl_repos = {repo.full_name.split('/')[-1]: repo for repo in
filter(lambda x: (x.full_name.split('/')[0] ==
self.GL_ORG_NAME),
self.config['GL_ORG_NAME']),
self.IGL.write_repositories)}
except RuntimeError: # pragma: no cover, for logging
self.log.exception('Something went wrong in fetching gitlab repos.')
Expand All @@ -92,9 +98,12 @@ def TEAMS(self, new):

def team_mapping(self):
return {
'newcomers': self.TEAMS[self.GH_ORG_NAME + ' newcomers'],
'developers': self.TEAMS[self.GH_ORG_NAME + ' developers'],
'maintainers': self.TEAMS[self.GH_ORG_NAME + ' maintainers'],
'newcomers':
self.TEAMS[self.config['GH_ORG_NAME'] + ' newcomers'],
'developers':
self.TEAMS[self.config['GH_ORG_NAME'] + ' developers'],
'maintainers':
self.TEAMS[self.config['GH_ORG_NAME'] + ' maintainers'],
}

def is_team_member(self, user, team):
Expand Down Expand Up @@ -232,7 +241,8 @@ def unassign_cmd(self, msg, match):
user = msg.frm.nick

try:
assert org == self.GH_ORG_NAME or org == self.GL_ORG_NAME
assert (org == self.config['GH_ORG_NAME'] or
org == self.config['GL_ORG_NAME'])
except AssertionError:
yield 'Repository not owned by our org.'
return
Expand Down Expand Up @@ -317,7 +327,8 @@ def assign_cmd(self, msg, match):
user = msg.frm.nick

try:
assert org == self.GH_ORG_NAME or org == self.GL_ORG_NAME
assert (org == self.config['GH_ORG_NAME'] or
org == self.config['GL_ORG_NAME'])
except AssertionError:
yield 'Repository not owned by our org.'
return
Expand All @@ -328,7 +339,8 @@ def register_check(function):
checks.append(function)
return function

if self.GH_ORG_NAME == 'coala' and self.GL_ORG_NAME == 'coala':
if (self.config['GH_ORG_NAME'] == 'coala' and
self.config['GL_ORG_NAME'] == 'coala'):
@register_check
def difficulty_level(user, iss):
"""
Expand Down Expand Up @@ -368,7 +380,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 Expand Up @@ -402,7 +414,7 @@ def eligible(user, iss):
yield tenv().get_template(
'labhub/errors/not-eligible.jinja2.md'
).render(
organization=self.GH_ORG_NAME,
organization=self.config['GH_ORG_NAME'],
)
elif user in iss.assignees:
yield ('The issue is already assigned to you.')
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)
plugin.activate()
self.plugins[plugin_name] = plugin
self.bot.plugin_manager.plugins[plugin_name] = plugin
Expand Down
18 changes: 11 additions & 7 deletions tests/labhub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ def setUp(self):
},
'_teams': self.teams,
}
self.labhub = self.load_plugin('LabHub', self.global_mocks)
configs = {
'GH_TOKEN': 'token',
'GL_TOKEN': 'token',
'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 @@ -72,8 +78,6 @@ def test_invite_cmd(self):
self.inject_mocks('LabHub', mock_dict)
testbot = self

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

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

mock_dict['is_room_member'].return_value = False
Expand Down Expand Up @@ -150,8 +154,8 @@ def test_hello_world_callback(self):
testbot.assertCommand('helloworld', 'newcomer')

def test_create_issue_cmd(self):
plugins.labhub.GitHubToken.assert_called_with(None)
plugins.labhub.GitLabPrivateToken.assert_called_with(None)
plugins.labhub.GitHubToken.assert_called_with('token')
plugins.labhub.GitLabPrivateToken.assert_called_with('token')

# Start ignoring PycodestyleBear, LineLengthBear
# TODO
Expand Down Expand Up @@ -331,18 +335,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

0 comments on commit e70b745

Please sign in to comment.