forked from pstauffer/gitlab-webhook-receiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab-webhook-receiver.py
executable file
·124 lines (102 loc) · 3.97 KB
/
gitlab-webhook-receiver.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
""" Gitlab Webhook Receiver """
# Based on: https://github.com/schickling/docker-hook
import json
import yaml
import subprocess
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
try:
# For Python 3.0 and later
from http.server import HTTPServer
from http.server import BaseHTTPRequestHandler
except ImportError:
# Fall back to Python 2
from BaseHTTPServer import BaseHTTPRequestHandler
from BaseHTTPServer import HTTPServer as HTTPServer
import sys
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG,
stream=sys.stdout)
class RequestHandler(BaseHTTPRequestHandler):
"""A POST request handler."""
def do_POST(self):
logging.info("Hook received")
# get payload
header_length = int(self.headers.getheader('content-length', "0"))
json_payload = self.rfile.read(header_length)
json_params = {}
if len(json_payload) > 0:
json_params = json.loads(json_payload)
# get gitlab secret token
gitlab_token_header = self.headers.getheader('X-Gitlab-Token')
# get project homepage
project = json_params['project']['homepage']
try:
# get command and token from config file
command = config[project]['command']
gitlab_token = config[project]['gitlab_token']
logging.info("Load project '%s' and run command '%s'", project, command)
except KeyError as err:
self.send_response(500, "KeyError")
if err == project:
logging.error("Project '%s' not found in %s", project, args.cfg)
elif err == 'command':
logging.error("Key 'command' not found in %s", args.cfg)
elif err == 'gitlab_token':
logging.error("Key 'gitlab_token' not found in %s", args.cfg)
self.end_headers()
return
# Check if the gitlab token is valid
if gitlab_token_header == gitlab_token:
logging.info("Start executing '%s'" % command)
try:
# run command in background
subprocess.Popen(command)
self.send_response(200, "OK")
except OSError as err:
self.send_response(500, "OSError")
logging.error("Command could not run successfully.")
logging.error(err)
else:
logging.error("Not authorized, Gitlab_Token not authorized")
self.send_response(401, "Gitlab Token not authorized")
self.end_headers()
def get_parser():
"""Get a command line parser."""
parser = ArgumentParser(description=__doc__,
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("--addr",
dest="addr",
default="0.0.0.0",
help="address where it listens")
parser.add_argument("--port",
dest="port",
type=int,
default=8666,
metavar="PORT",
help="port where it listens")
parser.add_argument("--cfg",
dest="cfg",
default="config.yaml",
help="path to the config file")
return parser
def main(addr, port):
"""Start a HTTPServer which waits for requests."""
httpd = HTTPServer((addr, port), RequestHandler)
httpd.serve_forever()
if __name__ == '__main__':
parser = get_parser()
if len(sys.argv) == 0:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
# load config file
try:
with open(args.cfg, 'r') as stream:
config = yaml.load(stream)
except IOError as err:
logging.error("Config file %s could not be loaded", args.cfg)
sys.exit(1)
main(args.addr, args.port)