1 | 4 | |
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.mock; |
31 | |
|
32 | |
import com.jcabi.aspects.Immutable; |
33 | |
import com.jcabi.aspects.Loggable; |
34 | |
import com.jcabi.github.Commit; |
35 | |
import com.jcabi.github.Coordinates; |
36 | |
import com.jcabi.github.MergeState; |
37 | |
import com.jcabi.github.Pull; |
38 | |
import com.jcabi.github.PullComments; |
39 | |
import com.jcabi.github.PullRef; |
40 | |
import com.jcabi.github.Repo; |
41 | |
import com.jcabi.xml.XML; |
42 | |
import java.io.IOException; |
43 | |
import java.util.Collections; |
44 | |
import java.util.List; |
45 | |
import javax.json.Json; |
46 | |
import javax.json.JsonObject; |
47 | |
import lombok.EqualsAndHashCode; |
48 | |
import lombok.ToString; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | @Immutable |
57 | |
@Loggable(Loggable.DEBUG) |
58 | 0 | @ToString |
59 | 0 | @EqualsAndHashCode(of = { "storage", "self", "coords", "num" }) |
60 | |
@SuppressWarnings("PMD.TooManyMethods") |
61 | |
final class MkPull implements Pull { |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private static final String USER_BRANCH_SEP = ":"; |
67 | |
|
68 | |
|
69 | |
|
70 | |
private static final String REF_PROP = "ref"; |
71 | |
|
72 | |
|
73 | |
|
74 | |
private static final String LABEL_PROP = "label"; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private final transient MkStorage storage; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private final transient String self; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private final transient Coordinates coords; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
private final transient int num; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
MkPull( |
105 | |
final MkStorage stg, |
106 | |
final String login, |
107 | |
final Coordinates rep, |
108 | 20 | final int number) { |
109 | 20 | this.storage = stg; |
110 | 20 | this.self = login; |
111 | 20 | this.coords = rep; |
112 | 20 | this.num = number; |
113 | 20 | } |
114 | |
|
115 | |
@Override |
116 | |
public Repo repo() { |
117 | 4 | return new MkRepo(this.storage, this.self, this.coords); |
118 | |
} |
119 | |
|
120 | |
@Override |
121 | |
public int number() { |
122 | 114 | return this.num; |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public PullRef base() throws IOException { |
127 | 3 | return new MkPullRef( |
128 | |
this.storage, |
129 | |
new MkBranches( |
130 | |
this.storage, |
131 | |
this.self, |
132 | |
this.coords |
133 | 1 | ).get( |
134 | 2 | this.storage.xml().xpath( |
135 | 1 | String.format("%s/base/text()", this.xpath()) |
136 | 1 | ).get(0) |
137 | |
) |
138 | |
); |
139 | |
} |
140 | |
|
141 | |
@Override |
142 | |
public PullRef head() throws IOException { |
143 | 2 | final String userbranch = this.storage.xml() |
144 | 1 | .xpath(String.format("%s/head/text()", this.xpath())) |
145 | 1 | .get(0); |
146 | 1 | final String[] parts = userbranch.split(MkPull.USER_BRANCH_SEP, 2); |
147 | 1 | if (parts.length != 2) { |
148 | 0 | throw new IllegalStateException("Invalid MkPull head"); |
149 | |
} |
150 | 1 | final String user = parts[0]; |
151 | 1 | final String branch = parts[1]; |
152 | 2 | return new MkPullRef( |
153 | |
this.storage, |
154 | |
new MkBranches( |
155 | |
this.storage, |
156 | |
this.self, |
157 | |
new Coordinates.Simple( |
158 | |
user, |
159 | 1 | this.coords.repo() |
160 | |
) |
161 | 1 | ).get(branch) |
162 | |
); |
163 | |
} |
164 | |
|
165 | |
@Override |
166 | |
public Iterable<Commit> commits() throws IOException { |
167 | 0 | return Collections.emptyList(); |
168 | |
} |
169 | |
|
170 | |
@Override |
171 | |
public Iterable<JsonObject> files() throws IOException { |
172 | 0 | return Collections.emptyList(); |
173 | |
} |
174 | |
|
175 | |
@Override |
176 | |
public void merge( |
177 | |
final String msg |
178 | |
) throws IOException { |
179 | |
|
180 | 0 | } |
181 | |
|
182 | |
@Override |
183 | |
public MergeState merge( |
184 | |
final String msg, |
185 | |
final String sha) |
186 | |
throws IOException { |
187 | 0 | throw new UnsupportedOperationException("Merge not supported"); |
188 | |
} |
189 | |
|
190 | |
@Override |
191 | |
public PullComments comments() throws IOException { |
192 | 24 | return new MkPullComments(this.storage, this.self, this.coords, this); |
193 | |
} |
194 | |
|
195 | |
@Override |
196 | |
public int compareTo( |
197 | |
final Pull pull |
198 | |
) { |
199 | 4 | return this.number() - pull.number(); |
200 | |
} |
201 | |
|
202 | |
@Override |
203 | |
public void patch( |
204 | |
final JsonObject json |
205 | |
) throws IOException { |
206 | 2 | new JsonPatch(this.storage).patch(this.xpath(), json); |
207 | 1 | } |
208 | |
|
209 | |
@Override |
210 | |
public JsonObject json() throws IOException { |
211 | 8 | final XML xml = this.storage.xml().nodes(this.xpath()).get(0); |
212 | 4 | final String branch = xml.xpath("base/text()").get(0); |
213 | 4 | final String head = xml.xpath("head/text()").get(0); |
214 | 4 | final String[] parts = head.split(MkPull.USER_BRANCH_SEP, 2); |
215 | 4 | final List<String> patches = xml.xpath("patch/text()"); |
216 | |
final String patch; |
217 | 4 | if (patches.isEmpty()) { |
218 | 3 | patch = ""; |
219 | |
} else { |
220 | 1 | patch = patches.get(0); |
221 | |
} |
222 | 8 | return Json.createObjectBuilder() |
223 | 4 | .add( |
224 | |
"number", |
225 | 4 | Integer.parseInt(xml.xpath("number/text()").get(0)) |
226 | |
) |
227 | 4 | .add( |
228 | |
"user", |
229 | 4 | Json.createObjectBuilder() |
230 | 4 | .add("login", xml.xpath("user/login/text()").get(0)) |
231 | 4 | .build() |
232 | |
) |
233 | 4 | .add("patch", patch) |
234 | 4 | .add( |
235 | |
"head", |
236 | 4 | Json.createObjectBuilder() |
237 | 4 | .add(MkPull.REF_PROP, parts[1]) |
238 | 4 | .add(MkPull.LABEL_PROP, head) |
239 | 4 | .build() |
240 | 4 | ).add( |
241 | |
"base", |
242 | 4 | Json.createObjectBuilder() |
243 | 4 | .add(MkPull.REF_PROP, branch) |
244 | 4 | .add( |
245 | |
MkPull.LABEL_PROP, |
246 | 4 | String.format( |
247 | |
"%s:%s", |
248 | 4 | this.coords.user(), |
249 | |
branch |
250 | |
) |
251 | 4 | ).build() |
252 | 4 | ).add( |
253 | |
"comments", |
254 | 4 | this.storage.xml().nodes(this.comment()).size() |
255 | 4 | ).build(); |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
private String xpath() { |
263 | 14 | return String.format( |
264 | |
"/github/repos/repo[@coords='%s']/pulls/pull[number='%d']", |
265 | 7 | this.coords, this.num |
266 | |
); |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
private String comment() { |
274 | 8 | return String.format( |
275 | |
|
276 | |
"/github/repos/repo[@coords='%s']/pulls/pull[number='%d']/comments/comment", |
277 | 4 | this.coords, this.num |
278 | |
); |
279 | |
} |
280 | |
|
281 | |
} |