forked from pep8speaks-org/pep8speaks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
59 lines (47 loc) · 2.04 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
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
import logging
import os
import sys
from flask import Flask, redirect, request
from flask_session import Session
from pep8speaks import handlers, utils
def create_app():
app = Flask(__name__)
sess = Session()
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
@app.route("/", methods=['GET', 'POST'])
def main():
"""Main function to handle all requests."""
if request.method == "POST":
# GitHub sends the secret key in the payload header
if utils.match_webhook_secret(request):
event = request.headers["X-GitHub-Event"]
app.logger.debug(f"Request Headers:\n{request.headers}")
app.logger.debug(f"Request body:\n{request.json}")
event_to_action = {
"pull_request": handlers.handle_pull_request,
"integration_installation": handlers.handle_integration_installation,
"integration_installation_repositories": handlers.handle_integration_installation_repo,
"installation_repositories": handlers.handle_integration_installation_repo,
"ping": handlers.handle_ping,
"issue_comment": handlers.handle_issue_comment,
"installation": handlers.handle_installation,
}
supported_event = event in event_to_action
if supported_event:
return event_to_action[event](request)
else:
return handlers.handle_unsupported_requests(request)
else:
app.logger.info("Received an unauthorized request")
return handlers.handle_unauthorized_requests()
else:
return redirect("https://pep8speaks.com")
app.secret_key = os.environ.setdefault("APP_SECRET_KEY", "")
app.config['SESSION_TYPE'] = 'filesystem'
sess.init_app(app)
app.debug = False
return app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)