diff --git a/src/main/java/com/spotify/github/v3/checks/Annotation.java b/src/main/java/com/spotify/github/v3/checks/Annotation.java index 4bb953b4..7e62f9e1 100644 --- a/src/main/java/com/spotify/github/v3/checks/Annotation.java +++ b/src/main/java/com/spotify/github/v3/checks/Annotation.java @@ -125,7 +125,7 @@ public interface Annotation { */ @Value.Check @SuppressWarnings("checkstyle:magicnumber") - default void check() { + default Annotation check() { // max values from https://docs.github.com/en/rest/checks/runs Preconditions.checkState(title().map(String::length).orElse(0) <= 255, "'title' exceeded max length of 255"); @@ -133,5 +133,16 @@ default void check() { "'message' exceeded max length of 64kB"); Preconditions.checkState(rawDetails().map(String::length).orElse(0) <= 64 * 1024, "'rawDetails' exceeded max length of 64kB"); + + // Omit this (start_column, end_column) parameter if start_line and end_line have different values + // from https://docs.github.com/en/rest/checks/runs + if (startLine() != endLine() && (startColumn().isPresent() || endColumn().isPresent())) { + return ImmutableAnnotation.builder() + .from(this) + .startColumn(Optional.empty()) + .endColumn(Optional.empty()) + .build(); + } + return this; } } diff --git a/src/test/java/com/spotify/github/v3/checks/AnnotationTest.java b/src/test/java/com/spotify/github/v3/checks/AnnotationTest.java index 2194259b..934b0426 100644 --- a/src/test/java/com/spotify/github/v3/checks/AnnotationTest.java +++ b/src/test/java/com/spotify/github/v3/checks/AnnotationTest.java @@ -22,10 +22,13 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import com.spotify.github.jackson.Json; import com.spotify.github.v3.checks.ImmutableAnnotation.Builder; +import java.util.Optional; import org.junit.Test; public class AnnotationTest { @@ -37,6 +40,8 @@ private Builder builder() { .path("path") .startLine(1) .endLine(2) + .startColumn(1) + .endColumn(9) .annotationLevel(AnnotationLevel.notice); } @@ -79,4 +84,23 @@ public void serializesWithEmptyFields() { String expected = "{\"path\":\"\",\"annotation_level\":\"notice\",\"message\":\"\",\"title\":\"\",\"start_line\":1,\"end_line\":2}"; assertThat(serializedAnnotation, is(expected)); } + + @Test + public void clearsColumnFieldsForMultiLineAnnotation() { + Annotation multiLineAnnotation = builder().startLine(1).endLine(2).build(); + assertTrue(multiLineAnnotation.startColumn().isEmpty()); + assertTrue(multiLineAnnotation.endColumn().isEmpty()); + + Annotation anotherMultiLineAnnotation = builder().startLine(1).endLine(2).startColumn(Optional.empty()).endColumn(1).build(); + assertTrue(anotherMultiLineAnnotation.startColumn().isEmpty()); + assertTrue(anotherMultiLineAnnotation.endColumn().isEmpty()); + + Annotation yetAnotherMultiLineAnnotation = builder().startLine(1).endLine(2).startColumn(1).endColumn(Optional.empty()).build(); + assertTrue(yetAnotherMultiLineAnnotation.startColumn().isEmpty()); + assertTrue(yetAnotherMultiLineAnnotation.endColumn().isEmpty()); + + Annotation singleLineAnnotation = builder().startLine(1).endLine(1).build(); + assertEquals(1, singleLineAnnotation.startColumn().orElse(0)); + assertEquals(9, singleLineAnnotation.endColumn().orElse(0)); + } }