Ejemplo n.º 1
0
def massgit():
    git_ids = request.form.getlist('git')
    # pylint:disable-msg=E1101
    projects = Project.query.filter_by(
        login=g.user.login, is_github=True).all()

    pfn = {}
    for p in projects:
        if p.full_name not in git_ids:
            db.session.delete(p)
            flash(_(("Repository %(name)s successfully removed (but files"
                     " remain on the server)"), name=p.full_name))
        pfn[p.full_name] = p

    db.session.commit()
    for gid in git_ids:
        if not pfn.get(gid):
            project = Project(
                login=g.user.login,
                full_name=gid,
                # 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' % gid,
                is_github=True
            )
            db.session.add(project)
            db.session.commit()
            flash(_("Repository successfully added"))
            project.sync()

    db.session.commit()
    return redirect(url_for('settings.repos') + "#tab_massgithub")
Ejemplo n.º 2
0
def batch():
    # pylint:disable-msg=E1101
    urls = request.form.get('urls', '') + "\n"  # this makes m
    n = 0
    for l in urls.split("\n"):
        l = l.strip()
        if not l:
            continue
        if not parse(l).valid:
            flash(_("Url %(url)s isn't accepted, parse error", url=l))
        else:
            dup = Project.query.filter_by(login=g.user.login,
                                          is_github=False, clone=l).first()

            if dup:
                flash(_("Url %(url)s is duplicate", url=l))
            else:
                project = Project(login=g.user.login, clone=l, is_github=False)

                if project:
                    db.session.add(project)
                    db.session.commit()
                    db.session.refresh(project)

                project.sync()

                n = n + 1

    flash(_("%(num)s repositories successfuly added", num=n))

    return redirect(url_for('settings.repos'))
Ejemplo n.º 3
0
def addclone():
    # According url schemes here http://git-scm.com/docs/git-push it is
    # near impossibru to validate url, so just check if its length is > 10
    # (let it be 10)
    #
    # TODO in the future, use http://schacon.github.io/git/git-ls-remote.html to validate the URL string
    # http://stackoverflow.com/questions/9610131/how-to-check-the-validity-of-a-remote-git-repository-url
    #
    # TODO we could do very light validation that the form field is not 0 in JS
    # TODO we could do very light validation of the URL in JS

    clone = request.form.get('clone')
    if len(clone) == 0:
        flash(_('Enter a URL.'))
        return redirect(url_for('frontend.splash'))

    if not clone.lower().endswith('.git'):
        clone += '.git'  # quick fix to validate clone git url

    # pylint:disable-msg=E1101
    # user = ProjectCache.get_user_cache(g.user.login)

    dup = Project.query.filter_by(login=g.user.login,
                                  is_github=False, clone=clone).first()
    if dup:
        flash(_("This repository is a duplicate"))

    if not parse(clone).valid:
        flash(_("Problem parsing git url"))
        return redirect(url_for('settings.repos') + "#tab_massgithub")

    project = Project(login=g.user.login, clone=clone, is_github=False)

    # if (clone in map(lambda x: x['clone_url'], user.data)
    #         or clone in map(lambda x: x['git_url'], user.data)):
    #     pass

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

    flash(_("Repository successfully added"))
    project.sync()
    return redirect(url_for('frontend.splash'))
Ejemplo n.º 4
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))