Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send comments as robotComments (#238) #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Joiner;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -18,7 +19,7 @@
@AllArgsConstructor
@Slf4j
public class ReviewInputBuilder {

private static final String ROBOT_ID = "sputnik";
private static final String MESSAGE_SEPARATOR = ". ";

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

String runId = UUID.randomUUID().toString();
reviewInput.robotComments = review.getFiles().stream()
.collect(Collectors.toMap(ReviewFile::getReviewFilename, reviewFile -> buildFileComments(reviewFile, runId)));
return reviewInput;
}

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

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

commentInput.robotId = ROBOT_ID;
commentInput.robotRunId = runId;
commentInput.line = comment.getLine();
commentInput.message = comment.getMessage();
return commentInput;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pl.touk.sputnik.connector.gerrit;

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

import java.util.List;

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

@ExtendWith(MockitoExtension.class)
Expand All @@ -28,10 +31,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