-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy path__main__.py
More file actions
52 lines (37 loc) · 1.28 KB
/
__main__.py
File metadata and controls
52 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Copyright (c) ACSONE SA/NV 2018
# Distributed under the MIT License (http://opensource.org/licenses/MIT).
"""OCA GitHub Bot
This is the main program, which provides the dispatching
mechanisms for webhook calls from github.
"""
import logging
import aiohttp
from aiohttp import web
from gidgethub import aiohttp as gh_aiohttp, sansio as gh_sansio
from . import config
from .router import router
_logger = logging.getLogger(__name__)
async def webhook(request):
"""This is the main webhook dispatcher
Handlers are declared with the @router.register decorator.
See https://gidgethub.readthedocs.io/en/latest/routing.html
"""
body = await request.read()
event = gh_sansio.Event.from_http(
request.headers, body, secret=config.GITHUB_SECRET
)
async with aiohttp.ClientSession() as session:
gh = gh_aiohttp.GitHubAPI(
session, config.GITHUB_LOGIN, oauth_token=config.GITHUB_TOKEN
)
await router.dispatch(event, gh)
return web.Response(status=200)
def main():
# configure logging
logging.basicConfig(level=logging.DEBUG)
# launch webhook app
app = web.Application()
app.router.add_post("/", webhook)
web.run_app(app, host=config.HTTP_HOST, port=config.HTTP_PORT)
if __name__ == "__main__":
main()