Ejemplo n.º 1
0
  @Transactional
  protected static ReviewComment saveReviewComment(
      Resource target, User sender, Content content, String messageID, Address[] allRecipients)
      throws MessagingException, IOException, NoSuchAlgorithmException {
    ReviewComment comment;
    CommentThread thread = CommentThread.find.byId(Long.valueOf(target.getId()));

    if (thread == null) {
      throw new IllegalArgumentException();
    }

    comment = new ReviewComment();
    comment.setContents(content.body);
    comment.author = new UserIdent(sender);
    comment.thread = thread;
    comment.save();

    Map<String, Attachment> relatedAttachments =
        saveAttachments(content.attachments, comment.asResource());

    if (new ContentType(content.type).match(MimeType.HTML)) {
      // replace cid with attachments
      comment.setContents(replaceCidWithAttachments(comment.getContents(), relatedAttachments));
      comment.update();
    }

    new OriginalEmail(messageID, comment.asResource()).save();

    // Add the event
    if (thread.isOnPullRequest()) {
      addEvent(
          NotificationEvent.forNewComment(sender, thread.pullRequest, comment),
          allRecipients,
          sender);
    } else {
      try {
        String commitId;

        if (thread instanceof CodeCommentThread) {
          commitId = ((CodeCommentThread) thread).commitId;
        } else if (thread instanceof NonRangedCodeCommentThread) {
          commitId = ((NonRangedCodeCommentThread) thread).commitId;
        } else {
          throw new IllegalArgumentException();
        }

        addEvent(
            NotificationEvent.forNewCommitComment(target.getProject(), comment, commitId, sender),
            allRecipients,
            sender);
      } catch (Exception e) {
        Logger.warn("Failed to send a notification", e);
      }
    }

    return comment;
  }