forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHPullRequest.java
More file actions
747 lines (677 loc) · 21.8 KB
/
GHPullRequest.java
File metadata and controls
747 lines (677 loc) · 21.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
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
* The MIT License
*
* Copyright (c) 2010, Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
// TODO: Auto-generated Javadoc
/**
* A pull request.
*
* @author Kohsuke Kawaguchi
* @see GHRepository#getPullRequest(int) GHRepository#getPullRequest(int)
*/
@SuppressWarnings({ "UnusedDeclaration" })
public class GHPullRequest extends GHIssue implements Refreshable {
/**
* Create default GHPullRequest instance
*/
public GHPullRequest() {
}
private static final String COMMENTS_ACTION = "/comments";
private static final String REQUEST_REVIEWERS = "/requested_reviewers";
private String patch_url, diff_url, issue_url;
private GHCommitPointer base;
private String merged_at;
private GHCommitPointer head;
// details that are only available when obtained from ID
private GHUser merged_by;
private int review_comments, additions, commits;
private boolean merged, maintainer_can_modify;
/** The draft. */
// making these package private to all for testing
boolean draft;
private Boolean mergeable;
private int deletions;
private String mergeable_state;
private int changed_files;
private String merge_commit_sha;
private AutoMerge auto_merge;
// pull request reviewers
private GHUser[] requested_reviewers;
private GHTeam[] requested_teams;
/**
* Wrap up.
*
* @param owner
* the owner
* @return the GH pull request
*/
GHPullRequest wrapUp(GHRepository owner) {
this.wrap(owner);
return this;
}
/**
* Gets the api route.
*
* @return the api route
*/
@Override
protected String getApiRoute() {
if (owner == null) {
// Issues returned from search to do not have an owner. Attempt to use url.
final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
// The url sourced above is of the form '/repos/<owner>/<reponame>/issues/', which
// subsequently issues requests against the `/issues/` handler, causing a 404 when
// asking for, say, a list of commits associated with a PR. Replace the `/issues/`
// with `/pulls/` to avoid that.
return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/")
.replace("/issues/", "/pulls/");
}
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/pulls/" + number;
}
/**
* The status of auto merging a pull request.
*
* @return the {@linkplain AutoMerge} or {@code null} if no auto merge is set.
*/
public AutoMerge getAutoMerge() {
return auto_merge;
}
/**
* The URL of the patch file. like https://github.com/jenkinsci/jenkins/pull/100.patch
*
* @return the patch url
*/
public URL getPatchUrl() {
return GitHubClient.parseURL(patch_url);
}
/**
* The URL of the patch file. like https://github.com/jenkinsci/jenkins/pull/100.patch
*
* @return the issue url
*/
public URL getIssueUrl() {
return GitHubClient.parseURL(issue_url);
}
/**
* This points to where the change should be pulled into, but I'm not really sure what exactly it means.
*
* @return the base
*/
public GHCommitPointer getBase() {
return base;
}
/**
* The change that should be pulled. The tip of the commits to merge.
*
* @return the head
*/
public GHCommitPointer getHead() {
return head;
}
/**
* The diff file, like https://github.com/jenkinsci/jenkins/pull/100.diff
*
* @return the diff url
*/
public URL getDiffUrl() {
return GitHubClient.parseURL(diff_url);
}
/**
* Gets merged at.
*
* @return the merged at
*/
public Date getMergedAt() {
return GitHubClient.parseDate(merged_at);
}
/**
* Gets the closed by.
*
* @return the closed by
*/
@Override
public GHUser getClosedBy() {
return null;
}
/**
* Gets the pull request.
*
* @return the pull request
*/
@Override
public PullRequest getPullRequest() {
return null;
}
//
// details that are only available via get with ID
//
/**
* Gets merged by.
*
* @return the merged by
* @throws IOException
* the io exception
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHUser getMergedBy() throws IOException {
populate();
return merged_by;
}
/**
* Gets review comments.
*
* @return the review comments
* @throws IOException
* the io exception
*/
public int getReviewComments() throws IOException {
populate();
return review_comments;
}
/**
* Gets additions.
*
* @return the additions
* @throws IOException
* the io exception
*/
public int getAdditions() throws IOException {
populate();
return additions;
}
/**
* Gets the number of commits.
*
* @return the number of commits
* @throws IOException
* the io exception
*/
public int getCommits() throws IOException {
populate();
return commits;
}
/**
* Is merged boolean.
*
* @return the boolean
* @throws IOException
* the io exception
*/
public boolean isMerged() throws IOException {
populate();
return merged;
}
/**
* Can maintainer modify boolean.
*
* @return the boolean
* @throws IOException
* the io exception
*/
public boolean canMaintainerModify() throws IOException {
populate();
return maintainer_can_modify;
}
/**
* Is draft boolean.
*
* @return the boolean
* @throws IOException
* the io exception
*/
public boolean isDraft() throws IOException {
populate();
return draft;
}
/**
* Is this PR mergeable?.
*
* @return null if the state has not been determined yet, for example when a PR is newly created. If this method is
* called on an instance whose mergeable state is not yet known, API call is made to retrieve the latest
* state.
* @throws IOException
* the io exception
*/
public Boolean getMergeable() throws IOException {
refresh(mergeable);
return mergeable;
}
/**
* for test purposes only.
*
* @return the mergeable no refresh
*/
Boolean getMergeableNoRefresh() {
return mergeable;
}
/**
* Gets deletions.
*
* @return the deletions
* @throws IOException
* the io exception
*/
public int getDeletions() throws IOException {
populate();
return deletions;
}
/**
* Gets mergeable state.
*
* @return the mergeable state
* @throws IOException
* the io exception
*/
public String getMergeableState() throws IOException {
populate();
return mergeable_state;
}
/**
* Gets changed files.
*
* @return the changed files
* @throws IOException
* the io exception
*/
public int getChangedFiles() throws IOException {
populate();
return changed_files;
}
/**
* See <a href="https://developer.github.com/changes/2013-04-25-deprecating-merge-commit-sha">GitHub blog post</a>
*
* @return the merge commit sha
* @throws IOException
* the io exception
*/
public String getMergeCommitSha() throws IOException {
populate();
return merge_commit_sha;
}
/**
* Gets requested reviewers.
*
* @return the requested reviewers
* @throws IOException
* the io exception
*/
public List<GHUser> getRequestedReviewers() throws IOException {
refresh(requested_reviewers);
return Collections.unmodifiableList(Arrays.asList(requested_reviewers));
}
/**
* Gets requested teams.
*
* @return the requested teams
* @throws IOException
* the io exception
*/
public List<GHTeam> getRequestedTeams() throws IOException {
refresh(requested_teams);
return Collections.unmodifiableList(Arrays.asList(requested_teams));
}
/**
* Fully populate the data by retrieving missing data.
*
* <p>
* Depending on the original API call where this object is created, it may not contain everything.
*/
private void populate() throws IOException {
if (mergeable_state != null)
return; // already populated
refresh();
}
/**
* Repopulates this object.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void refresh() throws IOException {
if (isOffline()) {
return; // cannot populate, will have to live with what we have
}
// we do not want to use getUrl() here as it points to the issues API
// and not the pull request one
URL absoluteUrl = GitHubRequest.getApiURL(root().getApiUrl(), getApiRoute());
root().createRequest().setRawUrlPath(absoluteUrl.toString()).fetchInto(this).wrapUp(owner);
}
/**
* Retrieves all the files associated to this pull request. The paginated response returns 30 files per page by
* default.
*
* @return the paged iterable
* @see <a href="https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files">List pull requests
* files</a>
*/
public PagedIterable<GHPullRequestFileDetail> listFiles() {
return root().createRequest()
.withUrlPath(String.format("%s/files", getApiRoute()))
.toIterable(GHPullRequestFileDetail[].class, null);
}
/**
* Retrieves all the reviews associated to this pull request.
*
* @return the paged iterable
*/
public PagedIterable<GHPullRequestReview> listReviews() {
return root().createRequest()
.withUrlPath(String.format("%s/reviews", getApiRoute()))
.toIterable(GHPullRequestReview[].class, item -> item.wrapUp(this));
}
/**
* Obtains all the review comments associated with this pull request.
*
* @return the paged iterable
*/
public PagedIterable<GHPullRequestReviewComment> listReviewComments() {
return root().createRequest()
.withUrlPath(getApiRoute() + COMMENTS_ACTION)
.toIterable(GHPullRequestReviewComment[].class, item -> item.wrapUp(this));
}
/**
* Retrieves all the commits associated to this pull request.
*
* @return the paged iterable
*/
public PagedIterable<GHPullRequestCommitDetail> listCommits() {
return root().createRequest()
.withUrlPath(String.format("%s/commits", getApiRoute()))
.toIterable(GHPullRequestCommitDetail[].class, item -> item.wrapUp(this));
}
/**
* Create review gh pull request review builder.
*
* @return the gh pull request review builder
*/
public GHPullRequestReviewBuilder createReview() {
return new GHPullRequestReviewBuilder(this);
}
/**
* Create gh pull request review comment builder.
*
* @return the gh pull request review comment builder.
*/
public GHPullRequestReviewCommentBuilder createReviewComment() {
return new GHPullRequestReviewCommentBuilder(this);
}
/**
* Create review comment gh pull request review comment.
*
* @param body
* the body
* @param sha
* the sha
* @param path
* the path
* @param position
* the position
* @return the gh pull request review comment
* @throws IOException
* the io exception
* @deprecated use {@link #createReviewComment()}
*/
@Deprecated
public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position)
throws IOException {
return createReviewComment().body(body).commitId(sha).path(path).position(position).create();
}
/**
* Request reviewers.
*
* @param reviewers
* the reviewers
* @throws IOException
* the io exception
*/
public void requestReviewers(List<GHUser> reviewers) throws IOException {
root().createRequest()
.method("POST")
.with("reviewers", getLogins(reviewers))
.withUrlPath(getApiRoute() + REQUEST_REVIEWERS)
.send();
}
/**
* Request team reviewers.
*
* @param teams
* the teams
* @throws IOException
* the io exception
*/
public void requestTeamReviewers(List<GHTeam> teams) throws IOException {
List<String> teamReviewers = new ArrayList<String>(teams.size());
for (GHTeam team : teams) {
teamReviewers.add(team.getSlug());
}
root().createRequest()
.method("POST")
.with("team_reviewers", teamReviewers)
.withUrlPath(getApiRoute() + REQUEST_REVIEWERS)
.send();
}
/**
* Set the base branch on the pull request.
*
* @param newBaseBranch
* the name of the new base branch
* @return the updated pull request
* @throws IOException
* the io exception
*/
public GHPullRequest setBaseBranch(String newBaseBranch) throws IOException {
return root().createRequest()
.method("PATCH")
.with("base", newBaseBranch)
.withUrlPath(getApiRoute())
.fetch(GHPullRequest.class);
}
/**
* Updates the branch. The same as pressing the button in the web GUI.
*
* @throws IOException
* the io exception
*/
public void updateBranch() throws IOException {
root().createRequest()
.method("PUT")
.with("expected_head_sha", head.getSha())
.withUrlPath(getApiRoute() + "/update-branch")
.send();
}
/**
* Merge this pull request.
*
* <p>
* The equivalent of the big green "Merge pull request" button.
*
* @param msg
* Commit message. If null, the default one will be used.
* @throws IOException
* the io exception
*/
public void merge(String msg) throws IOException {
merge(msg, null);
}
/**
* Merge this pull request.
*
* <p>
* The equivalent of the big green "Merge pull request" button.
*
* @param msg
* Commit message. If null, the default one will be used.
* @param sha
* SHA that pull request head must match to allow merge.
* @throws IOException
* the io exception
*/
public void merge(String msg, String sha) throws IOException {
merge(msg, sha, null);
}
/**
* Merge this pull request, using the specified merge method.
*
* <p>
* The equivalent of the big green "Merge pull request" button.
*
* @param msg
* Commit message. If null, the default one will be used.
* @param sha
* the sha
* @param method
* SHA that pull request head must match to allow merge.
* @throws IOException
* the io exception
*/
public void merge(String msg, String sha, MergeMethod method) throws IOException {
root().createRequest()
.method("PUT")
.with("commit_message", msg)
.with("sha", sha)
.with("merge_method", method)
.withUrlPath(getApiRoute() + "/merge")
.send();
}
/** The enum MergeMethod. */
public enum MergeMethod {
/** The merge. */
MERGE,
/** The squash. */
SQUASH,
/** The rebase. */
REBASE
}
/**
* Request to enable auto merge for a pull request.
*
* @param authorEmail
* The email address to associate with this merge.
* @param clientMutationId
* A unique identifier for the client performing the mutation.
* @param commitBody
* Commit body to use for the commit when the PR is mergable; if omitted, a default message will be used.
* NOTE: when merging with a merge queue any input value for commit message is ignored.
* @param commitHeadline
* Commit headline to use for the commit when the PR is mergable; if omitted, a default message will be
* used. NOTE: when merging with a merge queue any input value for commit headline is ignored.
* @param expectedHeadOid
* The expected head OID of the pull request.
* @param mergeMethod
* The merge method to use. If omitted, defaults to `MERGE`. NOTE: when merging with a merge queue any
* input value for merge method is ignored.
* @throws IOException
* the io exception
*/
public void enablePullRequestAutoMerge(String authorEmail,
String clientMutationId,
String commitBody,
String commitHeadline,
String expectedHeadOid,
MergeMethod mergeMethod) throws IOException {
StringBuilder inputBuilder = new StringBuilder();
addParameter(inputBuilder, "pullRequestId", this.getNodeId());
addOptionalParameter(inputBuilder, "authorEmail", authorEmail);
addOptionalParameter(inputBuilder, "clientMutationId", clientMutationId);
addOptionalParameter(inputBuilder, "commitBody", commitBody);
addOptionalParameter(inputBuilder, "commitHeadline", commitHeadline);
addOptionalParameter(inputBuilder, "expectedHeadOid", expectedHeadOid);
addOptionalParameter(inputBuilder, "mergeMethod", mergeMethod);
String graphqlBody = "mutation EnableAutoMerge { enablePullRequestAutoMerge(input: {" + inputBuilder + "}) { "
+ "pullRequest { id } } }";
root().createGraphQLRequest(graphqlBody).sendGraphQL();
refresh();
}
private void addOptionalParameter(StringBuilder inputBuilder, String name, Object value) {
if (value != null) {
addParameter(inputBuilder, name, value);
}
}
private void addParameter(StringBuilder inputBuilder, String name, Object value) {
Objects.requireNonNull(value);
String formatString = " %s: \"%s\"";
if (value instanceof Enum) {
formatString = " %s: %s";
}
inputBuilder.append(String.format(formatString, name, value));
}
/**
* The status of auto merging a {@linkplain GHPullRequest}.
*
*/
@SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD", justification = "Field comes from JSON deserialization")
public static class AutoMerge {
/**
* Create default AutoMerge instance
*/
public AutoMerge() {
}
private GHUser enabled_by;
private MergeMethod merge_method;
private String commit_title;
private String commit_message;
/**
* The user who enabled the auto merge of the pull request.
*
* @return the {@linkplain GHUser}
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHUser getEnabledBy() {
return enabled_by;
}
/**
* The merge method of the auto merge.
*
* @return the {@linkplain MergeMethod}
*/
public MergeMethod getMergeMethod() {
return merge_method;
}
/**
* the title of the commit, if e.g. {@linkplain MergeMethod#SQUASH} is used for the auto merge.
*
* @return the title of the commit
*/
public String getCommitTitle() {
return commit_title;
}
/**
* the message of the commit, if e.g. {@linkplain MergeMethod#SQUASH} is used for the auto merge.
*
* @return the message of the commit
*/
public String getCommitMessage() {
return commit_message;
}
}
}