Skip to content

Commit

Permalink
feat(#2, #31): fixed transformation with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Apr 1, 2024
1 parent 8dc41d4 commit 16e3b4b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 52 deletions.
50 changes: 24 additions & 26 deletions src/main/java/git/tracehub/codereview/action/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,18 @@

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.Authored;
import git.tracehub.codereview.action.github.EventPull;
import git.tracehub.codereview.action.github.FixedReviews;
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 git.tracehub.codereview.action.github.JsonReviews;
import git.tracehub.codereview.action.github.WithComments;
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.JsonArray;
import javax.json.JsonObject;

/**
Expand Down Expand Up @@ -88,24 +86,24 @@ public static void main(final String... args) throws Exception {
).readObject();
Logger.info(Entry.class, "event received %s", event.toString());
final String token = System.getenv().get("INPUT_GITHUB_TOKEN");
final Repo repo = new RtGithub(token)
.repos()
.get(new Coordinates.Simple(name));
final Pull pull = new PullRequest(repo, event).value();
Logger.info(Entry.class, "pull request found: %s", pull);
new ReviewIds(new Reviews(pull, new GhRequest(token))).value()
.forEach(
review -> {
final List<String> comments = new Authored(
new JsonComments(new GhRequest(token), pull, review)
).value();
Logger.info(
Entry.class,
"found comments for review #%s: %s",
review,
comments
);
}
);
final Pull.Smart pull = new Pull.Smart(
new EventPull(
new RtGithub(token).repos().get(new Coordinates.Simple(name)),
event
).value()
);
final String title = pull.title();
Logger.info(
Entry.class,
"pull request found: #%s '%s'",
pull.number(),
title
);
final JsonArray reviews = new WithComments(
new FixedReviews(new JsonReviews(pull, new GhRequest(token))),
new GhRequest(token),
pull
).value();
Logger.info(Entry.class, "found reviews: %s", reviews);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
*/
package git.tracehub.codereview.action.github;

import java.util.List;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.cactoos.Scalar;
import org.cactoos.list.ListOf;

/**
* All review comment bodies together with its author.
*
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class Authored implements Scalar<List<String>> {
public final class Authored implements Comments {

/**
* Comments.
Expand All @@ -44,10 +44,10 @@ public final class Authored implements Scalar<List<String>> {

@Override
@SneakyThrows
public List<String> value() {
final List<String> bodies = new ListOf<>();
public JsonArray value() {
final JsonArrayBuilder builder = Json.createArrayBuilder();
this.origin.value().forEach(
value -> bodies.add(
value -> builder.add(
String.format(
"%s: %s",
value.asJsonObject()
Expand All @@ -57,6 +57,6 @@ public List<String> value() {
)
)
);
return bodies;
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class PullRequest implements Scalar<Pull> {
public final class EventPull implements Scalar<Pull> {

/**
* Repo.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,36 @@
*/
package git.tracehub.codereview.action.github;

import java.util.List;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import lombok.RequiredArgsConstructor;
import org.cactoos.Scalar;
import org.cactoos.list.ListOf;

/**
* Identifiers collected from all reviews.
*
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class ReviewIds implements Scalar<List<Integer>> {
public final class FixedReviews implements Scalar<JsonArray> {

/**
* All reviews.
*/
private final Scalar<JsonArray> reviews;

@Override
public List<Integer> value() throws Exception {
final List<Integer> identifiers = new ListOf<>();
public JsonArray value() throws Exception {
final JsonArrayBuilder builder = Json.createArrayBuilder();
this.reviews.value().forEach(
value -> identifiers.add(
value.asJsonObject().getInt("id")
value -> builder.add(
Json.createObjectBuilder()
.add("id", value.asJsonObject().getInt("id"))
.add("body", value.asJsonObject().getString("body"))
.build()
)
);
return identifiers;
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class Reviews implements Scalar<JsonArray> {
public final class JsonReviews implements Scalar<JsonArray> {

/**
* Pull request.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.util.function.Consumer;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.cactoos.Scalar;

/**
* Reviews with comments.
*
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class WithComments implements Scalar<JsonArray> {

/**
* Origin.
*/
private final Scalar<JsonArray> origin;

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

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

@Override
public JsonArray value() throws Exception {
final JsonArray base = this.origin.value();
base.forEach(
review -> {
final JsonObject json = review.asJsonObject();
final int identifier = json.getInt("id");
json.put(
"comments",
new Authored(
new JsonComments(
this.request,
this.pull,
identifier
)
).value()
);
}
);
return base;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
import org.junit.jupiter.api.Test;

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

@Test
void readsJsonPull() throws Exception {
final Repo repo = new MkGithub().randomRepo();
final Pull created = repo.pulls().create("test", "master", "master");
final int expected = created.number();
final Pull pull = new PullRequest(
final Pull pull = new EventPull(
repo,
Json.createObjectBuilder()
.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@
import java.io.InputStreamReader;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArray;
import org.cactoos.io.ResourceOf;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link ReviewIds}.
* Test case for {@link FixedReviews}.
*
* @since 0.0.0
*/
final class ReviewIdsTest {
final class FixedReviewsTest {

@Test
void collectsAllIds() throws Exception {
final List<Integer> identifiers = new ReviewIds(
final JsonArray reviews = new FixedReviews(
() -> Json.createReader(
new InputStreamReader(
new ResourceOf(
Expand Down Expand Up @@ -85,10 +86,10 @@ void collectsAllIds() throws Exception {
MatcherAssert.assertThat(
String.format(
"Collected identifiers (%s) do not match with expected (%s)",
identifiers,
reviews,
expected
),
identifiers,
reviews,
new IsEqual<>(expected)
);
}
Expand Down

0 comments on commit 16e3b4b

Please sign in to comment.