Beispiel #1
0
def home():
    """Homepage for linking a Gitlab project to a Trello board."""
    if request.method == 'POST':
        project_id = request.form['project-id']
        board_id = request.form['board-id']

        # check if project is already linked with a board
        try:
            project = ProjectHasBoard.by_project(project_id)
            project.update({'board_id': board_id})
        except Exception:
            app.logger.info('Linking project %s with board %s'
                            % (project_id, board_id))
            link, created = get_or_create(
                db.session, ProjectHasBoard, project_id=project_id,
                board_id=board_id)
            if not created:
                app.logger.info('Already linked')
        db.session.commit()

    # Check for Trello App Token
    if app.config['TRELLO_TOKEN'] in (None, ''):
        token_url = trello.get_token_url(
            'Gitlab To Trello', expires='never', write_access=True)
        return redirect(token_url)

    # Get all gitlab projects
    projects = git.getprojects()

    if not projects:
        return render_template('index.html')

    # Check for package.json at project root
    for project in projects:
        # Add flag to project dict to show project type
        if (git.getfile(project['id'], 'package.json',
                        project['default_branch'])):
            # Node.js project
            project['project_type'] = 'nodejs'
        elif (git.getfile(project['id'], 'setup.py',
                          project['default_branch'])):
            # Python project
            project['project_type'] = 'python'

        # Check if project is already linked to a board
        try:
            phb = ProjectHasBoard.by_project(project['id'])
            project['board_id'] = phb.board_id
        except NoResultFound:
            app.logger.warn('Project %s has no board linked' % project['id'])

        # Check if project is already monitored
        if project.get('project_type'):
            try:
                is_monitored = ProjectDependency.is_monitored(project['id'])
                if is_monitored:
                    project['is_monitored'] = 'True'
            except Exception, e:
                app.logger.error(e)
Beispiel #2
0
def dispatcher():
    """Dispatcher for Gitlab webhook triggering."""
    data = request.json
    kind = data.get('object_kind')
    content = data.get('object_attributes')

    if (kind in 'issue') and (content.get('action') in 'open'):
        # when opening an issue, create a Trello card and comment on Gitlab
        try:
            link = ProjectHasBoard.by_project(content.get('project_id'))
        except Exception, e:
            # in case of orm_exc.NoResultFound
            app.logger.error(e)

        opening_list = trello.boards.get_list(link.board_id)[0]
        app.logger.info('Creating a card for issue #%s on %s list.' %
                        (content.get('iid'), opening_list.get('name')))
        card = trello.cards.new(
            '#%s %s' % (
                content.get('iid'), content.get('title')),
            opening_list.get('id'),
            '%s \n\n %s' % (content.get('description'), content.get('url')))

        # create link between a card and an issue
        ilink, created = get_or_create(
            db.session, IssueHasCard,
            issue_id=content.get('iid'), card_id=card.get('id'))

        if created:
            # create a comment in gitlab with card's shortUrl
            git.createissuewallnote(
                content.get('project_id'), content.get('iid'),
                'Created Trello card -> %s' % card.get('shortUrl'))