-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (60 loc) · 2.33 KB
/
main.py
File metadata and controls
74 lines (60 loc) · 2.33 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import asyncio
import importlib
import os
import sys
import traceback
import aiohttp
from aiohttp import web
import cachetools
from gidgethub import aiohttp as gh_aiohttp
from gidgethub import routing
from gidgethub import sansio, apps
from routers import issue, stage, commands
from dotenv import load_dotenv
router = routing.Router(
issue.router,
stage.router,
commands.router
)
cache = cachetools.LRUCache(maxsize=500)
load_dotenv()
async def main(request):
try:
body = await request.read()
secret = os.getenv("WEBHOOK_SECRET")
event = sansio.Event.from_http(request.headers, body, secret=secret)
print('GH delivery ID', event.delivery_id, file=sys.stderr)
if event.event == "ping":
return web.Response(status=200)
repo_full_name = event.data.get("repository", {}).get("full_name", "")
if not repo_full_name.startswith("HytaleModding/"):
print(f"Ignoring event from repository: {repo_full_name}")
return web.Response(status=200)
async with aiohttp.ClientSession() as session:
gh = gh_aiohttp.GitHubAPI(session, "HytaleModding-Bot/1.0", cache=cache)
if not event.data.get("installation"):
return web.Response(text="Must be installed as an App.", status=400)
installation_id = event.data["installation"]["id"]
installation_access_token = await apps.get_installation_access_token(
gh,
installation_id=installation_id,
app_id=os.getenv("GH_APP_ID"),
private_key=os.getenv("GH_PRIVATE_KEY"),
)
gh.oauth_token = installation_access_token["token"]
await asyncio.sleep(1)
await router.dispatch(event, gh, session=session)
try:
print('GH requests remaining:', gh.rate_limit.remaining)
except AttributeError:
pass
return web.Response(status=200)
except Exception as exc:
traceback.print_exc(file=sys.stderr)
return web.Response(status=500)
if __name__ == "__main__":
app = web.Application()
app.router.add_post("/", main)
port = int(os.getenv("PORT", 25574))
print(f"Starting bot on port {port}")
web.run_app(app, port=port, host="0.0.0.0")