Skip to content

Commit 19334d4

Browse files
Handle unfinished checks properly
1 parent 5f7a1c7 commit 19334d4

File tree

3 files changed

+111
-48
lines changed

3 files changed

+111
-48
lines changed

src/main/java/com/jcabi/github/Check.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
*/
4444
public interface Check {
4545

46+
/**
47+
* Undefined status or conclusion.
48+
*/
49+
String UNDEFINED_VALUE = "undefined";
50+
4651
/**
4752
* Checks whether Check was successful.
4853
* @return True if Check was successful.
@@ -70,7 +75,12 @@ enum Status {
7075
/**
7176
* Completed.
7277
*/
73-
COMPLETED("completed");
78+
COMPLETED("completed"),
79+
80+
/**
81+
* Undefined. If GitHub response doesn't contain the Status value.
82+
*/
83+
UNDEFINED(Check.UNDEFINED_VALUE);
7484

7585
/**
7686
* Status.
@@ -173,7 +183,12 @@ enum Conclusion {
173183
/**
174184
* Timed out.
175185
*/
176-
TIMED_OUT("timed_out");
186+
TIMED_OUT("timed_out"),
187+
188+
/**
189+
* Undefined. If GitHub response doesn't contain the Conclusion value.
190+
*/
191+
UNDEFINED(Check.UNDEFINED_VALUE);
177192

178193
/**
179194
* Conclusion.

src/main/java/com/jcabi/github/RtChecks.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,27 @@ public Collection<? extends Check> all() throws IOException {
123123
private static RtCheck check(final JsonValue value) {
124124
final JsonObject check = value.asJsonObject();
125125
return new RtCheck(
126-
check.getString("status"),
127-
check.getString("conclusion")
126+
RtChecks.getOrUndefined("status", check),
127+
RtChecks.getOrUndefined("conclusion", check)
128128
);
129129
}
130+
131+
/**
132+
* Retrieves String value from JsonObject by key or return "undefined".
133+
* @param key Json key.
134+
* @param check Retrieve from
135+
* @return Json String value or "undefined".
136+
*/
137+
private static String getOrUndefined(
138+
final String key,
139+
final JsonObject check
140+
) {
141+
final String res;
142+
if (check.containsKey(key)) {
143+
res = check.getString(key);
144+
} else {
145+
res = Check.UNDEFINED_VALUE;
146+
}
147+
return res;
148+
}
130149
}

src/test/java/com/jcabi/github/RtChecksTest.java

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@
3535
import com.jcabi.http.request.JdkRequest;
3636
import java.io.IOException;
3737
import java.net.HttpURLConnection;
38+
import java.util.Arrays;
39+
import java.util.Collection;
3840
import java.util.Random;
3941
import javax.json.Json;
42+
import javax.json.JsonArrayBuilder;
43+
import javax.json.JsonObjectBuilder;
4044
import org.hamcrest.MatcherAssert;
4145
import org.hamcrest.Matchers;
4246
import org.junit.Assert;
@@ -53,6 +57,11 @@
5357
*/
5458
public final class RtChecksTest {
5559

60+
/**
61+
* Conclusion key in json check.
62+
*/
63+
private static final String CONCLUSION_KEY = "conclusion";
64+
5665
/**
5766
* The rule for skipping test if there's BindException.
5867
* @checkstyle VisibilityModifierCheck (3 lines)
@@ -138,67 +147,70 @@ public void assertsOkResponse() throws IOException {
138147
}
139148
}
140149

150+
/**
151+
* Checks that library can handle unfinished checks.
152+
* @throws IOException If some I/O problem happens.
153+
*/
141154
@Test
142-
public void retrievesUnfinishedChecksWithoutConclusion() throws IOException {
155+
public void retrievesUnfinishedChecksWithoutConclusion()
156+
throws IOException {
143157
try (final MkContainer container = new MkGrizzlyContainer()
144158
.next(
145159
new MkAnswer.Simple(
146160
HttpURLConnection.HTTP_OK,
147-
Json.createObjectBuilder()
148-
.add("total_count", Json.createValue(1))
149-
.add("check_runs",
150-
Json.createArrayBuilder()
151-
.add(
152-
Json.createObjectBuilder()
153-
.add("id", Json.createValue(new Random().nextInt()))
154-
.add("status", "completed")
155-
.build()
156-
)
157-
)
158-
.build()
159-
.toString()
161+
RtChecksTest.jsonChecks(
162+
RtChecksTest.jsonCheck()
163+
.add(
164+
RtChecksTest.CONCLUSION_KEY,
165+
Check.Conclusion.SUCCESS.value()
166+
)
167+
)
160168
)
161169
).start(this.resource.port())
162170
) {
163171
final Checks checks = new RtChecks(
164172
new JdkRequest(container.home()),
165173
this.repo().pulls().get(0)
166174
);
167-
MatcherAssert.assertThat(checks.all(), Matchers.hasSize(1));
168-
for (final Check check : checks.all()) {
169-
MatcherAssert.assertThat(check.successful(), Matchers.is(false));
175+
final Collection<? extends Check> all = checks.all();
176+
MatcherAssert.assertThat(all, Matchers.hasSize(1));
177+
for (final Check check : all) {
178+
MatcherAssert.assertThat(
179+
check.successful(),
180+
Matchers.is(false)
181+
);
170182
}
171183
}
172184
}
173185

186+
/**
187+
* Checks that library can handle unfinished checks.
188+
* @throws IOException If some I/O problem happens.
189+
*/
174190
@Test
175-
public void retrievesUnfinishedChecksWithoutStatusAndConclusion() throws IOException {
191+
public void retrievesUnfinishedChecksWithoutStatusAndConclusion()
192+
throws IOException {
176193
try (final MkContainer container = new MkGrizzlyContainer()
177194
.next(
178195
new MkAnswer.Simple(
179196
HttpURLConnection.HTTP_OK,
180-
Json.createObjectBuilder()
181-
.add("total_count", Json.createValue(1))
182-
.add("check_runs",
183-
Json.createArrayBuilder()
184-
.add(
185-
Json.createObjectBuilder()
186-
.add("id", Json.createValue(new Random().nextInt()))
187-
.build()
188-
)
189-
)
190-
.build()
191-
.toString()
197+
RtChecksTest.jsonChecks(
198+
RtChecksTest.jsonCheck()
199+
)
192200
)
193201
).start(this.resource.port())
194202
) {
195203
final Checks checks = new RtChecks(
196204
new JdkRequest(container.home()),
197205
this.repo().pulls().get(0)
198206
);
199-
MatcherAssert.assertThat(checks.all(), Matchers.hasSize(1));
200-
for (final Check check : checks.all()) {
201-
MatcherAssert.assertThat(check.successful(), Matchers.is(false));
207+
final Collection<? extends Check> all = checks.all();
208+
MatcherAssert.assertThat(all, Matchers.hasSize(1));
209+
for (final Check check : all) {
210+
MatcherAssert.assertThat(
211+
check.successful(),
212+
Matchers.is(false)
213+
);
202214
}
203215
}
204216
}
@@ -209,19 +221,36 @@ public void retrievesUnfinishedChecksWithoutStatusAndConclusion() throws IOExcep
209221
* @return Json response body.
210222
*/
211223
private static String jsonWithCheckRuns() {
224+
return RtChecksTest.jsonChecks(
225+
RtChecksTest.jsonCheck()
226+
.add("status", Check.Status.COMPLETED.value())
227+
.add(
228+
RtChecksTest.CONCLUSION_KEY,
229+
Check.Conclusion.SUCCESS.value()
230+
)
231+
);
232+
}
233+
234+
/**
235+
* Creates Json Check Builder.
236+
* @return JsonObjectBuilder.
237+
*/
238+
private static JsonObjectBuilder jsonCheck() {
239+
return Json.createObjectBuilder()
240+
.add("id", Json.createValue(new Random().nextInt()));
241+
}
242+
243+
/**
244+
* Creates json checks.
245+
* @param checks All checks that have to be included.
246+
* @return Json.
247+
*/
248+
private static String jsonChecks(final JsonObjectBuilder... checks) {
249+
final JsonArrayBuilder all = Json.createArrayBuilder();
250+
Arrays.stream(checks).map(JsonObjectBuilder::build).forEach(all::add);
212251
return Json.createObjectBuilder()
213252
.add("total_count", Json.createValue(1))
214-
.add(
215-
"check_runs",
216-
Json.createArrayBuilder()
217-
.add(
218-
Json.createObjectBuilder()
219-
.add("id", Json.createValue(new Random().nextInt()))
220-
.add("status", "completed")
221-
.add("conclusion", "success")
222-
.build()
223-
)
224-
)
253+
.add("check_runs", all.build())
225254
.build()
226255
.toString();
227256
}

0 commit comments

Comments
 (0)