-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwebhook.py
227 lines (182 loc) · 6.84 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
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
# coding=utf8
"""
webhook.py - Sopel GitHub Module
Copyright 2015 Max Gurela
Copyright 2019 dgw
_______ __ __ __ __
| __|__| |_| |--.--.--.| |--.
| | | | _| | | || _ |
|_______|__|____|__|__|_____||_____|
________ __ __ __
| | | |.-----.| |--.| |--.-----.-----.| |--.-----.
| | | || -__|| _ || | _ | _ || <|__ --|
|________||_____||_____||__|__|_____|_____||__|__|_____|
"""
from __future__ import unicode_literals
from sopel import tools
from sopel.formatting import bold, color
from sopel.tools.time import get_timezone, format_time
from .formatting import get_formatted_response
from .formatting import fmt_repo
from .formatting import fmt_name
from threading import Thread
import bottle
import json
import requests
# Because I'm a horrible person
sopel_instance = None
def setup_webhook(sopel):
global sopel_instance
sopel_instance = sopel
host = sopel.config.github.webhook_host
port = sopel.config.github.webhook_port
base = StoppableWSGIRefServer(host=host, port=port)
server = Thread(target=bottle.run, kwargs={'server': base})
server.setDaemon(True)
server.start()
sopel.memory['gh_webhook_server'] = base
sopel.memory['gh_webhook_thread'] = server
conn = sopel.db.connect()
c = conn.cursor()
try:
c.execute('SELECT * FROM gh_hooks')
except Exception:
create_table(sopel, c)
conn.commit()
conn.close()
def create_table(bot, c):
primary_key = '(channel, repo_name)'
c.execute('''CREATE TABLE IF NOT EXISTS gh_hooks (
channel TEXT,
repo_name TEXT,
enabled BOOL DEFAULT 1,
url_color TINYINT DEFAULT 2,
tag_color TINYINT DEFAULT 6,
repo_color TINYINT DEFAULT 13,
name_color TINYINT DEFAULT 15,
hash_color TINYINT DEFAULT 14,
branch_color TINYINT DEFAULT 6,
PRIMARY KEY {0}
)'''.format(primary_key))
def shutdown_webhook(sopel):
global sopel_instance
sopel_instance = None
if 'gh_webhook_server' in sopel.memory:
print('Stopping webhook server')
sopel.memory['gh_webhook_server'].stop()
sopel.memory['gh_webhook_thread'].join()
print('GitHub webhook shutdown complete')
class StoppableWSGIRefServer(bottle.ServerAdapter):
server = None
def run(self, handler):
from wsgiref.simple_server import make_server, WSGIRequestHandler
if self.quiet:
class QuietHandler(WSGIRequestHandler):
def log_request(*args, **kw):
pass
self.options['handler_class'] = QuietHandler
self.server = make_server(self.host, self.port, handler, **self.options)
self.server.serve_forever()
def stop(self):
self.server.shutdown()
self.server.server_close()
def get_targets(repo):
conn = sopel_instance.db.connect()
c = conn.cursor()
c.execute('SELECT * FROM gh_hooks WHERE repo_name = ? AND enabled = 1', (repo.lower(), ))
return c.fetchall()
def process_payload(payload, targets):
if payload['event'] == 'ping':
for row in targets:
sopel_instance.say('[{}] {}: {} (Your webhook is now enabled)'.format(
fmt_repo(payload['repository']['name'], row),
fmt_name(payload['sender']['login'], row),
payload['zen']), row[0])
return
for row in targets:
messages = get_formatted_response(payload, row)
# Write the formatted message(s) to the channel
for message in messages:
sopel_instance.say(message, row[0])
@bottle.get("/webhook")
def show_hook_info():
return 'Listening for webhook connections!'
@bottle.post("/webhook")
def webhook():
try:
payload = bottle.request.json
except:
return bottle.abort(400, 'Something went wrong!')
payload['event'] = bottle.request.headers.get('X-GitHub-Event') or 'ping'
targets = get_targets(payload['repository']['full_name'])
# process hook payload in background
payload_handler = Thread(target=process_payload, args=(payload, targets))
payload_handler.start()
# send HTTP response ASAP, hopefully within GitHub's very short timeout
return '{"channels":' + json.dumps([target[0] for target in targets]) + '}'
@bottle.get('/auth')
def handle_auth_response():
code = bottle.request.query.code
state = bottle.request.query.state
repo = state.split(':')[0]
channel = state.split(':')[1]
data = {'client_id': sopel_instance.config.github.client_id,
'client_secret': sopel_instance.config.github.client_secret,
'code': code}
raw = requests.post('https://github.com/login/oauth/access_token', data=data, headers={'Accept': 'application/json'})
try:
res = json.loads(raw.text)
if 'error' in res:
raise ValueError('{err}: {desc}'.format(err=res['error'], desc=res['error_description']))
if 'scope' not in res:
raise ValueError('You\'ve already completed authorization on this repo')
if 'write:repo_hook' not in res['scope']:
raise ValueError('You didn\'t allow read/write on repo hooks!')
access_token = res['access_token']
data = {
"name": "web",
"active": True,
"events": ["*"],
"config": {
"url": sopel_instance.config.github.external_url,
"content_type": "json"
}
}
raw = requests.post('https://api.github.com/repos/{}/hooks?access_token={}'.format(repo, access_token), data=json.dumps(data))
res = json.loads(raw.text)
if 'ping_url' not in res:
if 'errors' in res:
raise ValueError(', '.join([error['message'] for error in res['errors']]))
else:
raise ValueError('Webhook creation failed, try again.')
raw = requests.get(res['ping_url'] + '?access_token={}'.format(access_token))
title = 'Done!'
header = 'Webhook setup complete!'
body = 'That was simple, right?! You should be seeing a completion message in {} any second now'.format(channel)
flair = 'There\'s no way it was that easy… things are never this easy…'
except Exception as e:
title = 'Error!'
header = 'Webhook setup failed!'
body = 'Please try using the link in {} again, something went wrong!'.format(channel)
flair = str(e)
page = '''
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<style>
body {{
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}}
</style>
</head>
<body>
<h1>{header}</h1>
<p>{body}</p>
<small><em>{flair}</em></small>
</body>
</html>
'''
return page.format(title=title, header=header, body=body, flair=flair)