Blame progit/issues.py

Pierre-Yves Chibon 47950c
#-*- coding: utf-8 -*-
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
"""
Pierre-Yves Chibon 47950c
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
 Authors:
Pierre-Yves Chibon 47950c
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
"""
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
import flask
Pierre-Yves Chibon 47950c
import os
Pierre-Yves Chibon 47950c
from math import ceil
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
import pygit2
Pierre-Yves Chibon 47950c
from sqlalchemy.exc import SQLAlchemyError
Pierre-Yves Chibon 47950c
from pygments import highlight
Pierre-Yves Chibon 47950c
from pygments.lexers import guess_lexer
Pierre-Yves Chibon 47950c
from pygments.lexers.text import DiffLexer
Pierre-Yves Chibon 47950c
from pygments.formatters import HtmlFormatter
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
import progit.doc_utils
Pierre-Yves Chibon 47950c
import progit.lib
Pierre-Yves Chibon 47950c
import progit.forms
Pierre-Yves Chibon 0dbb10
from progit import (APP, SESSION, LOG, __get_file_in_tree, cla_required,
Pierre-Yves Chibon c3d937
                    is_repo_admin, authenticated)
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
## URLs
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 013e6f
@APP.route('/<repo>/issue/<issueid>/add', methods=('GET', 'POST'))</issueid></repo>
Pierre-Yves Chibon 556a8e
@APP.route('/fork/<username>/<repo>/issue/<issueid>/add',</issueid></repo></username>
Pierre-Yves Chibon 556a8e
           methods=('GET', 'POST'))
Pierre-Yves Chibon 556a8e
def add_comment_issue(repo, issueid, username=None):
Pierre-Yves Chibon 556a8e
    ''' Add a comment to an issue. '''
Pierre-Yves Chibon 47950c
    repo = progit.lib.get_project(SESSION, repo, user=username)
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    if repo is None:
Pierre-Yves Chibon 47950c
        flask.abort(404, 'Project not found')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
    if not repo.issue_tracker:
Pierre-Yves Chibon 556a8e
        flask.abort(404, 'No issue tracker found for this project')
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 556a8e
    issue = progit.lib.get_issue(SESSION, issueid)
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 1d6527
    if issue is None or issue.project != repo:
Pierre-Yves Chibon 556a8e
        flask.abort(404, 'Issue not found')
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 556a8e
    form = progit.forms.AddIssueCommentForm()
Pierre-Yves Chibon 47950c
    if form.validate_on_submit():
Pierre-Yves Chibon 556a8e
        comment = form.comment.data
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
        try:
Pierre-Yves Chibon 556a8e
            message = progit.lib.add_issue_comment(
Pierre-Yves Chibon 47950c
                SESSION,
Pierre-Yves Chibon 556a8e
                issue=issue,
Pierre-Yves Chibon 556a8e
                comment=comment,
Pierre-Yves Chibon 47950c
                user=flask.g.fas_user.username,
Pierre-Yves Chibon 47950c
            )
Pierre-Yves Chibon 47950c
            SESSION.commit()
Pierre-Yves Chibon 47950c
            flask.flash(message)
Pierre-Yves Chibon 47950c
        except SQLAlchemyError, err:  # pragma: no cover
Pierre-Yves Chibon 47950c
            SESSION.rollback()
Pierre-Yves Chibon 47950c
            flask.flash(str(err), 'error')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
    return flask.redirect(flask.url_for(
Pierre-Yves Chibon 556a8e
        'view_issue', username=username, repo=repo.name, issueid=issue.id))
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
@APP.route('/<repo>/issues')</repo>
Pierre-Yves Chibon 556a8e
@APP.route('/fork/<username>/<repo>/issues')</repo></username>
Pierre-Yves Chibon 556a8e
def view_issues(repo, username=None):
Pierre-Yves Chibon 556a8e
    """ List all issues associated to a repo
Pierre-Yves Chibon 47950c
    """
Pierre-Yves Chibon 556a8e
    status = flask.request.args.get('status', None)
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 47950c
    repo = progit.lib.get_project(SESSION, repo, user=username)
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    if repo is None:
Pierre-Yves Chibon 47950c
        flask.abort(404, 'Project not found')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    if not repo.issue_tracker:
Pierre-Yves Chibon 47950c
        flask.abort(404, 'No issue tracker found for this project')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
    if status is not None:
Pierre-Yves Chibon 556a8e
        if status.lower() == 'closed':
Pierre-Yves Chibon 556a8e
            issues = progit.lib.get_issues(SESSION, repo, closed=True)
Pierre-Yves Chibon 556a8e
        else:
Pierre-Yves Chibon 556a8e
            issues = progit.lib.get_issues(SESSION, repo, status=status)
Pierre-Yves Chibon 556a8e
    else:
Pierre-Yves Chibon 556a8e
        issues = progit.lib.get_issues(SESSION, repo, status='Open')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
    return flask.render_template(
Pierre-Yves Chibon 556a8e
        'issues.html',
Pierre-Yves Chibon 556a8e
        select='issues',
Pierre-Yves Chibon 556a8e
        repo=repo,
Pierre-Yves Chibon 556a8e
        username=username,
Pierre-Yves Chibon 556a8e
        status=status,
Pierre-Yves Chibon 556a8e
        issues=issues,
Pierre-Yves Chibon 556a8e
    )
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 556a8e
@APP.route('/<repo>/new_issue', methods=('GET', 'POST'))</repo>
Pierre-Yves Chibon 556a8e
@APP.route('/fork/<username>/<repo>/new_issue', methods=('GET', 'POST'))</repo></username>
Pierre-Yves Chibon 556a8e
@cla_required
Pierre-Yves Chibon 556a8e
def new_issue(repo, username=None):
Pierre-Yves Chibon 556a8e
    """ Create a new issue
Pierre-Yves Chibon 556a8e
    """
Pierre-Yves Chibon 556a8e
    repo = progit.lib.get_project(SESSION, repo, user=username)
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 556a8e
    if repo is None:
Pierre-Yves Chibon 556a8e
        flask.abort(404, 'Project not found')
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon c5a9a2
    status = progit.lib.get_issue_statuses(SESSION)
Pierre-Yves Chibon c5a9a2
    form = progit.forms.IssueForm(status=status)
Pierre-Yves Chibon 47950c
    if form.validate_on_submit():
Pierre-Yves Chibon 47950c
        title = form.title.data
Pierre-Yves Chibon c5a9a2
        content = form.issue_content.data
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
        try:
Pierre-Yves Chibon 556a8e
            message = progit.lib.new_issue(
Pierre-Yves Chibon 47950c
                SESSION,
Pierre-Yves Chibon 556a8e
                repo=repo,
Pierre-Yves Chibon 47950c
                title=title,
Pierre-Yves Chibon 47950c
                content=content,
Pierre-Yves Chibon 556a8e
                user=flask.g.fas_user.username,
Pierre-Yves Chibon 47950c
            )
Pierre-Yves Chibon 47950c
            SESSION.commit()
Pierre-Yves Chibon 47950c
            flask.flash(message)
Pierre-Yves Chibon 556a8e
            return flask.redirect(flask.url_for(
Pierre-Yves Chibon a3258a
                'view_issues', username=username, repo=repo.name))
Pierre-Yves Chibon 47950c
        except progit.exceptions.ProgitException, err:
Pierre-Yves Chibon 47950c
            flask.flash(str(err), 'error')
Pierre-Yves Chibon 47950c
        except SQLAlchemyError, err:  # pragma: no cover
Pierre-Yves Chibon 47950c
            SESSION.rollback()
Pierre-Yves Chibon 47950c
            flask.flash(str(err), 'error')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    return flask.render_template(
Pierre-Yves Chibon 47950c
        'new_issue.html',
Pierre-Yves Chibon 47950c
        select='issues',
Pierre-Yves Chibon 47950c
        form=form,
Pierre-Yves Chibon 47950c
        repo=repo,
Pierre-Yves Chibon 47950c
        username=username,
Pierre-Yves Chibon 47950c
    )
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
@APP.route('/<repo>/issue/<issueid>', methods=('GET', 'POST'))</issueid></repo>
Pierre-Yves Chibon 556a8e
@APP.route('/fork/<username>/<repo>/issue/<issueid>',</issueid></repo></username>
Pierre-Yves Chibon 556a8e
           methods=('GET', 'POST'))
Pierre-Yves Chibon 556a8e
def view_issue(repo, issueid, username=None):
Pierre-Yves Chibon 47950c
    """ List all issues associated to a repo
Pierre-Yves Chibon 47950c
    """
Pierre-Yves Chibon 556a8e
Pierre-Yves Chibon 47950c
    repo = progit.lib.get_project(SESSION, repo, user=username)
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    if repo is None:
Pierre-Yves Chibon 47950c
        flask.abort(404, 'Project not found')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    if not repo.issue_tracker:
Pierre-Yves Chibon 47950c
        flask.abort(404, 'No issue tracker found for this project')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    issue = progit.lib.get_issue(SESSION, issueid)
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon c40e4b
    if issue is None or issue.project != repo:
Pierre-Yves Chibon 47950c
        flask.abort(404, 'Issue not found')
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    status = progit.lib.get_issue_statuses(SESSION)
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon c3d937
    form = None
Pierre-Yves Chibon c3d937
    if authenticated() and is_repo_admin(repo):
Pierre-Yves Chibon c3d937
        form = progit.forms.UpdateIssueStatusForm(status=status)
Pierre-Yves Chibon c3d937
Pierre-Yves Chibon c3d937
        if form.validate_on_submit():
Pierre-Yves Chibon c3d937
            try:
Pierre-Yves Chibon c3d937
                message = progit.lib.edit_issue(
Pierre-Yves Chibon c3d937
                    SESSION,
Pierre-Yves Chibon c3d937
                    issue=issue,
Pierre-Yves Chibon c3d937
                    status=form.status.data,
Pierre-Yves Chibon c3d937
                )
Pierre-Yves Chibon c3d937
                SESSION.commit()
Pierre-Yves Chibon c3d937
                flask.flash(message)
Pierre-Yves Chibon c3d937
                url = flask.url_for('view_issues', repo=repo.name)
Pierre-Yves Chibon c3d937
                if username:
Pierre-Yves Chibon c3d937
                    url = flask.url_for(
Pierre-Yves Chibon c3d937
                        'view_fork_issues', username=username, repo=repo.name)
Pierre-Yves Chibon c3d937
                return flask.redirect(url)
Pierre-Yves Chibon c3d937
            except SQLAlchemyError, err:  # pragma: no cover
Pierre-Yves Chibon c3d937
                SESSION.rollback()
Pierre-Yves Chibon c3d937
                flask.flash(str(err), 'error')
Pierre-Yves Chibon c3d937
        elif flask.request.method == 'GET':
Pierre-Yves Chibon c3d937
            form.status.data = issue.status
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
    return flask.render_template(
Pierre-Yves Chibon 47950c
        'issue.html',
Pierre-Yves Chibon 47950c
        select='issues',
Pierre-Yves Chibon 47950c
        repo=repo,
Pierre-Yves Chibon 47950c
        username=username,
Pierre-Yves Chibon 47950c
        issue=issue,
Pierre-Yves Chibon 47950c
        form=form,
Pierre-Yves Chibon 47950c
    )
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 47950c
Pierre-Yves Chibon 556a8e
@APP.route('/<repo>/issue/<issueid>/edit', methods=('GET', 'POST'))</issueid></repo>
Pierre-Yves Chibon 556a8e
@APP.route('/fork/<username>/<repo>/issue/<issueid>/edit',</issueid></repo></username>
Pierre-Yves Chibon f9a5d2
           methods=('GET', 'POST'))
Pierre-Yves Chibon 556a8e
@cla_required
Pierre-Yves Chibon 556a8e
def edit_issue(repo, issueid, username=None):
Pierre-Yves Chibon 556a8e
    """ Edit the specified issue
Pierre-Yves Chibon 556a8e
    """
Pierre-Yves Chibon f9a5d2
    repo = progit.lib.get_project(SESSION, repo, user=username)
Pierre-Yves Chibon f9a5d2
Pierre-Yves Chibon f9a5d2
    if repo is None:
Pierre-Yves Chibon f9a5d2
        flask.abort(404, 'Project not found')
Pierre-Yves Chibon f9a5d2
Pierre-Yves Chibon f9a5d2
    if not repo.issue_tracker:
Pierre-Yves Chibon f9a5d2
        flask.abort(404, 'No issue tracker found for this project')
Pierre-Yves Chibon f9a5d2
Pierre-Yves Chibon 0dbb10
    if not is_repo_admin(repo):
Pierre-Yves Chibon 0dbb10
        flask.abort(
Pierre-Yves Chibon 0dbb10
            403, 'You are not allowed to edit issues for this project')
Pierre-Yves Chibon 0dbb10
Pierre-Yves Chibon f9a5d2
    issue = progit.lib.get_issue(SESSION, issueid)
Pierre-Yves Chibon f9a5d2
Pierre-Yves Chibon 1d6527
    if issue is None or issue.project != repo:
Pierre-Yves Chibon f9a5d2
        flask.abort(404, 'Issue not found')
Pierre-Yves Chibon f9a5d2
Pierre-Yves Chibon 556a8e
    status = progit.lib.get_issue_statuses(SESSION)
Pierre-Yves Chibon 556a8e
    form = progit.forms.IssueForm(status=status)
Pierre-Yves Chibon f9a5d2
    if form.validate_on_submit():
Pierre-Yves Chibon 556a8e
        title = form.title.data
Pierre-Yves Chibon 824f5d
        content = form.issue_content.data
Pierre-Yves Chibon 556a8e
        status = form.status.data
Pierre-Yves Chibon f9a5d2
Pierre-Yves Chibon f9a5d2
        try:
Pierre-Yves Chibon 556a8e
            message = progit.lib.edit_issue(
Pierre-Yves Chibon f9a5d2
                SESSION,
Pierre-Yves Chibon f9a5d2
                issue=issue,
Pierre-Yves Chibon 556a8e
                title=title,
Pierre-Yves Chibon 556a8e
                content=content,
Pierre-Yves Chibon 556a8e
                status=status,
Pierre-Yves Chibon f9a5d2
            )
Pierre-Yves Chibon f9a5d2
            SESSION.commit()
Pierre-Yves Chibon f9a5d2
            flask.flash(message)
Pierre-Yves Chibon 556a8e
            url = flask.url_for(
Pierre-Yves Chibon 556a8e
                'view_issue', username=username,
Pierre-Yves Chibon 556a8e
                repo=repo.name, issueid=issue.id)
Pierre-Yves Chibon 556a8e
            return flask.redirect(url)
Pierre-Yves Chibon 556a8e
        except progit.exceptions.ProgitException, err:
Pierre-Yves Chibon 556a8e
            flask.flash(str(err), 'error')
Pierre-Yves Chibon f9a5d2
        except SQLAlchemyError, err:  # pragma: no cover
Pierre-Yves Chibon f9a5d2
            SESSION.rollback()
Pierre-Yves Chibon f9a5d2
            flask.flash(str(err), 'error')
Pierre-Yves Chibon 556a8e
    elif flask.request.method == 'GET':
Pierre-Yves Chibon 556a8e
        form.title.data = issue.title
Pierre-Yves Chibon 824f5d
        form.issue_content.data = issue.content
Pierre-Yves Chibon 556a8e
        form.status.data = issue.status
Pierre-Yves Chibon f9a5d2
Pierre-Yves Chibon 556a8e
    return flask.render_template(
Pierre-Yves Chibon 556a8e
        'new_issue.html',
Pierre-Yves Chibon 556a8e
        select='issues',
Pierre-Yves Chibon 556a8e
        type='edit',
Pierre-Yves Chibon 556a8e
        form=form,
Pierre-Yves Chibon 556a8e
        repo=repo,
Pierre-Yves Chibon 556a8e
        username=username,
Pierre-Yves Chibon 556a8e
        issue=issue,
Pierre-Yves Chibon 556a8e
    )