示例#1
0
文件: view.py 项目: thruflo/thruflo
 def blobs(self):
     if not hasattr(self, '_blobs'):
         document = self.document
         github = clients.github_factory(user=self.current_user)
         blobs = document.get_blobs(github)
         self._blobs = blobs
     return self._blobs
示例#2
0
文件: model.py 项目: thruflo/thruflo
 def get_github_repositories(self, save=True):
     """Makes two requests, to ``repos/show/:user`` and to
       ``repos/pushable`` (in parallel).
     """
     
     github = clients.github_factory(access_token=self.access_token)
     
     api_calls = [
         gevent.spawn(self._get_owned_repos, github),
         gevent.spawn(self._get_pushable_repos, github)
     ]
     
     gevent.joinall(api_calls)
     
     owned = self._owned.get('repositories')
     pushable = self._pushable.get('repositories')
     combined = owned + pushable
     
     repositories = []
     for item in combined:
         repositories.append(
             '%s/%s' % (
                 item['owner'], 
                 item['name']
             )
         )
     
     if save:
         self.repositories = repositories
         self.save()
     
     return repositories
示例#3
0
文件: view.py 项目: thruflo/thruflo
 def repository(self):
     """
     """
     
     if not hasattr(self, '_repository'):
         repository = model.Repository.get_or_create_from(
             self.owner, 
             self.name
         )
         github = clients.github_factory(user=self.current_user)
         repository.update_all(github)
         self._repository = repository
     return self._repository
示例#4
0
文件: view.py 项目: thruflo/thruflo
 def get(self, owner, name):
     """Takes a list of Blob ids via a ``blobs`` param and returns
       a dictionary of {blobid: data, ...}
     """
     
     raise NotImplementedError('@@ get blob content in greenlets')
     
     self.owner = owner
     self.name = name
     
     data = {}
     blobs = []
     
     blob_ids = self.get_arguments('keys[]')
     
     logging.debug('blob_ids')
     logging.debug(blob_ids)
     
     blob_ids = list(set(blob_ids))
     
     # validate
     for item in blob_ids:
         if not schema.valid_blob_id.match(item):
             blob_ids.remove(item)
     
     # fetch
     if blob_ids:
         blobs = model.Blob.view(
             '_all_docs',
             keys=blob_ids,
             include_docs=True
         ).all()
     
     # restrict access
     for item in blobs:
         if not item.repo == self.repository.path:
             blobs.remove(item)
         
     # get data through parallel api calls
     if blobs:
         batch = []
         github = clients.github_factory(user=self.current_user)
         for item in blobs:
             gevent.spawn(self._get_blob_data, item, github)
         gevent.joinall(batch)
         for item in batch:
             data[item.value[0]] = item.value[1]
         
     return data
示例#5
0
文件: view.py 项目: thruflo/thruflo
 def post(self):
     """We get a payload like this::
       
           {
             "after": "0df48053b961ad6f26956ea4bbcbe27c97b7a21b", 
             "before": "ad9aeac5f80097660619349f5dd93fb07a2b2eb0", 
             "commits": [
               {
                 "added": [], 
                 "author": {
                   "email": "*****@*****.**", 
                   "name": "James Arthur"
                 }, 
                 "id": "0df48053b961ad6f26956ea4bbcbe27c97b7a21b", 
                 "message": "blah", 
                 "modified": [
                   "README.md"
                 ], 
                 "removed": [], 
                 "timestamp": "2010-05-18T04:13:47-07:00", 
                 "url": "http:\/\/github.com\/thruflo\/Dummy-Bus-Dev\/commit\/0df48053b961ad6f26956ea4bbcbe27c97b7a21b"
               }
             ], 
             "ref": "refs\/heads\/master", 
             "repository": {
               "description": "Dummy user account to test github API", 
               "fork": false, 
               "forks": 0, 
               "has_downloads": false, 
               "has_issues": false, 
               "has_wiki": false, 
               "homepage": "http:\/\/dummy.thruflo.com", 
               "name": "Dummy-Bus-Dev", 
               "open_issues": 0, 
               "owner": {
                 "email": "*****@*****.**", 
                 "name": "thruflo"
               }, 
               "private": false, 
               "url": "http:\/\/github.com\/thruflo\/Dummy-Bus-Dev", 
               "watchers": 1
             }
           }
       
       We validate it by checking the commit ids against either
       the latest or the handled commits for that branch.
     """
     
     logging.debug('PostCommitHook')
     
     # get the payload
     payload = self.get_argument('payload')
     logging.debug('payload')
     logging.debug(payload)
     data = utils.json_decode(payload)
     
     logging.debug('data')
     logging.debug(data)
     
     # get a handle on the repository
     repo = data['repository']
     branch = data["ref"].split('/')[-1]
     path = '%s/%s' % (repo['owner']['name'], repo['name'])
     docid = model.Repository.get_id_from(path)
     
     # is this actually a repo we have stored?
     repository = model.Repository.get(docid)
     if repository is None:
         return ''
     
     logging.debug('repository')
     logging.debug(repository)
     
     # to validate, either the before matches the latest
     # sanity checked commit, or the latest handled commit
     latest = repository.latest_sanity_checked_commits[branch]
     handled = repository.handled_commits[branch]
     if data['before'] == latest:
         logging.debug('before matched latest commit')
     elif data['before'] in handled:
         logging.debug('before matched one a handled commit')
     else:
         return ''
     
     logging.debug('trying to fetch an authenticated user')
     
     users = repository.users
     users.reverse()
     for user in users:
         github = clients.github_factory(user=user)
         try:
             github.users.make_request('show')
         except RuntimeError:
             github = None
             logging.warning('@@ cache that this user failed to authenticate')
         else:
             break
     
     logging.debug('github')
     logging.debug(github)
     
     if github is not None:
         
         logging.debug('handling commits')
         
         relevant_commits = repository.handle_commits(
             github,
             branch,
             data['commits'],
             before=data['before']
         )
         
         logging.debug('relevant commits')
         logging.debug(relevant_commits)
         
         if relevant_commits:
             # save the repo
             repository.save()
             # augment the data with a list of invalid blob ids
             invalid_blobs = []
             for item in relevant_commits:
                 for k in 'removed', 'modified':
                     for filepath in item.get(k):
                         blob_id = model.Blob.get_id_from(
                             repository.path, 
                             branch, 
                             filepath
                         )
                         invalid_blobs.append(blob_id)
             invalid_blobs = list(set(invalid_blobs))
             # notify any live users
             redis = Redis(namespace=repository.path)
             k = data['after']
             v = {
                 'branch': branch,
                 'invalid_blobs': invalid_blobs,
                 'commits': relevant_commits
             }
             redis[k] = utils.json_encode(v)
             for user in repository.users:
                 redis('rpush', user.id, k)
         
     return ''
示例#6
0
文件: view.py 项目: thruflo/thruflo
 def get(self):
     logging.debug('/oauth/callback')
     code = self.get_argument('code')
     redirect_url = self.request.path_url
     # user logs in
     data = clients.oauth.access_token(code, redirect_url)
     access_token = data.get('access_token')
     logging.debug('access_token: %s' % access_token)
     # get data including `user_data['id']`
     github = clients.github_factory(access_token=access_token)
     user_data = github.request.make_request('user/show').get('user')
     user_id = str(user_data['id'])
     logging.debug('user_id: %s' % user_id)
     # we `get_or_create_and_subscribe_to_free_plan` the subscriber by `id`
     sub = clients.spreedly.get_or_create_subscriber(
         user_data['id'], 
         user_data['login']
     )
     logging.debug('sub')
     logging.debug(sub)
     # they're not subscribed to a plan sign them up the the free trial
     if sub['name'] == '' and sub['trial_elegible']:
         logging.debug('signing user up for a free trial')
         sub = clients.spreedly.subscribe(
             user_data['id'],
             config.spreedly['free_trial_plan_id'],
             trial=True
         )
     # `get_or_create` their `model.User` instance by `id`
     sub_level = 'none'
     if sub['trial_active']:
         sub_level = 'trial'
     elif sub['feature_level']:
         sub_level = sub['feature_level']
     logging.debug('sub_level: %s' % sub_level)
     params = {
         'name': user_data['name'],
         'login': user_data['login'],
         'email': user_data['email'],
         'location': user_data['location'],
         'gravatar_id': user_data['gravatar_id'],
         'sub_level': sub_level,
         'sub_expires': sub['active_until'],
         'access_token': access_token
     }
     user = model.User.get_or_create(docid=user_id, **params)
     # making sure the data we have is uptodate
     changed = False
     for k, v in params.iteritems():
         if v != getattr(user, k):
             changed = True
             setattr(user, k, v)
     if changed:
         logging.debug('user changed')
         user.save()
     # `set_secure_cookie`, either expiring before their next payment
     # or not setting the expires if that's already passed
     if user.sub_expires < datetime.utcnow():
         expires = None
     else:
         expires = sub['active_until']
     self.set_secure_cookie(
         'user_id', user_id,
         domain='.%s' % self.settings['domain'],
         expires_days=None,
         expires=expires
     )
     logging.debug('user authenticated, redirecting...')
     # redirect to a page which will validate their subscription status
     return self.redirect(
         self.get_argument('next', '/dashboard')
     )
示例#7
0
文件: view.py 项目: thruflo/thruflo
                 }
             }
         else: 
             blob = model.Blob.get_or_create_from(
                 params['repo'],
                 params['branch'],
                 params['path']
             )
             try:
                 blobs = self.document.blobs
                 blobs.insert(params['index'], blob.id)
                 self.document.blobs = blobs
             except IndexError:
                 return {'status': 400, 'errors': {'index': 'Out of range'}}
             else:
                 github = clients.github_factory(user=self.current_user)
                 data = blob.get_data(github)
                 self.document.save()
                 return {
                     'status': 200, 
                     'id': blob.id, 
                     'data': data
                 }
             
         
     
 
 
 @restricted
 def post(self, owner, name, slug):
     self.owner = owner