-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.py
executable file
·451 lines (391 loc) · 15.3 KB
/
app.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env python
import json
import time
import os
import random
import warnings
from urllib.parse import parse_qs, urlparse, urlencode
from collections import defaultdict
import requests
import werkzeug
from requests.exceptions import ReadTimeout
from flask import (
Flask,
request,
make_response,
jsonify,
send_file,
abort,
redirect,
send_from_directory,
)
from flask.views import MethodView
from flask_sqlalchemy import SQLAlchemy
from decouple import config
import rollbar
DEBUG = config("DEBUG", default=False)
GITHUB_REQUEST_TIMEOUT = config("GITHUB_REQUEST_TIMEOUT", default=10)
GITHUB_REQUEST_HEADERS = {
"User-Agent": config(
"REQUESTS_USER_AGENT", default="whatsdeployed (https://whatsdeployed.io)"
)
}
GITHUB_AUTH_TOKEN = config("GITHUB_AUTH_TOKEN", default=None)
if GITHUB_AUTH_TOKEN:
GITHUB_REQUEST_HEADERS["Authorization"] = "token {}".format(GITHUB_AUTH_TOKEN)
else:
warnings.warn("GITHUB_AUTH_TOKEN is NOT available. Worry about rate limits.")
ROLLBAR_ACCESS_TOKEN = config("ROLLBAR_ACCESS_TOKEN", default=None)
if ROLLBAR_ACCESS_TOKEN:
rollbar.init(ROLLBAR_ACCESS_TOKEN)
rollbar.report_message("Rollbar is configured correctly")
print("Rollbar enabled.")
else:
print("Rollbar NOT enabled.")
# Set static_folder=None to suppress the standard static server
app = Flask(__name__, static_folder=None)
app.config["SQLALCHEMY_DATABASE_URI"] = config(
"SQLALCHEMY_DATABASE_URI", "postgres://localhost/whatsdeployed"
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = DEBUG
db = SQLAlchemy(app)
class Shortlink(db.Model):
id = db.Column(db.Integer, primary_key=True)
link = db.Column(db.String(80), unique=True)
owner = db.Column(db.String(200))
repo = db.Column(db.String(200))
revisions = db.Column(db.Text)
def __init__(self, link, owner, repo, revisions):
self.link = link
self.owner = owner
self.repo = repo
assert isinstance(revisions, list), type(revisions)
self.revisions = json.dumps(revisions)
def __repr__(self):
return "<Shortlink %r>" % self.link
def extract_sha(content):
content = content.strip()
if content.startswith("{") and content.endswith("}"):
# If it starts and ends with curly braces, it just might be a json
# object
try:
data = json.loads(content)
# This is where it's at in Dockerflow-supported sites
if "commit" in data:
return data["commit"]
except json.decoder.JSONDecodeError:
pass
if 7 <= len(content) <= 40:
return content
class ShasView(MethodView):
def post(self):
deployments = []
environment = request.json
base_url = "https://api.github.com/repos/{owner}/{repo}".format(
repo=environment["repo"], owner=environment["owner"]
)
tags_url = base_url + ("/tags?sort=created&direction=desc")
tags = {}
while True:
try:
r = requests.get(
tags_url,
headers=GITHUB_REQUEST_HEADERS,
timeout=GITHUB_REQUEST_TIMEOUT,
)
r.raise_for_status()
for tag in r.json():
tags[tag["commit"]["sha"]] = tag["name"]
try:
tags_url = r.links["next"]["url"]
except KeyError:
break
except ReadTimeout:
break
for each in environment["deployments"]:
name = each["name"]
url = each["url"]
# Skip empty urls
if not url:
continue
# Fetch the sha and balk if it doesn't exist
try:
response = self.fetch_content(url)
if response.status_code != 200:
return make_response(
jsonify(
{
"error": "{} trying to load {}".format(
response.status_code, url
)
}
)
)
except ReadTimeout:
return make_response(
jsonify({"error": "Timeout error trying to load {}".format(url)})
)
content = response.text.strip()
sha = extract_sha(content)
if not sha:
# doesn't appear to be a git sha
error = "Doesn't look like a sha\n (%s) on %s" % (content, each["url"])
return make_response(jsonify({"error": error}))
deployments.append({"name": name, "sha": sha, "bugs": [], "url": url})
response = make_response(jsonify({"deployments": deployments, "tags": tags}))
return response
def fetch_content(self, url):
if "?" in url:
url += "&"
else:
url += "?"
url += "cachescramble=%s" % time.time()
return requests.get(
url, headers=GITHUB_REQUEST_HEADERS, timeout=GITHUB_REQUEST_TIMEOUT
)
class CulpritsView(MethodView):
def post(self):
groups = []
deployments = request.json["deployments"]
base_url = "https://api.github.com/repos/{owner}/{repo}".format(
repo=request.json["repo"], owner=request.json["owner"]
)
pulls_url = base_url + ("/pulls?sort=created&state=closed&direction=desc")
_looked_up = []
for deployment in deployments:
name = deployment["name"]
sha = deployment["sha"]
if sha in _looked_up:
# If you have, for example Stage on the exact same
# sha as Prod, then there's no going looking it up
# twice.
continue
users = []
links = []
try:
r = requests.get(
pulls_url,
headers=GITHUB_REQUEST_HEADERS,
timeout=GITHUB_REQUEST_TIMEOUT,
)
r.raise_for_status()
except ReadTimeout:
return make_response(
jsonify(
{"error": "Timeout error trying to load {}".format(pulls_url)}
)
)
for pr in r.json():
if pr["merge_commit_sha"] == sha:
links.append(pr["_links"]["html"]["href"])
author = pr["user"]
users.append(("Author", author))
committer = pr.get("committer")
if committer and committer != author:
users.append(("Committer", committer))
# let's also dig into what other people participated
for assignee in pr["assignees"]:
users.append(("Assignee", assignee))
# Other people who commented on the PR
issues_url = base_url + (
"/issues/{number}/comments".format(number=pr["number"])
)
r = requests.get(
issues_url,
headers=GITHUB_REQUEST_HEADERS,
timeout=GITHUB_REQUEST_TIMEOUT,
)
r.raise_for_status()
for comment in r.json():
try:
user = comment["user"]
users.append(("Commenter", user))
except TypeError:
print("COMMENT")
print(comment)
break
commits_url = base_url + ("/commits/{sha}".format(sha=sha))
r = requests.get(
commits_url,
headers=GITHUB_REQUEST_HEADERS,
timeout=GITHUB_REQUEST_TIMEOUT,
)
r.raise_for_status()
commit = r.json()
author = commit["author"]
if ("Author", author) not in users:
users.append(("Author", author))
committer = commit.get("committer")
if committer:
if committer["login"] == "web-flow":
# Then the author pressed the green button and let
# GitHub merge it.
# Change the label of the author to also be the committer
users.append(("Committer", author))
elif committer != author:
users.append(("Committer", committer))
# Now merge the labels for user
labels = defaultdict(list)
for label, user in users:
if label not in labels[user["login"]]:
labels[user["login"]].append(label)
labels_map = {}
for login, labels in labels.items():
labels_map[login] = " & ".join(labels)
unique_users = []
_logins = set()
for _, user in users:
if user["login"] not in _logins:
_logins.add(user["login"])
unique_users.append((labels_map[user["login"]], user))
groups.append({"name": name, "users": unique_users, "links": links})
_looked_up.append(sha)
response = make_response(jsonify({"culprits": groups}))
return response
class ShortenView(MethodView):
def post(self):
url = request.json["url"]
parsed = parse_qs(urlparse(url).query)
owner = parsed["owner"][0]
repo = parsed["repo"][0]
revisions = []
for i, name in enumerate(parsed["name[]"]):
revisions.append((name, parsed["url[]"][i]))
# Does it already exist??
shortlink = Shortlink.query.filter_by(
owner=owner, repo=repo, revisions=json.dumps(revisions)
).first()
if shortlink is not None:
link = shortlink.link
else:
def new_random_link(length):
pool = "abcdefghijklmnopqrstuvwxyz0123456789"
pool += pool.upper()
new = ""
while len(new) < length:
new += random.choice(list(pool))
return new
link = new_random_link(3)
while Shortlink.query.filter_by(link=link).count():
link = new_random_link(3)
shortlink = Shortlink(link, owner, repo, revisions)
db.session.add(shortlink)
db.session.commit()
new_url = "/s-{}".format(link)
return make_response(jsonify({"url": new_url}))
class LengthenView(MethodView):
def get(self, link):
shortlink = Shortlink.query.filter_by(link=link).first()
if shortlink is None:
abort(404)
response = {"repo": shortlink.repo, "owner": shortlink.owner, "deployments": []}
for k, v in json.loads(shortlink.revisions):
response["deployments"].append({"name": k, "url": v})
return make_response(jsonify(response))
class ShortenedView(MethodView):
def get(self):
urls = request.args.get("urls")
if not urls:
abort(400)
ids = [x.replace("/s-", "") for x in urls.split(",") if x.startswith("/s-")]
environments = []
shortlinks = Shortlink.query.filter(Shortlink.link.in_(ids)).all()
for shortlink in shortlinks:
environments.append(
{
"shortlink": shortlink.link,
"owner": shortlink.owner,
"repo": shortlink.repo,
"revisions": json.loads(shortlink.revisions),
}
)
return make_response(jsonify({"environments": environments}))
class ShortlinkRedirectView(MethodView):
def get(self, link):
shortlink = Shortlink.query.filter_by(link=link).first()
if shortlink is None:
abort(404)
qs = {
"repo": shortlink.repo,
"owner": shortlink.owner,
"name[]": [],
"url[]": [],
}
for k, v in json.loads(shortlink.revisions):
qs["name[]"].append(k)
qs["url[]"].append(v)
return redirect("/?" + urlencode(qs, True))
class GitHubAPI(MethodView):
"""The client needs to make queries to the GitHub API but a shortcoming
is that it's impossible to include an auth token. And if you can't
do that clients are likely to hit rate limits."""
def get(self, thing):
url = "https://api.github.com"
if thing == "commits":
copied = dict(request.args)
owner = request.args.get("owner")
repo = request.args.get("repo")
if not owner:
abort(400, "No 'owner'")
if not repo:
abort(400, "No 'repo'")
url += "/repos/{}/{}/commits".format(owner, repo)
copied.pop("owner")
copied.pop("repo")
if copied:
url += "?" + urlencode(copied, True)
response = requests.get(
url, headers=GITHUB_REQUEST_HEADERS, timeout=GITHUB_REQUEST_TIMEOUT
)
if response.status_code == 200:
return make_response(jsonify(response.json()))
else:
abort(response.status_code, response.content)
else:
abort(400)
class HealthCheckView(MethodView):
"""A dumb but comprehensive health check"""
def get(self):
# Can't really test anything because it might empty on first ever load.
Shortlink.query.count()
# Commented out for now because it's doing this healthcheck too often.
# # If you attempt to include Authorization headers, even on an endpoint
# # that doesn't need it, it will 401 if the auth token is wrong.
# response = requests.get(
# "https://api.github.com/",
# headers=GITHUB_REQUEST_HEADERS,
# timeout=GITHUB_REQUEST_TIMEOUT,
# )
# response.raise_for_status()
return "OK\n"
app.add_url_rule("/shas", view_func=ShasView.as_view("shas"))
app.add_url_rule("/culprits", view_func=CulpritsView.as_view("culprits"))
app.add_url_rule("/shortenit", view_func=ShortenView.as_view("shortenit"))
app.add_url_rule(
"/lengthenit/<string:link>", view_func=LengthenView.as_view("lengthenit")
)
app.add_url_rule("/shortened", view_func=ShortenedView.as_view("shortened"))
app.add_url_rule("/githubapi/<string:thing>", view_func=GitHubAPI.as_view("githubapi"))
app.add_url_rule(
"/s-<string:link>", view_func=ShortlinkRedirectView.as_view("shortlink")
)
app.add_url_rule("/__healthcheck__", view_func=HealthCheckView.as_view("healthcheck"))
@app.route("/", defaults={"path": "index.html"})
@app.route("/<path:path>")
def index_html(path):
# try to serve static files out of ./build/
try:
return send_from_directory("build", path)
except werkzeug.exceptions.NotFound:
# try to serve index.html in the requested path
if path.endswith("/"):
return send_from_directory("build", path + "index.html")
else:
# fall back to index.html
return send_file("build/index.html")
if __name__ == "__main__":
db.create_all()
app.debug = DEBUG
port = int(os.environ.get("PORT", 5000))
host = os.environ.get("HOST", "0.0.0.0")
app.run(host=host, port=port)