Coverage Report - com.jcabi.github.RtRepoCommits
 
Classes in this File Line Coverage Branch Coverage Complexity
RtRepoCommits
92%
35/38
0%
0/28
1
RtRepoCommits$1
100%
2/2
N/A
1
RtRepoCommits$AjcClosure1
100%
1/1
N/A
1
RtRepoCommits$AjcClosure11
0%
0/1
N/A
1
RtRepoCommits$AjcClosure3
100%
1/1
N/A
1
RtRepoCommits$AjcClosure5
100%
1/1
N/A
1
RtRepoCommits$AjcClosure7
100%
1/1
N/A
1
RtRepoCommits$AjcClosure9
100%
1/1
N/A
1
 
 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.RequestURI;
 36  
 import com.jcabi.http.response.RestResponse;
 37  
 import java.io.IOException;
 38  
 import java.net.HttpURLConnection;
 39  
 import java.util.Map;
 40  
 import javax.json.JsonObject;
 41  
 import javax.ws.rs.core.HttpHeaders;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Commits of a Github repository.
 46  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 47  
  * @version $Id: c84c6d90fcad87207ab70cbe69b2aaa3a79629ba $
 48  
  * @checkstyle MultipleStringLiteralsCheck (200 lines)
 49  
  */
 50  
 @Immutable
 51  
 @Loggable(Loggable.DEBUG)
 52  0
 @EqualsAndHashCode(of = { "request", "owner", "entry" })
 53  
 final class RtRepoCommits implements RepoCommits {
 54  
 
 55  
     /**
 56  
      * RESTful API entry point.
 57  
      */
 58  
     private final transient Request entry;
 59  
 
 60  
     /**
 61  
      * RESTful request for the commits.
 62  
      */
 63  
     private final transient Request request;
 64  
 
 65  
     /**
 66  
      * RESTful request for the comparison.
 67  
      */
 68  
     private final transient Request comp;
 69  
 
 70  
     /**
 71  
      * Parent repository.
 72  
      */
 73  
     private final transient Repo owner;
 74  
 
 75  
     /**
 76  
      * Public ctor.
 77  
      * @param req Entry point of API
 78  
      * @param repo Repository
 79  
      */
 80  8
     RtRepoCommits(final Request req, final Repo repo) {
 81  8
         this.entry = req;
 82  8
         this.owner = repo;
 83  8
         final RequestURI rep = req.uri()
 84  7
             .path("/repos")
 85  7
             .path(repo.coordinates().user())
 86  8
             .path(repo.coordinates().repo());
 87  8
         this.request = rep
 88  7
             .path("/commits")
 89  7
             .back();
 90  7
         this.comp = rep
 91  7
             .path("/compare")
 92  7
             .back();
 93  7
     }
 94  
 
 95  
     @Override
 96  
     public  Iterable<RepoCommit> iterate(
 97  
         final Map<String, String> params
 98  
     ) {
 99  3
         return new RtPagination<RepoCommit>(
 100  1
             this.request.uri().queryParams(params).back(),
 101  2
             new RtValuePagination.Mapping<RepoCommit, JsonObject>() {
 102  
                 @Override
 103  
                 public RepoCommit map(final JsonObject value) {
 104  1
                     return RtRepoCommits.this.get(value.getString("sha"));
 105  
                 }
 106  
             }
 107  
         );
 108  
     }
 109  
 
 110  
     @Override
 111  
     public RepoCommit get(
 112  
         final String sha
 113  
     ) {
 114  6
         return new RtRepoCommit(this.entry, this.owner, sha);
 115  
     }
 116  
 
 117  
     @Override
 118  
     public CommitsComparison compare(
 119  
         final String base,
 120  
         final String head) {
 121  4
         return new RtCommitsComparison(this.entry, this.owner, base, head);
 122  
     }
 123  
 
 124  
     @Override
 125  
     public String diff(
 126  
         final String base,
 127  
         final String head)
 128  
         throws IOException {
 129  3
         return this.comp.reset(HttpHeaders.ACCEPT)
 130  1
             .header(HttpHeaders.ACCEPT, "application/vnd.github.v3.diff")
 131  1
             .uri()
 132  1
             .path(String.format("%s...%s", base, head))
 133  1
             .back()
 134  1
             .fetch().as(RestResponse.class)
 135  1
             .assertStatus(HttpURLConnection.HTTP_OK)
 136  1
             .body();
 137  
     }
 138  
 
 139  
     @Override
 140  
     public String patch(
 141  
         final String base,
 142  
         final String head)
 143  
         throws IOException {
 144  3
         return this.comp.reset(HttpHeaders.ACCEPT)
 145  1
             .header(HttpHeaders.ACCEPT, "application/vnd.github.v3.patch")
 146  1
             .uri()
 147  1
             .path(String.format("%s...%s", base, head))
 148  1
             .back()
 149  1
             .fetch().as(RestResponse.class)
 150  1
             .assertStatus(HttpURLConnection.HTTP_OK)
 151  1
             .body();
 152  
     }
 153  
 
 154  
     @Override
 155  
     public String toString() {
 156  0
         return this.request.uri().get().toString();
 157  
     }
 158  
 
 159  
     @Override
 160  
     public JsonObject json() throws IOException {
 161  0
         return new RtJson(this.request).fetch();
 162  
     }
 163  
 }