diff --git a/rhodecode/model/comment.py b/rhodecode/model/comment.py --- a/rhodecode/model/comment.py +++ b/rhodecode/model/comment.py @@ -35,6 +35,7 @@ from rhodecode.model import BaseModel from rhodecode.model.db import ChangesetComment, User, Repository, \ Notification, PullRequest from rhodecode.model.notification import NotificationModel +from rhodecode.model.meta import Session log = logging.getLogger(__name__) @@ -57,8 +58,103 @@ class ChangesetCommentsModel(BaseModel): user_objects.append(user_obj) return user_objects + def _get_notification_data(self, repo, comment, user, comment_text, + line_no=None, revision=None, pull_request=None, + status_change=None, closing_pr=False): + """ + Get notification data + + :param comment_text: + :param line: + :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 = "%s" % (cs.short_id) + + _url = h.url('changeset_home', + repo_name=repo.repo_name, + revision=revision, + anchor='comment-%s' % comment.comment_id, + qualified=True, + ) + subj = safe_unicode( + h.link_to('Re changeset: %(desc)s %(line)s' % \ + {'desc': desc, 'line': line}, + _url) + ) + email_subject = 'User %s commented on changeset %s' % \ + (user.username, h.short_id(revision)) + # get the current participants of this changeset + recipients = ChangesetComment.get_users(revision=revision) + # add changeset author if it's in rhodecode system + cs_author = User.get_from_cs_author(cs.author) + if not cs_author: + #use repo owner if we cannot extract the author correctly + cs_author = repo.user + recipients += [cs_author] + email_kwargs = { + 'status_change': status_change, + 'cs_comment_user': h.person(user.email), + 'cs_target_repo': h.url('summary_home', repo_name=repo.repo_name, + qualified=True), + 'cs_comment_url': _url, + 'raw_id': revision, + 'message': cs.message + } + #pull request + elif pull_request: + notification_type = Notification.TYPE_PULL_REQUEST_COMMENT + desc = comment.pull_request.title + _url = h.url('pullrequest_show', + repo_name=pull_request.other_repo.repo_name, + pull_request_id=pull_request.pull_request_id, + anchor='comment-%s' % comment.comment_id, + qualified=True, + ) + subj = safe_unicode( + h.link_to('Re pull request #%(pr_id)s: %(desc)s %(line)s' % \ + {'desc': desc, + 'pr_id': comment.pull_request.pull_request_id, + 'line': line}, + _url) + ) + email_subject = 'User %s commented on pull request #%s' % \ + (user.username, comment.pull_request.pull_request_id) + # 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.author] + + # add the reviewers to notification + recipients += [x.user for x in pull_request.reviewers] + + #set some variables for email notification + email_kwargs = { + 'pr_id': pull_request.pull_request_id, + 'status_change': status_change, + 'closing_pr': closing_pr, + 'pr_comment_url': _url, + 'pr_comment_user': h.person(user.email), + 'pr_target_repo': h.url('summary_home', + repo_name=pull_request.other_repo.repo_name, + qualified=True) + } + + return subj, body, recipients, notification_type, email_kwargs, email_subject + def create(self, text, repo, user, revision=None, pull_request=None, - f_path=None, line_no=None, status_change=None, send_email=True): + f_path=None, line_no=None, status_change=None, closing_pr=False, + send_email=True): """ Creates new comment for changeset or pull request. IF status_change is not none this comment is associated with a @@ -72,9 +168,11 @@ class ChangesetCommentsModel(BaseModel): :param f_path: :param line_no: :param status_change: + :param closing_pr: :param send_email: """ if not text: + log.warning('Missing text for comment, skipping...') return repo = self._get_repo(repo) @@ -87,8 +185,6 @@ class ChangesetCommentsModel(BaseModel): comment.line_no = line_no if revision: - cs = repo.scm_instance.get_changeset(revision) - desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256)) comment.revision = revision elif pull_request: pull_request = self.__get_pull_request(pull_request) @@ -96,82 +192,24 @@ class ChangesetCommentsModel(BaseModel): else: raise Exception('Please specify revision or pull_request_id') - self.sa.add(comment) - self.sa.flush() - - # make notification - line = '' - body = text - - #changeset - if revision: - if line_no: - line = _('on line %s') % line_no - subj = safe_unicode( - h.link_to('Re commit: %(desc)s %(line)s' % \ - {'desc': desc, 'line': line}, - h.url('changeset_home', repo_name=repo.repo_name, - revision=revision, - anchor='comment-%s' % comment.comment_id, - qualified=True, - ) - ) - ) - notification_type = Notification.TYPE_CHANGESET_COMMENT - # get the current participants of this changeset - recipients = ChangesetComment.get_users(revision=revision) - # add changeset author if it's in rhodecode system - cs_author = User.get_from_cs_author(cs.author) - if not cs_author: - #use repo owner if we cannot extract the author correctly - cs_author = repo.user - recipients += [cs_author] - email_kwargs = { - 'status_change': status_change, - } - #pull request - elif pull_request: - _url = h.url('pullrequest_show', - repo_name=pull_request.other_repo.repo_name, - pull_request_id=pull_request.pull_request_id, - anchor='comment-%s' % comment.comment_id, - qualified=True, - ) - subj = safe_unicode( - h.link_to('Re pull request #%(pr_id)s: %(desc)s %(line)s' % \ - {'desc': comment.pull_request.title, - 'pr_id': comment.pull_request.pull_request_id, - 'line': line}, - _url) - ) - - notification_type = Notification.TYPE_PULL_REQUEST_COMMENT - # 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.author] - - # add the reviewers to notification - recipients += [x.user for x in pull_request.reviewers] - - #set some variables for email notification - email_kwargs = { - 'pr_id': pull_request.pull_request_id, - 'status_change': status_change, - 'pr_comment_url': _url, - 'pr_comment_user': h.person(user.email), - 'pr_target_repo': h.url('summary_home', - repo_name=pull_request.other_repo.repo_name, - qualified=True) - } + Session().add(comment) + Session().flush() if send_email: + (subj, body, recipients, notification_type, + email_kwargs, email_subject) = 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) # create notification objects, and emails NotificationModel().create( created_by=user, subject=subj, body=body, recipients=recipients, type_=notification_type, - email_kwargs=email_kwargs + email_kwargs=email_kwargs, email_subject=email_subject ) mention_recipients = set(self._extract_mentions(body))\ @@ -195,7 +233,7 @@ class ChangesetCommentsModel(BaseModel): :param comment_id: """ comment = self.__get_changeset_comment(comment) - self.sa.delete(comment) + Session().delete(comment) return comment @@ -204,11 +242,8 @@ class ChangesetCommentsModel(BaseModel): Get's main comments based on revision or pull_request_id :param repo_id: - :type repo_id: :param revision: - :type revision: :param pull_request: - :type pull_request: """ q = ChangesetComment.query()\ @@ -226,7 +261,7 @@ class ChangesetCommentsModel(BaseModel): return q.all() def get_inline_comments(self, repo_id, revision=None, pull_request=None): - q = self.sa.query(ChangesetComment)\ + q = Session().query(ChangesetComment)\ .filter(ChangesetComment.repo_id == repo_id)\ .filter(ChangesetComment.line_no != None)\ .filter(ChangesetComment.f_path != None)\