-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.py
41 lines (34 loc) · 1.25 KB
/
listener.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
#!/usr/bin/env python
import hashlib
import hmac
import json
import os
import urllib
from flask import abort
from flask import Flask
from flask import request
from subprocess import PIPE
from subprocess import Popen
basedir = os.path.dirname(os.path.abspath(__file__))
with open(basedir + '/data/projects.lst') as f:
# Gets us a list of repo names
projects = map(lambda x: x.rsplit('/', 1)[1][:-4], f.read().splitlines())
app = Flask(__name__)
app.config.from_envvar('DEPLOYEE_SETTINGS')
@app.route('/', methods=['POST'])
def webhook():
if request.headers.get('X-Uptime-Check') == 'True':
# This is an uptime check. Don't run refresh.
return 'OK'
# Verify the body
signature = hmac.new(app.config['HUB_SECRET'], msg=request.get_data(),
digestmod=hashlib.sha1).hexdigest()
if request.headers.get('X-Hub-Signature').split('=')[1] != signature:
abort(401)
repo = request.json.get('repository', {'name': None})['name']
branch = request.json.get('ref', 'x'*9)[-6:]
if repo in projects and branch == 'master':
Popen([basedir+'/scripts/refresh.sh', '-v', repo], stdout=PIPE, stderr=PIPE)
return 'OK'
if __name__ == '__main__':
app.run(host=app.config['HOST'], port=app.config['PORT'])