Esempio n. 1
0
    def _on_review_request_closed(self, user, review_request, type,
                                  description=None, **kwargs):
        """Handler for when review requests are closed.

        This will send a notification to any configured Slack channels when
        a review request is closed.

        Args:
            user (django.contrib.auth.models.User):
                The user who closed the review request.

            review_request (reviewboard.reviews.models.ReviewRequest):
                The review request that was closed.

            type (unicode):
                The close type.

            description (unicode):
                The close message,

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        if not user:
            user = review_request.submitter

        user_link = self.get_user_text_link(user, review_request.local_site)

        if type == ReviewRequest.DISCARDED:
            pre_text = 'Discarded by %s' % user_link
            fallback_text = 'Discarded by %s' % user_displayname(user)
        elif type == ReviewRequest.SUBMITTED:
            pre_text = 'Closed as completed by %s' % user_link
            fallback_text = 'Closed as completed by %s' % \
                user_displayname(user)
        else:
            logging.error('Slack: Tried to notify on review_request_closed '
                          'for review request pk=%d with unknown close type '
                          '"%s"',
                          review_request.pk, type)
            return

        if not user:
            user = review_request.submitter

        self.notify_review_request(review_request,
                                   fallback_text=fallback_text,
                                   body=description,
                                   pre_text=pre_text,
                                   local_site=review_request.local_site,
                                   event_name='review_request_closed')
Esempio n. 2
0
    def _on_review_request_closed(self, user, review_request, type,
                                  description=None, **kwargs):
        """Handler for when review requests are closed.

        This will send a notification to any configured channels when
        a review request is closed.

        Args:
            user (django.contrib.auth.models.User):
                The user who closed the review request.

            review_request (reviewboard.reviews.models.ReviewRequest):
                The review request that was closed.

            type (unicode):
                The close type.

            description (unicode):
                The close message,

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        if not user:
            user = review_request.submitter

        user_link = self.get_user_text_link(user, review_request.local_site)

        if type == ReviewRequest.DISCARDED:
            pre_text = 'Discarded by %s' % user_link
            fallback_text = 'Discarded by %s' % user_displayname(user)
        elif type == ReviewRequest.SUBMITTED:
            pre_text = 'Closed as completed by %s' % user_link
            fallback_text = 'Closed as completed by %s' % \
                user_displayname(user)
        else:
            logging.error('Tried to notify on review_request_closed for '
                          ' review request pk=%d with unknown close type "%s"',
                          review_request.pk, type)
            return

        if not user:
            user = review_request.submitter

        self.notify_review_request(review_request,
                                   fallback_text=fallback_text,
                                   body=description,
                                   pre_text=pre_text,
                                   local_site=review_request.local_site,
                                   event_name='review_request_closed')
Esempio n. 3
0
    def _on_reply_published(self, user, reply, **kwargs):
        """Handler for when a reply to a review is published.

        This will send a notification to any configured channels when
        a reply to a review is published.

        Args:
            user (django.contrib.auth.models.User):
                The user who published the reply.

            review (reviewboard.reviews.models.Review):
                The reply that was published.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        user_link = self.get_user_text_link(user,
                                            reply.review_request.local_site)
        pre_text = 'New reply from %s' % user_link
        fallback_text = 'New reply from %s' % user_displayname(user)

        self.notify_review_or_reply(user=user,
                                    review=reply,
                                    fallback_text=fallback_text,
                                    pre_text=pre_text,
                                    event_name='reply_published')
Esempio n. 4
0
    def _on_review_request_reopened(self, user, review_request, **kwargs):
        """Handler for when review requests are reopened.

        This will send a notification to any configured channels when
        a review request is reopened.

        Args:
            user (django.contrib.auth.models.User):
                The user who reopened the review request.

            review_request (reviewboard.reviews.models.ReviewRequest):
                The review request that was published.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        if not user:
            user = review_request.submitter

        user_link = self.get_user_text_link(user, review_request.local_site)
        pre_text = 'Reopened by %s' % user_link
        fallback_text = 'Reopened by %s' % user_displayname(user)

        self.notify_review_request(review_request,
                                   fallback_text=fallback_text,
                                   pre_text=pre_text,
                                   body=review_request.description,
                                   local_site=review_request.local_site,
                                   event_name='review_request_reopened')
Esempio n. 5
0
    def _on_reply_published(self, user, reply, **kwargs):
        """Handler for when a reply to a review is published.

        This will send a notification to any configured Slack channels when
        a reply to a review is published.

        Args:
            user (django.contrib.auth.models.User):
                The user who published the reply.

            review (reviewboard.reviews.models.Review):
                The reply that was published.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        user_link = self.get_user_text_link(user,
                                            reply.review_request.local_site)
        pre_text = 'New reply from %s' % user_link
        fallback_text = 'New reply from %s' % user_displayname(user)

        self.notify_review_or_reply(user=user,
                                    review=reply,
                                    fallback_text=fallback_text,
                                    pre_text=pre_text,
                                    event_name='reply_published')
Esempio n. 6
0
    def _on_review_request_reopened(self, user, review_request, **kwargs):
        """Handler for when review requests are reopened.

        This will send a notification to any configured Slack channels when
        a review request is reopened.

        Args:
            user (django.contrib.auth.models.User):
                The user who reopened the review request.

            review_request (reviewboard.reviews.models.ReviewRequest):
                The review request that was published.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        if not user:
            user = review_request.submitter

        user_link = self.get_user_text_link(user, review_request.local_site)
        pre_text = 'Reopened by %s' % user_link
        fallback_text = 'Reopened by %s' % user_displayname(user)

        self.notify_review_request(review_request,
                                   fallback_text=fallback_text,
                                   pre_text=pre_text,
                                   body=review_request.description,
                                   local_site=review_request.local_site,
                                   event_name='review_request_reopened')
Esempio n. 7
0
    def _on_review_published(self, user, review, **kwargs):
        """Handler for when a review is published.

        This will send a notification to any configured channels when
        a review is published.

        Args:
            user (django.contrib.auth.models.User):
                The user who published the review.

            review (reviewboard.reviews.models.Review):
                The review that was published.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        open_issues = 0
        first_comment = None

        for comment in review.get_all_comments():
            if not first_comment:
                first_comment = comment

            if (comment.issue_opened and
                comment.issue_status == BaseComment.OPEN):
                open_issues += 1

        if open_issues == 1:
            issue_text = '1 issue'
        else:
            issue_text = '%d issues' % open_issues

        user_link = self.get_user_text_link(user,
                                            review.review_request.local_site)
        pre_text = 'New review from %s' % user_link

        # There doesn't seem to be any image support inside the text fields,
        # but the :white_check_mark: emoji shows a green box with a check-mark
        # in it, and the :warning: emoji is a yellow exclamation point, which
        # are close enough.
        if review.ship_it:
            if open_issues:
                fields = [{
                    'title': 'Fix it, then Ship it!',
                    'value': ':warning: %s' % issue_text,
                    'short': True,
                }]
                extra_text = ' (Fix it, then Ship it!)'
                color = 'warning'
            else:
                fields = [{
                    'title': 'Ship it!',
                    'value': ':white_check_mark:',
                    'short': True,
                }]
                extra_text = ' (Ship it!)'
                color = 'success'
        elif open_issues:
            fields = [{
                'title': 'Open Issues',
                'value': ':warning: %s' % issue_text,
                'short': True,
            }]
            extra_text = ' (%s)' % issue_text
            color = 'warning'
        else:
            fields = []
            extra_text = ''
            color = None

        fallback_text = 'New review from %s%s' % (
            user_displayname(user), extra_text)

        self.notify_review_or_reply(user=user,
                                    review=review,
                                    pre_text=pre_text,
                                    fallback_text=fallback_text,
                                    first_comment=first_comment,
                                    fields=fields,
                                    color=color,
                                    event_name='review_published')
Esempio n. 8
0
    def _on_review_request_published(self, user, review_request, changedesc,
                                     **kwargs):
        """Handler for when review requests are published.

        This will send a notification to any configured channels when
        a review request is published.

        Args:
            user (django.contrib.auth.models.User):
                The user who published the review request.

            review_request (reviewboard.reviews.models.ReviewRequest):
                The review request that was published.

            changedesc (reviewboard.changedescs.models.ChangeDescription):
                The change description for the update, if any.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        user_link = self.get_user_text_link(user, review_request.local_site)
        fields = []

        if changedesc:
            fallback_text = 'New update from %s' % user_displayname(user)
            pre_text = 'New update from %s' % user_link

            # This might be empty, which is fine. We won't show an update
            # at that point.
            body = changedesc.text
        else:
            fallback_text = 'New review request from %s' % \
                user_displayname(user)
            pre_text = 'New review request from %s' % user_link
            body = None

            fields.append({
                'short': False,
                'title': 'Description',
                'value': review_request.description,
            })

        # Link to the diff in the update, if any.
        diffset = review_request.get_latest_diffset()

        if diffset:
            diff_url = local_site_reverse(
                'view-diff-revision',
                local_site=review_request.local_site,
                kwargs={
                    'review_request_id': review_request.display_id,
                    'revision': diffset.revision,
                })

            fields.append({
                'short': True,
                'title': 'Diff',
                'value': self.format_link(diff_url,
                                          'Revision %s' % diffset.revision),
            })

        if review_request.repository:
            fields.append({
                'short': True,
                'title': 'Repository',
                'value': review_request.repository.name,
            })

        if review_request.branch:
            fields.append({
                'short': True,
                'title': 'Branch',
                'value': review_request.branch,
            })

        # See if there are any new interesting file attachments to show.
        # These will only show up if the file is accessible.
        attachment = None

        if changedesc:
            # Only show new files added in this change.
            try:
                new_files = changedesc.fields_changed['files']['added']
            except KeyError:
                new_files = []

            for file_info in new_files:
                if (len(file_info) >= 3 and
                    file_info[1].endswith(self.VALID_IMAGE_URL_EXTS)):
                    # This one wins. Show it.
                    attachment = get_object_or_none(
                        review_request.file_attachments,
                        pk=file_info[2])
                    break
        else:
            # This is a new review request, so show the first valid image
            # we can find.
            for attachment in review_request.file_attachments.all():
                if attachment.filename.endswith(self.VALID_IMAGE_URL_EXTS):
                    # This one wins. Show it.
                    break
            else:
                attachment = None

        if attachment:
            image_url = attachment.get_absolute_url()
        else:
            image_url = None

        # Find any trophies we may want to show in the update.
        trophies = Trophy.objects.get_trophies(review_request)
        trophy_url = None

        if trophies:
            # For now, due to the need to look up resources from a stable
            # location, we're only supporting certain trophies. First one
            # wins.
            for trophy in trophies:
                try:
                    trophy_url = self.TROPHY_URLS[trophy.category]
                    break
                except KeyError:
                    pass

        self.notify_review_request(review_request,
                                   fallback_text=fallback_text,
                                   body=body,
                                   pre_text=pre_text,
                                   fields=fields,
                                   thumb_url=trophy_url,
                                   image_url=image_url,
                                   local_site=review_request.local_site,
                                   event_name='review_request_published')
Esempio n. 9
0
 def prepare_user_display_name(self, obj):
     return user_displayname(obj.submitter)
 def prepare_user_display_name(self, obj):
     return user_displayname(obj.submitter)
Esempio n. 11
0
    def _on_review_published(self, user, review, **kwargs):
        """Handler for when a review is published.

        This will send a notification to any configured Slack channels when
        a review is published.

        Args:
            user (django.contrib.auth.models.User):
                The user who published the review.

            review (reviewboard.reviews.models.Review):
                The review that was published.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        open_issues = 0
        first_comment = None

        for comment in review.get_all_comments():
            if not first_comment:
                first_comment = comment

            if (comment.issue_opened and
                comment.issue_status == BaseComment.OPEN):
                open_issues += 1

        if open_issues == 1:
            issue_text = '1 issue'
        else:
            issue_text = '%d issues' % open_issues

        user_link = self.get_user_text_link(user,
                                            review.review_request.local_site)
        pre_text = 'New review from %s' % user_link

        # There doesn't seem to be any image support inside the text fields,
        # but the :white_check_mark: emoji shows a green box with a check-mark
        # in it, and the :warning: emoji is a yellow exclamation point, which
        # are close enough.
        if review.ship_it:
            if open_issues:
                fields = [{
                    'title': 'Fix it, then Ship it!',
                    'value': ':warning: %s' % issue_text,
                    'short': True,
                }]
                extra_text = ' (Fix it, then Ship it!)'
                color = 'warning'
            else:
                fields = [{
                    'title': 'Ship it!',
                    'value': ':white_check_mark:',
                    'short': True,
                }]
                extra_text = ' (Ship it!)'
                color = 'success'
        elif open_issues:
            fields = [{
                'title': 'Open Issues',
                'value': ':warning: %s' % issue_text,
                'short': True,
            }]
            extra_text = ' (%s)' % issue_text
            color = 'warning'
        else:
            fields = []
            extra_text = ''
            color = None

        fallback_text = 'New review from %s%s' % (
            user_displayname(user), extra_text)

        self.notify_review_or_reply(user=user,
                                    review=review,
                                    pre_text=pre_text,
                                    fallback_text=fallback_text,
                                    first_comment=first_comment,
                                    fields=fields,
                                    color=color,
                                    event_name='review_published')
Esempio n. 12
0
    def _on_review_request_published(self, user, review_request, changedesc,
                                     **kwargs):
        """Handler for when review requests are published.

        This will send a notification to any configured Slack channels when
        a review request is published.

        Args:
            user (django.contrib.auth.models.User):
                The user who published the review request.

            review_request (reviewboard.reviews.models.ReviewRequest):
                The review request that was published.

            changedesc (reviewboard.changedescs.models.ChangeDescription):
                The change description for the update, if any.

            **kwargs (dict):
                Additional keyword arguments passed to the handler.
        """
        user_link = self.get_user_text_link(user, review_request.local_site)
        fields = []

        if changedesc:
            fallback_text = 'New update from %s' % user_displayname(user)
            pre_text = 'New update from %s' % user_link

            # This might be empty, which is fine. We won't show an update
            # at that point.
            body = changedesc.text
        else:
            fallback_text = 'New review request from %s' % \
                user_displayname(user)
            pre_text = 'New review request from %s' % user_link
            body = None

            fields.append({
                'short': False,
                'title': 'Description',
                'value': review_request.description,
            })

        # Link to the diff in the update, if any.
        diffset = review_request.get_latest_diffset()

        if diffset:
            diff_url = local_site_reverse(
                'view-diff-revision',
                local_site=review_request.local_site,
                kwargs={
                    'review_request_id': review_request.display_id,
                    'revision': diffset.revision,
                })

            fields.append({
                'short': True,
                'title': 'Diff',
                'value': self.format_link(diff_url,
                                          'Revision %s' % diffset.revision),
            })

        if review_request.repository:
            fields.append({
                'short': True,
                'title': 'Repository',
                'value': review_request.repository.name,
            })

        if review_request.branch:
            fields.append({
                'short': True,
                'title': 'Branch',
                'value': review_request.branch,
            })

        # See if there are any new interesting file attachments to show.
        # These will only show up if the file is accessible to Slack.
        attachment = None

        if changedesc:
            # Only show new files added in this change.
            try:
                new_files = changedesc.fields_changed['files']['added']
            except KeyError:
                new_files = []

            for file_info in new_files:
                if (len(file_info) >= 3 and
                    file_info[1].endswith(self.VALID_IMAGE_URL_EXTS)):
                    # This one wins. Show it.
                    attachment = get_object_or_none(
                        review_request.file_attachments,
                        pk=file_info[2])
                    break
        else:
            # This is a new review request, so show the first valid image
            # we can find.
            for attachment in review_request.file_attachments.all():
                if attachment.filename.endswith(self.VALID_IMAGE_URL_EXTS):
                    # This one wins. Show it.
                    break
            else:
                attachment = None

        if attachment:
            image_url = attachment.get_absolute_url()
        else:
            image_url = None

        # Find any trophies we may want to show in the update.
        trophies = Trophy.objects.get_trophies(review_request)
        trophy_url = None

        if trophies:
            # For now, due to the need to look up resources from a stable
            # location, we're only supporting certain trophies. First one
            # wins.
            for trophy in trophies:
                try:
                    trophy_url = self.TROPHY_URLS[trophy.category]
                    break
                except KeyError:
                    pass

        self.notify_review_request(review_request,
                                   fallback_text=fallback_text,
                                   body=body,
                                   pre_text=pre_text,
                                   fields=fields,
                                   thumb_url=trophy_url,
                                   image_url=image_url,
                                   local_site=review_request.local_site,
                                   event_name='review_request_published')