From f256eeaa08912275f4fdd6042bffae53a1ad2c44 Mon Sep 17 00:00:00 2001 From: Nitanshu Date: Tue, 3 Jul 2018 21:51:56 +0530 Subject: [PATCH] LabHub: Use activate and configuration templates __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 https://github.com/coala/corobo/issues/554 Closes https://github.com/coala/corobo/issues/382 --- config.py | 9 +++++++++ plugins/constants.py | 4 ---- plugins/labhub.py | 33 ++++++++++++++++++++++----------- tests/corobo_test_case.py | 6 +++++- tests/git_stats_test.py | 1 - tests/labhub_test.py | 14 +++++++++----- 6 files changed, 45 insertions(+), 22 deletions(-) diff --git a/config.py b/config.py index 03c5fab5..2fe5cbba 100644 --- a/config.py +++ b/config.py @@ -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'), + }, +} diff --git a/plugins/constants.py b/plugins/constants.py index c85f0529..91eebcc7 100644 --- a/plugins/constants.py +++ b/plugins/constants.py @@ -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 diff --git a/plugins/labhub.py b/plugins/labhub.py index 8e1e51e4..979e31d0 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -1,5 +1,4 @@ import datetime -import os import re import time @@ -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): @@ -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') @@ -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() @@ -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 @@ -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 diff --git a/tests/corobo_test_case.py b/tests/corobo_test_case.py index d56a366d..ec9fe2ed 100644 --- a/tests/corobo_test_case.py +++ b/tests/corobo_test_case.py @@ -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] diff --git a/tests/git_stats_test.py b/tests/git_stats_test.py index fd5b7670..2301b4f2 100644 --- a/tests/git_stats_test.py +++ b/tests/git_stats_test.py @@ -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} diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 7d8d0f90..05295b65 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -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) @@ -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 @@ -331,7 +335,6 @@ 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, @@ -339,10 +342,11 @@ def test_assign_cmd(self): }, } 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)