Skip to content

Commit 8e06622

Browse files
Omit start_column, end_column for multi-line annotation (#125)
* Omit start_column, end_column for multi-line annotation When start_line and end_line have different values & start_column or end_column has value, then Github API throws the following error while creating or updating a Check-run `HTTP 422: {"resource":"CheckRun","code":"invalid","field":"annotations"}`
1 parent 6d91e8d commit 8e06622

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/main/java/com/spotify/github/v3/checks/Annotation.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,24 @@ public interface Annotation {
125125
*/
126126
@Value.Check
127127
@SuppressWarnings("checkstyle:magicnumber")
128-
default void check() {
128+
default Annotation check() {
129129
// max values from https://docs.github.com/en/rest/checks/runs
130130
Preconditions.checkState(title().map(String::length).orElse(0) <= 255,
131131
"'title' exceeded max length of 255");
132132
Preconditions.checkState(message().length() <= 64 * 1024,
133133
"'message' exceeded max length of 64kB");
134134
Preconditions.checkState(rawDetails().map(String::length).orElse(0) <= 64 * 1024,
135135
"'rawDetails' exceeded max length of 64kB");
136+
137+
// Omit this (start_column, end_column) parameter if start_line and end_line have different values
138+
// from https://docs.github.com/en/rest/checks/runs
139+
if (startLine() != endLine() && (startColumn().isPresent() || endColumn().isPresent())) {
140+
return ImmutableAnnotation.builder()
141+
.from(this)
142+
.startColumn(Optional.empty())
143+
.endColumn(Optional.empty())
144+
.build();
145+
}
146+
return this;
136147
}
137148
}

src/test/java/com/spotify/github/v3/checks/AnnotationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222

2323
import static org.hamcrest.CoreMatchers.is;
2424
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.junit.Assert.assertTrue;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
2527
import static org.junit.jupiter.api.Assertions.assertThrows;
2628

2729
import com.spotify.github.jackson.Json;
2830
import com.spotify.github.v3.checks.ImmutableAnnotation.Builder;
31+
import java.util.Optional;
2932
import org.junit.Test;
3033

3134
public class AnnotationTest {
@@ -37,6 +40,8 @@ private Builder builder() {
3740
.path("path")
3841
.startLine(1)
3942
.endLine(2)
43+
.startColumn(1)
44+
.endColumn(9)
4045
.annotationLevel(AnnotationLevel.notice);
4146
}
4247

@@ -79,4 +84,23 @@ public void serializesWithEmptyFields() {
7984
String expected = "{\"path\":\"\",\"annotation_level\":\"notice\",\"message\":\"\",\"title\":\"\",\"start_line\":1,\"end_line\":2}";
8085
assertThat(serializedAnnotation, is(expected));
8186
}
87+
88+
@Test
89+
public void clearsColumnFieldsForMultiLineAnnotation() {
90+
Annotation multiLineAnnotation = builder().startLine(1).endLine(2).build();
91+
assertTrue(multiLineAnnotation.startColumn().isEmpty());
92+
assertTrue(multiLineAnnotation.endColumn().isEmpty());
93+
94+
Annotation anotherMultiLineAnnotation = builder().startLine(1).endLine(2).startColumn(Optional.empty()).endColumn(1).build();
95+
assertTrue(anotherMultiLineAnnotation.startColumn().isEmpty());
96+
assertTrue(anotherMultiLineAnnotation.endColumn().isEmpty());
97+
98+
Annotation yetAnotherMultiLineAnnotation = builder().startLine(1).endLine(2).startColumn(1).endColumn(Optional.empty()).build();
99+
assertTrue(yetAnotherMultiLineAnnotation.startColumn().isEmpty());
100+
assertTrue(yetAnotherMultiLineAnnotation.endColumn().isEmpty());
101+
102+
Annotation singleLineAnnotation = builder().startLine(1).endLine(1).build();
103+
assertEquals(1, singleLineAnnotation.startColumn().orElse(0));
104+
assertEquals(9, singleLineAnnotation.endColumn().orElse(0));
105+
}
82106
}

0 commit comments

Comments
 (0)