Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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 = '%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 = '%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_title': pull_request.title,
                '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
Ejemplo n.º 3
0
    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,
               renderer=None):
        """
        Creates new comment for commit or pull request.
        IF status_change is not none this comment is associated with a
        status change of commit or commit associated with pull request

        :param text:
        :param repo:
        :param user:
        :param revision:
        :param pull_request:
        :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

        if not renderer:
            renderer = self._get_renderer()

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

        #TODO (marcink): fix this and remove revision as param
        commit_id = revision
        pull_request_id = pull_request

        commit_obj = None
        pull_request_obj = None

        if commit_id:
            notification_type = EmailNotificationModel.TYPE_COMMIT_COMMENT
            # do a lookup, so we don't pass something bad here
            commit_obj = repo.scm_instance().get_commit(commit_id=commit_id)
            comment.revision = commit_obj.raw_id

        elif pull_request_id:
            notification_type = EmailNotificationModel.TYPE_PULL_REQUEST_COMMENT
            pull_request_obj = self.__get_pull_request(pull_request_id)
            comment.pull_request = pull_request_obj
        else:
            raise Exception('Please specify commit or pull_request_id')

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

        if send_email:
            kwargs = {
                'user': user,
                'renderer_type': renderer,
                'repo_name': repo.repo_name,
                'status_change': status_change,
                'comment_body': text,
                'comment_file': f_path,
                'comment_line': line_no,
            }

            if commit_obj:
                recipients = ChangesetComment.get_users(
                    revision=commit_obj.raw_id)
                # add commit author if it's in RhodeCode system
                cs_author = User.get_from_cs_author(commit_obj.author)
                if not cs_author:
                    # use repo owner if we cannot extract the author correctly
                    cs_author = repo.user
                recipients += [cs_author]

                commit_comment_url = h.url(
                    'changeset_home',
                    repo_name=repo.repo_name,
                    revision=commit_obj.raw_id,
                    anchor='comment-%s' % comment.comment_id,
                    qualified=True,
                )

                target_repo_url = h.link_to(
                    repo.repo_name,
                    h.url('summary_home',
                          repo_name=repo.repo_name,
                          qualified=True))

                # commit specifics
                kwargs.update({
                    'commit': commit_obj,
                    'commit_message': commit_obj.message,
                    'commit_target_repo': target_repo_url,
                    'commit_comment_url': commit_comment_url,
                })

            elif pull_request_obj:
                # get the current participants of this pull request
                recipients = ChangesetComment.get_users(
                    pull_request_id=pull_request_obj.pull_request_id)
                # add pull request author
                recipients += [pull_request_obj.author]

                # add the reviewers to notification
                recipients += [x.user for x in pull_request_obj.reviewers]

                pr_target_repo = pull_request_obj.target_repo
                pr_source_repo = pull_request_obj.source_repo

                pr_comment_url = h.url(
                    'pullrequest_show',
                    repo_name=pr_target_repo.repo_name,
                    pull_request_id=pull_request_obj.pull_request_id,
                    anchor='comment-%s' % comment.comment_id,
                    qualified=True,
                )

                # set some variables for email notification
                pr_target_repo_url = h.url('summary_home',
                                           repo_name=pr_target_repo.repo_name,
                                           qualified=True)

                pr_source_repo_url = h.url('summary_home',
                                           repo_name=pr_source_repo.repo_name,
                                           qualified=True)

                # pull request specifics
                kwargs.update({
                    'pull_request': pull_request_obj,
                    'pr_id': pull_request_obj.pull_request_id,
                    'pr_target_repo': pr_target_repo,
                    'pr_target_repo_url': pr_target_repo_url,
                    'pr_source_repo': pr_source_repo,
                    'pr_source_repo_url': pr_source_repo_url,
                    'pr_comment_url': pr_comment_url,
                    'pr_closing': closing_pr,
                })

            # pre-generate the subject for notification itself
            (
                subject,
                _h,
                _e,  # we don't care about those
                body_plaintext) = EmailNotificationModel().render_email(
                    notification_type, **kwargs)

            mention_recipients = set(
                self._extract_mentions(text)).difference(recipients)

            # create notification objects, and emails
            NotificationModel().create(
                created_by=user,
                notification_subject=subject,
                notification_body=body_plaintext,
                notification_type=notification_type,
                recipients=recipients,
                mention_recipients=mention_recipients,
                email_kwargs=kwargs,
            )

        action = ('user_commented_pull_request:{}'.format(
            comment.pull_request.pull_request_id) if comment.pull_request else
                  'user_commented_revision:{}'.format(comment.revision))
        action_logger(user, action, comment.repo)

        return comment