Changeset - d4061c6cc0e2
[Not reviewed]
default
0 5 0
Mads Kiilerich - 9 years ago 2016-07-28 16:34:29
madski@unity3d.com
notifications: tweak PR mail subject lines

The subject line is used for mail threading in gmail and can thus not be
changed without impacting users ... but now we do it.

* The tag '[Review]' is more spot-on than '[Added]'.
* The subject should be short so it fits on one line, so abbreviate "pull
request" to PR.
* Add the PR owner - convenient for filtering comments on own PRs from comments
on other PRs.
5 files changed with 15 insertions and 12 deletions:
0 comments (0 inline, 0 general)
kallithea/model/comment.py
Show inline comments
 
# -*- 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.comment
 
~~~~~~~~~~~~~~~~~~~~~~~
 

	
 
comments 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: Nov 11, 2011
 
:author: marcink
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
 
:license: GPLv3, see LICENSE.md for more details.
 
"""
 

	
 
import logging
 

	
 
from pylons.i18n.translation import _
 
from collections import defaultdict
 

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

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
class ChangesetCommentsModel(BaseModel):
 

	
 
    cls = ChangesetComment
 

	
 
    def __get_changeset_comment(self, changeset_comment):
 
        return self._get_instance(ChangesetComment, changeset_comment)
 

	
 
    def __get_pull_request(self, pull_request):
 
        return self._get_instance(PullRequest, pull_request)
 

	
 
    def _get_notification_data(self, repo, comment, user, comment_text,
 
                               line_no=None, revision=None, pull_request=None,
 
                               status_change=None, closing_pr=False):
 
        """
 
        :returns: tuple (subj,body,recipients,notification_type,email_kwargs)
 
        """
 
        # make notification
 
        body = comment_text  # text of the comment
 
        line = ''
 
        if line_no:
 
            line = _('on line %s') % line_no
 

	
 
        #changeset
 
        if revision:
 
            notification_type = Notification.TYPE_CHANGESET_COMMENT
 
            cs = repo.scm_instance.get_changeset(revision)
 
            desc = cs.short_id
 

	
 
            threading = ['%s-rev-%s@%s' % (repo.repo_name, revision, h.canonical_hostname())]
 
            if line_no: # TODO: url to file _and_ line number
 
                threading.append('%s-rev-%s-line-%s@%s' % (repo.repo_name, revision, line_no,
 
                                                           h.canonical_hostname()))
 
            comment_url = h.canonical_url('changeset_home',
 
                repo_name=repo.repo_name,
 
                revision=revision,
 
                anchor='comment-%s' % comment.comment_id)
 
            subj = safe_unicode(
 
                h.link_to('Re changeset: %(desc)s %(line)s' % \
 
                          {'desc': desc, 'line': line},
 
                          comment_url)
 
            )
 
            # get the current participants of this changeset
 
            recipients = ChangesetComment.get_users(revision=revision)
 
            # add changeset author if it's known locally
 
            cs_author = User.get_from_cs_author(cs.author)
 
            if not cs_author:
 
                #use repo owner if we cannot extract the author correctly
 
                # FIXME: just use committer name even if not a user
 
                cs_author = repo.user
 
            recipients += [cs_author]
 
            email_kwargs = {
 
                'status_change': status_change,
 
                'cs_comment_user': user.full_name_and_username,
 
                'cs_target_repo': h.canonical_url('summary_home', repo_name=repo.repo_name),
 
                'cs_comment_url': comment_url,
 
                'raw_id': revision,
 
                'message': cs.message,
 
                'message_short': h.shorter(cs.message, 50, firstline=True),
 
                'cs_author': cs_author,
 
                'repo_name': repo.repo_name,
 
                'short_id': h.short_id(revision),
 
                'branch': cs.branch,
 
                'comment_username': user.username,
 
                'threading': threading,
 
            }
 
        #pull request
 
        elif pull_request:
 
            notification_type = Notification.TYPE_PULL_REQUEST_COMMENT
 
            desc = comment.pull_request.title
 
            _org_ref_type, org_ref_name, _org_rev = comment.pull_request.org_ref.split(':')
 
            _other_ref_type, other_ref_name, _other_rev = comment.pull_request.other_ref.split(':')
 
            threading = ['%s-pr-%s@%s' % (pull_request.other_repo.repo_name,
 
                                          pull_request.pull_request_id,
 
                                          h.canonical_hostname())]
 
            if line_no: # TODO: url to file _and_ line number
 
                threading.append('%s-pr-%s-line-%s@%s' % (pull_request.other_repo.repo_name,
 
                                                          pull_request.pull_request_id, line_no,
 
                                                          h.canonical_hostname()))
 
            comment_url = pull_request.url(canonical=True,
 
                anchor='comment-%s' % comment.comment_id)
 
            subj = safe_unicode(
 
                h.link_to('Re pull request %(pr_nice_id)s: %(desc)s %(line)s' % \
 
                          {'desc': desc,
 
                           'pr_nice_id': comment.pull_request.nice_id(),
 
                           'line': line},
 
                          comment_url)
 
            )
 
            # get the current participants of this pull request
 
            recipients = ChangesetComment.get_users(pull_request_id=
 
                                                pull_request.pull_request_id)
 
            # add pull request author
 
            recipients += [pull_request.owner]
 

	
 
            # add the reviewers to notification
 
            recipients += pull_request.get_reviewer_users()
 

	
 
            #set some variables for email notification
 
            email_kwargs = {
 
                'pr_title': pull_request.title,
 
                'pr_title_short': h.shorter(pull_request.title, 50),
 
                'pr_nice_id': pull_request.nice_id(),
 
                'status_change': status_change,
 
                'closing_pr': closing_pr,
 
                'pr_comment_url': comment_url,
 
                'pr_comment_user': user.full_name_and_username,
 
                'pr_target_repo': h.canonical_url('summary_home',
 
                                   repo_name=pull_request.other_repo.repo_name),
 
                'pr_target_branch': other_ref_name,
 
                'pr_source_repo': h.canonical_url('summary_home',
 
                                   repo_name=pull_request.org_repo.repo_name),
 
                'pr_source_branch': org_ref_name,
 
                'pr_owner': pull_request.owner,
 
                'pr_owner_username': pull_request.owner.username,
 
                'repo_name': pull_request.other_repo.repo_name,
 
                'comment_username': user.username,
 
                'threading': threading,
 
            }
 

	
 
        return subj, body, recipients, notification_type, email_kwargs
 

	
 
    def create(self, text, repo, user, revision=None, pull_request=None,
 
               f_path=None, line_no=None, status_change=None, closing_pr=False,
 
               send_email=True):
 
        """
 
        Creates a new comment for either a changeset or a pull request.
 
        status_change and closing_pr is only for the optional email.
 

	
 
        Returns the created comment.
 
        """
 
        if not status_change and not text:
 
            log.warning('Missing text for comment, skipping...')
 
            return None
 

	
 
        repo = self._get_repo(repo)
 
        user = self._get_user(user)
 
        comment = ChangesetComment()
 
        comment.repo = repo
 
        comment.author = user
 
        comment.text = text
 
        comment.f_path = f_path
 
        comment.line_no = line_no
 

	
 
        if revision is not None:
 
            comment.revision = revision
 
        elif pull_request is not None:
 
            pull_request = self.__get_pull_request(pull_request)
 
            comment.pull_request = pull_request
 
        else:
 
            raise Exception('Please specify revision or pull_request_id')
 

	
 
        Session().add(comment)
 
        Session().flush()
 

	
 
        if send_email:
 
            (subj, body, recipients, notification_type,
 
             email_kwargs) = self._get_notification_data(
 
                                repo, comment, user,
 
                                comment_text=text,
 
                                line_no=line_no,
 
                                revision=revision,
 
                                pull_request=pull_request,
 
                                status_change=status_change,
 
                                closing_pr=closing_pr)
 
            email_kwargs['is_mention'] = False
 
            # create notification objects, and emails
 
            NotificationModel().create(
 
                created_by=user, subject=subj, body=body,
 
                recipients=recipients, type_=notification_type,
 
                email_kwargs=email_kwargs,
 
            )
 

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

	
 
        return comment
 

	
 
    def delete(self, comment):
 
        comment = self.__get_changeset_comment(comment)
 
        Session().delete(comment)
 

	
 
        return comment
 

	
 
    def get_comments(self, repo_id, revision=None, pull_request=None):
 
        """
 
        Gets general comments for either revision or pull_request.
 

	
 
        Returns a list, ordered by creation date.
 
        """
 
        return self._get_comments(repo_id, revision=revision, pull_request=pull_request,
 
                                  inline=False)
 

	
 
    def get_inline_comments(self, repo_id, revision=None, pull_request=None):
 
        """
 
        Gets inline comments for either revision or pull_request.
 

	
 
        Returns a list of tuples with file path and list of comments per line number.
 
        """
 
        comments = self._get_comments(repo_id, revision=revision, pull_request=pull_request,
 
                                      inline=True)
 

	
 
        paths = defaultdict(lambda: defaultdict(list))
 
        for co in comments:
 
            paths[co.f_path][co.line_no].append(co)
 
        return paths.items()
 

	
 
    def _get_comments(self, repo_id, revision=None, pull_request=None, inline=False):
 
        """
 
        Gets comments for either revision or pull_request_id, either inline or general.
 
        """
 
        q = Session().query(ChangesetComment)
 

	
 
        if inline:
 
            q = q.filter(ChangesetComment.line_no != None) \
 
                .filter(ChangesetComment.f_path != None)
 
        else:
 
            q = q.filter(ChangesetComment.line_no == None) \
 
                .filter(ChangesetComment.f_path == None)
 

	
 
        if revision is not None:
 
            q = q.filter(ChangesetComment.revision == revision) \
 
                .filter(ChangesetComment.repo_id == repo_id)
 
        elif pull_request is not None:
 
            pull_request = self.__get_pull_request(pull_request)
 
            q = q.filter(ChangesetComment.pull_request == pull_request)
 
        else:
 
            raise Exception('Please specify either revision or pull_request')
 

	
 
        return q.order_by(ChangesetComment.created_on).all()
kallithea/model/notification.py
Show inline comments
 
@@ -115,228 +115,228 @@ class NotificationModel(BaseModel):
 
        rec_objs = set(recipients_objs).difference(set([created_by_obj]))
 

	
 
        headers = None
 
        if 'threading' in email_kwargs:
 
            headers = {'References': ' '.join('<%s>' % x for x in email_kwargs['threading'])}
 

	
 
        # send email with notification to all other participants
 
        for rec in rec_objs:
 
            ## this is passed into template
 
            html_kwargs = {
 
                      'subject': subject,
 
                      'body': h.render_w_mentions(body, repo_name),
 
                      'when': h.fmt_date(notif.created_on),
 
                      'user': notif.created_by_user.username,
 
                      }
 

	
 
            txt_kwargs = {
 
                      'subject': subject,
 
                      'body': body,
 
                      'when': h.fmt_date(notif.created_on),
 
                      'user': notif.created_by_user.username,
 
                      }
 

	
 
            html_kwargs.update(email_kwargs)
 
            txt_kwargs.update(email_kwargs)
 
            email_subject = EmailNotificationModel() \
 
                                .get_email_description(type_, **txt_kwargs)
 
            email_txt_body = EmailNotificationModel() \
 
                                .get_email_tmpl(type_, 'txt', **txt_kwargs)
 
            email_html_body = EmailNotificationModel() \
 
                                .get_email_tmpl(type_, 'html', **html_kwargs)
 

	
 
            run_task(tasks.send_email, [rec.email], email_subject, email_txt_body,
 
                     email_html_body, headers, author=created_by_obj)
 

	
 
        return notif
 

	
 
    def delete(self, user, notification):
 
        # we don't want to remove actual notification just the assignment
 
        try:
 
            notification = self.__get_notification(notification)
 
            user = self._get_user(user)
 
            if notification and user:
 
                obj = UserNotification.query() \
 
                        .filter(UserNotification.user == user) \
 
                        .filter(UserNotification.notification
 
                                == notification) \
 
                        .one()
 
                Session().delete(obj)
 
                return True
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            raise
 

	
 
    def query_for_user(self, user, filter_=None):
 
        """
 
        Get notifications for given user, filter them if filter dict is given
 

	
 
        :param user:
 
        :param filter:
 
        """
 
        user = self._get_user(user)
 

	
 
        q = UserNotification.query() \
 
            .filter(UserNotification.user == user) \
 
            .join((Notification, UserNotification.notification_id ==
 
                                 Notification.notification_id)) \
 
            .options(joinedload('notification')) \
 
            .options(subqueryload('notification.created_by_user')) \
 
            .order_by(Notification.created_on.desc())
 

	
 
        if filter_:
 
            q = q.filter(Notification.type_.in_(filter_))
 

	
 
        return q
 

	
 
    def mark_read(self, user, notification):
 
        try:
 
            notification = self.__get_notification(notification)
 
            user = self._get_user(user)
 
            if notification and user:
 
                obj = UserNotification.query() \
 
                        .filter(UserNotification.user == user) \
 
                        .filter(UserNotification.notification
 
                                == notification) \
 
                        .one()
 
                obj.read = True
 
                Session().add(obj)
 
                return True
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            raise
 

	
 
    def mark_all_read_for_user(self, user, filter_=None):
 
        user = self._get_user(user)
 
        q = UserNotification.query() \
 
            .filter(UserNotification.user == user) \
 
            .filter(UserNotification.read == False) \
 
            .join((Notification, UserNotification.notification_id ==
 
                                 Notification.notification_id))
 
        if filter_:
 
            q = q.filter(Notification.type_.in_(filter_))
 

	
 
        # this is a little inefficient but sqlalchemy doesn't support
 
        # update on joined tables :(
 
        for obj in q.all():
 
            obj.read = True
 
            Session().add(obj)
 

	
 
    def get_unread_cnt_for_user(self, user):
 
        user = self._get_user(user)
 
        return UserNotification.query() \
 
                .filter(UserNotification.read == False) \
 
                .filter(UserNotification.user == user).count()
 

	
 
    def get_unread_for_user(self, user):
 
        user = self._get_user(user)
 
        return [x.notification for x in UserNotification.query() \
 
                .filter(UserNotification.read == False) \
 
                .filter(UserNotification.user == user).all()]
 

	
 
    def get_user_notification(self, user, notification):
 
        user = self._get_user(user)
 
        notification = self.__get_notification(notification)
 

	
 
        return UserNotification.query() \
 
            .filter(UserNotification.notification == notification) \
 
            .filter(UserNotification.user == user).scalar()
 

	
 
    def make_description(self, notification, show_age=True):
 
        """
 
        Creates a human readable description based on properties
 
        of notification object
 
        """
 
        #alias
 
        _n = notification
 

	
 
        if show_age:
 
            return {
 
                    _n.TYPE_CHANGESET_COMMENT: _('%(user)s commented on changeset %(age)s'),
 
                    _n.TYPE_MESSAGE: _('%(user)s sent message %(age)s'),
 
                    _n.TYPE_MENTION: _('%(user)s mentioned you %(age)s'),
 
                    _n.TYPE_REGISTRATION: _('%(user)s registered in Kallithea %(age)s'),
 
                    _n.TYPE_PULL_REQUEST: _('%(user)s opened new pull request %(age)s'),
 
                    _n.TYPE_PULL_REQUEST_COMMENT: _('%(user)s commented on pull request %(age)s'),
 
                }[notification.type_] % dict(
 
                    user=notification.created_by_user.username,
 
                    age=h.age(notification.created_on),
 
                )
 
        else:
 
            return {
 
                    _n.TYPE_CHANGESET_COMMENT: _('%(user)s commented on changeset at %(when)s'),
 
                    _n.TYPE_MESSAGE: _('%(user)s sent message at %(when)s'),
 
                    _n.TYPE_MENTION: _('%(user)s mentioned you at %(when)s'),
 
                    _n.TYPE_REGISTRATION: _('%(user)s registered in Kallithea at %(when)s'),
 
                    _n.TYPE_PULL_REQUEST: _('%(user)s opened new pull request at %(when)s'),
 
                    _n.TYPE_PULL_REQUEST_COMMENT: _('%(user)s commented on pull request at %(when)s'),
 
                }[notification.type_] % dict(
 
                    user=notification.created_by_user.username,
 
                    when=h.fmt_date(notification.created_on),
 
                )
 

	
 

	
 
class EmailNotificationModel(BaseModel):
 

	
 
    TYPE_CHANGESET_COMMENT = Notification.TYPE_CHANGESET_COMMENT
 
    TYPE_MESSAGE = Notification.TYPE_MESSAGE # only used for testing
 
    # Notification.TYPE_MENTION is not used
 
    TYPE_PASSWORD_RESET = 'password_link'
 
    TYPE_REGISTRATION = Notification.TYPE_REGISTRATION
 
    TYPE_PULL_REQUEST = Notification.TYPE_PULL_REQUEST
 
    TYPE_PULL_REQUEST_COMMENT = Notification.TYPE_PULL_REQUEST_COMMENT
 
    TYPE_DEFAULT = 'default'
 

	
 
    def __init__(self):
 
        super(EmailNotificationModel, self).__init__()
 
        self._template_root = kallithea.CONFIG['pylons.paths']['templates'][0]
 
        self._tmpl_lookup = kallithea.CONFIG['pylons.app_globals'].mako_lookup
 
        self.email_types = {
 
            self.TYPE_CHANGESET_COMMENT: 'changeset_comment',
 
            self.TYPE_PASSWORD_RESET: 'password_reset',
 
            self.TYPE_REGISTRATION: 'registration',
 
            self.TYPE_DEFAULT: 'default',
 
            self.TYPE_PULL_REQUEST: 'pull_request',
 
            self.TYPE_PULL_REQUEST_COMMENT: 'pull_request_comment',
 
        }
 
        self._subj_map = {
 
            self.TYPE_CHANGESET_COMMENT: _('[Comment] %(repo_name)s changeset %(short_id)s "%(message_short)s" on %(branch)s'),
 
            self.TYPE_MESSAGE: 'Test Message',
 
            # self.TYPE_PASSWORD_RESET
 
            self.TYPE_REGISTRATION: _('New user %(new_username)s registered'),
 
            # self.TYPE_DEFAULT
 
            self.TYPE_PULL_REQUEST: _('[Added] %(repo_name)s pull request %(pr_nice_id)s "%(pr_title_short)s" from %(pr_source_branch)s'),
 
            self.TYPE_PULL_REQUEST_COMMENT: _('[Comment] %(repo_name)s pull request %(pr_nice_id)s "%(pr_title_short)s" from %(pr_source_branch)s'),
 
            self.TYPE_PULL_REQUEST: _('[Review] %(repo_name)s PR %(pr_nice_id)s "%(pr_title_short)s" from %(pr_source_branch)s by %(pr_owner_username)s'),
 
            self.TYPE_PULL_REQUEST_COMMENT: _('[Comment] %(repo_name)s PR %(pr_nice_id)s "%(pr_title_short)s" from %(pr_source_branch)s by %(pr_owner_username)s'),
 
        }
 

	
 
    def get_email_description(self, type_, **kwargs):
 
        """
 
        return subject for email based on given type
 
        """
 
        tmpl = self._subj_map[type_]
 
        try:
 
            subj = tmpl % kwargs
 
        except KeyError as e:
 
            log.error('error generating email subject for %r from %s: %s', type_, ','.join(self._subj_map.keys()), e)
 
            raise
 
        l = [safe_unicode(x) for x in [kwargs.get('status_change'), kwargs.get('closing_pr') and _('Closing')] if x]
 
        if l:
 
            if subj.startswith('['):
 
                subj = '[' + ', '.join(l) + ': ' + subj[1:]
 
            else:
 
                subj = '[' + ', '.join(l) + '] ' + subj
 
        return subj
 

	
 
    def get_email_tmpl(self, type_, content_type, **kwargs):
 
        """
 
        return generated template for email based on given type
 
        """
 

	
 
        base = 'email_templates/' + self.email_types.get(type_, self.email_types[self.TYPE_DEFAULT]) + '.' + content_type
 
        email_template = self._tmpl_lookup.get_template(base)
 
        # translator and helpers inject
 
        _kwargs = {'_': _,
 
                   'h': h,
 
                   'c': c}
 
        _kwargs.update(kwargs)
 
        log.debug('rendering tmpl %s with kwargs %s', base, _kwargs)
 
        return email_template.render(**_kwargs)
kallithea/model/pull_request.py
Show inline comments
 
# -*- 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):
 

	
 
    cls = PullRequest
 

	
 
    def __get_pull_request(self, pull_request):
 
        return self._get_instance(PullRequest, pull_request)
 

	
 
    def get_pullrequest_cnt_for_user(self, user):
 
        return PullRequest.query() \
 
                                .join(PullRequestReviewers) \
 
                                .filter(PullRequestReviewers.user_id == user) \
 
                                .filter(PullRequest.status != PullRequest.STATUS_CLOSED) \
 
                                .count()
 

	
 
    def get_all(self, repo_name, from_=False, closed=False):
 
        """Get all PRs for repo.
 
        Default is all PRs to the repo, PRs from the repo if from_.
 
        Closed PRs are only included if closed is true."""
 
        repo = self._get_repo(repo_name)
 
        q = PullRequest.query()
 
        if from_:
 
            q = q.filter(PullRequest.org_repo == repo)
 
        else:
 
            q = q.filter(PullRequest.other_repo == repo)
 
        if not closed:
 
            q = q.filter(PullRequest.status != PullRequest.STATUS_CLOSED)
 
        return q.order_by(PullRequest.created_on.desc()).all()
 

	
 
    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 = self.__get_pull_request(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 = self.__get_pull_request(pull_request)
 
        Session().delete(pull_request)
 

	
 
    def close_pull_request(self, pull_request):
 
        pull_request = self.__get_pull_request(pull_request)
 
        pull_request.status = PullRequest.STATUS_CLOSED
 
        pull_request.updated_on = datetime.datetime.now()
 
        Session().add(pull_request)
kallithea/tests/models/test_dump_html_mails.ref.html
Show inline comments
 
@@ -97,717 +97,717 @@ This changeset did something clever whic
 
</p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>cs_comment, is_mention=False, status_change='Approved'</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Approved: Comment] repo/name changeset cafe1234 "This changeset did something cl..." on brunch
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo_target changeset cafe1234c0ff:
 
This is the new comment.
 

	
 
 - and here it ends indented.
 

	
 
The changeset status was changed to: Approved
 

	
 
URL: http://comment.org
 

	
 
Changeset: cafe1234c0ff
 
Description:
 
This changeset did something clever which is hard to explain
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo_target changeset cafe1234c0ff:</p>
 
<p><div class="formatted-fixed">This is the new comment.
 

	
 
 - and here it ends indented.</div></p>
 

	
 
    <p>The changeset status was changed to: <b>Approved</b></p>
 

	
 
<p>URL: <a href="http://comment.org">http://comment.org</a></p>
 

	
 
<p>Changeset: cafe1234c0ff</p>
 
<p>Description:<br/>
 
This changeset did something clever which is hard to explain
 
</p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>cs_comment, is_mention=True, status_change='Approved'</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Approved: Comment] repo/name changeset cafe1234 "This changeset did something cl..." on brunch
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo_target changeset cafe1234c0ff mentioned you:
 
This is the new comment.
 

	
 
 - and here it ends indented.
 

	
 
The changeset status was changed to: Approved
 

	
 
URL: http://comment.org
 

	
 
Changeset: cafe1234c0ff
 
Description:
 
This changeset did something clever which is hard to explain
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo_target changeset cafe1234c0ff mentioned you:</p>
 
<p><div class="formatted-fixed">This is the new comment.
 

	
 
 - and here it ends indented.</div></p>
 

	
 
    <p>The changeset status was changed to: <b>Approved</b></p>
 

	
 
<p>URL: <a href="http://comment.org">http://comment.org</a></p>
 

	
 
<p>Changeset: cafe1234c0ff</p>
 
<p>Description:<br/>
 
This changeset did something clever which is hard to explain
 
</p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>message</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: Test Message
 

	
 
--------------------
 

	
 

	
 
This is the body of the test message
 
 - nothing interesting here except indentation.
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<div class="formatted-fixed">This is the body of the test message
 
 - nothing interesting here except indentation.</div>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>registration</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: New user newbie registered
 

	
 
--------------------
 

	
 

	
 
Registration body
 

	
 
View this user here: http://newbie.org
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<div class="formatted-fixed">Registration body</div>
 

	
 
View this user here: <a href="http://newbie.org">http://newbie.org</a>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request, is_mention=False</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Added] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Review] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Requesting User (root) requested your review of repo/name pull request "The Title"
 

	
 
URL: http://pr.org/7
 

	
 
Description:
 
This PR is awesome because it does stuff
 
 - please approve indented!
 

	
 
Changesets:
 
123abc123abc: http://changeset_home/?repo_name=repo_org&amp;revision=123abc123abc123abc123abc123abc123abc123abc
 
Introduce one and two
 

	
 
and that's it
 

	
 
567fed567fed: http://changeset_home/?repo_name=repo_org&amp;revision=567fed567fed567fed567fed567fed567fed567fed
 
Make one plus two equal tree
 

	
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Requesting User (root) requested your review of repo/name pull request &#34;The Title&#34;</p>
 

	
 
<p>URL: <a href="http://pr.org/7">http://pr.org/7</a></p>
 

	
 
<p>Description:</p>
 
<p style="white-space: pre-wrap; font-family: monospace"><div class="formatted-fixed">This PR is awesome because it does stuff
 
 - please approve indented!</div></p>
 

	
 
<p>Changesets:</p>
 
<p style="white-space: pre-wrap">
 
<i><a href="http://changeset_home/?repo_name=repo_org&amp;revision=123abc123abc123abc123abc123abc123abc123abc">123abc123abc</a></i>:
 
Introduce one and two
 

	
 
and that&#39;s it
 

	
 
<i><a href="http://changeset_home/?repo_name=repo_org&amp;revision=567fed567fed567fed567fed567fed567fed567fed">567fed567fed</a></i>:
 
Make one plus two equal tree
 

	
 
</p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request, is_mention=True</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Added] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Review] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Requesting User (root) mentioned you on repo/name pull request "The Title"
 

	
 
URL: http://pr.org/7
 

	
 
Description:
 
This PR is awesome because it does stuff
 
 - please approve indented!
 

	
 
Changesets:
 
123abc123abc: http://changeset_home/?repo_name=repo_org&amp;revision=123abc123abc123abc123abc123abc123abc123abc
 
Introduce one and two
 

	
 
and that's it
 

	
 
567fed567fed: http://changeset_home/?repo_name=repo_org&amp;revision=567fed567fed567fed567fed567fed567fed567fed
 
Make one plus two equal tree
 

	
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Requesting User (root) mentioned you on repo/name pull request &#34;The Title&#34;</p>
 

	
 
<p>URL: <a href="http://pr.org/7">http://pr.org/7</a></p>
 

	
 
<p>Description:</p>
 
<p style="white-space: pre-wrap; font-family: monospace"><div class="formatted-fixed">This PR is awesome because it does stuff
 
 - please approve indented!</div></p>
 

	
 
<p>Changesets:</p>
 
<p style="white-space: pre-wrap">
 
<i><a href="http://changeset_home/?repo_name=repo_org&amp;revision=123abc123abc123abc123abc123abc123abc123abc">123abc123abc</a></i>:
 
Introduce one and two
 

	
 
and that&#39;s it
 

	
 
<i><a href="http://changeset_home/?repo_name=repo_org&amp;revision=567fed567fed567fed567fed567fed567fed567fed">567fed567fed</a></i>:
 
Make one plus two equal tree
 

	
 
</p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=False, status_change=None, closing_pr=False</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=True, status_change=None, closing_pr=False</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
--
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=False, status_change='Under Review', closing_pr=False</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Under Review: Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Under Review: Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 
The comment was made with status: Under Review
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 
       <p>The comment was made with status: <b>Under Review</b></p>
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=True, status_change='Under Review', closing_pr=False</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Under Review: Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Under Review: Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 
The comment was made with status: Under Review
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
--
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 
       <p>The comment was made with status: <b>Under Review</b></p>
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=False, status_change=None, closing_pr=True</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Closing: Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=True, status_change=None, closing_pr=True</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Closing: Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
--
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=False, status_change='Under Review', closing_pr=True</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Under Review, Closing: Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Under Review, Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 
The comment closed the pull request with status: Under Review
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
--
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 
       <p>The comment closed the pull request with status: <b>Under Review</b></p>
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>pull_request_comment, is_mention=True, status_change='Under Review', closing_pr=True</h1>
 
<pre>
 

	
 
From: u1
 
To: u2@example.com
 
Subject: [Under Review, Closing: Comment] repo/name pull request #7 "The Title" from devbranch
 
Subject: [Under Review, Closing: Comment] repo/name PR #7 "The Title" from devbranch by u2
 

	
 
--------------------
 

	
 

	
 
Comment from Opinionated User (jsmith) on repo/name pull request "The Title":
 
Me too!
 

	
 
 - and indented on second line
 

	
 
The comment closed the pull request with status: Under Review
 

	
 
URL: http://pr.org/comment
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<p>Comment from Opinionated User (jsmith) on repo/name pull request &#34;The Title&#34;:</p>
 
<p><div class="formatted-fixed">Me too!
 

	
 
 - and indented on second line</div></p>
 

	
 
       <p>The comment closed the pull request with status: <b>Under Review</b></p>
 

	
 
<p>URL: <a href="http://pr.org/comment">http://pr.org/comment</a></p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 

	
 
<h1>TYPE_PASSWORD_RESET</h1>
 
<pre>
 

	
 
From: u1
 
To: john@doe.com
 
Subject: Password reset link
 

	
 
--------------------
 

	
 

	
 
Hello John Doe
 

	
 
We have received a request to reset the password for your account.
 
To set a new password, click the following link:
 

	
 
http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746
 

	
 
Should you not be able to use the link above, please type the following code into the password reset form: decbf64715098db5b0bd23eab44bd792670ab746
 

	
 
If it weren't you who requested the password reset, just disregard this message.
 

	
 

	
 
-- 
 
This is an automatic notification. Don't reply to this mail.
 

	
 
--------------------</pre>
 

	
 

	
 

	
 
<h4>Hello John Doe</h4>
 

	
 
<p>We have received a request to reset the password for your account.</p>
 
<p>To set a new password, click the following link:</p>
 
<p><a href="http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746">http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746</a></p>
 

	
 
<p>Should you not be able to use the link above, please type the following code into the password reset form: <code>decbf64715098db5b0bd23eab44bd792670ab746</code></p>
 

	
 
<p>If it weren&#39;t you who requested the password reset, just disregard this message.</p>
 

	
 

	
 
<br/>
 
<br/>
 
-- <br/>
 
This is an automatic notification. Don&#39;t reply to this mail.
 

	
 
<pre>--------------------</pre>
 

	
 
</body></html>
kallithea/tests/models/test_notifications.py
Show inline comments
 
@@ -3,278 +3,279 @@ import os
 
import mock
 
import routes.util
 

	
 
from kallithea.tests import *
 
from kallithea.lib import helpers as h
 
from kallithea.model.db import User, Notification, UserNotification
 
from kallithea.model.user import UserModel
 
from kallithea.model.meta import Session
 
from kallithea.model.notification import NotificationModel, EmailNotificationModel
 

	
 
import kallithea.lib.celerylib
 
import kallithea.lib.celerylib.tasks
 

	
 

	
 
class TestNotifications(TestController):
 

	
 
    def setup_method(self, method):
 
        Session.remove()
 
        u1 = UserModel().create_or_update(username=u'u1',
 
                                        password=u'qweqwe',
 
                                        email=u'u1@example.com',
 
                                        firstname=u'u1', lastname=u'u1')
 
        Session().commit()
 
        self.u1 = u1.user_id
 

	
 
        u2 = UserModel().create_or_update(username=u'u2',
 
                                        password=u'qweqwe',
 
                                        email=u'u2@example.com',
 
                                        firstname=u'u2', lastname=u'u3')
 
        Session().commit()
 
        self.u2 = u2.user_id
 

	
 
        u3 = UserModel().create_or_update(username=u'u3',
 
                                        password=u'qweqwe',
 
                                        email=u'u3@example.com',
 
                                        firstname=u'u3', lastname=u'u3')
 
        Session().commit()
 
        self.u3 = u3.user_id
 

	
 
        self.remove_all_notifications()
 
        assert [] == Notification.query().all()
 
        assert [] == UserNotification.query().all()
 

	
 
    def test_create_notification(self):
 
        usrs = [self.u1, self.u2]
 
        def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
            assert recipients == ['u2@example.com']
 
            assert subject == 'Test Message'
 
            assert body == "\n\nhi there\n\n\n-- \nThis is an automatic notification. Don't reply to this mail.\n"
 
            assert '>hi there<' in html_body
 
            assert author.username == 'u1'
 
        with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
            notification = NotificationModel().create(created_by=self.u1,
 
                                               subject=u'subj', body=u'hi there',
 
                                               recipients=usrs)
 
        Session().commit()
 
        u1 = User.get(self.u1)
 
        u2 = User.get(self.u2)
 
        u3 = User.get(self.u3)
 
        notifications = Notification.query().all()
 
        assert len(notifications) == 1
 

	
 
        assert notifications[0].recipients == [u1, u2]
 
        assert notification.notification_id == notifications[0].notification_id
 

	
 
        unotification = UserNotification.query() \
 
            .filter(UserNotification.notification == notification).all()
 

	
 
        assert len(unotification) == len(usrs)
 
        assert set([x.user.user_id for x in unotification]) == set(usrs)
 

	
 
    def test_user_notifications(self):
 
        notification1 = NotificationModel().create(created_by=self.u1,
 
                                            subject=u'subj', body=u'hi there1',
 
                                            recipients=[self.u3])
 
        Session().commit()
 
        notification2 = NotificationModel().create(created_by=self.u1,
 
                                            subject=u'subj', body=u'hi there2',
 
                                            recipients=[self.u3])
 
        Session().commit()
 
        u3 = Session().query(User).get(self.u3)
 

	
 
        assert sorted([x.notification for x in u3.notifications]) == sorted([notification2, notification1])
 

	
 
    def test_delete_notifications(self):
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 
        notifications = Notification.query().all()
 
        assert notification in notifications
 

	
 
        Notification.delete(notification.notification_id)
 
        Session().commit()
 

	
 
        notifications = Notification.query().all()
 
        assert not notification in notifications
 

	
 
        un = UserNotification.query().filter(UserNotification.notification
 
                                             == notification).all()
 
        assert un == []
 

	
 
    def test_delete_association(self):
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 

	
 
        unotification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u3) \
 
                            .scalar()
 

	
 
        assert unotification.user_id == self.u3
 

	
 
        NotificationModel().delete(self.u3,
 
                                   notification.notification_id)
 
        Session().commit()
 

	
 
        u3notification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u3) \
 
                            .scalar()
 

	
 
        assert u3notification == None
 

	
 
        # notification object is still there
 
        assert Notification.query().all() == [notification]
 

	
 
        #u1 and u2 still have assignments
 
        u1notification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u1) \
 
                            .scalar()
 
        assert u1notification != None
 
        u2notification = UserNotification.query() \
 
                            .filter(UserNotification.notification ==
 
                                    notification) \
 
                            .filter(UserNotification.user_id == self.u2) \
 
                            .scalar()
 
        assert u2notification != None
 

	
 
    def test_notification_counter(self):
 
        NotificationModel().create(created_by=self.u1,
 
                            subject=u'title', body=u'hi there_delete',
 
                            recipients=[self.u3, self.u1])
 
        Session().commit()
 

	
 
        assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0
 
        assert NotificationModel().get_unread_cnt_for_user(self.u2) == 0
 
        assert NotificationModel().get_unread_cnt_for_user(self.u3) == 1
 

	
 
        notification = NotificationModel().create(created_by=self.u1,
 
                                           subject=u'title', body=u'hi there3',
 
                                    recipients=[self.u3, self.u1, self.u2])
 
        Session().commit()
 

	
 
        assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0
 
        assert NotificationModel().get_unread_cnt_for_user(self.u2) == 1
 
        assert NotificationModel().get_unread_cnt_for_user(self.u3) == 2
 

	
 
    @mock.patch.object(h, 'canonical_url', (lambda arg, **kwargs: 'http://%s/?%s' % (arg, '&'.join('%s=%s' % (k, v) for (k, v) in sorted(kwargs.items())))))
 
    def test_dump_html_mails(self):
 
        # Exercise all notification types and dump them to one big html file
 
        l = []
 

	
 
        def send_email(recipients, subject, body='', html_body='', headers=None, author=None):
 
            l.append('\n\n<h1>%s</h1>\n' % desc) # desc is from outer scope
 
            l.append('<pre>\n\n')
 
            l.append('From: %s\n' % author.username)
 
            l.append('To: %s\n' % ' '.join(recipients))
 
            l.append('Subject: %s\n' % subject)
 
            l.append('\n--------------------\n%s\n--------------------' % body)
 
            l.append('</pre>\n')
 
            l.append('\n%s\n' % html_body)
 
            l.append('<pre>--------------------</pre>\n')
 

	
 
        l.append('<html><body>\n')
 
        with mock.patch.object(kallithea.lib.celerylib.tasks, 'send_email', send_email):
 
            pr_kwargs = dict(
 
                pr_nice_id='#7',
 
                pr_title='The Title',
 
                pr_title_short='The Title',
 
                pr_url='http://pr.org/7',
 
                pr_target_repo='http://mainline.com/repo',
 
                pr_target_branch='trunk',
 
                pr_source_repo='https://dev.org/repo',
 
                pr_source_branch='devbranch',
 
                pr_owner=User.get(self.u2),
 
                pr_owner_username='u2'
 
                )
 

	
 
            for type_, body, kwargs in [
 
                (Notification.TYPE_CHANGESET_COMMENT,
 
                 u'This is the new comment.\n\n - and here it ends indented.',
 
                 dict(
 
                    short_id='cafe1234',
 
                    raw_id='cafe1234c0ffeecafe',
 
                    branch='brunch',
 
                    cs_comment_user='Opinionated User (jsmith)',
 
                    cs_comment_url='http://comment.org',
 
                    is_mention=[False, True],
 
                    message='This changeset did something clever which is hard to explain',
 
                    message_short='This changeset did something cl...',
 
                    status_change=[None, 'Approved'],
 
                    cs_target_repo='repo_target',
 
                    cs_url='http://changeset.com',
 
                    cs_author=User.get(self.u2))),
 
                (Notification.TYPE_MESSAGE,
 
                 u'This is the body of the test message\n - nothing interesting here except indentation.',
 
                 dict()),
 
                #(Notification.TYPE_MENTION, '$body', None), # not used
 
                (Notification.TYPE_REGISTRATION,
 
                 u'Registration body',
 
                 dict(
 
                    new_username='newbie',
 
                    registered_user_url='http://newbie.org',
 
                    new_email='new@email.com',
 
                    new_full_name='New Full Name')),
 
                (Notification.TYPE_PULL_REQUEST,
 
                 u'This PR is awesome because it does stuff\n - please approve indented!',
 
                 dict(
 
                    pr_user_created='Requesting User (root)', # pr_owner should perhaps be used for @mention in description ...
 
                    is_mention=[False, True],
 
                    pr_revisions=[('123abc'*7, "Introduce one and two\n\nand that's it"), ('567fed'*7, 'Make one plus two equal tree')],
 
                    org_repo_name='repo_org',
 
                    **pr_kwargs)),
 
                (Notification.TYPE_PULL_REQUEST_COMMENT,
 
                 u'Me too!\n\n - and indented on second line',
 
                 dict(
 
                    closing_pr=[False, True],
 
                    is_mention=[False, True],
 
                    pr_comment_user='Opinionated User (jsmith)',
 
                    pr_comment_url='http://pr.org/comment',
 
                    status_change=[None, 'Under Review'],
 
                    **pr_kwargs)),
 
                ]:
 
                kwargs['repo_name'] = u'repo/name'
 
                params = [(type_, type_, body, kwargs)]
 
                for param_name in ['is_mention', 'status_change', 'closing_pr']: # TODO: inline/general
 
                    if not isinstance(kwargs.get(param_name), list):
 
                        continue
 
                    new_params = []
 
                    for v in kwargs[param_name]:
 
                        for desc, type_, body, kwargs in params:
 
                            kwargs = dict(kwargs)
 
                            kwargs[param_name] = v
 
                            new_params.append(('%s, %s=%r' % (desc, param_name, v), type_, body, kwargs))
 
                    params = new_params
 

	
 
                for desc, type_, body, kwargs in params:
 
                    # desc is used as "global" variable
 
                    notification = NotificationModel().create(created_by=self.u1,
 
                                                       subject=u'unused', body=body, email_kwargs=kwargs,
 
                                                       recipients=[self.u2], type_=type_)
 

	
 
            # Email type TYPE_PASSWORD_RESET has no corresponding notification type - test it directly:
 
            desc = 'TYPE_PASSWORD_RESET'
 
            kwargs = dict(user='John Doe', reset_token='decbf64715098db5b0bd23eab44bd792670ab746', reset_url='http://reset.com/decbf64715098db5b0bd23eab44bd792670ab746')
 
            kallithea.lib.celerylib.run_task(kallithea.lib.celerylib.tasks.send_email, ['john@doe.com'],
 
                "Password reset link",
 
                EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'txt', **kwargs),
 
                EmailNotificationModel().get_email_tmpl(EmailNotificationModel.TYPE_PASSWORD_RESET, 'html', **kwargs),
 
                author=User.get(self.u1))
 

	
 
        l.append('\n</body></html>\n')
 
        out = ''.join(l)
 

	
 
        outfn = os.path.join(os.path.dirname(__file__), 'test_dump_html_mails.out.html')
 
        reffn = os.path.join(os.path.dirname(__file__), 'test_dump_html_mails.ref.html')
 
        with file(outfn, 'w') as f:
 
            f.write(out)
 
        with file(reffn) as f:
 
            ref = f.read()
 
        assert ref == out # copy test_dump_html_mails.out.html to test_dump_html_mails.ref.html to update expectations
 
        os.unlink(outfn)
0 comments (0 inline, 0 general)