-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgitlab-webhook.py
executable file
·175 lines (153 loc) · 6.32 KB
/
gitlab-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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
import os
import json
import argparse
import BaseHTTPServer
import shlex
import subprocess
import shutil
import logging
logger = logging.getLogger('gitlab-webhook-processor')
logger.setLevel(logging.DEBUG)
logging_handler = logging.StreamHandler()
logging_handler.setFormatter(
logging.Formatter("%(asctime)s %(levelname)s %(message)s",
"%B %d %H:%M:%S"))
logger.addHandler(logging_handler)
repository = ''
branch_dir = ''
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(self):
logger.info("Received POST request.")
self.rfile._sock.settimeout(5)
if not self.headers.has_key('Content-Length'):
return self.error_response()
json_data = self.rfile.read(
int(self.headers['Content-Length'])).decode('utf-8')
try:
data = json.loads(json_data)
except ValueError:
logger.error("Unable to load JSON data '%s'" % json_data)
return self.error_response()
data_repository = data.get('repository', {}).get('url')
if data_repository == repository:
branch_to_update = data.get('ref', '').split('refs/heads/')[-1]
branch_to_update = branch_to_update.replace('; ', '')
if branch_to_update == '':
logger.error("Unable to identify branch to update: '%s'" %
data.get('ref', ''))
return self.error_response()
elif (branch_to_update.find("/") != -1 or
branch_to_update in ['.', '..']):
# Avoid feature branches, malicious branches and similar.
logger.debug("Skipping update for branch '%s'." %
branch_to_update)
else:
self.ok_response()
branch_deletion = data['after'].replace('0', '') == ''
branch_addition = data['before'].replace('0', '') == ''
if branch_addition:
self.add_branch(branch_to_update)
elif branch_deletion:
self.remove_branch(branch_to_update)
else:
self.update_branch(branch_to_update)
self.post_install(branch_to_update)
return
else:
logger.debug(("Repository '%s' is not our repository '%s'. "
"Ignoring.") % (data_repository, repository))
self.ok_response()
logger.info("Finished processing POST request.")
def add_branch(self, branch):
os.chdir(branch_dir)
branch_path = os.path.join(branch_dir, branch)
if os.path.isdir(branch_path):
return self.update_branch(branch_path)
run_command("git clone --depth 1 -o origin -b %s %s %s" %
(branch, repository, branch))
os.chmod(branch_path, 0770)
logger.info("Added directory '%s'" % branch_path)
def update_branch(self, branch):
branch_path = os.path.join(branch_dir, branch)
if not os.path.isdir(branch_path):
return self.add_branch(branch)
os.chdir(branch_path)
run_command("git checkout -f %s" % branch)
run_command("git clean -fdx")
run_command("git fetch origin %s" % branch)
run_command("git reset --hard FETCH_HEAD")
logger.info("Updated branch '%s'" % branch_path)
def remove_branch(self, branch):
branch_path = os.path.join(branch_dir, branch)
if not os.path.isdir(branch_path):
logger.warn("Directory to remove does not exist: %s" % branch_path)
return
try:
shutil.rmtree(branch_path)
except (OSError, IOError), e:
logger.exception("Error removing directory '%s'" % branch_path)
else:
logger.info("Removed directory '%s'" % branch_path)
def ok_response(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
def post_install(self, branch):
script = "%s/%s/postinstall" % (branch_dir, branch)
if os.path.isfile(script):
if os.access(script, os.X_OK):
logger.info("Running post-install script: %s" % script)
run_command(script)
else:
logger.error("Post-install script is not executable: %s" %
script)
def error_response(self):
self.log_error("Bad Request.")
self.send_response(400)
self.send_header("Content-type", "text/plain")
self.end_headers()
def run_command(command):
logger.debug("Running command: %s" % command)
process = subprocess.Popen(shlex.split(command.encode("ascii")),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
process.wait()
if process.returncode != 0:
logger.error("Command '%s' exited with return code %s: %s" %
(command, process.returncode, process.stdout.read()))
return ''
return process.stdout.read()
def get_arguments():
parser = argparse.ArgumentParser(description=(
'Deploy Gitlab branches in repository to a directory.'))
parser.add_argument('repository', help=(
'repository location. Example: [email protected]:repo'))
parser.add_argument('branch_dir', help=(
'directory to clone branches to. Example: /opt/repo'))
parser.add_argument('-p', '--port', default=8000, metavar='8000',
help='server address (host:port). host is optional.')
return parser.parse_args()
def main():
global repository
global branch_dir
args = get_arguments()
repository = args.repository
branch_dir = os.path.abspath(os.path.expanduser(args.branch_dir))
address = str(args.port)
if address.find(':') == -1:
host = '0.0.0.0'
port = int(address)
else:
host, port = address.split(":", 1)
port = int(port)
server = BaseHTTPServer.HTTPServer((host, port), RequestHandler)
logger.info("Starting HTTP Server at %s:%s." % (host, port))
try:
server.serve_forever()
except KeyboardInterrupt:
pass
logger.info("Stopping HTTP Server.")
server.server_close()
if __name__ == '__main__':
main()