1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
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 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@Immutable |
52 | |
@Loggable(Loggable.DEBUG) |
53 | 0 | @EqualsAndHashCode(of = { "request", "owner" }) |
54 | |
final class RtPullComments implements PullComments { |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private final transient Request entry; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private final transient Request request; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private final transient Pull owner; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 6 | RtPullComments(final Request req, final Pull pull) { |
77 | 6 | this.entry = req; |
78 | 6 | this.owner = pull; |
79 | 6 | this.request = this.entry.uri() |
80 | |
|
81 | 6 | .path("/repos") |
82 | 6 | .path(pull.repo().coordinates().user()) |
83 | 6 | .path(pull.repo().coordinates().repo()) |
84 | 6 | .path("/pulls") |
85 | 6 | .path(Integer.toString(pull.number())) |
86 | 6 | .path("/comments") |
87 | 6 | .back(); |
88 | 6 | } |
89 | |
|
90 | |
@Override |
91 | |
public Pull pull() { |
92 | 0 | return this.owner; |
93 | |
} |
94 | |
|
95 | |
@Override |
96 | |
public PullComment get(final int number) { |
97 | 14 | return new RtPullComment(this.entry, this.owner, number); |
98 | |
} |
99 | |
|
100 | |
@Override |
101 | |
public Iterable<PullComment> iterate( |
102 | |
final Map<String, String> params |
103 | |
) { |
104 | 3 | return new RtPagination<PullComment>( |
105 | 1 | this.request.uri().queryParams(params).back(), |
106 | 3 | new RtValuePagination.Mapping<PullComment, JsonObject>() { |
107 | |
@Override |
108 | |
public PullComment map(final JsonObject value) { |
109 | 4 | return RtPullComments.this.get( |
110 | |
|
111 | 2 | value.getInt("id") |
112 | |
); |
113 | |
} |
114 | |
} |
115 | |
); |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public Iterable<PullComment> iterate( |
120 | |
final int number, |
121 | |
final Map<String, String> params) { |
122 | 2 | final Request newreq = this.entry.uri() |
123 | 1 | .path("/repos") |
124 | 1 | .path(this.owner.repo().coordinates().user()) |
125 | 1 | .path(this.owner.repo().coordinates().repo()) |
126 | 1 | .path("/pulls") |
127 | 1 | .path(String.valueOf(number)) |
128 | 1 | .path("/comments") |
129 | 1 | .back(); |
130 | 2 | return new RtPagination<PullComment>( |
131 | 1 | newreq.uri().queryParams(params).back(), |
132 | 3 | new RtValuePagination.Mapping<PullComment, JsonObject>() { |
133 | |
@Override |
134 | |
public PullComment map(final JsonObject value) { |
135 | 4 | return RtPullComments.this.get( |
136 | 2 | value.getInt("id") |
137 | |
); |
138 | |
} |
139 | |
} |
140 | |
); |
141 | |
} |
142 | |
|
143 | |
|
144 | |
@Override |
145 | |
public PullComment post( |
146 | |
final String body, |
147 | |
final String commit, |
148 | |
final String path, |
149 | |
final int position |
150 | |
) throws IOException { |
151 | 2 | final JsonStructure json = Json.createObjectBuilder() |
152 | |
|
153 | 1 | .add("body", body) |
154 | 1 | .add("commit_id", commit) |
155 | 1 | .add("path", path) |
156 | 1 | .add("position", position) |
157 | 1 | .build(); |
158 | 2 | return this.get( |
159 | 1 | this.request.method(Request.POST) |
160 | 1 | .body().set(json).back() |
161 | 1 | .fetch() |
162 | 1 | .as(RestResponse.class) |
163 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
164 | 1 | .as(JsonResponse.class) |
165 | 1 | .json().readObject().getInt("id") |
166 | |
); |
167 | |
} |
168 | |
|
169 | |
@Override |
170 | |
public PullComment reply( |
171 | |
final String body, |
172 | |
final int comment |
173 | |
) throws IOException { |
174 | 2 | final JsonStructure json = Json.createObjectBuilder() |
175 | 1 | .add("body", body) |
176 | 1 | .add("in_reply_to", comment) |
177 | 1 | .build(); |
178 | 2 | return this.get( |
179 | 1 | this.request.method(Request.POST) |
180 | 1 | .body().set(json).back() |
181 | 1 | .fetch() |
182 | 1 | .as(RestResponse.class) |
183 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
184 | 1 | .as(JsonResponse.class) |
185 | 1 | .json().readObject().getInt("id") |
186 | |
); |
187 | |
} |
188 | |
|
189 | |
@Override |
190 | |
public void remove(final int number) throws IOException { |
191 | 2 | this.request.uri().path(String.valueOf(number)).back() |
192 | 1 | .method(Request.DELETE) |
193 | 1 | .fetch().as(RestResponse.class) |
194 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
195 | 1 | } |
196 | |
} |