Coverage Report - com.jcabi.github.RtPull
 
Classes in this File Line Coverage Branch Coverage Complexity
RtPull
50%
17/34
0%
0/21
1.143
RtPull$1
50%
1/2
N/A
1.143
RtPull$AjcClosure1
0%
0/1
N/A
1.143
RtPull$AjcClosure11
0%
0/1
N/A
1.143
RtPull$AjcClosure13
0%
0/1
N/A
1.143
RtPull$AjcClosure15
100%
1/1
N/A
1.143
RtPull$AjcClosure17
0%
0/1
N/A
1.143
RtPull$AjcClosure19
100%
1/1
N/A
1.143
RtPull$AjcClosure3
100%
1/1
N/A
1.143
RtPull$AjcClosure5
100%
1/1
N/A
1.143
RtPull$AjcClosure7
100%
1/1
N/A
1.143
RtPull$AjcClosure9
100%
1/1
N/A
1.143
 
 1  0
 /**
 2  
  * Copyright (c) 2013-2015, 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.JsonResponse;
 36  
 import com.jcabi.http.response.RestResponse;
 37  
 import java.io.IOException;
 38  
 import java.net.HttpURLConnection;
 39  
 import javax.json.Json;
 40  
 import javax.json.JsonObject;
 41  
 import javax.json.JsonObjectBuilder;
 42  
 import javax.json.JsonStructure;
 43  
 import javax.validation.constraints.NotNull;
 44  
 import lombok.EqualsAndHashCode;
 45  
 import org.hamcrest.Matchers;
 46  
 
 47  
 /**
 48  
  * Github pull request.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 51  
  * @version $Id$
 52  
  * @since 0.3
 53  
  * @checkstyle MultipleStringLiterals (500 lines)
 54  
  */
 55  0
 @Immutable
 56  
 @Loggable(Loggable.DEBUG)
 57  0
 @EqualsAndHashCode(of = { "request", "owner", "num" })
 58  
 @SuppressWarnings("PMD.TooManyMethods")
 59  0
 final class RtPull implements Pull {
 60  
 
 61  
     /**
 62  
      * API entry point.
 63  
      */
 64  
     private final transient Request entry;
 65  
 
 66  
     /**
 67  
      * RESTful request.
 68  
      */
 69  
     private final transient Request request;
 70  
 
 71  
     /**
 72  
      * Repository we're in.
 73  
      */
 74  
     private final transient Repo owner;
 75  
 
 76  
     /**
 77  
      * Pull request number.
 78  
      */
 79  
     private final transient int num;
 80  
 
 81  
     /**
 82  
      * Public ctor.
 83  
      * @param req Request
 84  
      * @param repo Repository
 85  
      * @param number Number of the get
 86  
      */
 87  9
     RtPull(final Request req, final Repo repo, final int number) {
 88  9
         this.entry = req;
 89  9
         final Coordinates coords = repo.coordinates();
 90  9
         this.request = this.entry.uri()
 91  
             .path("/repos")
 92  
             .path(coords.user())
 93  
             .path(coords.repo())
 94  
             .path("/pulls")
 95  
             .path(Integer.toString(number))
 96  
             .back();
 97  9
         this.owner = repo;
 98  9
         this.num = number;
 99  9
     }
 100  
 
 101  
     @Override
 102  
     @NotNull(message = "toString is never NULL")
 103  
     public String toString() {
 104  0
         return this.request.uri().get().toString();
 105  
     }
 106  
 
 107  
     @Override
 108  
     @NotNull(message = "repository is never NULL")
 109  
     public Repo repo() {
 110  0
         return this.owner;
 111  
     }
 112  
 
 113  
     @Override
 114  
     public int number() {
 115  8
         return this.num;
 116  
     }
 117  
 
 118  
     @Override
 119  
     @NotNull(message = "Iterable of commits is never NULL")
 120  
     public Iterable<Commit> commits() throws IOException {
 121  2
         return new RtPagination<Commit>(
 122  
             this.request.uri().path("/commits").back(),
 123  1
             new RtValuePagination.Mapping<Commit, JsonObject>() {
 124  
                 @Override
 125  
                 public Commit map(final JsonObject object) {
 126  0
                     return new RtCommit(
 127  
                         RtPull.this.entry,
 128  
                         RtPull.this.owner,
 129  
                         object.getString("sha")
 130  
                     );
 131  
                 }
 132  
             }
 133  
         );
 134  
     }
 135  
 
 136  
     @Override
 137  
     @NotNull(message = "Iterable of files is never NULL")
 138  
     public Iterable<JsonObject> files() throws IOException {
 139  2
         return this.request
 140  
             .uri().path("/files").back()
 141  
             .fetch().as(RestResponse.class)
 142  
             .assertStatus(HttpURLConnection.HTTP_OK)
 143  
             .as(JsonResponse.class)
 144  
             .json().readArray().getValuesAs(JsonObject.class);
 145  
     }
 146  
 
 147  
     @Override
 148  
     public void merge(
 149  
         @NotNull(message = "message can't be NULL") final String msg)
 150  
         throws IOException {
 151  2
         final JsonStructure json = Json.createObjectBuilder()
 152  
             .add("commit_message", msg)
 153  
             .build();
 154  1
         this.merge(json).assertStatus(HttpURLConnection.HTTP_OK);
 155  1
     }
 156  
 
 157  
     @Override
 158  
     public MergeState merge(
 159  
         @NotNull(message = "message can't be NULL") final String msg,
 160  
         @NotNull(message = "sha can't be NULL") final String sha)
 161  
         throws IOException {
 162  0
         final JsonObjectBuilder builder = Json.createObjectBuilder()
 163  
             .add("commit_message", msg).add("sha", sha);
 164  0
         final RestResponse response = this.merge(builder.build())
 165  
             .assertStatus(
 166  
                 Matchers.isOneOf(
 167  
                     HttpURLConnection.HTTP_OK,
 168  
                     HttpURLConnection.HTTP_BAD_METHOD,
 169  
                     HttpURLConnection.HTTP_CONFLICT
 170  
                 )
 171  
             );
 172  
         final MergeState mergeState;
 173  0
         switch (response.status()) {
 174  
             case HttpURLConnection.HTTP_OK:
 175  0
                 mergeState = MergeState.SUCCESS;
 176  0
                 break;
 177  
             case HttpURLConnection.HTTP_BAD_METHOD:
 178  0
                 mergeState = MergeState.NOT_MERGEABLE;
 179  0
                 break;
 180  
             default:
 181  0
                 mergeState = MergeState.BAD_HEAD;
 182  
         }
 183  0
         return mergeState;
 184  
     }
 185  
 
 186  
     @Override
 187  
     @NotNull(message = "PullComments is never NULL")
 188  
     public PullComments comments() throws IOException {
 189  0
         return new RtPullComments(this.entry, this);
 190  
     }
 191  
 
 192  
     @Override
 193  
     @NotNull(message = "JSON is never NULL")
 194  
     public JsonObject json() throws IOException {
 195  4
         return new RtJson(this.request).fetch();
 196  
     }
 197  
 
 198  
     @Override
 199  
     public void patch(@NotNull(message = "JSON can't be NULL")
 200  
         final JsonObject json) throws IOException {
 201  0
         new RtJson(this.request).patch(json);
 202  0
     }
 203  
 
 204  
     @Override
 205  
     public int compareTo(
 206  
         @NotNull(message = "pull can't be NULL") final Pull pull
 207  
     ) {
 208  4
         return this.number() - pull.number();
 209  
     }
 210  
 
 211  
     /**
 212  
      * Helper method for merge operations.
 213  
      * @param payload The JSON payload for the merge
 214  
      * @return Response received from GitHub
 215  
      * @throws IOException If there is any I/O problem
 216  
      */
 217  
     private RestResponse merge(final JsonStructure payload)
 218  
         throws IOException {
 219  1
         return this.request.uri()
 220  
             .path("/merge").back()
 221  
             .body().set(payload).back()
 222  
             .method(Request.PUT)
 223  
             .fetch()
 224  
             .as(RestResponse.class);
 225  
     }
 226  
 
 227  
 }