This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwebhooks.py
More file actions
343 lines (276 loc) · 12.2 KB
/
webhooks.py
File metadata and controls
343 lines (276 loc) · 12.2 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# This file is a part of github2fedmsg, a pubsubhubbub to zeromq bridge.
# Copyright (C) 2014, Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pyramid.view import view_config
from pyramid.security import authenticated_userid
from pyramid.httpexceptions import HTTPFound, HTTPUnauthorized, HTTPForbidden
import github2fedmsg.models as m
import hashlib
import hmac
import json
import requests
import logging
from fedora_messaging.api import Message, publish
from fedora_messaging.exceptions import PublishReturned, ConnectionException
LOGGER = logging.getLogger(__name__)
# http://developer.github.com/v3/repos/hooks/
github_pubsubhubub_api_url = "https://api.github.com/hub"
# All events: http://developer.github.com/webhooks/#events
github_events = [
# "push", # Any git push to a Repository
# "commit_comment", # Any time a Commit is commented on.
# "create", # Any time a Repository, Branch, or Tag is created.
# "delete", # Any time a Branch or Tag is deleted.
# "release", # Any time a Release is published in the Repository.
# "issues", # Any time an Issue is opened or closed.
# "issue_comment", # Any time an Issue is commented on.
# "pull_request", # Any time a Pull Request is opened, closed, or
# # synchronized (updated due to a new push in the
# # branch that the pull request is tracking).
# "pull_request_review_comment", # Any time a Commit is commented on while
# # inside a Pull Request review (the Files
# # Changed tab).
# "gollum", # Any time a Wiki page is updated.
# "watch", # Any time a User watches the Repository.
# "download", # ?
# "fork", # Any time a Repository is forked.
# "fork_apply", # ?
# "member", # Any time a User is added as a collaborator to a
# # non-Organization Repository.
# "public", # Any time a Repository changes from private to public
# "status", # Any time a Repository has a status update from
# # the API.
# "team_add", # Any time a team is added or modified on a
# # Repository.
# "deployment", # Any time a Repository has a new deployment created
# # from the API.
# "deployment_status",# Any time a deployment for the Repository has a
# #status update from the API.
# "page_build", # Any time a Pages site is built or results in a
# # failed build.
"*", # Any time any event is triggered (Wildcard Event).
]
@view_config(route_name='webhook', request_method="POST", renderer='string')
def webhook(request):
""" Handle github webhook. """
github_secret = request.registry.settings.get("github.secret")
hex = hmac.new(github_secret, request.body, hashlib.sha1).hexdigest()
valid_sig = "sha1=%s" % hex
if 'X-Hub-Signature' not in request.headers:
msg = "No X-Hub-Signature provided"
raise HTTPUnauthorized(msg)
actual_sig = request.headers['X-Hub-Signature']
if actual_sig != valid_sig:
msg = "Invalid X-Hub-Signature"
raise HTTPForbidden(msg)
if 'payload' in request.params:
# pubsubhubbub
payload = request.params['payload']
payload = json.loads(payload)
else:
# whatever webhooks
payload = request.json_body
event_type = request.headers['X-Github-Event'].lower()
# github sends us a 'ping' when we first subscribe to let us know that
# it worked.
if event_type == 'ping':
event_type = 'webhook'
# They don't actually tell us which repo is signed up, so, for
# presentation purposes only, we'll rip out the repo name here and
# build a nice human-clickable url.
tokens = payload['hook']['url'].split('/')
owner, repo = tokens[-4], tokens[-3]
payload['compare'] = 'https://github.com/%s/%s' % (owner, repo)
# Convert github.watch messages to github.star messages.
# See http://da.gd/sUNkj
if event_type == 'watch':
event_type = 'star'
# Turn just 'issues' into 'issue.reopened'
if event_type == 'issues':
event_type = 'issue.' + payload['action']
# Same here
if event_type == 'pull_request':
event_type = 'pull_request.' + payload['action']
# Make issues comments match our scheme more nicely
if event_type == 'issue_comment':
event_type = 'issue.comment'
# Strip out a bunch of redundant information that github sends us
payload = prune_useless_urls(payload)
# Build a little table of github usernames to fas usernames so
# consumers can have an easy time.
fas_usernames = build_fas_lookup(payload)
payload['fas_usernames'] = fas_usernames
try:
msg = Message(
topic="github.{}".format(event_type),
body=payload,
)
publish(msg)
except PublishReturned as e:
LOGGER.warning(
"Fedora Messaging broker rejected message %s: %s",
msg.id, e
)
except ConnectionException as e:
LOGGER.warning("Error sending message %s: %s", msg.id, e)
return "OK"
def prune_useless_urls(payload):
""" Given *any* github message, strip out unneeded information. """
for k, v in payload.items():
if k == '_links':
payload['html_url'] = v['html']['href']
del payload[k]
elif isinstance(v, dict):
payload[k] = prune_useless_urls(v)
elif k.endswith('_url') and k not in ['html_url', 'target_url']:
del payload[k]
return payload
def build_fas_lookup(payload):
""" Given *any* github message, build a lookup of github usernames to fas
usernames for it.
This involves hand coding a bunch of knowledge about github message
formats.
"""
usernames = set()
# Trawl through every possible corner we can to find github usernames
if 'commits' in payload:
for commit in payload['commits']:
if 'committer' in commit:
if 'username' in commit['committer']:
usernames.add(commit['committer']['username'])
elif 'name' in commit['committer']:
usernames.add(commit['committer']['name'])
if 'author' in commit:
if 'username' in commit['author']:
usernames.add(commit['author']['username'])
elif 'name' in commit['author']:
usernames.add(commit['author']['name'])
if 'pusher' in payload:
usernames.add(payload['pusher']['name'])
if 'sender' in payload:
if 'login' in payload['sender']:
usernames.add(payload['sender']['login'])
if 'forkee' in payload:
if 'login' in payload['forkee']['owner']:
usernames.add(payload['forkee']['owner']['login'])
if 'repository' in payload:
if 'login' in payload['repository']['owner']:
usernames.add(payload['repository']['owner']['login'])
# Take all that, and roll it up into a dict mapping those to FAS
mapping = {}
for github_username in usernames:
if not github_username:
continue
user = m.User.query.filter_by(github_username=github_username).first()
if user:
mapping[github_username] = user.username
return mapping
@view_config(name='toggle', context=m.Repo, renderer='json')
def repo_toggle_enabled(request):
# TODO -- someday, learn how to do the __acls__ thing.. :/
userid = authenticated_userid(request)
if userid != request.context.user.username:
if userid not in [
member.username for member in request.context.user.users
]:
raise HTTPUnauthorized()
repo = request.context
# Toggle that attribute on our db model.
repo.enabled = not repo.enabled
# Now notify github
token = request.user.oauth_access_token
if not token:
raise HTTPForbidden("you need to link your account with github first")
# We used to do this with pubsubhubbub, but not all of github's oauth
# scopes work there.
# toggle_pubsubhubbub_hooks(request, repo, token)
toggle_webhook_directly(request, repo, token)
response = {
'status': 'ok',
'repo': request.context.__json__(),
'username': repo.user.username,
'github_username': repo.user.github_username,
'enabled': repo.enabled,
}
return response
def toggle_pubsubhubbub_hooks(request, repo, token):
data = {
"access_token": token,
"hub.mode": ['unsubscribe', 'subscribe'][repo.enabled],
"hub.callback": request.registry.settings.get("github.callback"),
"hub.secret": request.registry.settings.get("github.secret"),
}
for event in github_events:
data["hub.topic"] = "https://github.com/%s/%s/events/%s" % (
repo.user.github_username, repo.name, event)
# Subscribe to events via pubsubhubbub
result = requests.post(github_pubsubhubbub_api_url, data=data)
if result.status_code < 200 or result.status_code > 299:
d = result.json()
d['status_code'] = result.status_code
raise IOError(d)
def toggle_webhook_directly(request, repo, token):
actually_enabled = _get_webhook_status_directly(request, repo, token)
# Here, repo.enabled represents our database knowledge of the hook state.
# "actually_enabled" represents github's own record keeping.
if repo.enabled and not actually_enabled:
_enable_webhook_directly(request, repo, token)
elif repo.enabled and actually_enabled:
pass # nothing to do
elif not repo.enabled and actually_enabled:
_disable_webhook_directly(request, repo, actually_enabled, token)
elif not repo.enabled and not actually_enabled:
pass # nothing to do
def _get_webhook_status_directly(request, repo, token):
auth = {"access_token": token}
url = "https://api.github.com/repos/%s/%s/hooks" % (
repo.user.github_username, repo.name)
result = requests.get(url, params=auth)
if result.status_code < 200 or result.status_code > 299:
d = result.json()
d['status_code'] = result.status_code
raise IOError(d)
callback = request.registry.settings.get("github.callback")
hooks = result.json()
for hook in hooks:
if hook['name'] == 'web' and hook['config'].get('url') == callback:
return hook
return None
def _enable_webhook_directly(request, repo, token):
auth = {"access_token": token}
data = {
"name": "web",
"active": True,
"events": github_events,
"config": {
"url": request.registry.settings.get("github.callback"),
"content_type": "json",
"secret": request.registry.settings.get("github.secret"),
},
}
headers = {'content-type': 'application/json'}
url = "https://api.github.com/repos/%s/%s/hooks" % (
repo.user.github_username, repo.name)
result = requests.post(
url, params=auth, data=json.dumps(data), headers=headers)
if result.status_code < 200 or result.status_code > 299:
d = result.json()
d['status_code'] = result.status_code
raise IOError(d)
def _disable_webhook_directly(request, repo, hook, token):
auth = {"access_token": token}
url = "https://api.github.com/repos/%s/%s/hooks/%i" % (
repo.user.github_username, repo.name, hook['id'])
requests.delete(url, params=auth)