Skip to content

Commit

Permalink
Send comments as robotComments (#238)
Browse files Browse the repository at this point in the history
Robot comments show up in the Findings tab, instead of in the comments.
This makes it much easier to differentiate from them a reviewer's
comments.

The API requires a "runId" for the comment. I've chosen to just send a
random UUID for now, but eventually we could wire in something like a
build number or something.
  • Loading branch information
Marquis Wong authored and Marquis Wong committed Apr 23, 2021
1 parent ce4d75e commit b3bf26d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

import com.google.common.base.Joiner;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pl.touk.sputnik.review.Comment;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewFile;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Slf4j
public class ReviewInputBuilder {

private static final String MESSAGE_SEPARATOR = ". ";

@NotNull
Expand All @@ -29,23 +25,27 @@ public ReviewInput toReviewInput(@NotNull Review review, @Nullable String tag) {
if (StringUtils.isNotBlank(tag)) {
reviewInput.tag = tag;
}
reviewInput.comments = review.getFiles().stream()
reviewInput.robotComments = review.getFiles().stream()
.collect(Collectors.toMap(ReviewFile::getReviewFilename, this::buildFileComments));
return reviewInput;
}

@NotNull
private List<ReviewInput.CommentInput> buildFileComments(@NotNull ReviewFile reviewFile) {
private List<ReviewInput.RobotCommentInput> buildFileComments(@NotNull ReviewFile reviewFile) {
return reviewFile.getComments().stream()
.map(this::buildCommentInput)
.flatMap((Comment comment) -> Stream.of(buildCommentInput(comment)))
.collect(Collectors.toList());
}

@NotNull
private ReviewInput.CommentInput buildCommentInput(Comment comment) {
ReviewInput.CommentInput commentInput = new ReviewInput.CommentInput();
private ReviewInput.RobotCommentInput buildCommentInput(Comment comment) {
ReviewInput.RobotCommentInput commentInput = new ReviewInput.RobotCommentInput();

commentInput.robotId = "sputnik";
commentInput.robotRunId = UUID.randomUUID().toString();
commentInput.line = comment.getLine();
commentInput.message = comment.getMessage();
return commentInput;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package pl.touk.sputnik.connector.gerrit;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -11,8 +15,6 @@
import pl.touk.sputnik.configuration.ConfigurationBuilder;
import pl.touk.sputnik.review.Review;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
class ReviewInputBuilderTest {

Expand All @@ -28,10 +30,15 @@ void shouldBuildReviewInput() {
ReviewInput reviewInput = reviewInputBuilder.toReviewInput(review, TAG);

assertThat(reviewInput.message).isEqualTo("Total 8 violations found");
assertThat(reviewInput.comments).hasSize(4);
assertThat(reviewInput.comments).isNull();
assertThat(reviewInput.robotComments).hasSize(4);
assertThat(reviewInput.tag).isEqualTo(TAG);
assertThat(reviewInput.comments.get("filename1")).hasSize(2);
assertThat(reviewInput.comments.get("filename1").get(0).message).isEqualTo("test1");
List<RobotCommentInput> file1comments = reviewInput.robotComments.get("filename1");
assertThat(file1comments).hasSize(2);
RobotCommentInput comment1 = file1comments.get(0);
assertThat(comment1.message).isEqualTo("test1");
assertThat(comment1.robotId).isEqualTo("sputnik");
assertThat(comment1.robotRunId).isNotEmpty();
assertThat(reviewInput.labels.get("Code-Review")).isEqualTo((short) 1);
}

Expand Down

0 comments on commit b3bf26d

Please sign in to comment.