コード例 #1
0
ファイル: comment.py プロジェクト: adamscieszko/rhodecode
    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 new comment for changeset or pull request.
        IF status_change is not none this comment is associated with a
        status change of changeset or changesets 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

        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:
            comment.revision = revision
        elif pull_request:
            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, 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_subject=email_subject
            )

            mention_recipients = set(self._extract_mentions(body))\
                                    .difference(recipients)
            if mention_recipients:
                email_kwargs.update({'pr_mention': True})
                subj = _('[Mention]') + ' ' + subj
                NotificationModel().create(
                    created_by=user, subject=subj, body=body,
                    recipients=mention_recipients,
                    type_=notification_type,
                    email_kwargs=email_kwargs
                )

        return comment
コード例 #2
0
ファイル: comment.py プロジェクト: break123/rhodecode
    def create(self, text, repo, user, revision=None, pull_request=None,
               f_path=None, line_no=None, status_change=None):
        """
        Creates new comment for changeset or pull request.
        IF status_change is not none this comment is associated with a
        status change of changeset or changesets associated with pull request

        :param text:
        :param repo:
        :param user:
        :param revision:
        :param pull_request:
        :param f_path:
        :param line_no:
        :param status_change:
        """
        if not text:
            return

        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:
            cs = repo.scm_instance.get_changeset(revision)
            desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256))
            author_email = cs.author_email
            comment.revision = revision
        elif pull_request:
            pull_request = self.__get_pull_request(pull_request)
            comment.pull_request = pull_request
            desc = pull_request.pull_request_id
        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
            recipients += [User.get_by_email(author_email)]
            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: %(desc)s %(line)s' % \
                          {'desc': desc, '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)
            }
        # 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 = set(self._extract_mentions(body))\
                                .difference(recipients)
        if mention_recipients:
            email_kwargs.update({'pr_mention': True})
            subj = _('[Mention]') + ' ' + subj
            NotificationModel().create(
                created_by=user, subject=subj, body=body,
                recipients=mention_recipients,
                type_=notification_type,
                email_kwargs=email_kwargs
            )

        return comment
コード例 #3
0
    def create(self,
               text,
               repo_id,
               user_id,
               revision,
               f_path=None,
               line_no=None):
        """
        Creates new comment for changeset

        :param text:
        :param repo_id:
        :param user_id:
        :param revision:
        :param f_path:
        :param line_no:
        """

        if text:
            repo = Repository.get(repo_id)
            cs = repo.scm_instance.get_changeset(revision)
            desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256))
            author_email = cs.author_email
            comment = ChangesetComment()
            comment.repo = repo
            comment.user_id = user_id
            comment.revision = revision
            comment.text = text
            comment.f_path = f_path
            comment.line_no = line_no

            self.sa.add(comment)
            self.sa.flush()
            # make notification
            line = ''
            if line_no:
                line = _('on line %s') % line_no
            subj = safe_unicode(
                h.link_to('Re commit: %(commit_desc)s %(line)s' % \
                          {'commit_desc': desc, 'line': line},
                          h.url('changeset_home', repo_name=repo.repo_name,
                                revision=revision,
                                anchor='comment-%s' % comment.comment_id,
                                qualified=True,
                          )
                )
            )

            body = text

            # get the current participants of this changeset
            recipients = ChangesetComment.get_users(revision=revision)

            # add changeset author if it's in rhodecode system
            recipients += [User.get_by_email(author_email)]

            NotificationModel().create(
                created_by=user_id,
                subject=subj,
                body=body,
                recipients=recipients,
                type_=Notification.TYPE_CHANGESET_COMMENT)

            mention_recipients = set(self._extract_mentions(body))\
                                    .difference(recipients)
            if mention_recipients:
                subj = _('[Mention]') + ' ' + subj
                NotificationModel().create(
                    created_by=user_id,
                    subject=subj,
                    body=body,
                    recipients=mention_recipients,
                    type_=Notification.TYPE_CHANGESET_COMMENT)

            return comment
コード例 #4
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):
        """
        Creates new comment for changeset or pull request.
        IF status_change is not none this comment is associated with a
        status change of changeset or changesets 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

        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:
            comment.revision = revision
        elif pull_request:
            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,
             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_subject=email_subject)

            mention_recipients = set(self._extract_mentions(body))\
                                    .difference(recipients)
            if mention_recipients:
                email_kwargs.update({'pr_mention': True})
                subj = _('[Mention]') + ' ' + subj
                NotificationModel().create(created_by=user,
                                           subject=subj,
                                           body=body,
                                           recipients=mention_recipients,
                                           type_=notification_type,
                                           email_kwargs=email_kwargs)

        return comment
コード例 #5
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
コード例 #6
0
ファイル: comment.py プロジェクト: elfixit/rhodecode
    def create(self, text, repo_id, user_id, revision, f_path=None,
               line_no=None):
        """
        Creates new comment for changeset

        :param text:
        :param repo_id:
        :param user_id:
        :param revision:
        :param f_path:
        :param line_no:
        """

        if text:
            repo = Repository.get(repo_id)
            cs = repo.scm_instance.get_changeset(revision)
            desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256))
            author_email = cs.author_email
            comment = ChangesetComment()
            comment.repo = repo
            comment.user_id = user_id
            comment.revision = revision
            comment.text = text
            comment.f_path = f_path
            comment.line_no = line_no

            self.sa.add(comment)
            self.sa.flush()
            # make notification
            line = ''
            if line_no:
                line = _('on line %s') % line_no
            subj = safe_unicode(
                h.link_to('Re commit: %(commit_desc)s %(line)s' % \
                          {'commit_desc': desc, 'line': line},
                          h.url('changeset_home', repo_name=repo.repo_name,
                                revision=revision,
                                anchor='comment-%s' % comment.comment_id,
                                qualified=True,
                          )
                )
            )

            body = text

            # get the current participants of this changeset
            recipients = ChangesetComment.get_users(revision=revision)

            # add changeset author if it's in rhodecode system
            recipients += [User.get_by_email(author_email)]

            NotificationModel().create(
              created_by=user_id, subject=subj, body=body,
              recipients=recipients, type_=Notification.TYPE_CHANGESET_COMMENT
            )

            mention_recipients = set(self._extract_mentions(body))\
                                    .difference(recipients)
            if mention_recipients:
                subj = _('[Mention]') + ' ' + subj
                NotificationModel().create(
                    created_by=user_id, subject=subj, body=body,
                    recipients=mention_recipients,
                    type_=Notification.TYPE_CHANGESET_COMMENT
                )

            return comment