-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathGHCompare.java
More file actions
503 lines (443 loc) · 12.8 KB
/
GHCompare.java
File metadata and controls
503 lines (443 loc) · 12.8 KB
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
import javax.annotation.Nonnull;
// TODO: Auto-generated Javadoc
/**
* The model user for comparing 2 commits in the GitHub API.
*
* @author Michael Clarke
*/
public class GHCompare {
private String url, html_url, permalink_url, diff_url, patch_url;
private Status status;
private int ahead_by, behind_by, total_commits;
private Commit base_commit, merge_base_commit;
private Commit[] commits;
private GHCommit.File[] files;
private GHRepository owner;
@JacksonInject("GHCompare_usePaginatedCommits")
private boolean usePaginatedCommits;
/**
* Gets url.
*
* @return the url
*/
public URL getUrl() {
return GitHubClient.parseURL(url);
}
/**
* Gets html url.
*
* @return the html url
*/
public URL getHtmlUrl() {
return GitHubClient.parseURL(html_url);
}
/**
* Gets permalink url.
*
* @return the permalink url
*/
public URL getPermalinkUrl() {
return GitHubClient.parseURL(permalink_url);
}
/**
* Gets diff url.
*
* @return the diff url
*/
public URL getDiffUrl() {
return GitHubClient.parseURL(diff_url);
}
/**
* Gets patch url.
*
* @return the patch url
*/
public URL getPatchUrl() {
return GitHubClient.parseURL(patch_url);
}
/**
* Gets status.
*
* @return the status
*/
public Status getStatus() {
return status;
}
/**
* Gets ahead by.
*
* @return the ahead by
*/
public int getAheadBy() {
return ahead_by;
}
/**
* Gets behind by.
*
* @return the behind by
*/
public int getBehindBy() {
return behind_by;
}
/**
* Gets total commits.
*
* @return the total commits
*/
public int getTotalCommits() {
return total_commits;
}
/**
* Gets base commit.
*
* @return the base commit
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public Commit getBaseCommit() {
return base_commit;
}
/**
* Gets merge base commit.
*
* @return the merge base commit
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public Commit getMergeBaseCommit() {
return merge_base_commit;
}
/**
* Gets an array of commits.
*
* By default, the commit list is limited to 250 results.
*
* Since
* <a href="https://github.blog/changelog/2021-03-22-compare-rest-api-now-supports-pagination/">2021-03-22</a>,
* compare supports pagination of commits. This makes the initial {@link GHCompare} response return faster and
* supports comparisons with more than 250 commits. To read commits progressively using pagination, set
* {@link GHRepository#setCompareUsePaginatedCommits(boolean)} to true before calling
* {@link GHRepository#getCompare(String, String)}.
*
* @return A copy of the array being stored in the class.
*/
public Commit[] getCommits() {
try {
return listCommits().withPageSize(100).toArray();
} catch (IOException e) {
throw new GHException(e.getMessage(), e);
}
}
/**
* Iterable of commits for this comparison.
*
* By default, the commit list is limited to 250 results.
*
* Since
* <a href="https://github.blog/changelog/2021-03-22-compare-rest-api-now-supports-pagination/">2021-03-22</a>,
* compare supports pagination of commits. This makes the initial {@link GHCompare} response return faster and
* supports comparisons with more than 250 commits. To read commits progressively using pagination, set
* {@link GHRepository#setCompareUsePaginatedCommits(boolean)} to true before calling
* {@link GHRepository#getCompare(String, String)}.
*
* @return iterable of commits
*/
public PagedIterable<Commit> listCommits() {
if (usePaginatedCommits) {
return new GHCompareCommitsIterable();
} else {
// if not using paginated commits, adapt the returned commits array
return new PagedIterable<Commit>() {
@Nonnull
@Override
public Paginator<Commit> _paginator(int pageSize, int startPage) {
throw new UnsupportedOperationException("Paginator nor supported if not using paginated commits.");
}
@Nonnull
@Override
public PagedIterator<Commit> _iterator(int pageSize) {
return new PagedIterator<>(Collections.singleton(commits).iterator(), null);
}
};
}
}
/**
* Gets an array of files.
*
* By default, the file array is limited to 300 results. To retrieve the full list of files, iterate over each
* commit returned by {@link GHCompare#listCommits} and use {@link GHCommit#listFiles} to get the files for each
* commit.
*
* @return A copy of the array being stored in the class.
*/
public GHCommit.File[] getFiles() {
GHCommit.File[] newValue = new GHCommit.File[files.length];
System.arraycopy(files, 0, newValue, 0, files.length);
return newValue;
}
/**
* Wrap gh compare.
*
* @param owner
* the owner
* @return the gh compare
*/
@Deprecated
public GHCompare wrap(GHRepository owner) {
throw new RuntimeException("Do not use this method.");
}
/**
* Wrap gh compare.
*
* @param owner
* the owner
* @return the gh compare
*/
GHCompare lateBind(GHRepository owner) {
this.owner = owner;
for (Commit commit : commits) {
commit.wrapUp(owner);
}
merge_base_commit.wrapUp(owner);
base_commit.wrapUp(owner);
return this;
}
/**
* Compare commits had a child commit element with additional details we want to capture. This extension of GHCommit
* provides that.
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD" },
justification = "JSON API")
public static class Commit extends GHCommit {
private InnerCommit commit;
/**
* Gets commit.
*
* @return the commit
*/
public InnerCommit getCommit() {
return commit;
}
}
/**
* The type InnerCommit.
*/
public static class InnerCommit {
private String url, sha, message;
private User author, committer;
private Tree tree;
/**
* Gets url.
*
* @return the url
*/
public String getUrl() {
return url;
}
/**
* Gets sha.
*
* @return the sha
*/
public String getSha() {
return sha;
}
/**
* Gets message.
*
* @return the message
*/
public String getMessage() {
return message;
}
/**
* Gets author.
*
* @return the author
*/
@WithBridgeMethods(value = User.class, castRequired = true)
public GitUser getAuthor() {
return author;
}
/**
* Gets committer.
*
* @return the committer
*/
@WithBridgeMethods(value = User.class, castRequired = true)
public GitUser getCommitter() {
return committer;
}
/**
* Gets tree.
*
* @return the tree
*/
public Tree getTree() {
return tree;
}
}
/**
* The type Tree.
*/
public static class Tree {
private String url, sha;
/**
* Gets url.
*
* @return the url
*/
public String getUrl() {
return url;
}
/**
* Gets sha.
*
* @return the sha
*/
public String getSha() {
return sha;
}
}
/**
* The type User.
*
* @deprecated use {@link GitUser} instead.
*/
public static class User extends GitUser {
}
/**
* The enum Status.
*/
public static enum Status {
/** The behind. */
behind,
/** The ahead. */
ahead,
/** The identical. */
identical,
/** The diverged. */
diverged
}
/**
* Iterable for commit listing.
*/
class GHCompareCommitsIterable extends PagedIterable<Commit> {
private GHCompare result;
/**
* Instantiates a new GH compare commits iterable.
*/
public GHCompareCommitsIterable() {
}
@Nonnull
@Override
public Paginator<Commit> _paginator(int pageSize, int startPage) {
GitHubRequest request = getRequest();
// page_size must be set for GHCompare commit pagination
if (pageSize == 0) {
pageSize = 10;
}
return new Paginator<>(
adapt(GitHubPaginator
.create(owner.root().getClient(), GHCompare.class, request, pageSize, startPage)),
null);
}
/**
* Iterator.
*
* @param pageSize
* the page size
* @return the paged iterator
*/
@Nonnull
@Override
public PagedIterator<Commit> _iterator(int pageSize) {
GitHubRequest request = getRequest();
// page_size must be set for GHCompare commit pagination
if (pageSize == 0) {
pageSize = 10;
}
return new PagedIterator<>(
adapt(GitHubPageIterator.create(owner.root().getClient(), GHCompare.class, request, pageSize)),
item -> item.wrapUp(owner));
}
private GitHubRequest getRequest() {
return owner.root()
.createRequest()
.injectMappingValue("GHCompare_usePaginatedCommits", usePaginatedCommits)
.withUrlPath(owner.getApiTailUrl(url.substring(url.lastIndexOf("/compare/"))))
.build();
}
/**
* Adapt.
*
* @param base
* the base
* @return the iterator
*/
protected Iterator<Commit[]> adapt(final Iterator<GHCompare> base) {
return new Iterator<Commit[]>() {
public boolean hasNext() {
return base.hasNext();
}
public Commit[] next() {
GHCompare v = base.next();
if (result == null) {
result = v;
}
return v.commits;
}
};
}
protected NavigablePageIterator<Commit[]> adapt(final NavigablePageIterator<GHCompare> base) {
return new NavigablePageIterator<Commit[]>() {
@Override
public boolean hasPrevious() {
return base.hasPrevious();
}
@Override
public Commit[] previous() {
return base.previous().commits;
}
@Override
public Commit[] first() {
return base.first().commits;
}
@Override
public Commit[] last() {
return base.last().commits;
}
@Override
public int totalCount() {
return base.totalCount();
}
@Override
public int currentPage() {
return base.currentPage();
}
@Override
public boolean hasNext() {
return base.hasNext();
}
@Override
public Commit[] next() {
return base.next().commits;
}
@Override
public Commit[] jumpToPage(int page) {
return base.jumpToPage(page).commits;
}
@Override
public void refresh() {
base.refresh();
}
};
}
}
}