-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub-hook.py
More file actions
40 lines (35 loc) · 1.2 KB
/
Copy pathgithub-hook.py
File metadata and controls
40 lines (35 loc) · 1.2 KB
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 os, os.path
import json
import subprocess
from flask import Flask, request, redirect, abort
app = Flask(__name__)
GITROOT = '/var/git/github'
@app.route('/')
def index():
return redirect('http://github.com/hasgeek/doctypehtml5')
@app.route('/', methods=['POST'])
def commit():
payload = request.form.get('payload')
if not payload:
return "Missing form variable 'payload'"
payload = json.loads(payload)
reponame = payload['repository']['name']
savedir = os.getcwd()
repodir = os.path.join(GITROOT, reponame)
if os.access(repodir, os.W_OK | os.X_OK):
os.chdir(repodir)
process = subprocess.Popen(['git', 'pull'], env=os.environ,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = []
while process.poll is None:
result.extend(process.stdin.readlines())
result.extend(process.stderr.readlines())
os.chdir(savedir) # Is this really required?
return '\r\n'.join(result)
else:
return "Unknown repository, or no access"
application = app # For WSGI
if __name__ == '__main__':
app.run('0.0.0.0')