changeset 4404:231442b0b6aa

pull requests: handle @mention in PR description ... but so far without fancy js completion
author Mads Kiilerich <madski@unity3d.com>
date Fri, 01 Aug 2014 20:28:42 +0200
parents d9948f5ac7af
children 4fa8656ab14d
files kallithea/controllers/pullrequests.py kallithea/model/comment.py kallithea/model/pull_request.py kallithea/templates/email_templates/changeset_comment.html kallithea/templates/email_templates/pull_request.html
diffstat 5 files changed, 48 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/pullrequests.py	Tue Dec 10 19:30:37 2013 +0100
+++ b/kallithea/controllers/pullrequests.py	Fri Aug 01 20:28:42 2014 +0200
@@ -555,12 +555,15 @@
     def post(self, repo_name, pull_request_id):
         repo = RepoModel()._get_repo(repo_name)
         pull_request = PullRequest.get_or_404(pull_request_id)
+        old_description = pull_request.description
 
         _form = PullRequestPostForm()().to_python(request.POST)
 
         pull_request.title = _form['pullrequest_title']
         pull_request.description = _form['pullrequest_desc'].strip() or _('No description')
 
+        PullRequestModel().mention_from_description(pull_request, old_description)
+
         Session().commit()
         h.flash(_('Pull request updated'), category='success')
 
--- a/kallithea/model/comment.py	Tue Dec 10 19:30:37 2013 +0100
+++ b/kallithea/model/comment.py	Fri Aug 01 20:28:42 2014 +0200
@@ -222,6 +222,7 @@
                                 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,
@@ -232,7 +233,7 @@
             mention_recipients = set(self._extract_mentions(body))\
                                     .difference(recipients)
             if mention_recipients:
-                email_kwargs.update({'pr_mention': True})
+                email_kwargs['is_mention'] = True
                 subj = _('[Mention]') + ' ' + subj
                 NotificationModel().create(
                     created_by=user, subject=subj, body=body,
--- a/kallithea/model/pull_request.py	Tue Dec 10 19:30:37 2013 +0100
+++ b/kallithea/model/pull_request.py	Fri Aug 01 20:28:42 2014 +0200
@@ -34,9 +34,9 @@
 from kallithea.lib import helpers as h
 from kallithea.model import BaseModel
 from kallithea.model.db import PullRequest, PullRequestReviewers, Notification,\
-    ChangesetStatus
+    ChangesetStatus, User
 from kallithea.model.notification import NotificationModel
-from kallithea.lib.utils2 import safe_unicode
+from kallithea.lib.utils2 import extract_mentioned_users, safe_unicode
 
 
 log = logging.getLogger(__name__)
@@ -106,10 +106,14 @@
             comment,
             pull_request=new
         )
-        self.__add_reviewers(new, reviewers)
+
+        mention_recipients = set(User.get_by_username(username, case_insensitive=True)
+                                 for username in extract_mentioned_users(pr.description))
+        self.__add_reviewers(new, reviewers, mention_recipients)
+
         return new
 
-    def __add_reviewers(self, pr, reviewers):
+    def __add_reviewers(self, pr, reviewers, mention_recipients=None):
         #members
         for member in set(reviewers):
             _usr = self._get_user(member)
@@ -145,11 +149,34 @@
             'ref': org_ref_name,
             'pr_username': pr.author.username,
             'threading': [pr_url],
+            'is_mention': False,
             }
-        NotificationModel().create(created_by=pr.author, subject=subject, body=body,
-                                   recipients=reviewers,
-                                   type_=Notification.TYPE_PULL_REQUEST,
-                                   email_kwargs=email_kwargs)
+        if reviewers:
+            NotificationModel().create(created_by=pr.author, subject=subject, body=body,
+                                       recipients=reviewers,
+                                       type_=Notification.TYPE_PULL_REQUEST,
+                                       email_kwargs=email_kwargs)
+
+        if mention_recipients:
+            mention_recipients.discard(None)
+            mention_recipients.difference_update(reviewers)
+        if mention_recipients:
+            email_kwargs['is_mention'] = True
+            subject = _('[Mention]') + ' ' + subject
+
+            NotificationModel().create(created_by=pr.author, subject=subject, body=body,
+                                       recipients=mention_recipients,
+                                       type_=Notification.TYPE_PULL_REQUEST,
+                                       email_kwargs=email_kwargs)
+
+    def mention_from_description(self, pr, old_description=''):
+        mention_recipients = set(User.get_by_username(username, case_insensitive=True)
+                                 for username in extract_mentioned_users(pr.description))
+        mention_recipients.difference_update(User.get_by_username(username, case_insensitive=True)
+                                             for username in extract_mentioned_users(old_description))
+
+        log.debug("Mentioning %s" % mention_recipients)
+        self.__add_reviewers(pr, [], mention_recipients)
 
     def update_reviewers(self, pull_request, reviewers_ids):
         reviewers_ids = set(reviewers_ids)
--- a/kallithea/templates/email_templates/changeset_comment.html	Tue Dec 10 19:30:37 2013 +0100
+++ b/kallithea/templates/email_templates/changeset_comment.html	Fri Aug 01 20:28:42 2014 +0200
@@ -1,7 +1,11 @@
 ## -*- coding: utf-8 -*-
 <%inherit file="main.html"/>
 
+%if is_mention:
+<p>${_('Comment from %s on %s changeset %s mentioned you') % (cs_comment_user, cs_target_repo, h.short_id(raw_id))}:</p>
+%else:
 <p>${_('Comment from %s on %s changeset %s') % (cs_comment_user, cs_target_repo, h.short_id(raw_id))}:</p>
+%endif
 <p>${body}</p>
 
 %if status_change:
--- a/kallithea/templates/email_templates/pull_request.html	Tue Dec 10 19:30:37 2013 +0100
+++ b/kallithea/templates/email_templates/pull_request.html	Fri Aug 01 20:28:42 2014 +0200
@@ -1,7 +1,11 @@
 ## -*- coding: utf-8 -*-
 <%inherit file="main.html"/>
 
+%if is_mention:
+<p>${_('%s mentioned you on %s pull request "%s"') % (pr_user_created, repo_name, pr_title)}</p>
+%else:
 <p>${_('%s requested your review of %s pull request "%s"') % (pr_user_created, repo_name, pr_title)}</p>
+%endif
 
 <p>${_('URL')}: <a href="${pr_url}">${pr_url}</a></p>