-
Notifications
You must be signed in to change notification settings - Fork 0
/
www.py
executable file
·310 lines (233 loc) · 9.15 KB
/
www.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
#!/usr/bin/env python
import json
import re
import sys
import threading
import time
import urlparse
import decorator
import fiesta
import flask
from paste.exceptions.errormiddleware import ErrorMiddleware
import daemon
import db
import github
import errors
import settings
import sign
import werkzeug_monkeypatch
fiesta_api = fiesta.FiestaAPI(settings.fiesta_id,
settings.fiesta_secret,
"gitlists.com")
app = flask.Flask(__name__)
app.secret_key = settings.session_key
app.config["PROPAGATE_EXCEPTIONS"] = True
if settings.env == "prod":
app.wsgi_app = ErrorMiddleware(app.wsgi_app, debug=False,
error_email=settings.noc_address,
from_address="noreply@gitlists.com",
error_subject_prefix="[Gitlists] Error ",
smtp_server=settings.relay_host)
# GH API rate is 60 calls per minute, but we run this from two
# processes, so each gets 30 per minute.
SLEEP_INTERVAL = 2.1
class SendInvites(threading.Thread):
'''
We send invites from a separate thread to try to avoid going over
GH's rate limit (60 calls per minute for V2 API).
'''
def run(self):
while True:
invite = db.next_invite()
if not invite:
time.sleep(SLEEP_INTERVAL)
continue
repo_name = invite["repo_name"]
github_url = invite["github_url"]
inviter = invite["inviter"]
username = invite["username"]
group = fiesta.FiestaGroup.from_id(fiesta_api, invite["group_id"])
welcome_message = {"subject": "Invitation to %s@gitlists.com" % repo_name,
"markdown": """
[%s](%s) invited you to a [Gitlist](https://gitlists.com) for [%s](%s). Gitlists are dead-simple mailing lists for GitHub projects.
[Click here]($invite_url) to join the list. If you don't want to join, just ignore this message.
Have a great day!
""" % (inviter, "http://github.com/" + inviter, repo_name, github_url)}
user, cache = github.user_info(username)
if user.get("email", None):
group.add_member(user["email"],
display_name=user.get("name", ""),
welcome_message=welcome_message,
send_invite=True)
if not cache:
time.sleep(SLEEP_INTERVAL)
# Invite thread
send_invites = SendInvites()
send_invites.daemon = True
send_invites.start()
def gen_xsrf(actions):
xsrf = {}
for action in actions:
xsrf[action] = sign.sign(action + flask.session["g"])
return {"xsrf": xsrf}
def until(s, x):
s, _, _ = s.partition(x)
return s
def check_referrer():
referrer = flask.request.headers.get("REFERER")
if not referrer:
return flask.abort(403, "No referrer")
referrer = until(urlparse.urlsplit(referrer)[1], ":")
if referrer != until(flask.request.headers.get("HOST"), ":"):
return flask.abort(403, "Bad referrer.")
return None
def check_xsrf(action, timeout=60*60):
def check_xsrf(view, *args, **kwargs):
res = check_referrer()
if res:
return res
msg = action + flask.session["g"]
status = sign.check_sig(msg, flask.request.form.get("x"), timeout)
if status == sign.BAD:
return flask.abort(403, "Bad XSRF token.")
elif status == sign.TIMEOUT:
flask.flash("Your session timed out, please try again.")
redirect = flask.request.headers.get("REFERER", "/")
return flask.redirect(redirect)
return view(*args, **kwargs)
return decorator.decorator(check_xsrf)
@app.route("/rate_limited")
def rate_limited():
return flask.render_template("rate_limited.html")
@app.route("/")
@github.reauthorize
@github.rate_limit
def index():
if "g" in flask.session:
user = github.current_user()
flask.session["e"] = user["email"]
orgs = github.orgs()
org_repos = [github.repos(org=org["login"]) for org in orgs]
return flask.render_template("index_logged_in.html",
user=user,
repos=github.repos(),
orgs=orgs, org_repos=org_repos)
return flask.render_template("index.html", auth_url=github.auth_url())
def repo_data(name, org_name=None):
for r in github.repos(org_name):
if r["name"] == name:
return r
return None
def repo_page(name, org=None):
user = github.current_user()
repo = repo_data(name, org and org["login"])
if not repo:
return flask.abort(404, "No matching repo")
existing_own = list(db.existing_own(name, user["login"]))
existing_not_own = list(db.existing_not_own(name, user["login"]))
return flask.render_template("repo.html", repo=repo, org=org,
existing_own=existing_own,
existing_not_own=existing_not_own,
**gen_xsrf(["create"]))
@app.route("/repo/<name>")
@github.rate_limit
@github.authorized
def repo(name):
if "g" not in flask.session:
return flask.abort(403, "No user")
return repo_page(name)
@app.route("/repo/<org_handle>/<name>")
@github.rate_limit
@github.authorized
def org_repo(org_handle, name):
if "g" not in flask.session:
return flask.abort(403, "No user")
for org in github.orgs():
if org["login"] == org_handle:
return repo_page(name, org)
return flask.abort(404, "No matching org")
def repo_create(name, org=None):
user = github.current_user()
repo = repo_data(name, org and org["login"])
if not repo:
return flask.abort(404, "No matching repo")
username = org and org["login"] or user["login"]
to_invite = set()
to_invite.update(github.collaborators(username, name))
to_invite.update(github.contributors(username, name))
if org:
to_invite.update(github.members(org["login"]))
to_invite.update(github.forkers(username, name))
to_invite.update(github.watchers(username, name))
to_invite -= set([user["login"], "invalid-email-address"])
description = repo["description"]
group = fiesta_api.create_group(default_name=repo["name"],
description=description)
# Gitlists are public, archived and have the repo name as a subject-prefix
group.add_application("public", group_name=repo["name"])
group.add_application("subject_prefix", prefix=repo["name"])
group.add_application("archive")
github_url = "https://github.com/%s/%s" % (username, repo["name"])
welcome_message = {"subject": "Welcome to %s@gitlists.com" % repo["name"],
"markdown": """
Your [Gitlist](https://gitlists.com) for [%s](%s) has been created.
The [list page](https://fiesta.cc/list/%s) is the archive *and* where new members will need to go to join the list, so you might want to add it to your repo's README.
If you have any questions, send us an email at support@corp.fiesta.cc.
Have a great day!
""" % (repo["name"], github_url, group.id)}
group.add_member(user["email"],
display_name=user.get("name", ""),
welcome_message=welcome_message)
for username in to_invite:
db.pending_invite(repo["name"], github_url, user["login"],
username, group.id)
db.new_list(repo["name"], user["login"], group.id)
flask.flash("Your Gitlist has been created - check your email at '%s'." % user["email"])
return flask.redirect("/")
@app.route("/repo/<name>", methods=["POST"])
@github.rate_limit
@github.authorized
@check_xsrf("create")
def create_repo(name):
if "g" not in flask.session:
return flask.abort(403, "No user")
return repo_create(name)
@app.route("/repo/<org_handle>/<name>", methods=["POST"])
@github.rate_limit
@github.authorized
@check_xsrf("create")
def create_org_repo(org_handle, name):
if "g" not in flask.session:
return flask.abort(403, "No user")
for org in github.orgs():
if org["login"] == org_handle:
return repo_create(name, org)
return flask.abort(404, "No matching org")
@app.route("/auth/github")
def auth_github():
return github.finish_auth()
@app.route("/favicon.ico")
def favicon():
return app.send_static_file("i/favicon.ico")
@app.route("/error", methods=["GET", "POST"])
def get_error():
"""
Force an exception so we can test our error handling.
"""
raise Exception("error")
class GitlistsDaemon(daemon.Daemon):
def __init__(self, port, *args, **kwargs):
self.port = port
return daemon.Daemon.__init__(self, *args, **kwargs)
def run(self):
db.create_indexes()
app.run(host=settings.host, port=self.port)
if __name__ == '__main__':
port = 7176
if len(sys.argv) > 2:
port = int(sys.argv[2])
if settings.env == "prod":
daemon.go(GitlistsDaemon(port, "/tmp/gitlists-%s.pid" % port))
else:
db.create_indexes()
app.run(host=settings.host, port=port, debug=True)