Files @ 8ad40ef0ea80
Branch filter:

Location: kallithea/kallithea/model/pull_request.py - annotation

Søren Løvborg
db: add some PullRequest.query() shortcuts

This makes database query code more explicit and increases readability.

E.g. the function name get_pullrequest_cnt_for_user was bad, because the
concept of "pullrequest for user" is incredibly vague, and could refer
to any kind of association between PRs and users. (Quiz time! Does it
mean that the user is the PR owner, that the user is reviewing, or that
the user has commented on the PR and thus is receiving notifications?)

A descriptive name could be "get_open_pull_request_count_for_reviewer",
because the function is indeed only concerned with reviewers and only
with open pull requests. But at this point, we might as well say
PullRequest.query(reviewer_id=user, include_closed=False).count()
which is only slightly longer, and doesn't require us to write dozens
of little wrapper functions (including, any moment now, a separate
function for listing the PRs instead of counting them).

Note that we're not actually going down an abstraction level by doing
this. We're still operating on the concepts of "pull request", "open"
and "reviewer", and are not leaking database implementation details.

The query() shortcuts are designed so they default to not altering
the query. Any processing requires explicit opt-in by the caller.
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
24c0d584ba86
d1addaf7a91e
1948ede028ef
1948ede028ef
d1addaf7a91e
d1addaf7a91e
1948ede028ef
ad38f9f93b3b
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
278a742731cd
278a742731cd
d1addaf7a91e
d1addaf7a91e
9a23b444a7fe
d1addaf7a91e
edb24bc0f71a
1658beb26ff9
d1addaf7a91e
231442b0b6aa
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
1658beb26ff9
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
68011c4e6f33
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
f54dca244402
f54dca244402
14d75d4b03cd
d1addaf7a91e
68011c4e6f33
f54dca244402
93e055489e68
93e055489e68
f54dca244402
f54dca244402
f54dca244402
f54dca244402
68011c4e6f33
f54dca244402
d1addaf7a91e
d1addaf7a91e
231442b0b6aa
1658beb26ff9
ba5fee3879c8
f24851239566
231442b0b6aa
cc9a7766d8bd
cc9a7766d8bd
1658beb26ff9
1658beb26ff9
cc9a7766d8bd
1658beb26ff9
1658beb26ff9
cc9a7766d8bd
cc9a7766d8bd
d1addaf7a91e
cc9a7766d8bd
cc9a7766d8bd
d1addaf7a91e
e30401bac6e1
a9d2e2958514
a9d2e2958514
a9d2e2958514
d1addaf7a91e
d1addaf7a91e
196c46444d98
f24851239566
cc9a7766d8bd
196c46444d98
cc9a7766d8bd
d1addaf7a91e
cc9a7766d8bd
55f2f2b4c462
3a221939a19f
55f2f2b4c462
cc9a7766d8bd
bd5d70b6f445
f24851239566
f9bc28c44f30
d1addaf7a91e
55f2f2b4c462
55f2f2b4c462
460520415b67
196c46444d98
3a221939a19f
3a221939a19f
3a221939a19f
3a221939a19f
3a221939a19f
3a221939a19f
3a221939a19f
d4061c6cc0e2
f24851239566
5e7c69707f08
231442b0b6aa
55f2f2b4c462
231442b0b6aa
f24851239566
231442b0b6aa
231442b0b6aa
231442b0b6aa
231442b0b6aa
231442b0b6aa
231442b0b6aa
231442b0b6aa
231442b0b6aa
231442b0b6aa
552800808386
f24851239566
231442b0b6aa
231442b0b6aa
231442b0b6aa
231442b0b6aa
f24851239566
ba5fee3879c8
ba5fee3879c8
231442b0b6aa
0210d0b769d4
1658beb26ff9
d1addaf7a91e
f24851239566
d1addaf7a91e
91b38dc6d891
edb24bc0f71a
278a742731cd
278a742731cd
278a742731cd
278a742731cd
278a742731cd
d1addaf7a91e
278a742731cd
278a742731cd
278a742731cd
278a742731cd
278a742731cd
d1addaf7a91e
0210d0b769d4
278a742731cd
d1addaf7a91e
0210d0b769d4
278a742731cd
278a742731cd
278a742731cd
d1addaf7a91e
d1addaf7a91e
91b38dc6d891
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
91b38dc6d891
d1addaf7a91e
d1addaf7a91e
d1addaf7a91e
# -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
kallithea.model.pull_request
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pull request model for Kallithea

This file was forked by the Kallithea project in July 2014.
Original author and date, and relevant copyright and licensing information is below:
:created_on: Jun 6, 2012
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH, and others.
:license: GPLv3, see LICENSE.md for more details.
"""

import logging
import datetime

from pylons.i18n.translation import _

from sqlalchemy.orm import joinedload

from kallithea.model.meta import Session
from kallithea.lib import helpers as h
from kallithea.lib.exceptions import UserInvalidException
from kallithea.model import BaseModel
from kallithea.model.db import PullRequest, PullRequestReviewers, Notification, \
    ChangesetStatus, User
from kallithea.model.notification import NotificationModel
from kallithea.lib.utils2 import extract_mentioned_users, safe_unicode


log = logging.getLogger(__name__)


class PullRequestModel(BaseModel):

    def _get_valid_reviewers(self, seq):
        """ Generate User objects from a sequence of user IDs, usernames or
        User objects. Raises UserInvalidException if the DEFAULT user is
        specified, or if a given ID or username does not match any user.
        """
        for user_spec in seq:
            user = self._get_user(user_spec)
            if user is None or user.username == User.DEFAULT_USER:
                raise UserInvalidException(user_spec)
            yield user

    def create(self, created_by, org_repo, org_ref, other_repo, other_ref,
               revisions, reviewers, title, description=None):
        from kallithea.model.changeset_status import ChangesetStatusModel

        created_by_user = self._get_user(created_by)
        org_repo = self._get_repo(org_repo)
        other_repo = self._get_repo(other_repo)

        new = PullRequest()
        new.org_repo = org_repo
        new.org_ref = org_ref
        new.other_repo = other_repo
        new.other_ref = other_ref
        new.revisions = revisions
        new.title = title
        new.description = description
        new.owner = created_by_user
        Session().add(new)
        Session().flush()

        #reset state to under-review
        from kallithea.model.comment import ChangesetCommentsModel
        comment = ChangesetCommentsModel().create(
            text=u'',
            repo=org_repo,
            user=new.owner,
            pull_request=new,
            send_email=False,
            status_change=ChangesetStatus.STATUS_UNDER_REVIEW,
        )
        ChangesetStatusModel().set_status(
            org_repo,
            ChangesetStatus.STATUS_UNDER_REVIEW,
            new.owner,
            comment,
            pull_request=new
        )

        reviewers = set(self._get_valid_reviewers(reviewers))
        mention_recipients = extract_mentioned_users(new.description)
        self.__add_reviewers(created_by_user, new, reviewers, mention_recipients)

        return new

    def __add_reviewers(self, user, pr, reviewers, mention_recipients):
        # reviewers and mention_recipients should be sets of User objects.
        #members
        for reviewer in reviewers:
            reviewer = PullRequestReviewers(reviewer, pr)
            Session().add(reviewer)

        revision_data = [(x.raw_id, x.message)
                         for x in map(pr.org_repo.get_changeset, pr.revisions)]

        #notification to reviewers
        pr_url = pr.url(canonical=True)
        threading = ['%s-pr-%s@%s' % (pr.other_repo.repo_name,
                                      pr.pull_request_id,
                                      h.canonical_hostname())]
        subject = safe_unicode(
            h.link_to(
              _('%(user)s wants you to review pull request %(pr_nice_id)s: %(pr_title)s') % \
                {'user': user.username,
                 'pr_title': pr.title,
                 'pr_nice_id': pr.nice_id()},
                pr_url)
            )
        body = pr.description
        _org_ref_type, org_ref_name, _org_rev = pr.org_ref.split(':')
        _other_ref_type, other_ref_name, _other_rev = pr.other_ref.split(':')
        email_kwargs = {
            'pr_title': pr.title,
            'pr_title_short': h.shorter(pr.title, 50),
            'pr_user_created': user.full_name_and_username,
            'pr_repo_url': h.canonical_url('summary_home', repo_name=pr.other_repo.repo_name),
            'pr_url': pr_url,
            'pr_revisions': revision_data,
            'repo_name': pr.other_repo.repo_name,
            'org_repo_name': pr.org_repo.repo_name,
            'pr_nice_id': pr.nice_id(),
            'pr_target_repo': h.canonical_url('summary_home',
                               repo_name=pr.other_repo.repo_name),
            'pr_target_branch': other_ref_name,
            'pr_source_repo': h.canonical_url('summary_home',
                               repo_name=pr.org_repo.repo_name),
            'pr_source_branch': org_ref_name,
            'pr_owner': pr.owner,
            'pr_owner_username': pr.owner.username,
            'pr_username': user.username,
            'threading': threading,
            'is_mention': False,
            }
        if reviewers:
            NotificationModel().create(created_by=user, subject=subject, body=body,
                                       recipients=reviewers,
                                       type_=Notification.TYPE_PULL_REQUEST,
                                       email_kwargs=email_kwargs)

        if mention_recipients:
            mention_recipients.difference_update(reviewers)
        if mention_recipients:
            email_kwargs['is_mention'] = True
            subject = _('[Mention]') + ' ' + subject
            # FIXME: this subject is wrong and unused!
            NotificationModel().create(created_by=user, subject=subject, body=body,
                                       recipients=mention_recipients,
                                       type_=Notification.TYPE_PULL_REQUEST,
                                       email_kwargs=email_kwargs)

    def mention_from_description(self, user, pr, old_description=''):
        mention_recipients = (extract_mentioned_users(pr.description) -
                              extract_mentioned_users(old_description))

        log.debug("Mentioning %s", mention_recipients)
        self.__add_reviewers(user, pr, set(), mention_recipients)

    def update_reviewers(self, user, pull_request, reviewers_ids):
        reviewers_ids = set(reviewers_ids)
        pull_request = PullRequest.guess_instance(pull_request)
        current_reviewers = PullRequestReviewers.query() \
            .options(joinedload('user')) \
            .filter_by(pull_request=pull_request) \
            .all()
        current_reviewer_users = set(x.user for x in current_reviewers)
        new_reviewer_users = set(self._get_valid_reviewers(reviewers_ids))

        to_add = new_reviewer_users - current_reviewer_users
        to_remove = current_reviewer_users - new_reviewer_users

        if not to_add and not to_remove:
            return # all done

        log.debug("Adding %s reviewers", to_add)
        self.__add_reviewers(user, pull_request, to_add, set())

        log.debug("Removing %s reviewers", to_remove)
        for prr in current_reviewers:
            if prr.user in to_remove:
                Session().delete(prr)

    def delete(self, pull_request):
        pull_request = PullRequest.guess_instance(pull_request)
        Session().delete(pull_request)

    def close_pull_request(self, pull_request):
        pull_request = PullRequest.guess_instance(pull_request)
        pull_request.status = PullRequest.STATUS_CLOSED
        pull_request.updated_on = datetime.datetime.now()
        Session().add(pull_request)