diff --git a/README.md b/README.md index 0f321c9..f735d86 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ orginally developed for Bloomberg's GHE install. receives push events all it takes is: ```py -from github_webhook import Webhook +from github_webhook import Webhook, EventType from flask import Flask app = Flask(__name__) # Standard Flask app @@ -27,6 +27,12 @@ def hello_world(): def on_push(data): print("Got push with: {0}".format(data)) +@webhook.hook(event_type=EventType.Ping) # Defines a handler for the 'ping' event +def on_push(data): + print("Got push with: {0}".format(data)) + + + if __name__ == "__main__": app.run(host="0.0.0.0", port=80) ``` diff --git a/github_webhook/__init__.py b/github_webhook/__init__.py index 89ffe17..ecba570 100644 --- a/github_webhook/__init__.py +++ b/github_webhook/__init__.py @@ -8,7 +8,7 @@ :license: Apache License, Version 2.0 """ -from github_webhook.webhook import Webhook +from github_webhook.webhook import (Webhook, EventType) # ----------------------------------------------------------------------------- # Copyright 2015 Bloomberg Finance L.P. diff --git a/github_webhook/event_type.py b/github_webhook/event_type.py new file mode 100644 index 0000000..d92e4ad --- /dev/null +++ b/github_webhook/event_type.py @@ -0,0 +1,30 @@ +from enum import Enum + + +class EventType(Enum): + """ + Event enum type + """ + CommitComment = 'commit_comment' + Create = 'create' + Delete = 'delete' + Deployment = 'deployment' + DeploymentStatus = 'deployment_status' + Fork = 'fork' + Gollum = 'gollum' + IssueComment = 'issue_comment' + Issues = 'issues' + Member = 'member' + Membership = 'membership' + PageBuild = 'page_build' + Ping = 'ping' + Public = 'public' + PullRequest = 'pull_request' + PullRequestReview = 'pull_request_review' + PullRequestReviewComment = 'pull_request_review_comment' + Push = 'push' + Release = 'release' + Repository = 'repository' + Status = 'status' + TeamAdd = 'team_add' + Watch = 'watch' diff --git a/github_webhook/test_webhook.py b/github_webhook/test_webhook.py index 4df373d..354f79e 100644 --- a/github_webhook/test_webhook.py +++ b/github_webhook/test_webhook.py @@ -3,6 +3,10 @@ from __future__ import print_function import unittest +from nose.tools import assert_equal + +from github_webhook.event_type import EventType + try: from unittest.mock import Mock except ImportError: @@ -24,6 +28,24 @@ def test_constructor(self): app.add_url_rule.assert_called_once_with( '/postreceive', view_func=webhook._postreceive, methods=['POST']) + def test_hook(self): + # GIVEN + app = Mock() + + def test_handler(): + return "OK" + + # WHEN + webhook = Webhook(app) + + webhook.hook()(test_handler) + assert_equal(webhook._hooks['push'][0], test_handler) + webhook.hook(EventType.CommitComment)(test_handler) + assert_equal(webhook._hooks['commit_comment'][0], test_handler) + webhook.hook('deployment')(test_handler) + assert_equal(webhook._hooks['deployment'][0], test_handler) + + # ----------------------------------------------------------------------------- # Copyright 2015 Bloomberg Finance L.P. # diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index 6486566..7c99eeb 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -5,6 +5,7 @@ import six from flask import abort, request +from .event_type import EventType class Webhook(object): @@ -26,16 +27,19 @@ def __init__(self, app, endpoint='/postreceive', secret=None): secret = secret.encode('utf-8') self._secret = secret - def hook(self, event_type='push'): + def hook(self, event_type=EventType.Push): """ Registers a function as a hook. Multiple hooks can be registered for a given type, but the order in which they are invoke is unspecified. :param event_type: The event type this hook will be invoked for. + :type event_type: Union[str, EventType] """ + event_type_str = event_type.value if isinstance(event_type, EventType) else event_type + def decorator(func): - self._hooks[event_type].append(func) + self._hooks[event_type_str].append(func) return func return decorator diff --git a/setup.py b/setup.py index ae11487..29eccac 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ author_email="achamberlai9@bloomberg.net, fphillips7@bloomberg.net, dkiss1@bloomberg.net, dbeer1@bloomberg.net", license='Apache 2.0', packages=["github_webhook"], - install_requires=['flask', 'six'], + install_requires=['flask', 'six', 'enum34'], tests_require=['mock', 'nose'], classifiers=[