Created
January 1, 2016 23:44
-
-
Save JohnPeel/88022aed5ba4ce161e69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from flask import Flask, request | |
import os | |
import json | |
import urllib | |
app = Flask(__name__) | |
app.debug = False | |
@app.route('/github-webhook', methods=['GET', 'POST']) | |
def index(): | |
if request.method == 'GET': | |
return 'OK' | |
elif request.method == 'POST': | |
event = request.headers.get('X-Github-Event') | |
signature = request.headers.get('X-Hub-Signature') | |
id = request.headers.get('X-Github-Delivery') | |
data = request.data | |
data_json = json.loads(data) | |
if (event == 'ping'): | |
return json.dumps({'msg': 'Hi!'}) | |
if (event != 'push'): | |
return json.dumps({'msg': 'wrong event type'}) | |
owner = data_json['repository']['owner']['name'] | |
repo = data_json['repository']['name'] | |
branch = data_json['ref'][11:] | |
new_commit = data_json['head_commit']['id'] | |
path = '/var/www/up.dgby.org/htdocs/%s/%s/%s' % (owner, repo, branch) | |
try: | |
os.makedirs(path) | |
except: pass | |
url = 'https://github.com/%s/%s/tarball/%s' % (owner, repo, branch) | |
version = os.path.join(path, 'version') | |
commit = os.path.join(path, 'commit') | |
archive = os.path.join(path, '%s-%s.tar.gz' % (repo, branch)) | |
if (not os.path.exists(version)): | |
with open(version, 'a+') as f: | |
f.write('0') | |
if (not os.path.exists(commit)): | |
with open(commit, 'a+') as f: | |
f.write('4b825dc642cb6eb9a060e54bf8d69288fbee4904') | |
with open(commit, 'r+') as f: | |
if (new_commit != f.read()): | |
urllib.urlretrieve(url, archive) | |
f.seek(0) | |
f.write(new_commit) | |
with open(version, 'r') as a: | |
ver = int(a.read()) | |
with open(version, 'w') as a: | |
a.write(str(ver + 1)) | |
return 'OK' | |
if (__name__ == '__main__'): | |
app.run(host = '127.0.0.1', port = 54333) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment