def get(self): token_url = 'https://github.com/login/oauth/access_token' # get code/state from Github OAuth data = {k: self.get_argument(k) for k in self.request.arguments} code = data['code'] state = '' if 'state' in data: state = data['state'] parameters = { 'client_id': _SETTINGS.get('client_id'), 'client_secret': _SETTINGS.get('client_secret'), 'code': code, 'state': state } body = urllib.urlencode(parameters) # get access_token from Github http_client = HTTPClient() response = http_client.fetch(token_url, method='POST', body=body) ret_token = response.body token_obj = dict(urlparse.parse_qsl(ret_token)) # set cookie for storing access token self.set_secure_cookie('github_token', token_obj['access_token']) # get user login gh = GithubInfo(token_obj['access_token']) user_login = gh.get_user().get_login() # User login self.set_secure_cookie('user', user_login) # if no repoUrl attribute, back to home page repo_url = self.get_secure_cookie('repoUrl') if not repo_url: self.redirect(_HOME_PAGE) repo_fullname = urlparse.urlparse(repo_url).path[1:] # Create New Task for generating test cases taskid = int(time.time()) gh = GithubInfo(self.get_secure_cookie('github_token')) try: project_repo_summary = gh.get_repo_info_summary_with_snapshot( repo_fullname) TaskWorker(project_repo_summary, taskid).start() except UnknownObjectException as e: logger.error(e) error_msg = 'Get error from {0} repo: {1}'.format( repo_fullname, e.data) error = {'error_msg': error_msg} url = '{}?{}'.format(_HOME_PAGE, urllib.urlencode(error)) self.redirect(url) return # if has state, redirect to that page with taskid if state: parameters = {'taskid': taskid} url = '{}?{}'.format(state, urllib.urlencode(parameters)) self.redirect(url)
def get_repo_info(self): # if not login, redirect to home page if not self.current_user: self.redirect(_HOME_PAGE) return data = {k: self.get_argument(k) for k in self.request.arguments} if self.check_repoinfo(data): repo_url = data.get('repoUrl') repo_fullname = urlparse.urlparse(repo_url).path[1:] # get cookie for storing access token gh = GithubInfo(self.get_secure_cookie('github_token')) result = gh.get_repo_info_summary(repo_fullname) self.write(json_encode(result)) else: raise tornado.web.HTTPError(400)
def get(self): if not self.current_user: self.redirect(_HOME_PAGE) return # get cookie for storing access token gh = GithubInfo(self.get_secure_cookie('github_token')) user = tornado.escape.xhtml_escape(self.current_user) name = gh.get_user().get_name() email = gh.get_user().get_email() location = gh.get_user().get_location() self.render(_STATIC_HTML + 'test.html', user=user, name=name, email=email, location=location)