Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand All@@ -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)
```
Expand Down
2 changes: 1 addition & 1 deletion github_webhook/__init__.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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.
Expand Down
30 changes: 30 additions & 0 deletions github_webhook/event_type.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
fromenumimportEnum


classEventType(Enum):
"""
Event enum type

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to link to https://developer.github.com/v3/activity/events/types/ in the docstring so it shows up in the docs, plus anyone keeping this event list up to date in the future knows where to look

"""
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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a half dozen events that are missing here compared to the list on:
https://developer.github.com/v3/activity/events/types/

22 changes: 22 additions & 0 deletions github_webhook/test_webhook.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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:
Expand All@@ -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.
#
Expand Down
8 changes: 6 additions & 2 deletions github_webhook/webhook.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@

import six
from flask import abort, request
from .event_type import EventType


class Webhook(object):
Expand All@@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@
author_email="[email protected], [email protected], [email protected], [email protected]",
license='Apache 2.0',
packages=["github_webhook"],
install_requires=['flask', 'six'],
install_requires=['flask', 'six', 'enum34'],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use the python_version specifier here to limit this dependency to Python 2 only?

tests_require=['mock', 'nose'],

classifiers=[
Expand Down