Coverage Report - com.jcabi.github.RtPull
 
Classes in this File Line Coverage Branch Coverage Complexity
RtPull
62%
38/61
0%
0/25
1.125
RtPull$1
20%
1/5
N/A
1.125
RtPull$AjcClosure1
0%
0/1
N/A
1.125
RtPull$AjcClosure11
0%
0/1
N/A
1.125
RtPull$AjcClosure13
0%
0/1
N/A
1.125
RtPull$AjcClosure15
100%
1/1
N/A
1.125
RtPull$AjcClosure17
100%
1/1
N/A
1.125
RtPull$AjcClosure19
100%
1/1
N/A
1.125
RtPull$AjcClosure21
0%
0/1
N/A
1.125
RtPull$AjcClosure23
100%
1/1
N/A
1.125
RtPull$AjcClosure3
100%
1/1
N/A
1.125
RtPull$AjcClosure5
100%
1/1
N/A
1.125
RtPull$AjcClosure7
100%
1/1
N/A
1.125
RtPull$AjcClosure9
100%
1/1
N/A
1.125
 
 1  2
 /**
 2  
  * Copyright (c) 2013-2017, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.github;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.response.RestResponse;
 36  
 import java.io.IOException;
 37  
 import java.net.HttpURLConnection;
 38  
 import javax.json.Json;
 39  
 import javax.json.JsonObject;
 40  
 import javax.json.JsonObjectBuilder;
 41  
 import javax.json.JsonStructure;
 42  
 import lombok.EqualsAndHashCode;
 43  
 import org.hamcrest.Matchers;
 44  
 
 45  
 /**
 46  
  * Github pull request.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id: 2b4a2c75941ec5d4f7f131352f1b4697c973fdf0 $
 50  
  * @since 0.3
 51  
  * @checkstyle MultipleStringLiterals (500 lines)
 52  
  */
 53  0
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = { "request", "owner", "num" })
 56  
 @SuppressWarnings("PMD.TooManyMethods")
 57  0
 final class RtPull implements Pull {
 58  
 
 59  
     /**
 60  
      * API entry point.
 61  
      */
 62  
     private final transient Request entry;
 63  
 
 64  
     /**
 65  
      * RESTful request.
 66  
      */
 67  
     private final transient Request request;
 68  
 
 69  
     /**
 70  
      * Repository we're in.
 71  
      */
 72  
     private final transient Repo owner;
 73  
 
 74  
     /**
 75  
      * Pull request number.
 76  
      */
 77  
     private final transient int num;
 78  
 
 79  
     /**
 80  
      * Public ctor.
 81  
      * @param req Request
 82  
      * @param repo Repository
 83  
      * @param number Number of the get
 84  
      */
 85  11
     RtPull(final Request req, final Repo repo, final int number) {
 86  11
         this.entry = req;
 87  11
         final Coordinates coords = repo.coordinates();
 88  11
         this.request = this.entry.uri()
 89  11
             .path("/repos")
 90  11
             .path(coords.user())
 91  11
             .path(coords.repo())
 92  11
             .path("/pulls")
 93  11
             .path(Integer.toString(number))
 94  11
             .back();
 95  11
         this.owner = repo;
 96  11
         this.num = number;
 97  11
     }
 98  
 
 99  
     @Override
 100  
     public String toString() {
 101  0
         return this.request.uri().get().toString();
 102  
     }
 103  
 
 104  
     @Override
 105  
     public Repo repo() {
 106  0
         return this.owner;
 107  
     }
 108  
 
 109  
     @Override
 110  
     public int number() {
 111  8
         return this.num;
 112  
     }
 113  
 
 114  
     @Override
 115  
     public Iterable<Commit> commits() throws IOException {
 116  3
         return new RtPagination<Commit>(
 117  1
             this.request.uri().path("/commits").back(),
 118  1
             new RtValuePagination.Mapping<Commit, JsonObject>() {
 119  
                 @Override
 120  
                 public Commit map(final JsonObject object) {
 121  0
                     return new RtCommit(
 122  0
                         RtPull.this.entry,
 123  0
                         RtPull.this.owner,
 124  0
                         object.getString("sha")
 125  
                     );
 126  
                 }
 127  
             }
 128  
         );
 129  
     }
 130  
 
 131  
     @Override
 132  
     public Iterable<JsonObject> files() throws IOException {
 133  3
         return new RtPagination<JsonObject>(
 134  1
                 this.request.uri().path("/files").back(),
 135  
                 RtPagination.COPYING
 136  
         );
 137  
     }
 138  
 
 139  
     @Override
 140  
     public void merge(
 141  
         final String msg)
 142  
         throws IOException {
 143  2
         final JsonStructure json = Json.createObjectBuilder()
 144  1
             .add("commit_message", msg)
 145  1
             .build();
 146  1
         this.merge(json).assertStatus(HttpURLConnection.HTTP_OK);
 147  1
     }
 148  
 
 149  
     @Override
 150  
     public MergeState merge(
 151  
         final String msg,
 152  
         final String sha)
 153  
         throws IOException {
 154  0
         final JsonObjectBuilder builder = Json.createObjectBuilder()
 155  0
             .add("commit_message", msg).add("sha", sha);
 156  0
         final RestResponse response = this.merge(builder.build())
 157  0
             .assertStatus(
 158  0
                 Matchers.isOneOf(
 159  0
                     HttpURLConnection.HTTP_OK,
 160  0
                     HttpURLConnection.HTTP_BAD_METHOD,
 161  0
                     HttpURLConnection.HTTP_CONFLICT
 162  
                 )
 163  
             );
 164  
         final MergeState mergeState;
 165  0
         switch (response.status()) {
 166  
             case HttpURLConnection.HTTP_OK:
 167  0
                 mergeState = MergeState.SUCCESS;
 168  0
                 break;
 169  
             case HttpURLConnection.HTTP_BAD_METHOD:
 170  0
                 mergeState = MergeState.NOT_MERGEABLE;
 171  0
                 break;
 172  
             default:
 173  0
                 mergeState = MergeState.BAD_HEAD;
 174  
                 break;
 175  
         }
 176  0
         return mergeState;
 177  
     }
 178  
 
 179  
     @Override
 180  
     public PullComments comments() throws IOException {
 181  0
         return new RtPullComments(this.entry, this);
 182  
     }
 183  
 
 184  
     @Override
 185  
     public PullRef base() throws IOException {
 186  3
         return new RtPullRef(
 187  1
             this.owner.github(),
 188  1
             this.json().getJsonObject("base")
 189  
         );
 190  
     }
 191  
 
 192  
     @Override
 193  
     public PullRef head() throws IOException {
 194  3
         return new RtPullRef(
 195  1
             this.owner.github(),
 196  1
             this.json().getJsonObject("head")
 197  
         );
 198  
     }
 199  
 
 200  
     @Override
 201  
     public JsonObject json() throws IOException {
 202  8
         return new RtJson(this.request).fetch();
 203  
     }
 204  
 
 205  
     @Override
 206  
     public void patch(final JsonObject json) throws IOException {
 207  0
         new RtJson(this.request).patch(json);
 208  0
     }
 209  
 
 210  
     @Override
 211  
     public int compareTo(
 212  
         final Pull pull
 213  
     ) {
 214  4
         return this.number() - pull.number();
 215  
     }
 216  
 
 217  
     /**
 218  
      * Helper method for merge operations.
 219  
      * @param payload The JSON payload for the merge
 220  
      * @return Response received from GitHub
 221  
      * @throws IOException If there is any I/O problem
 222  
      */
 223  
     private RestResponse merge(final JsonStructure payload)
 224  
         throws IOException {
 225  2
         return this.request.uri()
 226  1
             .path("/merge").back()
 227  1
             .body().set(payload).back()
 228  1
             .method(Request.PUT)
 229  1
             .fetch()
 230  1
             .as(RestResponse.class);
 231  
     }
 232  
 
 233  
 }