Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Github webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
ovv committed Dec 21, 2017
1 parent 0ef01a1 commit 341437c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sirbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@


class SirBot(aiohttp.web.Application):
def __init__(self, **kwargs):
def __init__(self, user_agent=None, **kwargs):
super().__init__(**kwargs)

self.plugins = dict()
self.http_session = aiohttp.ClientSession(loop=kwargs.get('loop') or asyncio.get_event_loop())
self.user_agent = user_agent or 'sir-bot-a-lot'
self.router.add_route('GET', '/sirbot/plugins', endpoints.plugins)

def start(self, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions sirbot/plugins/github/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .plugin import GithubPlugin
42 changes: 42 additions & 0 deletions sirbot/plugins/github/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import logging

from gidgethub import ValidationFailure
from gidgethub.aiohttp import GitHubAPI
from gidgethub.sansio import Event
from gidgethub.routing import Router

from aiohttp.web import Response

LOG = logging.getLogger(__name__)


class GithubPlugin:
__name__ = 'github'

def __init__(self, *, verify=None):
self.api = None
self.router = Router()
self.verify = verify or os.environ['GITHUB_VERIFY']

def load(self, sirbot):
LOG.info('Loading github plugin')
self.api = GitHubAPI(session=sirbot.http_session, requester=sirbot.user_agent)

sirbot.router.add_route('POST', '/github', dispatch)


async def dispatch(request):
github = request.app.plugins['github']
payload = await request.read()

try:
event = Event.from_http(request.headers, payload, secret=github.verify)
await github.router.dispatch(event, app=request.app)
except ValidationFailure as e:
LOG.debug('Github webhook failed verification: %s, %s', request.headers, payload)
except Exception as e:
LOG.exception(e)
return Response(status=500)
else:
return Response(status=200)

0 comments on commit 341437c

Please sign in to comment.