This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.py
49 lines (36 loc) · 1.44 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
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
from blinker import signal
import json, yaml
from hashlib import sha1
import hmac
class AuthenticationException(Exception):
pass
class Server(Resource):
on_pr = signal("pull_event")
on_comment = signal("comment_event")
isLeaf = True
secret = yaml.load(open("config.yml"))["post_secret"]
def render_GET(self, request):
return "<html><body>What are you doin here buddy?</body></html>"
def render_POST(self, request):
body = request.content.read()
# Get hash (drop sha1=)
sig = request.getHeader("X-Hub-Signature")[5:]
#http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html#authednotify
hash = hmac.new(self.secret, request.content.getvalue(), sha1)
if hash.digest().encode("hex") != sig:
raise AuthenticationException("Could not identify")
parsed = json.loads(body)
data = {k: v for k, v in parsed.iteritems()
if k in ("issue", "comment", "number", "action")}
event = self.on_pr if "number" in data else self.on_comment
reactor.callInThread(event.send, data) # self.on_pr.send(data)
return ""
def start(port):
resource = Server()
factory = Site(resource)
print "Booting server on port %d" % (port)
reactor.listenTCP(port, factory)
reactor.run()