1
+ from __future__ import print_function
2
+
3
+ import datetime
1
4
import hashlib
2
5
import hmac
3
6
import json
4
7
import sys
8
+ import traceback
5
9
6
10
from .config import Config , InvalidTokenException
7
11
from .newpr import HighfiveHandler , UnsupportedRepoError
@@ -24,15 +28,19 @@ def create_app(config, webhook_secret=None, config_dir=None):
24
28
def new_pr ():
25
29
raw_data = flask .request .get_data ()
26
30
31
+ # Load all the headers
32
+ try :
33
+ event = str (flask .request .headers ['X-GitHub-Event' ])
34
+ delivery = str (flask .request .headers ['X-GitHub-Delivery' ])
35
+ signature = str (flask .request .headers ['X-Hub-Signature' ])
36
+ except KeyError :
37
+ return 'Error: some required webhook headers are missing\n ' , 400
38
+
27
39
# Check the signature only if the secret is configured
28
40
if 'payload' in flask .request .form and webhook_secret is not None :
29
41
expected = hmac .new (str (webhook_secret ), digestmod = hashlib .sha1 )
30
42
expected .update (raw_data )
31
43
expected = expected .hexdigest ()
32
- try :
33
- signature = str (flask .request .headers ['X-Hub-Signature' ])
34
- except KeyError :
35
- return 'Error: missing signature\n ' , 400
36
44
if not hmac .compare_digest ('sha1=' + expected , signature ):
37
45
return 'Error: invalid signature\n ' , 403
38
46
@@ -42,9 +50,18 @@ def new_pr():
42
50
return 'Error: missing or invalid payload\n ' , 400
43
51
try :
44
52
handler = HighfiveHandler (Payload (payload ), config , config_dir )
45
- return handler .run (flask . request . headers [ 'X-GitHub-Event' ] )
53
+ return handler .run (event )
46
54
except UnsupportedRepoError :
47
55
return 'Error: this repository is not configured!\n ' , 400
56
+ except :
57
+ print ()
58
+ print ('An exception occured while processing a webhook!' )
59
+ print ('Time:' , datetime .datetime .now ())
60
+ print ('Delivery ID:' , delivery )
61
+ print ('Event name:' , event )
62
+ print ('Payload:' , json .dumps (payload ))
63
+ print (traceback .format_exc ())
64
+ return 'Internal server error\n ' , 500
48
65
49
66
@app .route ('/' )
50
67
def index ():
@@ -62,9 +79,9 @@ def cli(port, github_token, webhook_secret, config_dir):
62
79
try :
63
80
config = Config (github_token )
64
81
except InvalidTokenException :
65
- print 'error: invalid github token provided!'
82
+ print ( 'error: invalid github token provided!' )
66
83
sys .exit (1 )
67
- print 'Found a valid GitHub token for user @' + config .github_username
84
+ print ( 'Found a valid GitHub token for user @' + config .github_username )
68
85
69
86
app = create_app (config , webhook_secret , config_dir )
70
87
waitress .serve (app , port = port )
0 commit comments