Skip to content

Commit

Permalink
Merge pull request #28 from tracehubpm/docker.io
Browse files Browse the repository at this point in the history
feat(#2): JsonComments, comments per review id, cov up to 0.5
  • Loading branch information
h1alexbel authored Apr 1, 2024
2 parents 2548ee6 + 0ceb34a commit c213ca5
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 42 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -428,27 +428,27 @@ SOFTWARE.
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.00</minimum>
<minimum>0.28</minimum>
</limit>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.00</minimum>
<minimum>0.26</minimum>
</limit>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.00</minimum>
<minimum>0.47</minimum>
</limit>
<limit>
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.00</minimum>
<minimum>0.47</minimum>
</limit>
<limit>
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.00</minimum>
<minimum>0.47</minimum>
</limit>
<limit>
<counter>CLASS</counter>
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/git/tracehub/codereview/action/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@
package git.tracehub.codereview.action;

import com.jcabi.github.Coordinates;
import com.jcabi.github.Pull;
import com.jcabi.github.Repo;
import com.jcabi.github.RtGithub;
import com.jcabi.log.Logger;
import git.tracehub.codereview.action.github.JsonPull;
import java.io.IOException;
import git.tracehub.codereview.action.github.Authored;
import git.tracehub.codereview.action.github.GhRequest;
import git.tracehub.codereview.action.github.JsonComments;
import git.tracehub.codereview.action.github.PullRequest;
import git.tracehub.codereview.action.github.ReviewIds;
import git.tracehub.codereview.action.github.Reviews;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import javax.json.Json;
import javax.json.JsonObject;

Expand All @@ -48,7 +54,7 @@ public final class Entry {
* Application entry point.
*
* @param args Application arguments
* @throws IOException if I/O fails.
* @throws Exception if something went wrong.
* @todo #2:25min Implement GhIdentity.
* For now we do create RtGithub right here, in the main method.
* Let's implement some sort of "smart" identity that based on
Expand All @@ -67,7 +73,7 @@ public final class Entry {
* into processing if: pull request is too small (we need a specific number),
* or some other alerts that would be reasonable.
*/
public static void main(final String... args) throws IOException {
public static void main(final String... args) throws Exception {
final String name = System.getenv().get("GITHUB_REPOSITORY");
final JsonObject event = Json.createReader(
new StringReader(
Expand All @@ -84,7 +90,21 @@ public static void main(final String... args) throws IOException {
final Repo repo = new RtGithub(
System.getenv().get("INPUT_GITHUBTOKEN")
).repos().get(new Coordinates.Simple(name));
final JsonObject pull = new JsonPull(repo, event).value();
final Pull pull = new PullRequest(repo, event).value();
Logger.info(Entry.class, "pull request found: %s", pull);
new ReviewIds(new Reviews(pull, new GhRequest())).value()
.forEach(
review -> {
final List<String> comments = new Authored(
new JsonComments(new GhRequest(), pull, review)
).value();
Logger.info(
Entry.class,
"found comments for review #%s: %s",
review,
comments
);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.cactoos.Scalar;
import org.cactoos.list.ListOf;

Expand All @@ -42,7 +43,8 @@ public final class Authored implements Scalar<List<String>> {
private final Comments origin;

@Override
public List<String> value() throws Exception {
@SneakyThrows
public List<String> value() {
final List<String> bodies = new ListOf<>();
this.origin.value().forEach(
value -> bodies.add(
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/git/tracehub/codereview/action/github/GhRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package git.tracehub.codereview.action.github;

import com.jcabi.http.Request;
import com.jcabi.http.request.JdkRequest;
import org.cactoos.Scalar;

/**
* Request to GitHub API.
*
* @since 0.0.0
*/
public final class GhRequest implements Scalar<Request> {

@Override
public Request value() throws Exception {
return new JdkRequest("https://api.github.com");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package git.tracehub.codereview.action.github;

import com.jcabi.github.Pull;
import com.jcabi.http.Request;
import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonArray;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.cactoos.Scalar;

/**
* All review comments, presented in JSON format.
*
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class JsonComments implements Comments {

/**
* Request.
*/
private final Scalar<Request> request;

/**
* Pull request.
*/
private final Pull pull;

/**
* Review ID.
*/
private final int review;

@Override
@SneakyThrows
public JsonArray value() {
return Json.createReader(
new StringReader(
this.request.value()
.uri()
.path(
String.format(
"repos/%s/%s/pulls/%s/reviews/%s/comments",
this.pull.repo().coordinates().user(),
this.pull.repo().coordinates().repo(),
this.pull.number(),
this.review
)
)
.back()
.fetch()
.body()
)
).readArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package git.tracehub.codereview.action.github;

import com.jcabi.github.Pull;
import com.jcabi.github.Repo;
import java.io.IOException;
import javax.json.JsonObject;
Expand All @@ -35,7 +36,7 @@
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class JsonPull implements Scalar<JsonObject> {
public final class PullRequest implements Scalar<Pull> {

/**
* Repo.
Expand All @@ -48,9 +49,9 @@ public final class JsonPull implements Scalar<JsonObject> {
private final JsonObject event;

@Override
public JsonObject value() throws IOException {
public Pull value() throws IOException {
return this.repo.pulls().get(
this.event.getJsonObject("pull_request").getInt("number")
).json();
);
}
}
71 changes: 71 additions & 0 deletions src/main/java/git/tracehub/codereview/action/github/Reviews.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package git.tracehub.codereview.action.github;

import com.jcabi.github.Pull;
import com.jcabi.http.Request;
import java.io.StringReader;
import javax.json.Json;
import javax.json.JsonArray;
import lombok.RequiredArgsConstructor;
import org.cactoos.Scalar;

/**
* Code Reviews for pull request.
*
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class Reviews implements Scalar<JsonArray> {

/**
* Pull request.
*/
private final Pull pull;

/**
* Request.
*/
private final Scalar<Request> request;

@Override
public JsonArray value() throws Exception {
return Json.createReader(
new StringReader(
this.request.value()
.uri()
.path(
String.format(
"/repos/%s/%s/pulls/%s/reviews",
this.pull.repo().coordinates().user(),
this.pull.repo().coordinates().repo(),
this.pull.number()
)
).back()
.fetch()
.body()
)
).readArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,42 @@
import com.jcabi.github.Repo;
import com.jcabi.github.mock.MkGithub;
import javax.json.Json;
import javax.json.JsonObject;
import org.cactoos.io.ResourceOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link JsonPull}.
* Test case for {@link PullRequest}.
*
* @since 0.0.0
*/
final class JsonPullTest {
final class PullRequestTest {

@Test
void readsJsonPull() throws Exception {
final Repo repo = new MkGithub().randomRepo();
final Pull created = repo.pulls().create("test", "master", "master");
final JsonObject pull = new JsonPull(
final int expected = created.number();
final Pull pull = new PullRequest(
repo,
Json.createObjectBuilder()
.add(
"pull_request",
Json.createObjectBuilder()
.add("number", created.number())
.add("number", expected)
.build()
)
.build()
).value();
final JsonObject expected = Json.createReader(
new ResourceOf(
"git/tracehub/codereview/action/github/pull.json"
).stream()
).readObject();
final int number = pull.number();
MatcherAssert.assertThat(
String.format(
"Received JSON (%s) does not match with expected (%s)",
"Received Pull Request number #%s (%s) does not match with expected (%s)",
number,
pull,
expected
),
pull,
number,
new IsEqual<>(expected)
);
}
Expand Down
Loading

0 comments on commit c213ca5

Please sign in to comment.