Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e74346f

Browse filesBrowse files
committed
support create PR review in single API call & remove @Preview
1 parent 6961c46 commit e74346f
Copy full SHA for e74346f

File tree

Expand file treeCollapse file tree

5 files changed

+214
-129
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+214
-129
lines changed

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHPullRequest.java
+48-24Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package org.kohsuke.github;
2525

26+
import javax.annotation.Nullable;
2627
import java.io.IOException;
2728
import java.net.URL;
2829
import java.util.ArrayList;
@@ -35,13 +36,16 @@
3536

3637
/**
3738
* A pull request.
38-
*
39+
*
3940
* @author Kohsuke Kawaguchi
4041
* @see GHRepository#getPullRequest(int)
4142
*/
4243
@SuppressWarnings({"UnusedDeclaration"})
4344
public class GHPullRequest extends GHIssue {
4445

46+
private static final String COMMENTS_ACTION = "/comments";
47+
private static final String COMMIT_ID_FIELD = "commit_id";
48+
4549
private String patch_url, diff_url, issue_url;
4650
private GHCommitPointer base;
4751
private String merged_at;
@@ -245,7 +249,6 @@ public PagedIterable<GHPullRequestReview> listReviews() {
245249
return new PagedIterable<GHPullRequestReview>() {
246250
public PagedIterator<GHPullRequestReview> _iterator(int pageSize) {
247251
return new PagedIterator<GHPullRequestReview>(root.retrieve()
248-
.withPreview(BLACK_CAT)
249252
.asIterator(String.format("%s/reviews", getApiRoute()),
250253
GHPullRequestReview[].class, pageSize)) {
251254
@Override
@@ -265,7 +268,7 @@ protected void wrapUp(GHPullRequestReview[] page) {
265268
public PagedIterable<GHPullRequestReviewComment> listReviewComments() throws IOException {
266269
return new PagedIterable<GHPullRequestReviewComment>() {
267270
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
268-
return new PagedIterator<GHPullRequestReviewComment>(root.retrieve().asIterator(getApiRoute() + "/comments",
271+
return new PagedIterator<GHPullRequestReviewComment>(root.retrieve().asIterator(getApiRoute() + COMMENTS_ACTION,
269272
GHPullRequestReviewComment[].class, pageSize)) {
270273
protected void wrapUp(GHPullRequestReviewComment[] page) {
271274
for (GHPullRequestReviewComment c : page)
@@ -295,37 +298,58 @@ protected void wrapUp(GHPullRequestCommitDetail[] page) {
295298
};
296299
}
297300

298-
@Preview
299-
@Deprecated
300-
public GHPullRequestReview createReview(String body, GHPullRequestReviewComment... comments)
301+
public GHPullRequestReview createReview(@Nullable String commitId, String body, GHPullRequestReviewEvent event,
302+
GHPullRequestReviewComment... comments) throws IOException {
303+
return createReview(commitId, body, event, Arrays.asList(comments));
304+
}
305+
306+
public GHPullRequestReview createReview(@Nullable String commitId, String body, GHPullRequestReviewEvent event,
307+
List<GHPullRequestReviewComment> comments) throws IOException {
308+
List<DraftReviewComment> draftComments = toDraftReviewComments(comments);
309+
return new Requester(root).method("POST")
310+
.with(COMMIT_ID_FIELD, commitId)
311+
.with("body", body)
312+
.with("event", event.action())
313+
._with("comments", draftComments)
314+
.to(getApiRoute() + "/reviews", GHPullRequestReview.class)
315+
.wrapUp(this);
316+
}
317+
318+
public GHPullRequestReviewDraft newDraftReview(@Nullable String commitId, String body, GHPullRequestReviewComment... comments)
301319
throws IOException {
302-
return createReview(body, Arrays.asList(comments));
320+
return newDraftReview(commitId, body, Arrays.asList(comments));
303321
}
304322

305-
@Preview
306-
@Deprecated
307-
public GHPullRequestReview createReview(String body, List<GHPullRequestReviewComment> comments)
323+
public GHPullRequestReviewDraft newDraftReview(@Nullable String commitId, String body, List<GHPullRequestReviewComment> comments)
308324
throws IOException {
325+
List<DraftReviewComment> draftComments = toDraftReviewComments(comments);
326+
return new Requester(root).method("POST")
327+
.with(COMMIT_ID_FIELD, commitId)
328+
.with("body", body)
329+
._with("comments", draftComments)
330+
.to(getApiRoute() + "/reviews", GHPullRequestReviewDraft.class)
331+
.wrapUp(this);
332+
}
333+
334+
private static List<DraftReviewComment> toDraftReviewComments(List<GHPullRequestReviewComment> comments) {
309335
List<DraftReviewComment> draftComments = new ArrayList<DraftReviewComment>(comments.size());
310336
for (GHPullRequestReviewComment c : comments) {
311337
Integer position = c.getPosition();
312-
draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), position == null ? 0 : position /*FIXME do not use GHPullRequestReviewComment for new comments*/));
338+
if (position == null) {
339+
throw new IllegalArgumentException("GHPullRequestReviewComment must have a position");
340+
}
341+
draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), position));
313342
}
314-
return new Requester(root).method("POST")
315-
.with("body", body)
316-
//.with("event", event.name())
317-
._with("comments", draftComments)
318-
.withPreview(BLACK_CAT)
319-
.to(getApiRoute() + "/reviews", GHPullRequestReview.class).wrapUp(this);
343+
return draftComments;
320344
}
321345

322346
public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position) throws IOException {
323347
return new Requester(root).method("POST")
324348
.with("body", body)
325-
.with("commit_id", sha)
349+
.with(COMMIT_ID_FIELD, sha)
326350
.with("path", path)
327351
.with("position", position)
328-
.to(getApiRoute() + "/comments", GHPullRequestReviewComment.class).wrapUp(this);
352+
.to(getApiRoute() + COMMENTS_ACTION, GHPullRequestReviewComment.class).wrapUp(this);
329353
}
330354

331355
/**
@@ -335,7 +359,7 @@ public GHPullRequestReviewComment createReviewCommentReply(GHPullRequestReviewCo
335359
return new Requester(owner.root).method("POST")
336360
.with("body", body)
337361
.with("in_reply_to", comment.getId())
338-
.to(getApiRoute() + "/comments", GHPullRequestReviewComment.class)
362+
.to(getApiRoute() + COMMENTS_ACTION, GHPullRequestReviewComment.class)
339363
.wrapUp(this);
340364
}
341365

@@ -377,10 +401,10 @@ public void merge(String msg, String sha) throws IOException {
377401
*/
378402
public void merge(String msg, String sha, MergeMethod method) throws IOException {
379403
new Requester(root).method("PUT")
380-
.with("commit_message",msg)
381-
.with("sha",sha)
382-
.with("merge_method",method)
383-
.to(getApiRoute()+"/merge");
404+
.with("commit_message", msg)
405+
.with("sha", sha)
406+
.with("merge_method", method)
407+
.to(getApiRoute() + "/merge");
384408
}
385409

386410
public enum MergeMethod{ MERGE, SQUASH, REBASE }

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHPullRequestReview.java
+8-101Lines changed: 8 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -24,127 +24,34 @@
2424
package org.kohsuke.github;
2525

2626
import java.io.IOException;
27-
import java.net.URL;
28-
import javax.annotation.CheckForNull;
29-
30-
import static org.kohsuke.github.Previews.*;
27+
import java.util.List;
3128

3229
/**
3330
* Review to the pull request
3431
*
3532
* @see GHPullRequest#listReviews()
36-
* @see GHPullRequest#createReview(String, GHPullRequestReviewComment...)
33+
* @see GHPullRequest#createReview(String, String, GHPullRequestReviewEvent, List)
3734
*/
38-
public class GHPullRequestReview extends GHObject {
39-
GHPullRequest owner;
40-
41-
private String body;
42-
private GHUser user;
43-
private String commit_id;
35+
public class GHPullRequestReview extends GHPullRequestReviewAbstract {
4436
private GHPullRequestReviewState state;
4537

46-
/*package*/ GHPullRequestReview wrapUp(GHPullRequest owner) {
47-
this.owner = owner;
48-
return this;
49-
}
50-
51-
/**
52-
* Gets the pull request to which this review is associated.
53-
*/
54-
public GHPullRequest getParent() {
55-
return owner;
56-
}
57-
58-
/**
59-
* The comment itself.
60-
*/
61-
public String getBody() {
62-
return body;
63-
}
64-
65-
/**
66-
* Gets the user who posted this review.
67-
*/
68-
public GHUser getUser() throws IOException {
69-
return owner.root.getUser(user.getLogin());
70-
}
71-
72-
public String getCommitId() {
73-
return commit_id;
74-
}
75-
76-
@CheckForNull
38+
@Override
7739
public GHPullRequestReviewState getState() {
7840
return state;
7941
}
8042

81-
@Override
82-
public URL getHtmlUrl() {
83-
return null;
84-
}
85-
86-
protected String getApiRoute() {
87-
return owner.getApiRoute()+"/reviews/"+id;
88-
}
89-
90-
/**
91-
* Updates the comment.
92-
*/
93-
@Preview
94-
@Deprecated
95-
public void submit(String body, GHPullRequestReviewEvent event) throws IOException {
96-
new Requester(owner.root).method("POST")
97-
.with("body", body)
98-
.with("event", event.action())
99-
.withPreview("application/vnd.github.black-cat-preview+json")
100-
.to(getApiRoute()+"/events",this);
101-
this.body = body;
102-
}
103-
104-
/**
105-
* Deletes this review.
106-
*/
107-
@Preview
108-
@Deprecated
109-
public void delete() throws IOException {
110-
new Requester(owner.root).method("DELETE")
111-
.withPreview(BLACK_CAT)
112-
.to(getApiRoute());
43+
GHPullRequestReview wrapUp(GHPullRequest owner) {
44+
this.owner = owner;
45+
return this;
11346
}
11447

11548
/**
11649
* Dismisses this review.
11750
*/
118-
@Preview
119-
@Deprecated
12051
public void dismiss(String message) throws IOException {
12152
new Requester(owner.root).method("PUT")
12253
.with("message", message)
123-
.withPreview(BLACK_CAT)
124-
.to(getApiRoute()+"/dismissals");
54+
.to(getApiRoute() + "/dismissals");
12555
state = GHPullRequestReviewState.DISMISSED;
12656
}
127-
128-
/**
129-
* Obtains all the review comments associated with this pull request review.
130-
*/
131-
@Preview
132-
@Deprecated
133-
public PagedIterable<GHPullRequestReviewComment> listReviewComments() throws IOException {
134-
return new PagedIterable<GHPullRequestReviewComment>() {
135-
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
136-
return new PagedIterator<GHPullRequestReviewComment>(
137-
owner.root.retrieve()
138-
.withPreview(BLACK_CAT)
139-
.asIterator(getApiRoute() + "/comments",
140-
GHPullRequestReviewComment[].class, pageSize)) {
141-
protected void wrapUp(GHPullRequestReviewComment[] page) {
142-
for (GHPullRequestReviewComment c : page)
143-
c.wrapUp(owner);
144-
}
145-
};
146-
}
147-
};
148-
}
149-
15057
}
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2011, Eric Maupin
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package org.kohsuke.github;
25+
26+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27+
28+
import java.io.IOException;
29+
import java.net.URL;
30+
31+
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_FIELD"}, justification = "JSON API")
32+
public abstract class GHPullRequestReviewAbstract extends GHObject {
33+
protected GHPullRequest owner;
34+
35+
protected String body;
36+
private GHUser user;
37+
private String commit_id;
38+
39+
public abstract GHPullRequestReviewState getState();
40+
41+
/**
42+
* Gets the pull request to which this review is associated.
43+
*/
44+
public GHPullRequest getParent() {
45+
return owner;
46+
}
47+
48+
/**
49+
* The comment itself.
50+
*/
51+
public String getBody() {
52+
return body;
53+
}
54+
55+
/**
56+
* Gets the user who posted this review.
57+
*/
58+
public GHUser getUser() throws IOException {
59+
return owner.root.getUser(user.getLogin());
60+
}
61+
62+
public String getCommitId() {
63+
return commit_id;
64+
}
65+
66+
@Override
67+
public URL getHtmlUrl() {
68+
return null;
69+
}
70+
71+
String getApiRoute() {
72+
return owner.getApiRoute() + "/reviews/" + id;
73+
}
74+
75+
/**
76+
* Deletes this review.
77+
*/
78+
public void delete() throws IOException {
79+
new Requester(owner.root).method("DELETE")
80+
.to(getApiRoute());
81+
}
82+
83+
/**
84+
* Obtains all the review comments associated with this pull request review.
85+
*/
86+
public PagedIterable<GHPullRequestReviewComment> listReviewComments() {
87+
return new PagedIterable<GHPullRequestReviewComment>() {
88+
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
89+
return new PagedIterator<GHPullRequestReviewComment>(
90+
owner.root.retrieve()
91+
.asIterator(getApiRoute() + "/comments",
92+
GHPullRequestReviewComment[].class, pageSize)) {
93+
protected void wrapUp(GHPullRequestReviewComment[] page) {
94+
for (GHPullRequestReviewComment c : page)
95+
c.wrapUp(owner);
96+
}
97+
};
98+
}
99+
};
100+
}
101+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.