예제 #1
0
def delhook(full_name):
    auth = github.get_session(token=g.user.token)

    old_hooks = auth.get('/repos/%s/hooks' % full_name)
    if old_hooks.status_code != 200:
        logging.error('Repos API reading error for user %s' % g.user.login)
        flash(_('GitHub API access error, please try again later'))
        return redirect(url_for('settings.repos') + "#tab_github")

    exist_id = False
    if old_hooks.json():
        for i in old_hooks.json():
            if i.has_key('name') and i['name'] == 'web':
                if i.has_key('config') and i['config'].has_key('url') \
                        and i['config']['url'] == current_app.config.get('HOOK_URL').format(id=full_name):
                    exist_id = i['id']

    if exist_id:
        resp = auth.delete('/repos/%(full_name)s/hooks/%(id)s' %
                           {'full_name': full_name, 'id': exist_id})
        if resp.status_code != 204:
            flash(_('Error deleting old webhook: Delete it manually, or retry'))
    else:
        flash(_("Webhook is not registered on Github, it was probably deleted manually"))

    # pylint:disable-msg=E1101
    project = Project.query.filter_by(
        login=g.user.login, full_name=full_name).first()

    if project:
        db.session.delete(project)
        db.session.commit()

    return redirect(url_for('settings.repos') + "#tab_github")
예제 #2
0
def update():
    """Update the list of the user's githib repos"""
    auth = github.get_session(token=g.user.token)
    if g.user is not None:
        if g.user.login != u'offline':
            page = 0
            _repos = []
            while True:
                resp = auth.get('/user/repos?per_page=100&page=%s' % page,
                                data={'type': 'all'})
                if resp.status_code == 200:
                    _repos.extend(resp.json())
                    if len(resp.json()) == 100:
                        page = page + 1
                        continue
                else:
                    flash(_('Could not connect to Github to load repos list.'))
                break
            orgs = auth.get('/user/orgs')
            if orgs.status_code == 200:
                for x in orgs.json():
                    opage = 0
                    while True:
                        oresp = auth.get(
                            '/orgs/%s/repos?per_page=100&page=%s' %
                            (x['login'], opage),
                            data={'type': 'all'})
                        if oresp.status_code == 200:
                            _repos.extend(oresp.json())
                            if len(oresp.json()) == 100:
                                opage = opage + 1
                                continue
                        else:
                            flash(
                                _('Error get repos for organization %(login)s',
                                  login=x['login']))
                        break

            if _repos:
                cache = ProjectCache.query.filter_by(
                    login=g.user.login).first()
                if not cache:
                    cache = ProjectCache()
                    cache.login = g.user.login
                cache.data = _repos
                # note the time
                cache.updated = datetime.datetime.utcnow()
                # add the cache to the database
                db.session.add(cache)
                db.session.commit()
                flash(_('Repositories refreshed.'))
        else:
            flash(_('Offline user has no Github account.'))
    else:
        flash(_('No user found.'))
    return redirect(url_for('settings.repos') + "#tab_massgithub")
예제 #3
0
def profile():
    auth = github.get_session(token=g.user.token)
    _repos = None
    if g.user is not None:
        resp = auth.get('/user/repos', data={'type': 'public'})
        if resp.status_code == 200:
            _repos = resp.json()
        else:
            flash(_('Unable to load repos list.'))
    return render_template('settings/index.html', repos=_repos)
예제 #4
0
def profile():
    auth = github.get_session(token=g.user.token)
    _repos = None
    if g.user is not None:
        resp = auth.get('/user/repos', data={'type': 'public'})
        if resp.status_code == 200:
            _repos = resp.json()
        else:
            flash(_('Unable to load repos list.'))
    return render_template('settings/index.html', repos=_repos)
예제 #5
0
def update():
    """Update the list of the user's githib repos"""
    auth = github.get_session(token=g.user.token)
    if g.user is not None:
        if g.user.login != u'offline':
            page = 0
            _repos = []
            while True:
                resp = auth.get('/user/repos?per_page=100&page=%s' % page, data={'type': 'all'})
                if resp.status_code == 200:
                    _repos.extend(resp.json())
                    if len(resp.json()) == 100:
                        page = page + 1
                        continue
                else:
                    flash(_('Could not connect to Github to load repos list.'))
                break
            orgs = auth.get('/user/orgs')
            if orgs.status_code == 200:
                for x in orgs.json():
                    opage = 0
                    while True:
                        oresp = auth.get('/orgs/%s/repos?per_page=100&page=%s' % (x['login'], opage), data={'type': 'all'})
                        if oresp.status_code == 200:
                            _repos.extend(oresp.json())
                            if len(oresp.json()) == 100:
                                opage = opage + 1
                                continue
                        else:
                            flash(_('Error get repos for organization %(login)s', login=x['login']))
                        break

            if _repos:
                cache = ProjectCache.query.filter_by(login=g.user.login).first()
                if not cache:
                    cache = ProjectCache()
                    cache.login = g.user.login
                cache.data = _repos
                # note the time
                cache.updated = datetime.datetime.utcnow()
                # add the cache to the database
                db.session.add(cache)
                db.session.commit()
                flash(_('Repositories refreshed.'))
        else:
            flash(_('Offline user has no Github account.'))
    else:
        flash(_('No user found.'))
    return redirect(url_for('settings.repos') + "#tab_massgithub")
예제 #6
0
def delhook(full_name):
    auth = github.get_session(token=g.user.token)

    old_hooks = auth.get('/repos/%s/hooks' % full_name)
    if old_hooks.status_code != 200:
        logging.error('Repos API reading error for user %s' % g.user.login)
        flash(_('GitHub API access error, please try again later'))
        return redirect(url_for('settings.repos') + "#tab_github")

    exist_id = False
    if old_hooks.json():
        for i in old_hooks.json():
            if i.has_key('name') and i['name'] == 'web':
                if i.has_key('config') and i['config'].has_key('url') \
                        and i['config']['url'] == current_app.config.get('HOOK_URL').format(id=full_name):
                    exist_id = i['id']

    if exist_id:
        resp = auth.delete('/repos/%(full_name)s/hooks/%(id)s' % {
            'full_name': full_name,
            'id': exist_id
        })
        if resp.status_code != 204:
            flash(
                _('Error deleting old webhook: Delete it manually, or retry'))
    else:
        flash(
            _("Webhook is not registered on Github, it was probably deleted manually"
              ))

    # pylint:disable-msg=E1101
    project = Project.query.filter_by(login=g.user.login,
                                      full_name=full_name).first()

    if project:
        db.session.delete(project)
        db.session.commit()

    return redirect(url_for('settings.repos') + "#tab_github")
예제 #7
0
def addhook(full_name):
    auth = github.get_session(token=g.user.token)
    old_hooks = auth.get('/repos/%s/hooks' % full_name)
    if old_hooks.status_code != 200:
        logging.error('Repos API reading error for user %s' % g.user.login)
        flash(_('GitHub API access error, please try again later'))
        return redirect(url_for('settings.repos') + "#tab_github")

    exist_id = False
    if old_hooks.json():
        for i in old_hooks.json():
            if 'name' in i and i['name'] == 'web':
                if 'config' in i and 'url' in i['config'] \
                        and i['config']['url'] == current_app.config.get('HOOK_URL').format(id=full_name):
                    exist_id = i['id']

    if exist_id:
        logging.warn('Delete old webhook for user %s, repo %s and id %s' %
                     (g.user.login, full_name, exist_id))
        resp = auth.delete('/repos/%(full_name)s/hooks/%(id)s' %
                           {'full_name': full_name, 'id': exist_id})
        if resp.status_code != 204:
            flash(_('Error deleting old webhook, delete if manually or retry'))
            return redirect(url_for('settings.repos') + "#tab_github")

    resp = auth.post('/repos/%(full_name)s/hooks' % {'full_name': full_name},
                     data=json.dumps({
                                     'name': 'web',
                                     'active': True,
                                     'events': ['push'],
                                     'config': {
                                         'url': current_app.config.get('HOOK_URL').format(id=full_name),
                                         'content_type': 'json',
                                         'secret': signify(full_name)  # TODO: sign from name and SECRET.
                                     }
                                     })
                     )
    if resp.status_code < 300:  # no errors, in 2xx range
        project = Project.query.filter_by(
            login=g.user.login, full_name=full_name).first()
        if not project:
            project = Project(
                login=g.user.login,
                full_name=full_name,
                # TODO: Use actual github clone string used by Github
                # clone='[email protected]:%s/%s.git' % (g.user.login, full_name),
                clone='git://github.com/%s.git' % full_name,
                is_github=True
            )
        project_data = auth.get('/repos/%s' % full_name)
        if project_data.status_code == 200:
            project.cache_update(data=project_data.json())
        else:
            flash(_('Repository information update error'))
            return redirect(url_for('settings.repos') + "#tab_github")
        project.is_github = True
        db.session.add(project)
        db.session.commit()
    else:
        logging.error('Web hook registration error for %s' % full_name)
        flash(_('Repository webhook update error'))
        return redirect(url_for('settings.repos') + "#tab_github")

    flash(_('Added webhook for %(name)s.', name=full_name))
    project.sync()
    return redirect(url_for('project.queue', username=project.login, project_id=project.id))
예제 #8
0
def addhook(full_name):
    auth = github.get_session(token=g.user.token)
    old_hooks = auth.get('/repos/%s/hooks' % full_name)
    if old_hooks.status_code != 200:
        logging.error('Repos API reading error for user %s' % g.user.login)
        flash(_('GitHub API access error, please try again later'))
        return redirect(url_for('settings.repos') + "#tab_github")

    exist_id = False
    if old_hooks.json():
        for i in old_hooks.json():
            if 'name' in i and i['name'] == 'web':
                if 'config' in i and 'url' in i['config'] \
                        and i['config']['url'] == current_app.config.get('HOOK_URL').format(id=full_name):
                    exist_id = i['id']

    if exist_id:
        logging.warn('Delete old webhook for user %s, repo %s and id %s' %
                     (g.user.login, full_name, exist_id))
        resp = auth.delete('/repos/%(full_name)s/hooks/%(id)s' % {
            'full_name': full_name,
            'id': exist_id
        })
        if resp.status_code != 204:
            flash(_('Error deleting old webhook, delete if manually or retry'))
            return redirect(url_for('settings.repos') + "#tab_github")

    resp = auth.post(
        '/repos/%(full_name)s/hooks' % {'full_name': full_name},
        data=json.dumps({
            'name': 'web',
            'active': True,
            'events': ['push'],
            'config': {
                'url': current_app.config.get('HOOK_URL').format(id=full_name),
                'content_type': 'json',
                'secret':
                signify(full_name)  # TODO: sign from name and SECRET.
            }
        }))
    if resp.status_code < 300:  # no errors, in 2xx range
        project = Project.query.filter_by(login=g.user.login,
                                          full_name=full_name).first()
        if not project:
            project = Project(
                login=g.user.login,
                full_name=full_name,
                # TODO: Use actual github clone string used by Github
                # clone='[email protected]:%s/%s.git' % (g.user.login, full_name),
                clone='git://github.com/%s.git' % full_name,
                is_github=True)
        project_data = auth.get('/repos/%s' % full_name)
        if project_data.status_code == 200:
            project.cache_update(data=project_data.json())
        else:
            flash(_('Repository information update error'))
            return redirect(url_for('settings.repos') + "#tab_github")
        project.is_github = True
        db.session.add(project)
        db.session.commit()
    else:
        logging.error('Web hook registration error for %s' % full_name)
        flash(_('Repository webhook update error'))
        return redirect(url_for('settings.repos') + "#tab_github")

    flash(_('Added webhook for %(name)s.', name=full_name))
    project.sync()
    return redirect(url_for('settings.repos') + "#tab_github")