Skip to content

Commit d0a56db

Browse files
committed
Merge pull request hub4j#406
... with some further changes
2 parents 0f7c160 + acbf286 commit d0a56db

File tree

9 files changed

+247
-114
lines changed

9 files changed

+247
-114
lines changed

src/main/java/org/kohsuke/github/GHPullRequest.java

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,22 @@
2323
*/
2424
package org.kohsuke.github;
2525

26-
import javax.annotation.CheckForNull;
2726
import java.io.IOException;
2827
import java.net.URL;
29-
import java.util.ArrayList;
30-
import java.util.Arrays;
3128
import java.util.Collection;
3229
import java.util.Date;
33-
import java.util.List;
34-
35-
import static org.kohsuke.github.Previews.*;
3630

3731
/**
3832
* A pull request.
39-
*
33+
*
4034
* @author Kohsuke Kawaguchi
4135
* @see GHRepository#getPullRequest(int)
4236
*/
4337
@SuppressWarnings({"UnusedDeclaration"})
4438
public class GHPullRequest extends GHIssue {
4539

40+
private static final String COMMENTS_ACTION = "/comments";
41+
4642
private String patch_url, diff_url, issue_url;
4743
private GHCommitPointer base;
4844
private String merged_at;
@@ -91,7 +87,7 @@ protected String getApiRoute() {
9187
public URL getPatchUrl() {
9288
return GitHub.parseURL(patch_url);
9389
}
94-
90+
9591
/**
9692
* The URL of the patch file.
9793
* like https://github.com/jenkinsci/jenkins/pull/100.patch
@@ -114,7 +110,7 @@ public GHCommitPointer getBase() {
114110
public GHCommitPointer getHead() {
115111
return head;
116112
}
117-
113+
118114
@Deprecated
119115
public Date getIssueUpdatedAt() throws IOException {
120116
return super.getUpdatedAt();
@@ -262,7 +258,6 @@ public PagedIterable<GHPullRequestReview> listReviews() {
262258
return new PagedIterable<GHPullRequestReview>() {
263259
public PagedIterator<GHPullRequestReview> _iterator(int pageSize) {
264260
return new PagedIterator<GHPullRequestReview>(root.retrieve()
265-
.withPreview(BLACK_CAT)
266261
.asIterator(String.format("%s/reviews", getApiRoute()),
267262
GHPullRequestReview[].class, pageSize)) {
268263
@Override
@@ -282,7 +277,7 @@ protected void wrapUp(GHPullRequestReview[] page) {
282277
public PagedIterable<GHPullRequestReviewComment> listReviewComments() throws IOException {
283278
return new PagedIterable<GHPullRequestReviewComment>() {
284279
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
285-
return new PagedIterator<GHPullRequestReviewComment>(root.retrieve().asIterator(getApiRoute() + "/comments",
280+
return new PagedIterator<GHPullRequestReviewComment>(root.retrieve().asIterator(getApiRoute() + COMMENTS_ACTION,
286281
GHPullRequestReviewComment[].class, pageSize)) {
287282
protected void wrapUp(GHPullRequestReviewComment[] page) {
288283
for (GHPullRequestReviewComment c : page)
@@ -312,32 +307,8 @@ protected void wrapUp(GHPullRequestCommitDetail[] page) {
312307
};
313308
}
314309

315-
@Preview
316-
@Deprecated
317-
public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event,
318-
GHPullRequestReviewComment... comments)
319-
throws IOException {
320-
return createReview(body, event, Arrays.asList(comments));
321-
}
322-
323-
@Preview
324-
@Deprecated
325-
public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event,
326-
List<GHPullRequestReviewComment> comments)
327-
throws IOException {
328-
// if (event == null) {
329-
// event = GHPullRequestReviewState.PENDING;
330-
// }
331-
List<DraftReviewComment> draftComments = new ArrayList<DraftReviewComment>(comments.size());
332-
for (GHPullRequestReviewComment c : comments) {
333-
draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), c.getPosition()));
334-
}
335-
return new Requester(root).method("POST")
336-
.with("body", body)
337-
//.with("event", event.name())
338-
._with("comments", draftComments)
339-
.withPreview(BLACK_CAT)
340-
.to(getApiRoute() + "/reviews", GHPullRequestReview.class).wrapUp(this);
310+
public GHPullRequestReviewBuilder createReview() {
311+
return new GHPullRequestReviewBuilder(this);
341312
}
342313

343314
public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position) throws IOException {
@@ -346,7 +317,7 @@ public GHPullRequestReviewComment createReviewComment(String body, String sha, S
346317
.with("commit_id", sha)
347318
.with("path", path)
348319
.with("position", position)
349-
.to(getApiRoute() + "/comments", GHPullRequestReviewComment.class).wrapUp(this);
320+
.to(getApiRoute() + COMMENTS_ACTION, GHPullRequestReviewComment.class).wrapUp(this);
350321
}
351322

352323
/**
@@ -387,10 +358,10 @@ public void merge(String msg, String sha) throws IOException {
387358
*/
388359
public void merge(String msg, String sha, MergeMethod method) throws IOException {
389360
new Requester(root).method("PUT")
390-
.with("commit_message",msg)
391-
.with("sha",sha)
392-
.with("merge_method",method)
393-
.to(getApiRoute()+"/merge");
361+
.with("commit_message", msg)
362+
.with("sha", sha)
363+
.with("merge_method", method)
364+
.to(getApiRoute() + "/merge");
394365
}
395366

396367
public enum MergeMethod{ MERGE, SQUASH, REBASE }
@@ -401,28 +372,4 @@ private void fetchIssue() throws IOException {
401372
fetchedIssueDetails = true;
402373
}
403374
}
404-
405-
private static class DraftReviewComment {
406-
private String body;
407-
private String path;
408-
private int position;
409-
410-
public DraftReviewComment(String body, String path, int position) {
411-
this.body = body;
412-
this.path = path;
413-
this.position = position;
414-
}
415-
416-
public String getBody() {
417-
return body;
418-
}
419-
420-
public String getPath() {
421-
return path;
422-
}
423-
424-
public int getPosition() {
425-
return position;
426-
}
427-
}
428375
}

src/main/java/org/kohsuke/github/GHPullRequestReview.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@
2323
*/
2424
package org.kohsuke.github;
2525

26+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27+
28+
import javax.annotation.CheckForNull;
2629
import java.io.IOException;
2730
import java.net.URL;
2831

29-
import static org.kohsuke.github.Previews.*;
30-
3132
/**
32-
* Review to the pull request
33+
* Review to a pull request.
3334
*
3435
* @see GHPullRequest#listReviews()
35-
* @see GHPullRequest#createReview(String, GHPullRequestReviewState, GHPullRequestReviewComment...)
36+
* @see GHPullRequestReviewBuilder
3637
*/
38+
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_FIELD"}, justification = "JSON API")
3739
public class GHPullRequestReview extends GHObject {
3840
GHPullRequest owner;
3941

@@ -72,6 +74,7 @@ public String getCommitId() {
7274
return commit_id;
7375
}
7476

77+
@CheckForNull
7578
public GHPullRequestReviewState getState() {
7679
return state;
7780
}
@@ -85,56 +88,53 @@ protected String getApiRoute() {
8588
return owner.getApiRoute()+"/reviews/"+id;
8689
}
8790

91+
/**
92+
* @deprecated
93+
* Former preview method that changed when it got public. Left here for backward compatibility.
94+
* Use {@link #submit(String, GHPullRequestReviewEvent)}
95+
*/
96+
public void submit(String body, GHPullRequestReviewState state) throws IOException {
97+
submit(body,state.toEvent());
98+
}
99+
88100
/**
89101
* Updates the comment.
90102
*/
91-
@Preview
92-
@Deprecated
93-
public void submit(String body, GHPullRequestReviewState event) throws IOException {
103+
public void submit(String body, GHPullRequestReviewEvent event) throws IOException {
94104
new Requester(owner.root).method("POST")
95105
.with("body", body)
96106
.with("event", event.action())
97-
.withPreview("application/vnd.github.black-cat-preview+json")
98107
.to(getApiRoute()+"/events",this);
99108
this.body = body;
100-
this.state = event;
109+
this.state = event.toState();
101110
}
102111

103112
/**
104113
* Deletes this review.
105114
*/
106-
@Preview
107-
@Deprecated
108115
public void delete() throws IOException {
109116
new Requester(owner.root).method("DELETE")
110-
.withPreview(BLACK_CAT)
111117
.to(getApiRoute());
112118
}
113119

114120
/**
115121
* Dismisses this review.
116122
*/
117-
@Preview
118-
@Deprecated
119123
public void dismiss(String message) throws IOException {
120124
new Requester(owner.root).method("PUT")
121125
.with("message", message)
122-
.withPreview(BLACK_CAT)
123126
.to(getApiRoute()+"/dismissals");
124127
state = GHPullRequestReviewState.DISMISSED;
125128
}
126129

127130
/**
128131
* Obtains all the review comments associated with this pull request review.
129132
*/
130-
@Preview
131-
@Deprecated
132133
public PagedIterable<GHPullRequestReviewComment> listReviewComments() throws IOException {
133134
return new PagedIterable<GHPullRequestReviewComment>() {
134135
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
135136
return new PagedIterator<GHPullRequestReviewComment>(
136137
owner.root.retrieve()
137-
.withPreview(BLACK_CAT)
138138
.asIterator(getApiRoute() + "/comments",
139139
GHPullRequestReviewComment[].class, pageSize)) {
140140
protected void wrapUp(GHPullRequestReviewComment[] page) {
@@ -145,5 +145,4 @@ protected void wrapUp(GHPullRequestReviewComment[] page) {
145145
}
146146
};
147147
}
148-
149148
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Builds up a creation of new {@link GHPullRequestReview}.
9+
*
10+
* @author Kohsuke Kawaguchi
11+
* @see GHPullRequest#createReview()
12+
*/
13+
public class GHPullRequestReviewBuilder {
14+
private final GHPullRequest pr;
15+
private final Requester builder;
16+
private final List<DraftReviewComment> comments = new ArrayList<DraftReviewComment>();
17+
18+
/*package*/ GHPullRequestReviewBuilder(GHPullRequest pr) {
19+
this.pr = pr;
20+
this.builder = new Requester(pr.root);
21+
}
22+
23+
// public GHPullRequestReview createReview(@Nullable String commitId, String body, GHPullRequestReviewEvent event,
24+
// List<GHPullRequestReviewComment> comments) throws IOException
25+
26+
/**
27+
* The SHA of the commit that needs a review. Not using the latest commit SHA may render your review comment outdated if a subsequent commit modifies the line you specify as the position. Defaults to the most recent commit in the pull request when you do not specify a value.
28+
*/
29+
public GHPullRequestReviewBuilder commitId(String commitId) {
30+
builder.with("commit_id",commitId);
31+
return this;
32+
}
33+
34+
/**
35+
* Required when using REQUEST_CHANGES or COMMENT for the event parameter. The body text of the pull request review.
36+
*/
37+
public GHPullRequestReviewBuilder body(String body) {
38+
builder.with("body",body);
39+
return this;
40+
}
41+
42+
/**
43+
* The review action you want to perform. The review actions include: APPROVE, REQUEST_CHANGES, or COMMENT.
44+
* By leaving this blank, you set the review action state to PENDING,
45+
* which means you will need to {@linkplain GHPullRequestReview#submit() submit the pull request review} when you are ready.
46+
*/
47+
public GHPullRequestReviewBuilder event(GHPullRequestReviewEvent event) {
48+
builder.with("event",event.action());
49+
return this;
50+
}
51+
52+
/**
53+
* @param body The relative path to the file that necessitates a review comment.
54+
* @param path The position in the diff where you want to add a review comment. Note this value is not the same as the line number in the file. For help finding the position value, read the note below.
55+
* @param position Text of the review comment.
56+
*/
57+
public GHPullRequestReviewBuilder comment(String body, String path, int position) {
58+
comments.add(new DraftReviewComment(body,path,position));
59+
return this;
60+
}
61+
62+
public GHPullRequestReview create() throws IOException {
63+
return builder.method("POST")._with("comments",comments)
64+
.to(pr.getApiRoute() + "/reviews", GHPullRequestReview.class)
65+
.wrapUp(pr);
66+
}
67+
68+
private static class DraftReviewComment {
69+
private String body;
70+
private String path;
71+
private int position;
72+
73+
DraftReviewComment(String body, String path, int position) {
74+
this.body = body;
75+
this.path = path;
76+
this.position = position;
77+
}
78+
79+
public String getBody() {
80+
return body;
81+
}
82+
83+
public String getPath() {
84+
return path;
85+
}
86+
87+
public int getPosition() {
88+
return position;
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)