Skip to content

Commit 0f22161

Browse files
Marquis WongMarquis Wong
authored andcommitted
Send comments as robotComments (#238)
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.
1 parent ce4d75e commit 0f22161

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/main/java/pl/touk/sputnik/connector/gerrit/ReviewInputBuilder.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import pl.touk.sputnik.review.ReviewFile;
1313

1414
import java.util.HashMap;
15-
import java.util.List;
15+
import java.util.UUID;
1616
import java.util.stream.Collectors;
17+
import java.util.stream.Stream;
1718

1819
@AllArgsConstructor
1920
@Slf4j
2021
public class ReviewInputBuilder {
21-
2222
private static final String MESSAGE_SEPARATOR = ". ";
2323

2424
@NotNull
@@ -29,23 +29,27 @@ public ReviewInput toReviewInput(@NotNull Review review, @Nullable String tag) {
2929
if (StringUtils.isNotBlank(tag)) {
3030
reviewInput.tag = tag;
3131
}
32-
reviewInput.comments = review.getFiles().stream()
32+
reviewInput.robotComments = review.getFiles().stream()
3333
.collect(Collectors.toMap(ReviewFile::getReviewFilename, this::buildFileComments));
3434
return reviewInput;
3535
}
3636

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

4444
@NotNull
45-
private ReviewInput.CommentInput buildCommentInput(Comment comment) {
46-
ReviewInput.CommentInput commentInput = new ReviewInput.CommentInput();
45+
private ReviewInput.RobotCommentInput buildCommentInput(Comment comment) {
46+
ReviewInput.RobotCommentInput commentInput = new ReviewInput.RobotCommentInput();
47+
48+
commentInput.robotId = "sputnik";
49+
commentInput.robotRunId = UUID.randomUUID().toString();
4750
commentInput.line = comment.getLine();
4851
commentInput.message = comment.getMessage();
4952
return commentInput;
5053
}
54+
5155
}

src/test/java/pl/touk/sputnik/connector/gerrit/ReviewInputBuilderTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package pl.touk.sputnik.connector.gerrit;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
35
import com.google.gerrit.extensions.api.changes.ReviewInput;
6+
import com.google.gerrit.extensions.api.changes.ReviewInput.RobotCommentInput;
7+
import java.util.List;
48
import org.junit.jupiter.api.Test;
59
import org.junit.jupiter.api.extension.ExtendWith;
610
import org.junit.jupiter.params.ParameterizedTest;
@@ -11,8 +15,6 @@
1115
import pl.touk.sputnik.configuration.ConfigurationBuilder;
1216
import pl.touk.sputnik.review.Review;
1317

14-
import static org.assertj.core.api.Assertions.assertThat;
15-
1618
@ExtendWith(MockitoExtension.class)
1719
class ReviewInputBuilderTest {
1820

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

3032
assertThat(reviewInput.message).isEqualTo("Total 8 violations found");
31-
assertThat(reviewInput.comments).hasSize(4);
33+
assertThat(reviewInput.comments).isNull();
34+
assertThat(reviewInput.robotComments).hasSize(4);
3235
assertThat(reviewInput.tag).isEqualTo(TAG);
33-
assertThat(reviewInput.comments.get("filename1")).hasSize(2);
34-
assertThat(reviewInput.comments.get("filename1").get(0).message).isEqualTo("test1");
36+
List<RobotCommentInput> file1comments = reviewInput.robotComments.get("filename1");
37+
assertThat(file1comments).hasSize(2);
38+
RobotCommentInput comment1 = file1comments.get(0);
39+
assertThat(comment1.message).isEqualTo("test1");
40+
assertThat(comment1.robotId).isEqualTo("sputnik");
41+
assertThat(comment1.robotRunId).isNotEmpty();
3542
assertThat(reviewInput.labels.get("Code-Review")).isEqualTo((short) 1);
3643
}
3744

0 commit comments

Comments
 (0)