Exemplo n.º 1
0
def new_hook(u, p, sid):
    if p.owner.id != g.user.id:
        # Project isn't public and the viewer isn't the project owner.
        # (403 Forbidden)
        return abort(403)

    hook = HookService.services.get(sid)
    form = hook.form()
    if form:
        form = form()

    if form and hook.validate(form, request):
        h = Hook.new(sid, config=hook.pack_form(form))
        p.hooks.append(h)
        db.session.add(h)
        db.session.commit()
        return redirect(url_for('.details', p=p.name, u=u.username))
    elif form is None and request.method == 'POST':
        h = Hook.new(sid)
        p.hooks.append(h)
        db.session.add(h)
        db.session.commit()
        return redirect(url_for('.details', p=p.name, u=u.username))

    return render_template('new_hook.html',
                           project=p,
                           services=HookService.services,
                           service=hook,
                           form=form)
Exemplo n.º 2
0
def new_hook(u, p, sid):
    if p.owner.id != g.user.id:
        # Project isn't public and the viewer isn't the project owner.
        # (403 Forbidden)
        return abort(403)

    hook = HookService.services.get(sid)
    form = hook.form()
    if form:
        form = form()

    if form and hook.validate(form, request):
        h = Hook.new(sid, config=hook.pack_form(form))
        p.hooks.append(h)
        g.db.session.add(h)
        g.db.session.commit()
        return redirect(url_for('.details', p=p.name, u=u.username))
    elif form is None and request.method == 'POST':
        h = Hook.new(sid)
        p.hooks.append(h)
        g.db.session.add(h)
        g.db.session.commit()
        return redirect(url_for('.details', p=p.name, u=u.username))

    return render_template('new_hook.html',
        project=p,
        services=HookService.services,
        service=hook,
        form=form
    )
Exemplo n.º 3
0
def github():
    """
    Import/merge the users existing Github projects, optionally setting up
    web hooks for them.
    """
    if 'code' in request.args:
        # We've finished step one and have a temporary token.
        r = requests.post('https://github.com/login/oauth/access_token',
            params={
                'client_id': app.config['SERVICE_GITHUB_CLIENT_ID'],
                'client_secret': app.config['SERVICE_GITHUB_CLIENT_SECRET'],
                'code': request.args['code']
        }, headers={
            'Accept': 'application/json'
        })
        result = r.json()
        token = AuthToken.new(result['access_token'], 'github')
        g.db.session.add(token)
        g.user.tokens.append(token)
        g.db.session.commit()
        return redirect(url_for('.github'))

    # Check to see if the user has previously authenticated.
    access_token = AuthToken.query.filter_by(
        name='github',
        owner_id=g.user.id
    ).first()

    if access_token is None:
        # The user hasn't setup Github yet, we need to get their OAuth
        # token.
        return redirect(
            'https://github.com/login/oauth/authorize?{0}'.format(
                urllib.urlencode({
                    'client_id': app.config['SERVICE_GITHUB_CLIENT_ID'],
                    'scope': 'repo'
                })
            )
        )

    # Set authentication and pull a JSON blob of all their
    # repos that they have actually created (or forked).
    # If we leave type as the default ("all") we also get
    # repos they have permission on, which we probably don't want.
    git = Github(access_token.token)
    # Test to make sure our token is still good...
    try:
        git.get_user().login
    except GithubException as exception:
        # Nope!
        if exception.status == 401:
            # The user almost certainly removed the OAuth token
            # from their github applications page. Remove the now-obsolete
            # token and refresh.
            access_token = AuthToken.query.filter_by(
                name='github',
                owner_id=g.user.id
            ).first()
            if access_token:
                g.user.tokens.remove(access_token)
                g.db.session.delete(access_token)
                g.db.session.commit()
            return redirect(request.path)

    user = git.get_user()
    # Get all of the users own repositories
    user_repos = user.get_repos(type='all')
    # ... and get all of the repos in the users organizations ...
    all_repos = chain(user_repos, *[o.get_repos() for o in user.get_orgs()])
    admin_repos = (r for r in all_repos if r.permissions.admin)

    summary = None
    options_form = GithubForm()
    if options_form.validate_on_submit():
        summary = []

        for repo in admin_repos:
            # User didn't check the box, don't import this project.
            # A hack-ish solution to wtform's BooleanField limitation,
            # or I would be using wtf.FieldList(wtf.BooleanField(...)).
            if request.form.get(str(repo.id), None) != 'y':
                continue

            # Make sure this project doesn't already exists.
            p = Project.by_name_and_owner(repo.name, g.user)
            if p is None:
                p = Project.new(
                    repo.name,
                    public=not repo.private,
                    website=repo.homepage
                )
                p.full_name = '{0}/{1}'.format(g.user.username, p.name)
                g.user.projects.append(p)
                g.db.session.add(p)
                # We need to commit to generate the project.id which is
                # used for the following steps.
                g.db.session.commit()

                summary.append((
                    'Project {0} created.'.format(repo.name),
                    True
                ))
            elif p is not None and not options_form.update_projects.data:
                summary.append((
                    'Skipping existing project {0}.'.format(repo.name),
                    False
                ))
                continue
            else:
                summary.append((
                    'Project {0} updated.'.format(repo.name),
                    True
                ))

            if options_form.set_hooks.data:
                # The user wanted us to auto-create web hooks for them.
                h = Hook.query.filter_by(
                    project_id=p.id,
                    service_id=10
                ).first()
                if h is None:
                    # No hooks for github have been setup yet, so go ahead
                    # and create one.
                    h = Hook.new(10)
                    p.hooks.append(h)
                    g.db.session.add(h)

                    repo.create_hook('web', {
                        'url': url_for(
                            'projects.hook_receive',
                            pid=p.id,
                            key=h.key,
                            _external=True
                        )
                    })

            if options_form.set_commits.data and p.public:
                # The user wanted us to add their public projects
                # to #[email protected] for them, but only if it
                # isn't already there.
                c = Channel.query.filter_by(
                    host='irc.freenode.net',
                    channel='#commits',
                    project_id=p.id
                ).first()
                if c is None:
                    # It does *not* already exist, so go ahead and add it.
                    c = Channel.new(
                        '#commits',
                        'irc.freenode.net',
                        6667,
                        ssl=False,
                        public=True
                    )
                    p.channels.append(c)
                    g.db.session.add(c)

        g.db.session.commit()

    return render_template('github.html',
        options_form=options_form,
        summary=summary,
        user_repos=admin_repos
    )
Exemplo n.º 4
0
def github():
    """
    Import/merge the users existing Github projects, optionally setting up
    web hooks for them.
    """
    if 'code' in request.args:
        # We've finished step one and have a temporary token.
        r = requests.post(
            'https://github.com/login/oauth/access_token',
            params={
                'client_id': current_app.config['SERVICE_GITHUB_CLIENT_ID'],
                'client_secret':
                current_app.config['SERVICE_GITHUB_CLIENT_SECRET'],
                'code': request.args['code']
            },
            headers={'Accept': 'application/json'})
        result = r.json()
        token = AuthToken.new(result['access_token'], 'github')
        db.session.add(token)
        g.user.tokens.append(token)
        db.session.commit()
        return redirect(url_for('.github'))

    # Check to see if the user has previously authenticated.
    access_token = AuthToken.query.filter_by(name='github',
                                             owner_id=g.user.id).first()

    if access_token is None:
        # The user hasn't setup Github yet, we need to get their OAuth
        # token.
        return redirect('https://github.com/login/oauth/authorize?{0}'.format(
            urllib.urlencode({
                'client_id':
                current_app.config['SERVICE_GITHUB_CLIENT_ID'],
                'scope':
                'repo'
            })))

    # Set authentication and pull a JSON blob of all their
    # repos that they have actually created (or forked).
    # If we leave type as the default ("all") we also get
    # repos they have permission on, which we probably don't want.
    git = Github(access_token.token, user_agent="Notifico Github Import/0.1")
    # Test to make sure our token is still good...
    try:
        git.get_user().login
    except GithubException as exception:
        # Nope!
        if exception.status == 401:
            # The user almost certainly removed the OAuth token
            # from their github applications page. Remove the now-obsolete
            # token and refresh.
            access_token = AuthToken.query.filter_by(
                name='github', owner_id=g.user.id).first()
            if access_token:
                g.user.tokens.remove(access_token)
                db.session.delete(access_token)
                db.session.commit()
            return redirect(request.path)

    user = git.get_user()
    # Get all of the users own repositories
    user_repos = user.get_repos(type='all')
    # ... and get all of the repos in the users organizations ...
    all_repos = chain(user_repos, *[o.get_repos() for o in user.get_orgs()])
    admin_repos = (r for r in all_repos if r.permissions.admin)

    summary = None
    options_form = GithubForm()
    if options_form.validate_on_submit():
        summary = []

        for repo in admin_repos:
            # User didn't check the box, don't import this project.
            # A hack-ish solution to wtform's BooleanField limitation,
            # or I would be using wtf.FieldList(wtf.BooleanField(...)).
            if request.form.get(str(repo.id), None) != 'y':
                continue

            # Make sure this project doesn't already exists.
            p = Project.by_name_and_owner(repo.name, g.user)
            if p is None:
                p = Project.new(repo.name,
                                public=not repo.private,
                                website=repo.homepage)
                p.full_name = '{0}/{1}'.format(g.user.username, p.name)
                g.user.projects.append(p)
                db.session.add(p)
                # We need to commit to generate the project.id which is
                # used for the following steps.
                db.session.commit()

                summary.append(
                    ('Project {0} created.'.format(repo.name), True))
            elif p is not None and not options_form.update_projects.data:
                summary.append(
                    ('Skipping existing project {0}.'.format(repo.name),
                     False))
                continue
            else:
                summary.append(
                    ('Project {0} updated.'.format(repo.name), True))

            if options_form.set_hooks.data:
                # The user wanted us to auto-create web hooks for them.
                h = Hook.query.filter_by(project_id=p.id,
                                         service_id=10).first()
                if h is None:
                    # No hooks for github have been setup yet, so go ahead
                    # and create one.
                    h = Hook.new(10)
                    p.hooks.append(h)
                    db.session.add(h)

                    repo.create_hook(
                        'web', {
                            'url':
                            url_for('projects.hook_receive',
                                    pid=p.id,
                                    key=h.key,
                                    _external=True)
                        })

            if options_form.set_commits.data and p.public:
                # The user wanted us to add their public projects
                # to #[email protected] for them, but only if it
                # isn't already there.
                c = Channel.query.filter_by(host='chat.freenode.net',
                                            channel='#commits',
                                            project_id=p.id).first()
                if c is None:
                    # It does *not* already exist, so go ahead and add it.
                    c = Channel.new('#commits',
                                    'chat.freenode.net',
                                    6667,
                                    ssl=False,
                                    public=True)
                    p.channels.append(c)
                    db.session.add(c)

        db.session.commit()

    return render_template('github.html',
                           options_form=options_form,
                           summary=summary,
                           user_repos=admin_repos)
Exemplo n.º 5
0
def github():
    """
    Import/merge the users existing Github projects, optionally setting up
    web hooks for them.
    """
    if 'code' in request.args:
        # We've finished step one and have a temporary token.
        r = requests.post('https://github.com/login/oauth/access_token',
            params={
                'client_id': app.config['SERVICE_GITHUB_CLIENT_ID'],
                'client_secret': app.config['SERVICE_GITHUB_CLIENT_SECRET'],
                'code': request.args['code']
        }, headers={
            'Accept': 'application/json'
        })
        result = r.json
        token = AuthToken.new(result['access_token'], 'github')
        g.db.session.add(token)
        g.user.tokens.append(token)
        g.db.session.commit()
        return redirect(url_for('.github'))

    # Check to see if the user has previously authenticated.
    access_token = AuthToken.query.filter_by(
        name='github',
        owner_id=g.user.id
    ).first()

    if access_token is None:
        # The user hasn't setup Github yet, we need to get their OAuth
        # token.
        return redirect(
            'https://github.com/login/oauth/authorize?{0}'.format(
                urllib.urlencode({
                    'client_id': app.config['SERVICE_GITHUB_CLIENT_ID'],
                    'scope': 'repo'
                })
            )
        )

    # Set authentication and pull a JSON blob of all their
    # repos that they have actually created (or forked).
    # If we leave type as the default ("all") we also get
    # repos they have permission on, which we probably don't want.
    git = Github(access_token.token)
    user_repos = git.get_user().get_repos(type='all')

    summary = None
    options_form = GithubForm()
    if options_form.validate_on_submit():
        summary = []
        # WTForms "forgets" disabled fields. This should /always/ be True.
        options_form.projects.data = True

        for repo in user_repos:
            # User didn't check the box, don't import this project.
            # A hack-ish solution to wtform's BooleanField limitation,
            # or I would be using wtf.FieldList(wtf.BooleanField(...)).
            if request.form.get(str(repo.id), None) != 'y':
                continue

            p = Project.by_name_and_owner(repo.name, g.user)
            if p is not None:
                summary.append((
                    (
                        'Skipping {0} as a project with that name already'
                        ' exists.'
                    ).format(repo.name),
                    False
                ))
                continue

            p = Project.new(
                repo.name,
                public=not repo.private,
                website=repo.homepage
            )
            p.full_name = '{0}/{1}'.format(g.user.username, p.name)
            g.user.projects.append(p)
            g.db.session.add(p)

            summary.append((
                'Project {0} created.'.format(repo.name),
                True
            ))

            if options_form.set_hooks.data:
                # The user wanted us to auto-create web hooks for them.
                # We need to commit first to generate the project.id.
                g.db.session.commit()

                # Create the hook on Notifico's side...
                h = Hook.new(10)
                p.hooks.append(h)
                g.db.session.add(h)

                # ... then create the hook on Github's side.
                repo.create_hook('web', {
                    'url': url_for(
                        'projects.hook_recieve',
                        pid=p.id,
                        key=h.key,
                        _external=True
                    )
                })

        g.db.session.commit()

    return render_template('github.html',
        options_form=options_form,
        summary=summary,
        user_repos=user_repos
    )