-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
46 lines (34 loc) · 1.26 KB
/
server.py
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
import os
import aiohttp
from aiohttp import web
from gidgethub import routing, sansio
from gidgethub import aiohttp as gh_aiohttp
routes = web.RouteTableDef()
router = routing.Router()
@router.register("issues", action="opened")
async def issue_opened_event(event, gh, *args, **kwargs):
"""
Whenever an issue is opened, greet the author and say thanks.
"""
url = event.data["issue"]["comments_url"]
author = event.data["issue"]["user"]["login"]
message = f"Thanks for the report @{author}! I will look into it ASAP! (I'm a bot)."
await gh.post(url, data={"body": message})
@routes.post("/")
async def main(request):
body = await request.read()
secret = os.environ.get("GH_SECRET")
oauth_token = os.environ.get("GH_AUTH")
event = sansio.Event.from_http(request.headers, body, secret=secret)
async with aiohttp.ClientSession() as session:
gh = gh_aiohttp.GitHubAPI(session, "mariatta",
oauth_token=oauth_token)
await router.dispatch(event, gh)
return web.Response(status=200)
if __name__ == "__main__":
app = web.Application()
app.add_routes(routes)
port = os.environ.get("PORT")
if port is not None:
port = int(port)
web.run_app(app, port=port)