Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RtPulls |
|
| 1.0;1 | ||||
RtPulls$1 |
|
| 1.0;1 | ||||
RtPulls$AjcClosure1 |
|
| 1.0;1 | ||||
RtPulls$AjcClosure3 |
|
| 1.0;1 | ||||
RtPulls$AjcClosure5 |
|
| 1.0;1 | ||||
RtPulls$AjcClosure7 |
|
| 1.0;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.response.JsonResponse; | |
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.Json; | |
41 | import javax.json.JsonObject; | |
42 | import javax.json.JsonStructure; | |
43 | import lombok.EqualsAndHashCode; | |
44 | ||
45 | /** | |
46 | * Github pull requests. | |
47 | * | |
48 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
49 | * @version $Id: 282fe9819fc9d6a0d7b22980134bf80d8aa16871 $ | |
50 | * @since 0.3 | |
51 | * @checkstyle MultipleStringLiterals (500 lines) | |
52 | */ | |
53 | @Immutable | |
54 | @Loggable(Loggable.DEBUG) | |
55 | 0 | @EqualsAndHashCode(of = { "entry", "request", "owner" }) |
56 | final class RtPulls implements Pulls { | |
57 | ||
58 | /** | |
59 | * API entry point. | |
60 | */ | |
61 | private final transient Request entry; | |
62 | ||
63 | /** | |
64 | * RESTful request. | |
65 | */ | |
66 | private final transient Request request; | |
67 | ||
68 | /** | |
69 | * Repository. | |
70 | */ | |
71 | private final transient Repo owner; | |
72 | ||
73 | /** | |
74 | * Public ctor. | |
75 | * @param req Request | |
76 | * @param repo Repository | |
77 | */ | |
78 | 4 | RtPulls(final Request req, final Repo repo) { |
79 | 4 | this.entry = req; |
80 | 4 | final Coordinates coords = repo.coordinates(); |
81 | 4 | this.request = this.entry.uri() |
82 | 4 | .path("/repos") |
83 | 4 | .path(coords.user()) |
84 | 4 | .path(coords.repo()) |
85 | 4 | .path("/pulls") |
86 | 4 | .back(); |
87 | 4 | this.owner = repo; |
88 | 4 | } |
89 | ||
90 | @Override | |
91 | public String toString() { | |
92 | 0 | return this.request.uri().get().toString(); |
93 | } | |
94 | ||
95 | @Override | |
96 | public Repo repo() { | |
97 | 0 | return this.owner; |
98 | } | |
99 | ||
100 | @Override | |
101 | public Pull get(final int number) { | |
102 | 8 | return new RtPull(this.entry, this.owner, number); |
103 | } | |
104 | ||
105 | @Override | |
106 | public Pull create( | |
107 | final String title, | |
108 | final String head, | |
109 | final String base) | |
110 | throws IOException { | |
111 | 2 | final JsonStructure json = Json.createObjectBuilder() |
112 | 1 | .add("title", title) |
113 | 1 | .add("head", head) |
114 | 1 | .add("base", base) |
115 | 1 | .build(); |
116 | 2 | return this.get( |
117 | 1 | this.request.method(Request.POST) |
118 | 1 | .body().set(json).back() |
119 | 1 | .fetch().as(RestResponse.class) |
120 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
121 | 1 | .as(JsonResponse.class) |
122 | 1 | .json().readObject().getInt("number") |
123 | ); | |
124 | } | |
125 | ||
126 | @Override | |
127 | public Iterable<Pull> iterate(final Map<String, String> params) { | |
128 | 3 | return new RtPagination<Pull>( |
129 | 1 | this.request.uri().queryParams(params).back(), |
130 | 3 | new RtValuePagination.Mapping<Pull, JsonObject>() { |
131 | @Override | |
132 | public Pull map(final JsonObject object) { | |
133 | 2 | return RtPulls.this.get(object.getInt("number")); |
134 | } | |
135 | } | |
136 | ); | |
137 | } | |
138 | ||
139 | } |