Beispiel #1
0
    def __init__(self,
                 pb,
                 mr,
                 prefetched_issues,
                 users_by_id,
                 prefetched_projects,
                 prefetched_configs,
                 autolink=None,
                 all_ref_artifacts=None,
                 ending=None,
                 highlight=None):
        """Constructs an ActivityView out of an Activity protocol buffer.

    Args:
      pb: an IssueComment or Activity protocol buffer.
      mr: HTTP request info, used by the artifact autolink.
      prefetched_issues: dictionary of the issues for the comments being shown.
      users_by_id: dict {user_id: UserView} for all relevant users.
      prefetched_projects: dict {project_id: project} including all the projects
          that we might need.
      prefetched_configs: dict {project_id: config} for those projects.
      autolink: Autolink instance.
      all_ref_artifacts: list of all artifacts in the activity stream.
      ending: ending type for activity titles, 'in_project' or 'by_user'
      highlight: what to highlight in the middle column on user updates pages
          i.e. 'project', 'user', or None
    """
        template_helpers.PBProxy.__init__(self, pb)

        activity_type = 'ProjectIssueUpdate'  # TODO(jrobbins): more types

        self.comment = None
        self.issue = None
        self.field_changed = None
        self.multiple_fields_changed = ezt.boolean(False)
        self.project = None
        self.user = None
        self.timestamp = time.time(
        )  # Bogus value makes bad ones highly visible.

        if isinstance(pb, tracker_pb2.IssueComment):
            self.timestamp = pb.timestamp
            issue = prefetched_issues[pb.issue_id]
            if self.timestamp == issue.opened_timestamp:
                issue_change_id = None  # This comment is the description.
            else:
                issue_change_id = pb.timestamp  # instead of seq num.

            self.comment = tracker_views.IssueCommentView(
                mr.project_name, pb, users_by_id, autolink, all_ref_artifacts,
                mr, issue)

            # TODO(jrobbins): pass effective_ids of the commenter so that he/she
            # can be identified as a project member or not.
            config = prefetched_configs[issue.project_id]
            self.issue = tracker_views.IssueView(issue, users_by_id, config)
            self.user = self.comment.creator
            project = prefetched_projects[issue.project_id]
            self.project_name = project.project_name
            self.project = project_views.ProjectView(project)

        else:
            logging.warn('unknown activity object %r', pb)

        nested_page_data = {
            'activity_type': activity_type,
            'issue_change_id': issue_change_id,
            'comment': self.comment,
            'issue': self.issue,
            'project': self.project,
            'user': self.user,
            'timestamp': self.timestamp,
            'ending_type': ending,
        }

        self.escaped_title = self._TITLE_TEMPLATE.GetResponse(
            nested_page_data).strip()
        self.escaped_body = self._BODY_TEMPLATE.GetResponse(
            nested_page_data).strip()

        if autolink is not None and all_ref_artifacts is not None:
            # TODO(jrobbins): actually parse the comment text.  Actually render runs.
            runs = autolink.MarkupAutolinks(
                mr, [template_helpers.TextRun(self.escaped_body)],
                all_ref_artifacts)
            self.escaped_body = ''.join(run.content for run in runs)

        self.date_bucket, self.date_relative = timestr.GetHumanScaleDate(
            self.timestamp)
        time_tuple = time.localtime(self.timestamp)
        self.date_tooltip = time.asctime(time_tuple)

        # We always highlight the user for starring activities
        if activity_type.startswith('UserStar'):
            self.highlight = 'user'
        else:
            self.highlight = highlight
Beispiel #2
0
  def _MakeApprovalEmailTasks(
      self, hostport, issue, project, approval_value, approval_name,
      comment, users_by_id, user_ids_from_fields, perms):
    """Formulate emails to be sent."""

    # TODO(jojwang): avoid need to make MonorailRequest and autolinker
    # for comment_view OR make make tracker_views._ParseTextRuns public
    # and only pass text_runs to email_data.
    mr = monorailrequest.MonorailRequest(self.services)
    mr.project_name = project.project_name
    mr.project = project
    mr.perms = perms
    autolinker = autolink.Autolink()

    approval_url = framework_helpers.IssueCommentURL(
        hostport, project, issue.local_id, seq_num=comment.sequence)

    comment_view = tracker_views.IssueCommentView(
        project.project_name, comment, users_by_id, autolinker, {}, mr, issue)
    domain_url = framework_helpers.FormatAbsoluteURLForDomain(
        hostport, project.project_name, '/issues/')

    commenter_view = users_by_id[comment.user_id]
    email_data = {
        'domain_url': domain_url,
        'approval_url': approval_url,
        'comment': comment_view,
        'issue_local_id': issue.local_id,
        'summary': issue.summary,
        }
    subject = '%s Approval: %s (Issue %s)' % (
        approval_name, issue.summary, issue.local_id)
    email_body = self.email_template.GetResponse(email_data)
    body = notify_helpers._TruncateBody(email_body)

    recipient_ids = self._GetApprovalEmailRecipients(
        approval_value, comment, issue, user_ids_from_fields,
        omit_ids=[comment.user_id])
    direct, indirect = self.services.usergroup.ExpandAnyGroupEmailRecipients(
        mr.cnxn, recipient_ids)
    # group ids were found in recipient_ids.
    # Re-set recipient_ids to remove group_ids
    if indirect:
      recipient_ids = set(direct + indirect)
      users_by_id.update(framework_views.MakeAllUserViews(
          mr.cnxn, self.services.user, indirect))  # already contains direct

    # TODO(jojwang): monorail:3588, refine email contents based on direct
    # and indirect user_ids returned.

    email_tasks = []
    for rid in recipient_ids:
      # TODO(jojwang): monorail:3588, add reveal_addr based on
      # recipient member status
      from_addr = emailfmt.FormatFromAddr(
          project, commenter_view=commenter_view, can_reply_to=False)
      dest_email = users_by_id[rid].email
      email_tasks.append(
          dict(from_addr=from_addr, to=dest_email, subject=subject, body=body)
      )

    return email_tasks
Beispiel #3
0
  def _MakeIssueAndCommentViews(
      self, mr, issue, users_by_id, descriptions, comments, config,
      issue_reporters=None, comment_reporters=None):
    """Create view objects that help display parts of an issue.

    Args:
      mr: commonly used info parsed from the request.
      issue: issue PB for the currently viewed issue.
      users_by_id: dictionary of {user_id: UserView,...}.
      descriptions: list of IssueComment PBs for the issue report history.
      comments: list of IssueComment PBs on the current issue.
      issue_reporters: list of user IDs who have flagged the issue as spam.
      comment_reporters: map of comment ID to list of flagging user IDs.
      config: ProjectIssueConfig for the project that contains this issue.

    Returns:
      (issue_view, description_views, comment_views). One IssueView for
      the whole issue, a list of IssueCommentViews for the issue descriptions,
      and then a list of IssueCommentViews for each additional comment.
    """
    with mr.profiler.Phase('getting related issues'):
      open_related, closed_related = (
          tracker_helpers.GetAllowedOpenAndClosedRelatedIssues(
              self.services, mr, issue))
      all_related_iids = list(issue.blocked_on_iids) + list(issue.blocking_iids)
      if issue.merged_into:
        all_related_iids.append(issue.merged_into)
      all_related = self.services.issue.GetIssues(mr.cnxn, all_related_iids)

    with mr.profiler.Phase('making issue view'):
      issue_view = tracker_views.IssueView(
          issue, users_by_id, config,
          open_related=open_related, closed_related=closed_related,
          all_related={rel.issue_id: rel for rel in all_related})

    with mr.profiler.Phase('autolinker object lookup'):
      all_ref_artifacts = None
      if self.services.autolink:
        all_ref_artifacts = self.services.autolink.GetAllReferencedArtifacts(
            mr, [c.content for c in descriptions + comments
                 if not c.deleted_by])

    with mr.profiler.Phase('making comment views'):
      reporter_auth = authdata.AuthData.FromUserID(
          mr.cnxn, descriptions[0].user_id, self.services)
      desc_views = [
          tracker_views.IssueCommentView(
              mr.project_name, d, users_by_id,
              self.services.autolink, all_ref_artifacts, mr,
              issue, effective_ids=reporter_auth.effective_ids)
          for d in descriptions]
      # TODO(jrobbins): get effective_ids of each comment author, but
      # that is too slow right now.
      comment_views = [
          tracker_views.IssueCommentView(
              mr.project_name, c, users_by_id, self.services.autolink,
              all_ref_artifacts, mr, issue)
          for c in comments]

    issue_view.flagged_spam = mr.auth.user_id in issue_reporters
    if comment_reporters is not None:
      for c in comment_views:
        c.flagged_spam = mr.auth.user_id in comment_reporters.get(c.id, [])

    return issue_view, desc_views, comment_views
Beispiel #4
0
  def _MakeEmailTasks(
      self, cnxn, project, issue, config, old_owner_id,
      users_by_id, all_comments, comment, starrer_ids,
      contributor_could_view, hostport, omit_ids, perms):
    """Formulate emails to be sent."""
    detail_url = framework_helpers.IssueCommentURL(
        hostport, project, issue.local_id, seq_num=comment.sequence)

    # TODO(jrobbins): avoid the need to make a MonorailRequest object.
    mr = monorailrequest.MonorailRequest(self.services)
    mr.project_name = project.project_name
    mr.project = project
    mr.perms = perms

    # We do not autolink in the emails, so just use an empty
    # registry of autolink rules.
    # TODO(jrobbins): offer users an HTML email option w/ autolinks.
    autolinker = autolink.Autolink()
    was_created = ezt.boolean(comment.sequence == 0)

    email_data = {
        # Pass open_related and closed_related into this method and to
        # the issue view so that we can show it on new issue email.
        'issue': tracker_views.IssueView(issue, users_by_id, config),
        'summary': issue.summary,
        'comment': tracker_views.IssueCommentView(
            project.project_name, comment, users_by_id,
            autolinker, {}, mr, issue),
        'comment_text': comment.content,
        'detail_url': detail_url,
        'was_created': was_created,
        }

    # Generate three versions of email body: link-only is just the link,
    # non-members see some obscured email addresses, and members version has
    # all full email addresses exposed.
    body_link_only = self.link_only_email_template.GetResponse(
      {'detail_url': detail_url, 'was_created': was_created})
    body_for_non_members = self.email_template.GetResponse(email_data)
    framework_views.RevealAllEmails(users_by_id)
    email_data['comment'] = tracker_views.IssueCommentView(
        project.project_name, comment, users_by_id,
        autolinker, {}, mr, issue)
    body_for_members = self.email_template.GetResponse(email_data)

    logging.info('link-only body is:\n%r' % body_link_only)
    logging.info('body for non-members is:\n%r' % body_for_non_members)
    logging.info('body for members is:\n%r' % body_for_members)

    commenter_email = users_by_id[comment.user_id].email
    omit_addrs = set([commenter_email] +
                     [users_by_id[omit_id].email for omit_id in omit_ids])

    auth = authdata.AuthData.FromUserID(
        cnxn, comment.user_id, self.services)
    commenter_in_project = framework_bizobj.UserIsInProject(
        project, auth.effective_ids)
    noisy = tracker_helpers.IsNoisy(len(all_comments) - 1, len(starrer_ids))

    # Give each user a bullet-list of all the reasons that apply for that user.
    group_reason_list = notify_reasons.ComputeGroupReasonList(
        cnxn, self.services, project, issue, config, users_by_id,
        omit_addrs, contributor_could_view, noisy=noisy,
        starrer_ids=starrer_ids, old_owner_id=old_owner_id,
        commenter_in_project=commenter_in_project)

    commenter_view = users_by_id[comment.user_id]
    detail_url = framework_helpers.FormatAbsoluteURLForDomain(
        hostport, issue.project_name, urls.ISSUE_DETAIL,
        id=issue.local_id)
    email_tasks = notify_helpers.MakeBulletedEmailWorkItems(
        group_reason_list, issue, body_link_only, body_for_non_members,
        body_for_members, project, hostport, commenter_view, detail_url,
        seq_num=comment.sequence)

    return email_tasks