Skip to content

Commit a47c212

Browse files
authored
Validate check run output text and summary length (#118)
* validate check run output text and summary length * Add tests for checkrun output validation * simplify character limits
1 parent 5dd80ee commit a47c212

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,6 +22,7 @@
2222

2323
import com.fasterxml.jackson.annotation.JsonInclude;
2424
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25+
import com.google.common.base.Preconditions;
2526
import com.spotify.github.GithubStyle;
2627
import java.util.List;
2728
import java.util.Optional;
@@ -87,4 +88,20 @@ public interface CheckRunOutput {
8788
* @return the optional
8889
*/
8990
Optional<String> annotationsUrl();
91+
92+
/**
93+
* Automatically validates the maximum length of properties.
94+
* <p>
95+
* GitHub does not validate these properly on their side (at least in GHE 3.2) and returns 422
96+
* HTTP responses instead. To avoid that, let's validate the data in this client library.
97+
*/
98+
@Value.Check
99+
@SuppressWarnings("checkstyle:magicnumber")
100+
default void check() {
101+
// max values from https://docs.github.com/en/enterprise-server@3.5/rest/checks/runs#create-a-check-run
102+
Preconditions.checkState(summary().map(String::length).orElse(0) <= 65535,
103+
"'summary' exceeded max length of 65535 characters");
104+
Preconditions.checkState(text().map(String::length).orElse(0) <= 65535,
105+
"'text' exceeded max length of 65535 characters");
106+
}
90107
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@
2020

2121
package com.spotify.github.v3.checks;
2222

23-
import static org.hamcrest.CoreMatchers.is;
24-
import static org.hamcrest.MatcherAssert.assertThat;
2523
import static org.junit.jupiter.api.Assertions.assertThrows;
2624

27-
import com.spotify.github.FixtureHelper;
28-
import com.spotify.github.jackson.Json;
2925
import com.spotify.github.v3.checks.ImmutableCheckRunAction.Builder;
30-
import java.io.IOException;
31-
import java.time.ZonedDateTime;
3226
import org.junit.Test;
3327

3428
public class CheckRunActionTest {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*-
2+
* -\-\-
3+
* github-client
4+
* --
5+
* Copyright (C) 2016 - 2022 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.checks;
22+
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
25+
import com.spotify.github.v3.checks.ImmutableCheckRunOutput.Builder;
26+
import org.junit.Test;
27+
28+
public class CheckRunOutputTest {
29+
30+
private Builder builder() {
31+
return ImmutableCheckRunOutput.builder();
32+
}
33+
34+
@Test
35+
public void allowsCreationWithinLimits() {
36+
builder().build();
37+
builder()
38+
.text("t".repeat(65535)).summary("s".repeat(65535)).build();
39+
}
40+
41+
@Test
42+
public void failsCreationWhenMaxLengthExceeded() {
43+
assertThrows(IllegalStateException.class,
44+
() -> builder().text("t".repeat(65536)).build());
45+
assertThrows(IllegalStateException.class,
46+
() -> builder().summary("s".repeat(65536)).build());
47+
}
48+
}
49+

0 commit comments

Comments
 (0)