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 coala#554
Closes coala#382
  • Loading branch information
nvzard committed Aug 5, 2018
1 parent b9315d1 commit f256eea
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 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': 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
33 changes: 22 additions & 11 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 @@ -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
1 change: 0 additions & 1 deletion tests/git_stats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def setUp(self):
plugins.labhub.github3.organization.return_value = self.mock_org

self.plugin = plugins.git_stats.GitStats
self.plugin.__bases__ = (BotPlugin, )
self.labhub = self.load_plugin('LabHub')
self.git_stats = self.load_plugin('GitStats')
self.git_stats.REPOS = {'test': self.mock_repo}
Expand Down
14 changes: 9 additions & 5 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': 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 @@ -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 @@ -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 f256eea

Please sign in to comment.