forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHCommit.java
More file actions
647 lines (569 loc) · 17.2 KB
/
Copy pathGHCommit.java
File metadata and controls
647 lines (569 loc) · 17.2 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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package org.kohsuke.github;
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.time.Instant;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
// TODO: Auto-generated Javadoc
/**
* A commit in a repository.
*
* @author Kohsuke Kawaguchi
* @see GHRepository#getCommit(String) GHRepository#getCommit(String)
* @see GHCommitComment#getCommit() GHCommitComment#getCommit()
*/
@SuppressFBWarnings(value = { "NP_UNWRITTEN_FIELD", "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
public class GHCommit {
/**
* A file that was modified.
*/
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "It's being initialized by JSON deserialization")
public static class File {
/** The deletions. */
int changes, additions, deletions;
/** The previous filename. */
String filename, previousFilename;
/** The patch. */
String rawUrl, blobUrl, sha, patch;
/** The status. */
String status;
/**
* Create default File instance
*/
public File() {
}
/**
* Gets blob url.
*
* @return URL like
* 'https://github.com/jenkinsci/jenkins/blob/1182e2ebb1734d0653142bd422ad33c21437f7cf/core/pom.xml'
* that resolves to the HTML page that describes this file.
*/
public URL getBlobUrl() {
return GitHubClient.parseURL(blobUrl);
}
/**
* Gets file name.
*
* @return Full path in the repository.
*/
@SuppressFBWarnings(value = "NM_CONFUSING",
justification = "It's a part of the library's API and cannot be renamed")
public String getFileName() {
return filename;
}
/**
* Gets lines added.
*
* @return Number of lines added.
*/
public int getLinesAdded() {
return additions;
}
/**
* Gets lines changed.
*
* @return Number of lines added + removed.
*/
public int getLinesChanged() {
return changes;
}
/**
* Gets lines deleted.
*
* @return Number of lines removed.
*/
public int getLinesDeleted() {
return deletions;
}
/**
* Gets patch.
*
* @return The actual change.
*/
public String getPatch() {
return patch;
}
/**
* Gets previous filename.
*
* @return Previous path, in case file has moved.
*/
public String getPreviousFilename() {
return previousFilename;
}
/**
* Gets raw url.
*
* @return URL like
* 'https://raw.github.com/jenkinsci/jenkins/4eb17c197dfdcf8ef7ff87eb160f24f6a20b7f0e/core/pom.xml' that
* resolves to the actual content of the file.
*/
public URL getRawUrl() {
return GitHubClient.parseURL(rawUrl);
}
/**
* Gets sha.
*
* @return [0 -9a-f]{40} SHA1 checksum.
*/
public String getSha() {
return sha;
}
/**
* Gets status.
*
* @return "modified", "added", or "removed"
*/
public String getStatus() {
return status;
}
}
/**
* The type Parent.
*/
public static class Parent {
/** The sha. */
String sha;
/** The url. */
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
String url;
/**
* Create default Parent instance
*/
public Parent() {
}
}
/**
* Short summary of this commit.
*/
@SuppressFBWarnings(
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
"UWF_UNWRITTEN_FIELD" },
justification = "JSON API")
public static class ShortInfo extends GitCommit {
private int commentCount = -1;
/**
* Creates instance of {@link GHCommit.ShortInfo}.
*/
public ShortInfo() {
// Empty constructor required for Jackson binding
}
/**
* Instantiates a new short info.
*
* @param commit
* the commit
*/
ShortInfo(GitCommit commit) {
// Inherited copy constructor, used for bridge method from {@link GitCommit},
// which is used in {@link GHContentUpdateResponse}) to {@link GHCommit}.
super(commit);
};
/**
* Gets comment count.
*
* @return the comment count
* @throws GHException
* the GH exception
*/
public int getCommentCount() throws GHException {
if (commentCount < 0) {
throw new GHException("Not available on this endpoint.");
}
return commentCount;
}
/**
* Gets the parent SHA 1 s.
*
* @return the parent SHA 1 s
*/
@Override
public List<String> getParentSHA1s() {
List<String> shortInfoParents = super.getParentSHA1s();
if (shortInfoParents == null) {
throw new GHException("Not available on this endpoint. Try calling getParentSHA1s from outer class.");
}
return shortInfoParents;
}
}
/**
* The type Stats.
*/
public static class Stats {
/** The deletions. */
int total, additions, deletions;
/**
* Create default Stats instance
*/
public Stats() {
}
}
/**
* The Class User.
*/
static class User {
/** The id. */
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
int id;
/** The login. */
String login;
/** The gravatar id. */
// TODO: what if someone who doesn't have an account on GitHub makes a commit?
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
String url, avatarUrl, gravatarId;
}
private ShortInfo commit;
private GHRepository owner;
/** The committer. */
User author, committer;
/** The files. */
List<File> files;
/** The parents. */
List<Parent> parents;
/** The stats. */
Stats stats;
/** The sha. */
String url, htmlUrl, sha, message;
/**
* Creates an instance of {@link GHCommit}.
*/
public GHCommit() {
// empty constructor needed for Jackson binding
}
/**
* Instantiates a new GH commit.
*
* @param shortInfo
* the short info
*/
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "acceptable")
GHCommit(ShortInfo shortInfo) {
// Constructs a (relatively sparse) GHCommit from a GitCommit. Used for
// bridge method from {@link GitCommit}, which is used in
// {@link GHContentUpdateResponse}) to {@link GHCommit}.
commit = shortInfo;
owner = commit.getOwner();
htmlUrl = commit.getHtmlUrl();
sha = commit.getSha();
url = commit.getUrl();
parents = commit.getParents();
message = commit.getMessage();
}
/**
* Create comment gh commit comment.
*
* @param body
* the body
* @return the gh commit comment
* @throws IOException
* the io exception
*/
public GHCommitComment createComment(String body) throws IOException {
return createComment(body, null, null, null);
}
/**
* Creates a commit comment.
* <p>
* I'm not sure how path/line/position parameters interact with each other.
*
* @param body
* body of the comment
* @param path
* path of file being commented on
* @param line
* target line for comment
* @param position
* position on line
* @return created GHCommitComment
* @throws IOException
* if comment is not created
*/
public GHCommitComment createComment(String body, String path, Integer line, Integer position) throws IOException {
GHCommitComment r = owner.root()
.createRequest()
.method("POST")
.with("body", body)
.with("path", path)
.with("line", line)
.with("position", position)
.withUrlPath(
String.format("/repos/%s/%s/commits/%s/comments", owner.getOwnerName(), owner.getName(), sha))
.fetch(GHCommitComment.class);
return r.wrap(owner);
}
/**
* Gets author.
*
* @return the author
* @throws IOException
* the io exception
*/
public GHUser getAuthor() throws IOException {
populate();
return resolveUser(author);
}
/**
* Gets the date the change was authored on.
*
* @return the date the change was authored on.
* @throws IOException
* if the information was not already fetched and an attempt at fetching the information failed.
*/
@WithBridgeMethods(value = Date.class, adapterMethod = "instantToDate")
public Instant getAuthoredDate() throws IOException {
return getCommitShortInfo().getAuthoredDate();
}
/**
* Gets check-runs for given sha.
*
* @return check runs for given sha.
* @throws IOException
* on error
*/
public PagedIterable<GHCheckRun> getCheckRuns() throws IOException {
return owner.getCheckRuns(sha);
}
/**
* Gets the date the change was committed on.
*
* @return the date the change was committed on.
* @throws IOException
* if the information was not already fetched and an attempt at fetching the information failed.
*/
@WithBridgeMethods(value = Date.class, adapterMethod = "instantToDate")
public Instant getCommitDate() throws IOException {
return getCommitShortInfo().getCommitDate();
}
/**
* Gets commit short info.
*
* @return the commit short info
* @throws IOException
* the io exception
*/
public ShortInfo getCommitShortInfo() throws IOException {
if (commit == null)
populate();
return commit;
}
/**
* Gets committer.
*
* @return the committer
* @throws IOException
* the io exception
*/
public GHUser getCommitter() throws IOException {
populate();
return resolveUser(committer);
}
/**
* Gets html url.
*
* @return URL of this commit like
* "https://github.com/kohsuke/sandbox-ant/commit/8ae38db0ea5837313ab5f39d43a6f73de3bd9000"
*/
public URL getHtmlUrl() {
return GitHubClient.parseURL(htmlUrl);
}
/**
* Gets last status.
*
* @return the last status of this commit, which is what gets shown in the UI.
* @throws IOException
* on error
*/
public GHCommitStatus getLastStatus() throws IOException {
return owner.getLastCommitStatus(sha);
}
/**
* Gets lines added.
*
* @return Number of lines added.
* @throws IOException
* if the field was not populated and refresh fails
*/
public int getLinesAdded() throws IOException {
populate();
return stats.additions;
}
/**
* Gets lines changed.
*
* @return the number of lines added + removed.
* @throws IOException
* if the field was not populated and refresh fails
*/
public int getLinesChanged() throws IOException {
populate();
return stats.total;
}
/**
* Gets lines deleted.
*
* @return Number of lines removed.
* @throws IOException
* if the field was not populated and refresh fails
*/
public int getLinesDeleted() throws IOException {
populate();
return stats.deletions;
}
/**
* Gets owner.
*
* @return the repository that contains the commit.
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHRepository getOwner() {
return owner;
}
/**
* Gets parent sha 1 s.
*
* @return SHA1 of parent commit objects.
*/
public List<String> getParentSHA1s() {
if (parents == null || parents.size() == 0)
return Collections.emptyList();
return new AbstractList<String>() {
@Override
public String get(int index) {
return parents.get(index).sha;
}
@Override
public int size() {
return parents.size();
}
};
}
/**
* Resolves the parent commit objects and return them.
*
* @return parent commit objects
* @throws IOException
* on error
*/
public List<GHCommit> getParents() throws IOException {
populate();
List<GHCommit> r = new ArrayList<GHCommit>();
for (String sha1 : getParentSHA1s())
r.add(owner.getCommit(sha1));
return r;
}
/**
* Gets sha 1.
*
* @return [0 -9a-f]{40} SHA1 checksum.
*/
public String getSHA1() {
return sha;
}
/**
* Use this method to walk the tree.
*
* @return a GHTree to walk
* @throws IOException
* on error
*/
public GHTree getTree() throws IOException {
return owner.getTree(getCommitShortInfo().getTreeSHA1());
}
/**
* Gets url.
*
* @return API URL of this object.
*/
public URL getUrl() {
return GitHubClient.parseURL(url);
}
/**
* Retrieves a list of branches where this commit is the head commit.
*
* @return {@link PagedIterable} with the branches where the commit is the head commit
*/
public PagedIterable<GHBranch> listBranchesWhereHead() {
return owner.root()
.createRequest()
.withUrlPath(String.format("/repos/%s/%s/commits/%s/branches-where-head",
owner.getOwnerName(),
owner.getName(),
sha))
.toIterable(GHBranch[].class, item -> item.wrap(owner));
}
/**
* List comments paged iterable.
*
* @return {@link PagedIterable} with all the commit comments in this repository.
*/
public PagedIterable<GHCommitComment> listComments() {
return owner.listCommitComments(sha);
}
/**
* List of files changed/added/removed in this commit. Uses a paginated list if the files returned by GitHub exceed
* 300 in quantity.
*
* @return the List of files
* @see <a href="https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#get-a-commit">Get a
* commit</a>
* @throws IOException
* on error
*/
public PagedIterable<File> listFiles() throws IOException {
populate();
return new GHCommitFileIterable(owner, sha, files);
}
/**
* Retrieves a list of pull requests which contain this commit.
*
* @return {@link PagedIterable} with the pull requests which contain this commit
*/
public PagedIterable<GHPullRequest> listPullRequests() {
return owner.root()
.createRequest()
.withUrlPath(String.format("/repos/%s/%s/commits/%s/pulls", owner.getOwnerName(), owner.getName(), sha))
.toIterable(GHPullRequest[].class, item -> item.wrapUp(owner));
}
/**
* List statuses paged iterable.
*
* @return status of this commit, newer ones first.
* @throws IOException
* if statuses cannot be read
*/
public PagedIterable<GHCommitStatus> listStatuses() throws IOException {
return owner.listCommitStatuses(sha);
}
private GHUser resolveUser(User author) throws IOException {
if (author == null || author.login == null)
return null;
return owner.root().getUser(author.login);
}
/**
* Some of the fields are not always filled in when this object is retrieved as a part of another API call.
*
* @throws IOException
* on error
*/
void populate() throws IOException {
if (files == null && stats == null)
owner.root().createRequest().withUrlPath(owner.getApiTailUrl("commits/" + sha)).fetchInto(this);
}
/**
* Wrap up.
*
* @param owner
* the owner
* @return the GH commit
*/
GHCommit wrapUp(GHRepository owner) {
this.owner = owner;
return this;
}
}