Пример #1
0
def sync():
    if 'github_token' in session:
        user_repos = github.get('user/repos')
        user_repos = _get_repos(user_repos)

        orgs = github.get('user/orgs')
        for x in json.loads(orgs.raw_data):
            org_repos = github.get('orgs/{0}/repos'.format(x['login']))
            org_repos = _get_repos(org_repos)
            user_repos = dict(user_repos, **org_repos)

        return jsonify(user_repos)
    return redirect(url_for('login'))
Пример #2
0
def sync():
    if 'github_token' in session:
        repos_count = 0
        token = session.get('github_token')[0]
        user = User.query.filter_by(github_access_token=token).first()
        # get repos of currently logged in user
        user_repos = github.get('user/repos')
        user_data = json.loads(user_repos.raw_data)
        repos_count += len(user_data)
        for x in user_data:
            kwargs = _get_data(user, x)
            r = Repo(**kwargs)
            db.session.add(r)
            user.repos.append(r)
            db.session.commit()

        # get orgs the user is part of
        orgs = github.get('user/orgs')
        for x in json.loads(orgs.raw_data):
            # get repos from each org
            org_repos = github.get('orgs/{0}/repos'.format(x['login']))
            org_data = json.loads(org_repos.raw_data)
            repos_count += len(org_data)
            for x in org_data:
                kwargs = _get_data(user, x)
                r = Repo(**kwargs)
                db.session.add(r)
                user.repos.append(r)
                db.session.commit()
            # check if there's more results
            # if 'Link' in org_repos._resp.headers:
            #     next_link = parse_header_next_link(org_repos._resp.headers['Link'])
            #     if next_link:
            #         org_repos = github.get(next_link)

        msg = '{} repos added to the queue.'
        flash(msg.format(repos_count), 'info')
        return redirect(url_for('repos'))
    return redirect(url_for('login'))
Пример #3
0
def sync():
    if 'github_token' in session:
        repos_count = 0
        token = session.get('github_token')[0]
        user = User.query.filter_by(github_access_token=token).first()
        # get repos of currently logged in user
        user_repos = github.get('user/repos')
        user_data = json.loads(user_repos.raw_data)
        repos_count += len(user_data)
        for x in user_data:
            kwargs = _get_data(user, x)
            r = Repo(**kwargs)
            db.session.add(r)
            user.repos.append(r)
            db.session.commit()

        # get orgs the user is part of
        orgs = github.get('user/orgs')
        for x in json.loads(orgs.raw_data):
            # get repos from each org
            org_repos = github.get('orgs/{0}/repos'.format(x['login']))
            org_data = json.loads(org_repos.raw_data)
            repos_count += len(org_data)
            for x in org_data:
                kwargs = _get_data(user, x)
                r = Repo(**kwargs)
                db.session.add(r)
                user.repos.append(r)
                db.session.commit()
            # check if there's more results
            # if 'Link' in org_repos._resp.headers:
            #     next_link = parse_header_next_link(org_repos._resp.headers['Link'])
            #     if next_link:
            #         org_repos = github.get(next_link)

        msg = '{} repos added to the queue.'
        flash(msg.format(repos_count), 'info')
        return redirect(url_for('repos'))
    return redirect(url_for('login'))
Пример #4
0
def authorized(resp):
    if resp is None:
        flash('Access denied: reason=%s error=%s' % (
            request.args['error_reason'],
            request.args['error_description']), 'error')

    if 'access_token' in resp:
        token = (resp['access_token'], '')
        session['github_token'] = token

        user = User.query.filter_by(github_access_token=token[0]).first()
        if user is None:
            username = github.get('user').data.get('login', 'there')
            user = User(username, token[0])
            db.session.add(user)
            db.session.commit()

        session['user_id'] = user.id
        session['user_name'] = user.username

        return redirect(url_for('sync'))
    return flash(str(resp), 'error')
Пример #5
0
def authorized(resp):
    if resp is None:
        flash(
            'Access denied: reason=%s error=%s' %
            (request.args['error_reason'], request.args['error_description']),
            'error')

    if 'access_token' in resp:
        token = (resp['access_token'], '')
        session['github_token'] = token

        user = User.query.filter_by(github_access_token=token[0]).first()
        if user is None:
            username = github.get('user').data.get('login', 'there')
            user = User(username, token[0])
            db.session.add(user)
            db.session.commit()

        session['user_id'] = user.id
        session['user_name'] = user.username

        return redirect(url_for('sync'))
    return flash(str(resp), 'error')
Пример #6
0
def process_requirements(repo, github_token):
    with app.test_request_context():
        # TODO: hacky, needs good comment
        session['github_token'] = github_token

        token = github_token[0]
        user = User.query.filter_by(github_access_token=token).first()
        # TODO: log this event
        if not user:
            return False

        requirement_file = '/repos/{0}/{1}/contents/requirements.txt'
        content = github.get(requirement_file.format(repo.owner, repo.name))

        # not found so good bye
        if content._resp.getcode() == 404:
            r = Repo.query.get(repo.id)
            r.log = 'requirements.txt not found.'
            db.session.commit()
            return False

        content = content.data['content']
        content = base64.b64decode(content)

        reqs = parse(content)
        for x in reqs:
            kwargs = {
                'repo_id': repo.id,
                'name': x.project_name,
                'version': x.specs[0][1],
                'version_pypi': '',
            }
            p = Package(**kwargs)
            db.session.add(p)
            db.session.commit()
        return True