-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
211 lines (187 loc) · 8.04 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
from flask import Flask, redirect, session, request, render_template, url_for, flash, jsonify, send_file, make_response
from helpers.s3 import S3
from helpers.constants import Constants
from helpers.githubuser import GithubUser, PublicGithubUser
from helpers.githubbot import GithubBot
from helpers.sources.osenv import OSConstants
from helpers.sources.mongo import MongoConstants
from helpers.extensions import LanguageExtensions
import os, time, datetime
app = Flask(__name__)
dev = os.environ.get('dev') == 'true' or not os.environ.get('PORT')
constants = Constants(OSConstants())
app.secret_key = constants.get('SK')
extensions = LanguageExtensions()
LANGS = dict(zip(constants.get('GH_REPOS').split(','), constants.get('LANGS').split(';')))
CURRENT = dict(zip(constants.get('GH_REPOS').split(','), constants.get('CURRENT').split(';')))
try:
s3 = S3(constants.get('AWS_ACCESS_KEY'), constants.get('AWS_SECRET_KEY'), constants.get('AWS_BUCKET'))
except:
s3 = None
collections = zip(constants.get('GH_REPOS').split(','), constants.get('STORAGE_COLLECTIONS').split(','))
storages = {}
for repo_name, collection_name in collections:
try:
storage = Constants(MongoConstants(collection_name, constants.get('MONGOLAB_URI')))
except:
storage = None
storages[repo_name] = storage
bots ={}
for repo_name in constants.get('GH_REPOS').split(','):
try:
bot = GithubBot(constants, repo_name, LANGS[repo_name], CURRENT[repo_name])
except:
bot = None
bots[repo_name] = bot
@app.before_request
def preprocess_request():
if request.endpoint in {'redirect_view', 'proxy_view', 'pending_view', 'leaderboard_view', 'user_leaderboard_view'}:
if request.view_args.get('object_key'):
session['object_key'] = request.view_args.get('object_key')
if session.get('verified') != True:
session['next'] = request.url
return redirect(url_for('login_view'))
if session.get('next'):
return redirect(session.pop('next'))
if not s3:
flash('Your S3 keys are invalid!', 'danger')
return 'Your S3 keys are invalid!'
@app.after_request
def postprocess_request(response):
if not dev:
response.headers.setdefault('Strict-Transport-Security', 'max-age=31536000')
return response
def cached(response_data, since, expires=86400):
response = make_response(response_data)
response.headers['Last-Modified'] = since
response.headers['Expires'] = since + datetime.timedelta(seconds=expires)
if expires > 0:
response.headers['Cache-Control'] = 'public, max-age={}'.format(expires)
else:
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
return response
@app.route('/redirect/<path:object_key>')
def redirect_view(object_key):
url = s3.get_url(object_key, constants.get('EXPIRES'), force_http=constants.get('HTTP') == 'true')
response = redirect(url if url else url_for('pending_view', object_key=object_key))
return cached(response, datetime.datetime.utcnow(), expires=0)
@app.route('/proxy/<path:object_key>')
def proxy_view(object_key):
f, since = s3.get_file(object_key)
return cached(send_file(f), since) if f else redirect(url_for('pending_view', object_key=object_key))
@app.route('/go/<path:object_key>')
def go_view(object_key):
url = url_for('{}_view'.format(constants.get('MODE')), object_key=object_key)
response = redirect('{}?{}'.format(url, request.query_string))
return cached(response, datetime.datetime.utcnow(), expires=0)
@app.route('/pending/<path:object_key>')
def pending_view(object_key):
response = render_template('pending.html', object_key=object_key, bucket=constants.get('AWS_BUCKET'), time=time.time())
return cached(response, datetime.datetime.utcnow(), expires=0)
@app.route('/login')
def login_view():
session['state'] = str(hash(str(time.time())+constants.get('SK')))
query = 'client_id={}&scope={}&state={}'.format(
constants.get('GH_CLIENT_ID'),
constants.get('GH_SCOPE'),
session['state'])
return redirect('https://github.com/login/oauth/authorize?{}'.format(query))
@app.route('/access_denied')
def no_auth_view():
org_verified = session.get('org_verified', False)
repo_verified = session.get('repo_verified', False)
return render_template('no_auth.html', org_verified=org_verified, repo_verified=repo_verified, org=constants.get('GH_ORG_NAME'), repos=[b.repo.name for b in bots.values()])
@app.route('/callback')
def callback_view():
if request.args.get('state') == session.get('state'):
code = request.args.get('code')
user = GithubUser(code=code, client_id=constants.get('GH_CLIENT_ID'), secret=constants.get('GH_SECRET'))
if user.is_valid():
session['token'] = user.token
org_verified = user.verify_org(constants.get('GH_ORG'))
repo_verified = any([user.verify_repo(str(b.repo.id)) for b in bots.values()])
if org_verified and repo_verified:
flash('You are now logged in!', 'success')
session['verified'] = True
else:
flash('You do not satisfy the authentication requirements!', 'danger')
session['verified'] = False
session['org_verified'] = org_verified
session['repo_verified'] = repo_verified
return redirect(url_for('no_auth_view'))
else:
flash('Your GitHub credentials are not valid!', 'danger')
session['verified'] = False
if session.get('object_key'):
object_key = session.pop('object_key')
return redirect(url_for('go_view', object_key=object_key))
if session.get('next'):
return redirect(session.pop('next'))
return redirect(url_for('demo_view'))
@app.route('/hook/<pull_request_id>/<path:object_key>')
def hook_view(pull_request_id, object_key):
repo_name = request.args.get('repo_name')
if not repo_name:
return jsonify({'status': 'no repo name'})
bot = bots.get(repo_name)
storage = storages.get(repo_name)
if bot:
if not pull_request_id.isdigit():
# pull_request_id is the branch name
if storage:
args = request.args.copy().to_dict()
commit = args.pop('commit_id')
if commit:
value = storage.get(pull_request_id, {})
value[commit] = args
value['current'] = args
storage.set(pull_request_id, value)
try:
pull_request_id = bot.get_pr_by_branch(pull_request_id).number
except:
return jsonify({'status': 'no such pull request'})
url = url_for('go_view', object_key=object_key, _external=True)
if bot.process_hook(int(pull_request_id), url, request.args, storage):
return jsonify({'status': 'success'})
else:
return jsonify({'status': 'restarting'})
return jsonify({'status': 'no bot credentials'})
@app.route('/')
def demo_view():
if session.get('token'):
return redirect(url_for('leaderboard_view'))
else:
return redirect(url_for('login_view'))
@app.route('/leaderboard')
def leaderboard_view():
leaderboards = {r:s.all({'value.contribution':{'$exists': True}}, ('value.net_contribution', -1)) for r,s in storages.iteritems()}
return render_template('leaderboard.html', \
org=constants.get('GH_ORG_NAME'), user=GithubUser(token=session.get('token')), all_langs=LANGS, \
leaderboards=leaderboards)
@app.route('/leaderboard/user/<login>')
def user_leaderboard_view(login):
leaderboards = {}
for repo, storage in storages.iteritems():
bot = bots.get(repo)
try:
recorded = storage.get(login).get('recorded')
all_pr = {x:bot.get_pr_by_number_or_id(x) for x in recorded}
leaderboards[repo] = {'recorded': recorded, 'all_pr': all_pr}
except AttributeError:
continue
user = PublicGithubUser(login)
return render_template('user_leaderboard.html', leaderboards=leaderboards, user=user, all_langs=LANGS, org=constants.get('GH_ORG_NAME'),)
@app.template_filter('min')
def min_filter(l):
return min(l)
@app.template_filter('sum')
def sum_filter(l):
return sum(l)
@app.template_filter('lang_nice')
def lang_nice_filter(s):
return extensions.get_language_from_extension(s)
if __name__ == '__main__':
if dev:
app.run(host='0.0.0.0', port=5000, debug=True)
else:
app.run(host='0.0.0.0', port=int(os.environ.get('PORT')), debug=False)