-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzenchart.py
executable file
·191 lines (169 loc) · 6.83 KB
/
zenchart.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
#!/usr/bin/python
import os, random, gc
import webapp2, jinja2, logging
import urllib, cgi, json, urllib2, urlparse
from google.appengine.ext import ndb, deferred
from google.appengine.api import taskqueue
from webapp2_extras import sessions
import github, zenhub
from github import Github
from model import Auth, Repo
from config import config
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__) + '/html'),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
self.session_store = sessions.get_store(request=self.request)
try:
webapp2.RequestHandler.dispatch(self)
finally:
self.session_store.save_sessions(self.response)
gc.collect()
def user(self):
"""Return the current user data or redirect to /login"""
user = None
if not 'user' in self.session and os.environ['APPLICATION_ID'].startswith('dev'):
if self.request.get('paToken'):
user = Github({'paToken': self.request.get('paToken')}).user()
if user:
logging.info("Read user data %s" % json.dumps(user))
user['paToken'] = self.request.get('paToken')
self.session['user'] = user
return user
# No user for now
return None
if 'user' in self.session:
return self.session['user']
logging.info('No user detected; redirecting to /login')
self.redirect('/login?%s' % urllib.urlencode({'r': self.request.path}), abort=True)
@webapp2.cached_property
def session(self):
return self.session_store.get_session(backend='memcache')
class MainPage(BaseHandler):
def get(self):
user = None
if 'user' in self.session:
user = self.session['user']
# List repositories that we know about
if user:
repos = Repo.repos(user)
else:
repos = []
self.response.write(JINJA_ENVIRONMENT.get_template('index.html').render({'user': user, 'repos': repos}))
class RepoAddPage(BaseHandler):
def get(self):
user = self.user()
repoNames = map(lambda r: r.name, Repo.repos(user))
gitRepos = filter(lambda r: r['full_name'] not in repoNames, Github(user).repos())
self.response.write(JINJA_ENVIRONMENT.get_template('repoAdd.html').render({
'user': user,
'repos': gitRepos}))
def post(self):
user = self.user()
repo = Repo.create(user,
name=self.request.get('repo'),
auth=Auth(zenhubToken = self.request.get('zenhubToken')))
repo.data = Github(user).repo(repo.name)
logging.info("Adding repo %s" % repo)
repo.put()
# Initiate full sync
deferred.defer(github.syncIssues, user, repo.key, True)
self.redirect(repo.url())
class RepoPage(BaseHandler):
def get(self, id):
user = self.user()
repo = Repo.get(user, id)
# Charting here?
if self.request.get('s'):
logging.info("Deferring github repo issue sync")
deferred.defer(github.syncIssues, user, repo.key)
self.redirect(repo.url())
elif self.request.get('f'):
deferred.defer(github.syncIssues, user, repo.key, True)
self.redirect(repo.url())
else:
issues = sorted(repo.issues(), key=lambda x: x.number, reverse=True)
if not repo:
self.response.set_status(404)
else:
page = {'user': user, 'repo': repo, 'issues': issues}
self.response.write(JINJA_ENVIRONMENT.get_template('repo.html').render(page))
class RepoDeletePage(BaseHandler):
def post(self, id):
repo = Repo.get(self.user(), id)
if repo:
repo.delete()
self.redirect('/')
else:
self.response.set_status(404)
class IssuePage(BaseHandler):
def get(self, id, number):
user = self.user()
repo = Repo.get(user, id)
if not repo:
self.response.set_status(404)
else:
issue = repo.issue(number)
if not issue:
self.response.set_status(404)
else:
full = { 'github': issue.github, 'zenhub': issue.zenhub }
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(full, indent=2))
class LoginPage(BaseHandler):
def get(self):
self.session['return'] = self.request.get('r')
if self.request.get('paToken'):
user = session.user()
self.redirect('/')
else:
# Github login (see https://developer.github.com/v3/oauth/)
state = '%030x' % random.randrange(16**30)
url = 'https://github.com/login/oauth/authorize?%s' % urllib.urlencode({
'client_id': config['github']['id'],
'scope': 'repo',
'state': state})
self.session['state'] = state
logging.info('Redirecting to %s to get user info' % url)
self.redirect(url)
class LogoutPage(BaseHandler):
def get(self):
self.session.clear()
self.redirect('/')
class GithubAuthPage(BaseHandler):
def get(self):
code = self.request.GET['code']
state = self.request.GET['state']
if state != self.session['state']:
self.response.set_status(403)
else:
request = urllib2.Request('https://github.com/login/oauth/access_token', urllib.urlencode({
'client_id': config['github']['id'],
'client_secret': config['github']['secret'],
'code': code
}))
data = urlparse.parse_qs(urllib2.urlopen(request).read())
logging.info('result is %s' % data)
aToken = data['access_token'][0]
user = Github({'aToken': aToken}).user()
user['aToken'] = aToken
self.session['user'] = user
if 'return' in self.session and self.session['return']:
returnTo = self.session['return']
logging.info("Login complete for user %s, returning to %s" % (json.dumps(user), returnTo))
del self.session['return']
else:
returnTo = '/'
self.redirect(returnTo)
app = webapp2.WSGIApplication([
('/', MainPage),
('/repo/add', RepoAddPage),
('/login', LoginPage),
('/logout', LogoutPage),
('/auth/github', GithubAuthPage),
webapp2.Route(r'/repo/<id:\d+>', RepoPage),
webapp2.Route(r'/repo/<id:\d+>/delete', RepoDeletePage),
webapp2.Route(r'/repo/<id:\d+>/issue/<number:\d+>', IssuePage),
],debug=True,config=config)