Skip to content

Commit

Permalink
Merge ea15ec8 into 7cdf71f
Browse files Browse the repository at this point in the history
  • Loading branch information
jaumef committed Oct 9, 2017
2 parents 7cdf71f + ea15ec8 commit 0ed0e63
Show file tree
Hide file tree
Showing 27 changed files with 1,213 additions and 1,336 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ source = ./hookshub
[report]
exclude_lines =
if __name__ == .__main__.:
def reload_hooks
def _pickle_method
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ install:
- pip install coveralls
- pip install -e .
script:
- mamba --enable-coverage
- mamba --enable-coverage --format documentation
after_success:
- coveralls
- coverage report
139 changes: 139 additions & 0 deletions hookshub/hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import logging


class Hook(object):
"""
This class is used as a template for hook import as a plugin.
All the methods can be overwritten but the super's __init__ must run inside
__init__
Enable may configure alternative parameters for the hook to run, that's why
initiates at False and runs enable() on __init__
"""
def __init__(self, method, event=False, repository=False, branch=False):
self._name = str(self.__class__)
self._hook = method
self._event = event
self._repository = repository
self._branch = branch
self._enabled = False
self.enable()

def is_enabled(self):
return self._enabled

def get_args(self, webhook=False, conf=False):
"""
:param webhook: WebHook with origin's payload
:param conf: environment conf to be used
:return: Args to call "run_hook" method
"""
dict = {}
if conf:
dict.update({
'conf': True
})
if webhook:
dict.update({
'webhook': True
})
if not dict:
dict = {
'Default': True
}
return dict

def enable(self):
self._enabled = True
return self.is_enabled()

def disable(self):
self._enabled = False
return self.is_enabled()

def run_hook(self, args=False):
if not args:
raise EnvironmentError(
'Args for hook {} were not set before running'.format(
self.title
)
)
return self._hook(args), self.title

@property
def title(self):
return self._name

@property
def hook(self):
return self._hook

@property
def event(self):
return self._event

@property
def repository(self):
return self._repository

@property
def branch(self):
return self._branch

@property
def enabled(self):
return self._enabled


#
# Using Hooks Manager (hookshub.plugins) a hook may be disabled or enabled
#
# With "reload_hooks()" the hooks are reloaded with no need to
# restart the service
#
# "get_hooks()" must return all hooks loaded
#


def get_hooks(event=False, repository=False, branch=False):
from hookshub.plugins import plugins
results = []
hooks = plugins.get_hooks()
for hook in hooks:
if event and hook.event:
ev = hook.event == event
else:
ev = True
if repository and hook.repository:
repo = hook.repository == repository
else:
repo = True
if branch and hook.branch:
br = hook.branch == branch
else:
br = True
if ev and repo and br:
results.append((hook.name, hook.hook))
return results


# Reload Hooks cannot be tested truthfully without working environment, se we
# don't test it.
def reload_hooks():
# Update working set before using it
import imp
import pkg_resources
imp.reload(pkg_resources)
# Import plugin managers after reloading pkg_resources
from hookshub.plugins import plugins
from pkg_resources import working_set
logger = logging.getLogger()
for entrypoint in working_set.iter_entry_points('hookshub.plugins'):
try:
plugin = entrypoint.load()
except Exception as e:
logger.error('Could not load plugin {}:\n{}'.format(
entrypoint, e
))
else:
plugins.register(plugin)
Loading

0 comments on commit 0ed0e63

Please sign in to comment.