Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RtChecks more safe #1625

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/jcabi/github/Checks.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public interface Checks {
* @return Checks.
* @throws IOException If there is any I/O problem.
*/
Collection<Check> all() throws IOException;
Collection<? extends Check> all() throws IOException;

}
28 changes: 19 additions & 9 deletions src/main/java/com/jcabi/github/RtChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@

import com.jcabi.http.Request;
import com.jcabi.http.response.JsonResponse;
import com.jcabi.http.response.RestResponse;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.json.JsonObject;
import javax.json.JsonValue;
Expand Down Expand Up @@ -86,23 +90,29 @@ class RtChecks implements Checks {
* @throws IOException If there is any I/O problem.
*/
@Override
public Collection<Check> all() throws IOException {
public Collection<? extends Check> all() throws IOException {
final Coordinates coords = this.pull.repo().coordinates();
return this.request.uri()
final RestResponse rest = this.request.uri()
.path("/repos")
.path(coords.user())
.path(coords.repo())
.path("/commits")
.path(this.pull.head().ref())
.path(this.pull.head().sha())
.path("/check-runs")
.back()
.method(Request.GET).fetch().as(JsonResponse.class)
.method(Request.GET).fetch()
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK);
final JsonObject object = rest.as(JsonResponse.class)
.json()
.readObject()
.getJsonArray("check_runs")
.stream()
.map(RtChecks::check)
.collect(Collectors.toList());
.readObject();
return Optional.ofNullable(object.getJsonArray("check_runs"))
.map(
obj -> obj.stream()
.map(RtChecks::check)
.collect(Collectors.toList())
)
.orElseGet(Collections::emptyList);
}

/**
Expand Down
69 changes: 66 additions & 3 deletions src/test/java/com/jcabi/github/RtChecksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import javax.json.Json;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void getsAllChecks() throws IOException {
.next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
RtChecksTest.json()
RtChecksTest.jsonWithCheckRuns()
)
)
.start(this.resource.port())) {
Expand All @@ -85,12 +86,64 @@ public void getsAllChecks() throws IOException {
}
}

/**
* Checks whether RtChecks can return empty checks if they are absent.
*
* @throws IOException If some I/O problem happens.
*/
@Test
public void returnsEmptyChecksIfTheyAreAbsent() throws IOException {
try (final MkContainer container = new MkGrizzlyContainer()
.next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_OK,
RtChecksTest.empty()
)
)
.start(this.resource.port())) {
MatcherAssert.assertThat(
((Checks) new RtChecks(
new JdkRequest(container.home()),
this.repo().pulls().get(0)
)).all(),
Matchers.iterableWithSize(0)
);
}
}

/**
* Checks whether RtChecks can throw an exception
* if response code is not 200.
*
* @throws IOException If some I/O problem happens.
*/
@Test
public void assertsOkResponse() throws IOException {
try (final MkContainer container = new MkGrizzlyContainer()
.next(
new MkAnswer.Simple(
HttpURLConnection.HTTP_NOT_FOUND,
RtChecksTest.jsonWithCheckRuns()
)
).start(this.resource.port())
) {
final Checks checks = new RtChecks(
new JdkRequest(container.home()),
this.repo().pulls().get(0)
);
Assert.assertThrows(
AssertionError.class,
checks::all
);
}
}

/**
* Creates json response body.
*
* @return Json response body.
*/
private static String json() {
private static String jsonWithCheckRuns() {
return Json.createObjectBuilder()
.add("total_count", Json.createValue(1))
.add(
Expand All @@ -108,6 +161,16 @@ private static String json() {
.toString();
}

/**
* Creates json response body without check runs.
* @return Json response body.
*/
private static String empty() {
return Json.createObjectBuilder()
.build()
.toString();
}

/**
* Create and return repo for testing.
* @return Repo
Expand All @@ -126,7 +189,7 @@ private Repo repo() throws IOException {
Mockito.doReturn(pull).when(pulls).get(0);
Mockito.doReturn(repo).when(pull).repo();
Mockito.doReturn(ref).when(pull).head();
Mockito.doReturn("abcdef1").when(ref).ref();
Mockito.doReturn("abcdef1").when(ref).sha();
return repo;
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/jcabi/github/RtPullTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void canFetchChecks() throws IOException {
))
.start(this.resource.port())
) {
final Collection<Check> all = new RtPull(
final Collection<? extends Check> all = new RtPull(
new ApacheRequest(container.home()),
this.repo(),
new Random().nextInt()
Expand Down