-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhookit
executable file
·269 lines (219 loc) · 8.63 KB
/
hookit
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#! /usr/bin/env python
import os
import json
import argparse
import logging
import sys
import hashlib
import hmac
import subprocess
import shlex
try:
from urllib.parse import urlparse, urlencode, urljoin, urlsplit
from urllib.request import urlopen, Request
from urllib.error import HTTPError
from http.server import BaseHTTPRequestHandler, HTTPServer
except ImportError:
from urlparse import urlparse, urljoin, urlsplit
from urllib import urlencode
from urllib2 import urlopen, Request, HTTPError
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
GITHUB_API_URL = "https://api.github.com/"
class Hookit():
""" Generic auto hooker for GitHub """
def __init__(self):
info = retrieve_info({
'GITHUB_ACCESS_TOKEN': {
"description": 'GitHub Access Token' },
'REPOSITORY_NAME': {
"description": 'Repository name' },
'HOOK_SECRET': {
"description": 'Hook secret' },
'CALLBACK_URL': {
"description": 'Callback URL' },
'ON_PUSH_CALL': {
"description": 'On push command' }
})
self.create_hook(info)
self.start_server(info)
def create_hook(self, info):
url = info['CALLBACK_URL']
repository_name = info['REPOSITORY_NAME']
access_token = info['GITHUB_ACCESS_TOKEN']
hook_secret = info['HOOK_SECRET']
payload = {
"name": "web",
"active": True,
"events": [
"push"
],
"config": {
"url": url,
"content_type": "json",
"secret": hook_secret
}
}
hooks_url = GITHUB_API_URL + "repos/" + repository_name + "/hooks"
try:
response = post(hooks_url, payload, access_token)
hook_id = response['id']
logger.info("Webhook for %s created with id %s" %
(repository_name, hook_id, ))
except HTTPError as e:
error_response = json.loads(e.read())
if e.code == 422:
logger.warning(error_response['errors'][0]['message'])
else:
logger.error(error_response['message'])
raise
def start_server(self, info):
url_info = urlparse(info['CALLBACK_URL'])
port = url_info.port
if port is None:
port = 80 if url_info.scheme == 'http' else 443
host = url_info.hostname
server_address = ('0.0.0.0', port)
httpd = HTTPServer(server_address, self.gen_request_handler(info))
logger.info("Listening to POSTs at %s" % (info['CALLBACK_URL']))
httpd.serve_forever()
def gen_request_handler(self, info):
hookit = self
class S(BaseHTTPRequestHandler):
def _set_headers(self, status=200):
self.send_response(status)
self.end_headers()
def do_POST(self):
content_length = int(self.headers['Content-Length'])
client_address = self.client_address[0]
if 'X-Hub-Signature' not in self.headers:
logger.warning("POST without signature from %s" %
(client_address,))
return
received_signature = self.headers['X-Hub-Signature']
post_data = self.rfile.read(content_length)
sha1 = hmac.new(info['HOOK_SECRET'].encode('utf-8'),
post_data, hashlib.sha1)
signature = 'sha1=' + sha1.hexdigest()
signatures_match = hmac.compare_digest(received_signature,
signature)
if signatures_match:
self._set_headers(200)
logger.info("Valid POST from %s" % (client_address,))
hookit.on_push_call(info)
else:
self._set_headers(401)
logger.warning("POST with wrong signature from %s" %
(client_address,))
return S
def on_push_call(self, info):
subprocess.call(shlex.split(info['ON_PUSH_CALL']))
def get_repo_path(repo_name):
return repo_name.split('/')[-1]
def post(url, payload, token):
logging.debug("POST %s" % (url, ))
data = json.dumps(payload).encode('utf-8')
clen = len(data)
header = {
'Content-Type': 'application/json',
'Content-Length': clen,
'Authorization': 'token %s' % (token,)
}
req = Request(url, data, header)
f = urlopen(req)
response = f.read()
f.close()
return json.loads(response)
def retrieve_info(schema):
def parse_line(line):
sline = line.strip()
key, value = sline.split('=', 1)
skey = key.strip().upper()
svalue = value.strip()
return skey, svalue
def valid_line(line):
return not line.strip().startswith('#') and '=' in line
def load_from_file(filepath):
with open(filepath) as env:
loaded_vars = (parse_line(line) for line in env
if valid_line(line))
required_vars_dict = {key_value[0]: key_value[1]
for key_value in loaded_vars
if key_value[0] in schema}
for key in required_vars_dict:
logger.info("Loaded %s from %s" % (key, filepath))
return required_vars_dict
def load_from_env():
vars_dict = {key: os.environ[key] for key in schema
if key in os.environ}
for key in vars_dict:
logger.info("Loaded %s from environment variable." % (key,))
return vars_dict
def ask_user(variable):
return input('\033[44m' + variable['description'] + ':\033[0m ')
info_vars = {}
if os.path.exists('.env'):
info_vars = load_from_file('.env')
else:
info_vars = load_from_env()
for key in schema:
if key not in info_vars:
info_vars[key] = ask_user(schema[key])
return info_vars
class ColorfulFormater(logging.Formatter):
err_fmt = """\033[94m%(asctime)s\033[0m - \033[94m%(name)s\033[0m -\
\033[41m%(levelname)s\033[0m - \033[31m%(message)s\033[0m"""
war_fmt = """\033[94m%(asctime)s\033[0m - \033[94m%(name)s\033[0m -\
\033[45m%(levelname)s\033[0m - \033[31m%(message)s\033[0m"""
dbg_fmt = """\033[94m%(asctime)s\033[0m - \033[94m%(name)s\033[0m -\
\033[94m%(levelname)s\033[0m - \033[32m%(message)s\033[0m"""
info_fmt = """\033[94m%(asctime)s\033[0m - \033[94m%(name)s\033[0m -\
\033[94m%(levelname)s\033[0m - \033[32m%(message)s\033[0m"""
def __init__(self):
FORMAT = """\033[94m%(asctime)s\033[0m - \033[94m%(name)s\033[0m -\
\033[94m%(levelname)s\033[0m - \033[32m%(message)s\033[0m"""
logging.Formatter.__init__(self, FORMAT)
def format(self, record):
# Save the original format configured by the user
# when the logger formatter was instantiated
format_orig = None
has_style = hasattr(self, '_style')
if has_style:
format_orig = self._style._fmt
else:
format_orig = self._fmt
# Replace the original format with one customized by logging level
if record.levelno == logging.DEBUG:
if has_style:
self._style._fmt = ColorfulFormater.dbg_fmt
else:
self._fmt = ColorfulFormater.dbg_fmt
elif record.levelno == logging.INFO:
if has_style:
self._style._fmt = ColorfulFormater.info_fmt
else:
self._fmt = ColorfulFormater.info_fmt
elif record.levelno == logging.ERROR:
if has_style:
self._style._fmt = ColorfulFormater.err_fmt
else:
self._fmt = ColorfulFormater.err_fmt
elif record.levelno == logging.WARNING:
if has_style:
self._style._fmt = ColorfulFormater.war_fmt
else:
self._fmt = ColorfulFormater.war_fmt
# Call the original formatter class to do the grunt work
result = logging.Formatter.format(self, record)
# Restore the original format configured by the user
if has_style:
self._style._fmt = format_orig
else:
self._fmt = format_orig
return result
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(ColorfulFormater())
logging.root.addHandler(handler)
logging.root.setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
if __name__ == "__main__":
Hookit()