From 8d14d76cf5347c9b13e17674c7fac4a2730ff39b Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 17 Mar 2023 14:46:36 +0300 Subject: [PATCH] Add checks to Pull interface --- src/main/java/com/jcabi/github/Pull.java | 43 +++++++- src/main/java/com/jcabi/github/RtPull.java | 5 + .../java/com/jcabi/github/mock/MkChecks.java | 50 +++++++++ .../java/com/jcabi/github/mock/MkPull.java | 19 +++- .../java/com/jcabi/github/RtPullTest.java | 102 ++++++++++++++++-- .../com/jcabi/github/mock/MkPullTest.java | 10 ++ 6 files changed, 211 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/jcabi/github/mock/MkChecks.java diff --git a/src/main/java/com/jcabi/github/Pull.java b/src/main/java/com/jcabi/github/Pull.java index 8be41d9a9..ae4b8e893 100644 --- a/src/main/java/com/jcabi/github/Pull.java +++ b/src/main/java/com/jcabi/github/Pull.java @@ -113,7 +113,8 @@ void merge(String msg) * @throws IOException IOException If there is any I/O problem */ MergeState merge(String msg, - String sha) throws IOException; + String sha + ) throws IOException; /** * Get Pull Comments. @@ -123,13 +124,21 @@ MergeState merge(String msg, */ PullComments comments() throws IOException; + /** + * Get Pull Checks. + * @return Checks. + * @throws IOException If there is any I/O problem. + * @see Checks API + */ + Checks checks() throws IOException; + /** * Smart pull request with extra features. */ @Immutable @ToString @Loggable(Loggable.DEBUG) - @EqualsAndHashCode(of = { "pull", "jsn" }) + @EqualsAndHashCode(of = {"pull", "jsn"}) final class Smart implements Pull { /** * Encapsulated pull request. @@ -139,6 +148,7 @@ final class Smart implements Pull { * SmartJson object for convenient JSON parsing. */ private final transient SmartJson jsn; + /** * Public ctor. * @param pll Pull request @@ -149,6 +159,7 @@ public Smart( this.pull = pll; this.jsn = new SmartJson(pll); } + /** * Is it open? * @return TRUE if it's open @@ -157,6 +168,7 @@ public Smart( public boolean isOpen() throws IOException { return Issue.OPEN_STATE.equals(this.state()); } + /** * Get its state. * @return State of pull request @@ -165,6 +177,7 @@ public boolean isOpen() throws IOException { public String state() throws IOException { return this.jsn.text("state"); } + /** * Change its state. * @param state State of pull request @@ -177,6 +190,7 @@ public void state( Json.createObjectBuilder().add("state", state).build() ); } + /** * Get its title. * @return Title of pull request @@ -185,6 +199,7 @@ public void state( public String title() throws IOException { return this.jsn.text("title"); } + /** * Change its title. * @param text Title of pull request @@ -197,6 +212,7 @@ public void title( Json.createObjectBuilder().add("title", text).build() ); } + /** * Get its body. * @return Body of pull request @@ -205,6 +221,7 @@ public void title( public String body() throws IOException { return this.jsn.text("body"); } + /** * Change its body. * @param text Body of pull request @@ -217,6 +234,7 @@ public void body( Json.createObjectBuilder().add("body", text).build() ); } + /** * Get its URL. * @return URL of pull request @@ -225,6 +243,7 @@ public void body( public URL url() throws IOException { return new URL(this.jsn.text("url")); } + /** * Get its HTML URL. * @return URL of pull request @@ -233,6 +252,7 @@ public URL url() throws IOException { public URL htmlUrl() throws IOException { return new URL(this.jsn.text("html_url")); } + /** * When this pull request was created. * @return Date of creation @@ -247,6 +267,7 @@ public Date createdAt() throws IOException { throw new IllegalStateException(ex); } } + /** * When this pull request was updated. * @return Date of update @@ -261,6 +282,7 @@ public Date updatedAt() throws IOException { throw new IllegalStateException(ex); } } + /** * When this pull request was closed. * @return Date of closing @@ -275,6 +297,7 @@ public Date closedAt() throws IOException { throw new IllegalStateException(ex); } } + /** * When this pull request was merged. * @return Date of merging @@ -310,6 +333,7 @@ public User author() throws IOException { public Issue issue() { return this.pull.repo().issues().get(this.pull.number()); } + /** * Get comments count. * @return Count of comments @@ -319,22 +343,27 @@ public Issue issue() { public int commentsCount() throws IOException { return this.jsn.number("comments"); } + @Override public Repo repo() { return this.pull.repo(); } + @Override public int number() { return this.pull.number(); } + @Override public Iterable commits() throws IOException { return this.pull.commits(); } + @Override public Iterable files() throws IOException { return this.pull.files(); } + @Override public void merge( final String msg @@ -345,7 +374,8 @@ public void merge( @Override public MergeState merge( final String msg, - final String sha) + final String sha + ) throws IOException { return this.pull.merge(msg, sha); } @@ -355,16 +385,23 @@ public PullComments comments() throws IOException { return this.pull.comments(); } + @Override + public Checks checks() throws IOException { + return this.pull.checks(); + } + @Override public JsonObject json() throws IOException { return this.pull.json(); } + @Override public void patch( final JsonObject json ) throws IOException { this.pull.patch(json); } + @Override public int compareTo( final Pull obj diff --git a/src/main/java/com/jcabi/github/RtPull.java b/src/main/java/com/jcabi/github/RtPull.java index 5cae961df..69acde02e 100644 --- a/src/main/java/com/jcabi/github/RtPull.java +++ b/src/main/java/com/jcabi/github/RtPull.java @@ -178,6 +178,11 @@ public PullComments comments() { return new RtPullComments(this.entry, this); } + @Override + public Checks checks() throws IOException { + return new RtChecks(this.entry, this); + } + @Override public PullRef base() throws IOException { return new RtPullRef( diff --git a/src/main/java/com/jcabi/github/mock/MkChecks.java b/src/main/java/com/jcabi/github/mock/MkChecks.java new file mode 100644 index 000000000..ac4eecb09 --- /dev/null +++ b/src/main/java/com/jcabi/github/mock/MkChecks.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2013-2023, jcabi.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the jcabi.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.jcabi.github.mock; + +import com.jcabi.github.Check; +import com.jcabi.github.Checks; +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +/** + * Mock Github Checks. + * + * @author Volodya Lombrozo (volodya.lombrozo@gmail.com) + * @version $Id$ + * @since 1.6.0 + */ +public final class MkChecks implements Checks { + @Override + public Collection all() throws IOException { + return Collections.emptyList(); + } +} diff --git a/src/main/java/com/jcabi/github/mock/MkPull.java b/src/main/java/com/jcabi/github/mock/MkPull.java index 68173cf4a..f35f4dc39 100644 --- a/src/main/java/com/jcabi/github/mock/MkPull.java +++ b/src/main/java/com/jcabi/github/mock/MkPull.java @@ -31,6 +31,7 @@ import com.jcabi.aspects.Immutable; import com.jcabi.aspects.Loggable; +import com.jcabi.github.Checks; import com.jcabi.github.Commit; import com.jcabi.github.Coordinates; import com.jcabi.github.MergeState; @@ -53,12 +54,13 @@ * Mock Github pull. * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) * @since 0.5 */ @Immutable @Loggable(Loggable.DEBUG) @ToString -@EqualsAndHashCode(of = { "storage", "self", "coords", "num" }) +@EqualsAndHashCode(of = {"storage", "self", "coords", "num"}) @SuppressWarnings("PMD.TooManyMethods") final class MkPull implements Pull { /** @@ -123,7 +125,8 @@ final class MkPull implements Pull { final MkStorage stg, final String login, final Coordinates rep, - final int number) { + final int number + ) { this.storage = stg; this.self = login; this.coords = rep; @@ -200,7 +203,8 @@ public void merge( @Override public MergeState merge( final String msg, - final String sha) { + final String sha + ) { throw new UnsupportedOperationException("Merge not supported"); } @@ -209,6 +213,11 @@ public PullComments comments() throws IOException { return new MkPullComments(this.storage, this.self, this.coords, this); } + @Override + public Checks checks() { + return new MkChecks(); + } + @Override public int compareTo( final Pull pull @@ -241,8 +250,8 @@ public JsonObject json() throws IOException { json.add( MkPull.USER_PROP, Json.createObjectBuilder() - .add("login", xml.xpath("user/login/text()").get(0)) - .build() + .add("login", xml.xpath("user/login/text()").get(0)) + .build() ); } else if (MkPull.HEAD_PROP.equals(val.getKey())) { json.add( diff --git a/src/test/java/com/jcabi/github/RtPullTest.java b/src/test/java/com/jcabi/github/RtPullTest.java index 828b13098..a0a677c2e 100644 --- a/src/test/java/com/jcabi/github/RtPullTest.java +++ b/src/test/java/com/jcabi/github/RtPullTest.java @@ -38,7 +38,10 @@ import com.jcabi.http.request.FakeRequest; import java.io.IOException; import java.net.HttpURLConnection; +import java.util.Collection; +import java.util.Random; import javax.json.Json; +import javax.json.JsonObject; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Ignore; @@ -52,6 +55,7 @@ * @author Carlos Miranda (miranda.cma@gmail.com) * @version $Id$ */ +@SuppressWarnings("PMD.TooManyMethods") public final class RtPullTest { /** * Property name for ref name in pull request ref JSON object. @@ -184,16 +188,7 @@ public void fetchesHead() throws IOException { final MkContainer container = new MkGrizzlyContainer().next( new MkAnswer.Simple( HttpURLConnection.HTTP_OK, - Json.createObjectBuilder() - .add( - "head", - Json.createObjectBuilder() - .add(RtPullTest.REF_PROP, ref) - .add(RtPullTest.SHA_PROP, sha) - .build() - ) - .build() - .toString() + RtPullTest.head(ref, sha).toString() ) ).start(this.resource.port()) ) { @@ -250,6 +245,44 @@ public void executeMerge() throws Exception { } } + /** + * RtPull should be able to fetch pull checks. + * @throws IOException If some I/O problem occurs. + */ + @Test + public void canFetchChecks() throws IOException { + try ( + final MkContainer container = new MkGrizzlyContainer() + .next( + new MkAnswer.Simple( + HttpURLConnection.HTTP_OK, + RtPullTest.head().toString() + ) + ) + .next( + new MkAnswer.Simple( + HttpURLConnection.HTTP_OK, + RtPullTest.check().toString() + )) + .start(this.resource.port()) + ) { + final Collection all = new RtPull( + new ApacheRequest(container.home()), + this.repo(), + new Random().nextInt() + ).checks().all(); + MatcherAssert.assertThat( + all, + Matchers.hasSize(1) + ); + MatcherAssert.assertThat( + all.iterator().next().successful(), + Matchers.is(true) + ); + container.stop(); + } + } + /** * RtPull should be able to compare different instances. * @@ -289,4 +322,53 @@ private Repo repo() { return repo; } + /** + * Check as JSON object. + * @return Check as JSON object. + */ + private static JsonObject check() { + return Json.createObjectBuilder() + .add("total_count", Json.createValue(1)) + .add( + "check_runs", + Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add("id", Json.createValue(new Random().nextInt())) + .add("status", "completed") + .add("conclusion", "success") + .build() + ) + ).build(); + } + + /** + * Head as JSON object. + * @return Head as JSON object. + */ + private static JsonObject head() { + return RtPullTest.head( + "ref-ref", + "6d299617d9094ae6940b3958bbabab68fd1ddabb" + ); + } + + /** + * Head as JSON object. + * @param ref Ref. + * @param sha Sha. + * @return Head as JSON object. + */ + private static JsonObject head(final String ref, final String sha) { + return Json.createObjectBuilder() + .add( + "head", + Json.createObjectBuilder() + .add(RtPullTest.REF_PROP, ref) + .add(RtPullTest.SHA_PROP, sha) + .build() + ) + .build(); + } + } diff --git a/src/test/java/com/jcabi/github/mock/MkPullTest.java b/src/test/java/com/jcabi/github/mock/MkPullTest.java index 6897527e5..bcc70fb2a 100644 --- a/src/test/java/com/jcabi/github/mock/MkPullTest.java +++ b/src/test/java/com/jcabi/github/mock/MkPullTest.java @@ -50,6 +50,7 @@ * @checkstyle MultipleStringLiterals (500 lines) * @checkstyle JavadocMethodCheck (500 lines) */ +@SuppressWarnings("PMD.TooManyMethods") public final class MkPullTest { /** * Login of test user. @@ -242,6 +243,15 @@ public void issueIsPull() throws Exception { ); } + @Test + public void retrievesAllChecks() throws Exception { + final Pull pull = MkPullTest.pullRequest(); + MatcherAssert.assertThat( + pull.checks().all(), + Matchers.hasSize(0) + ); + } + /** * Create an repo to work with. * @return Repo