Skip to content

Commit

Permalink
Improve base VCS
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow committed Mar 3, 2020
1 parent b2477bd commit 02945ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
38 changes: 22 additions & 16 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@

class VCS:

def __init__(self, name, config, default_host):
self._name = name
def __init__(self, config, default_host):
self._config = config
self._default_host = default_host

def get_config(self, item, default_value=None):
return self._config.get(item, default_value)

@property
def name(self):
return self._name
def name(cls):
raise NotImplementedError

@property
def token(self):
token = self.get_config('token', '')
if len(token) == 0:
raise NotiError(self._name, 'Wrong configuration: Please make sure you have the right token')
raise NotiError(self.name, 'Wrong configuration: Please make sure you have the right token')

return token

Expand Down Expand Up @@ -169,13 +168,16 @@ def __init__(self, vcs, message, help_link=None):
self.help_link = help_link

class Gitlab(VCS):

name = 'Gitlab'

def __init__(self, config):
super().__init__('gitlab', config, 'https://gitlab.com')
super().__init__(config, 'https://gitlab.com')

try:
import gitlab
except:
raise NotiError('gitlab', 'Missing dependencies: You need to install python-gitlab', 'https://python-gitlab.readthedocs.io/en/stable/install.html')
raise NotiError(self.name, 'Missing dependencies: You need to install python-gitlab', 'https://python-gitlab.readthedocs.io/en/stable/install.html')

self._gl = gitlab.Gitlab(self.host, private_token=self.token)

Expand Down Expand Up @@ -262,13 +264,16 @@ def __init__(self, mr, review):
)

class Github(VCS):

name = 'Github'

def __init__(self, config):
super().__init__('github', config, 'https://api.github.com')
super().__init__(config, 'https://api.github.com')

try:
import github
except:
raise NotiError('github', 'Missing dependencies: You need to install PyGithub', 'https://pygithub.readthedocs.io/en/latest/introduction.html#download-and-install')
raise NotiError(self.name, 'Missing dependencies: You need to install PyGithub', 'https://pygithub.readthedocs.io/en/latest/introduction.html#download-and-install')

self._gh = github.Github(self.token, base_url=self.host)

Expand Down Expand Up @@ -532,13 +537,14 @@ def main(vcs, bp):

if __name__ == "__main__":
conf = NotiConfig()
registry = [Gitlab, Github]
vcs = []
try:
if 'gitlab' in conf.user_config:
vcs.append(Gitlab(conf.get_config('gitlab')))
if 'github' in conf.user_config:
vcs.append(Github(conf.get_config('github')))
except NotiError as e:
bp.fatal(f"[{e.vcs}] {e.message}", e.help_link)
for s in registry:
key = s.name.lower()
if key in conf.user_config:
try:
vcs.append(s(conf.get_config(key)))
except NotiError as e:
bp.fatal(f"[{e.vcs}] {e.message}", e.help_link)

main(vcs, bp)
16 changes: 14 additions & 2 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

class DummyVCS(VCS):

name = '_name_'

def __init__(self, config):
super().__init__('_name_', config, '_host_')
super().__init__(config, '_host_')

class DummyVCSNoname(VCS):

def __init__(self, config):
super().__init__(config, '_host_')


class TestVCS:
Expand All @@ -30,4 +37,9 @@ def test_name(self, vcs):
assert vcs.name == '_name_'

def test_token(self, vcs):
assert vcs.token == '_token_'
assert vcs.token == '_token_'

def test_no_name_vcs(self):
vcs = DummyVCSNoname({})
with pytest.raises(NotImplementedError):
assert vcs.name

0 comments on commit 02945ef

Please sign in to comment.