-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhook.py
60 lines (49 loc) · 1.51 KB
/
webhook.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
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import commands
import hmac
import json
import os
import web
from hashlib import sha1
#==============================================================================
# environment variable
SECRET = os.getenv('GITLAB_MIRROR_WEBHOOK_SECRET')
WORK_DIR = os.etenv('GITLAB_MIRROR_WORK_DIR')
#==============================================================================
urls = (
'/', 'Index'
)
def getRepoName(data):
try:
return json.loads(data).get('repository').get('name')
except Exception, e:
print e
def getSignature():
sig = web.ctx.env.get('HTTP_X_HUB_SIGNATURE')
if sig:
shaName, signature = sig.split('=')
if shaName == 'sha1':
return signature
return ''
def verifySignature(secret, signature, data):
if secret and signature:
mac = hmac.new(str(secret), msg=data, digestmod=sha1)
expected = str(mac.hexdigest())
print ("expected signature: %s, request signature: %s" %(expected, str(signature)))
return expected == str(signature)
return not secret and not signature
class Index:
def GET(self):
return "Hello World"
def POST(self):
signature = getSignature()
if verifySignature(SECRET, signature, web.data()):
repository = getRepoName(web.data())
update_gitlab_mirror = "cd $s && ./update_mirror.sh %s" %(WORK_DIR, repository)
out = commands.getoutput(update_gitlab_mirror)
return out
return web.unauthorized()
app = web.application(urls, globals())
if __name__ == "__main__":
app.run()