From 15991fd2f763a40dd21c2a7715735abd87de9423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 30 Nov 2017 14:38:47 +0100 Subject: [PATCH 1/5] PullRequest review state and event do not have same values this fixes parsing of response of WS call submitting a review to fail when event of new review is REQUEST_CHANGES also, specifying event when creating a pending PR review is useless and providing it when submitting the review is enough --- .../org/kohsuke/github/GHPullRequest.java | 15 +++---- .../kohsuke/github/GHPullRequestReview.java | 7 ++-- .../github/GHPullRequestReviewEvent.java | 41 +++++++++++++++++++ .../github/GHPullRequestReviewState.java | 20 +++------ .../org/kohsuke/github/PullRequestTest.java | 2 +- 5 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 src/main/java/org/kohsuke/github/GHPullRequestReviewEvent.java diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index 9342623829..99db3bf32c 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -23,7 +23,6 @@ */ package org.kohsuke.github; -import javax.annotation.CheckForNull; import java.io.IOException; import java.net.URL; import java.util.ArrayList; @@ -298,23 +297,19 @@ protected void wrapUp(GHPullRequestCommitDetail[] page) { @Preview @Deprecated - public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event, - GHPullRequestReviewComment... comments) + public GHPullRequestReview createReview(String body, GHPullRequestReviewComment... comments) throws IOException { - return createReview(body, event, Arrays.asList(comments)); + return createReview(body, Arrays.asList(comments)); } @Preview @Deprecated - public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event, - List comments) + public GHPullRequestReview createReview(String body, List comments) throws IOException { -// if (event == null) { -// event = GHPullRequestReviewState.PENDING; -// } List draftComments = new ArrayList(comments.size()); for (GHPullRequestReviewComment c : comments) { - draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), c.getPosition())); + Integer position = c.getPosition(); + draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), position == null ? 0 : position /*FIXME do not use GHPullRequestReviewComment for new comments*/)); } return new Requester(root).method("POST") .with("body", body) diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReview.java b/src/main/java/org/kohsuke/github/GHPullRequestReview.java index d7afe59aad..c96e8f1491 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReview.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReview.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.net.URL; +import javax.annotation.CheckForNull; import static org.kohsuke.github.Previews.*; @@ -32,7 +33,7 @@ * Review to the pull request * * @see GHPullRequest#listReviews() - * @see GHPullRequest#createReview(String, GHPullRequestReviewState, GHPullRequestReviewComment...) + * @see GHPullRequest#createReview(String, GHPullRequestReviewComment...) */ public class GHPullRequestReview extends GHObject { GHPullRequest owner; @@ -72,6 +73,7 @@ public String getCommitId() { return commit_id; } + @CheckForNull public GHPullRequestReviewState getState() { return state; } @@ -90,14 +92,13 @@ protected String getApiRoute() { */ @Preview @Deprecated - public void submit(String body, GHPullRequestReviewState event) throws IOException { + public void submit(String body, GHPullRequestReviewEvent event) throws IOException { new Requester(owner.root).method("POST") .with("body", body) .with("event", event.action()) .withPreview("application/vnd.github.black-cat-preview+json") .to(getApiRoute()+"/events",this); this.body = body; - this.state = event; } /** diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewEvent.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewEvent.java new file mode 100644 index 0000000000..4a5d66094f --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewEvent.java @@ -0,0 +1,41 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Eric Maupin + * + * 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; + +public enum GHPullRequestReviewEvent { + PENDING(null), + APPROVE("APPROVE"), + REQUEST_CHANGES("REQUEST_CHANGES"), + COMMENT("COMMENT"); + + private final String _action; + + GHPullRequestReviewEvent(String action) { + _action = action; + } + + public String action() { + return _action; + } +} diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewState.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewState.java index fca3fabfb3..eff7fbf60b 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReviewState.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewState.java @@ -1,19 +1,9 @@ package org.kohsuke.github; public enum GHPullRequestReviewState { - PENDING(null), - APPROVED("APPROVE"), - REQUEST_CHANGES("REQUEST_CHANGES"), - COMMENTED("COMMENT"), - DISMISSED(null); - - private final String _action; - - GHPullRequestReviewState(String action) { - _action = action; - } - - public String action() { - return _action; - } + PENDING, + APPROVED, + CHANGES_REQUESTED, + COMMENTED, + DISMISSED } diff --git a/src/test/java/org/kohsuke/github/PullRequestTest.java b/src/test/java/org/kohsuke/github/PullRequestTest.java index 5ea2628303..bf45e35c46 100644 --- a/src/test/java/org/kohsuke/github/PullRequestTest.java +++ b/src/test/java/org/kohsuke/github/PullRequestTest.java @@ -45,7 +45,7 @@ public void testPullRequestReviews() throws Exception { assertThat(review.getState(), is(GHPullRequestReviewState.PENDING)); assertThat(review.getBody(), is("Some draft review")); assertThat(review.getCommitId(), notNullValue()); - review.submit("Some review comment", GHPullRequestReviewState.COMMENTED); + review.submit("Some review comment", GHPullRequestReviewEvent.COMMENT); List comments = review.listReviewComments().asList(); assertEquals(1, comments.size()); GHPullRequestReviewComment comment = comments.get(0); From fa16261d7a7e96552b5694c0c24120da5afba1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 30 Nov 2017 14:39:16 +0100 Subject: [PATCH 2/5] position of pr comment can be null and original_position is not parsed position of pr comment is null when comment is outdated (ie. not located in diff patch anymore) --- .../kohsuke/github/GHPullRequestReviewComment.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java index 33097bc29d..f1a2c603da 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.net.URL; +import javax.annotation.CheckForNull; import static org.kohsuke.github.Previews.*; @@ -41,8 +42,8 @@ public class GHPullRequestReviewComment extends GHObject implements Reactable { private String body; private GHUser user; private String path; - private int position; - private int originalPosition; + private int position = -1; + private int original_position = -1; public static GHPullRequestReviewComment draft(String body, String path, int position) { GHPullRequestReviewComment result = new GHPullRequestReviewComment(); @@ -82,12 +83,14 @@ public String getPath() { return path; } - public int getPosition() { - return position; + @CheckForNull + public Integer getPosition() { + return position == -1 ? null : position; } + @CheckForNull public int getOriginalPosition() { - return originalPosition; + return original_position == -1 ? null : original_position; } @Override From 892d30516546be46f7cebd3a0f920db0801b3e29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 8 Jan 2018 15:29:22 +0100 Subject: [PATCH 3/5] add Requester#with override for long value --- src/main/java/org/kohsuke/github/Requester.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index 00f11f2426..73a8bfb34e 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -134,6 +134,10 @@ public Requester with(String key, int value) { return _with(key, value); } + public Requester with(String key, long value) { + return _with(key, value); + } + public Requester with(String key, Integer value) { if (value!=null) _with(key, value); From 6961c467a64a43584e026b2438e7a254fcc8f481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 1 Dec 2017 17:37:27 +0100 Subject: [PATCH 4/5] create review comment reply from GHPullRequest --- .../java/org/kohsuke/github/GHPullRequest.java | 15 +++++++++++++-- .../github/GHPullRequestReviewComment.java | 11 +++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index 99db3bf32c..369dff911b 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -90,7 +90,7 @@ protected String getApiRoute() { public URL getPatchUrl() { return GitHub.parseURL(patch_url); } - + /** * The URL of the patch file. * like https://github.com/jenkinsci/jenkins/pull/100.patch @@ -113,7 +113,7 @@ public GHCommitPointer getBase() { public GHCommitPointer getHead() { return head; } - + @Deprecated public Date getIssueUpdatedAt() throws IOException { return super.getUpdatedAt(); @@ -328,6 +328,17 @@ public GHPullRequestReviewComment createReviewComment(String body, String sha, S .to(getApiRoute() + "/comments", GHPullRequestReviewComment.class).wrapUp(this); } + /** + * Create a reply to the current comment. + */ + public GHPullRequestReviewComment createReviewCommentReply(GHPullRequestReviewComment comment, String body) throws IOException { + return new Requester(owner.root).method("POST") + .with("body", body) + .with("in_reply_to", comment.getId()) + .to(getApiRoute() + "/comments", GHPullRequestReviewComment.class) + .wrapUp(this); + } + /** * Merge this pull request. * diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java index f1a2c603da..5ab8861f8d 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java @@ -44,6 +44,8 @@ public class GHPullRequestReviewComment extends GHObject implements Reactable { private String path; private int position = -1; private int original_position = -1; + private long in_reply_to_id = -1L; + public static GHPullRequestReviewComment draft(String body, String path, int position) { GHPullRequestReviewComment result = new GHPullRequestReviewComment(); @@ -89,10 +91,15 @@ public Integer getPosition() { } @CheckForNull - public int getOriginalPosition() { + public Integer getOriginalPosition() { return original_position == -1 ? null : original_position; } + @CheckForNull + public Long getInReplyToId() { + return in_reply_to_id == -1 ? null : in_reply_to_id; + } + @Override public URL getHtmlUrl() { return null; @@ -129,7 +136,7 @@ public GHReaction createReaction(ReactionContent content) throws IOException { public PagedIterable listReactions() { return new PagedIterable() { public PagedIterator _iterator(int pageSize) { - return new PagedIterator(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute()+"/reactions", GHReaction[].class, pageSize)) { + return new PagedIterator(owner.root.retrieve().withPreview(SQUIRREL_GIRL).asIterator(getApiRoute() + "/reactions", GHReaction[].class, pageSize)) { @Override protected void wrapUp(GHReaction[] page) { for (GHReaction c : page) From e74346fed6d5206d8ab54e4dab5542df597ddd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 4 Dec 2017 11:11:58 +0100 Subject: [PATCH 5/5] support create PR review in single API call & remove @Preview --- .../org/kohsuke/github/GHPullRequest.java | 72 ++++++++---- .../kohsuke/github/GHPullRequestReview.java | 109 ++---------------- .../github/GHPullRequestReviewAbstract.java | 101 ++++++++++++++++ .../github/GHPullRequestReviewDraft.java | 53 +++++++++ .../org/kohsuke/github/PullRequestTest.java | 8 +- 5 files changed, 214 insertions(+), 129 deletions(-) create mode 100644 src/main/java/org/kohsuke/github/GHPullRequestReviewAbstract.java create mode 100644 src/main/java/org/kohsuke/github/GHPullRequestReviewDraft.java diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index 369dff911b..5a62936f8d 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -23,6 +23,7 @@ */ package org.kohsuke.github; +import javax.annotation.Nullable; import java.io.IOException; import java.net.URL; import java.util.ArrayList; @@ -35,13 +36,16 @@ /** * A pull request. - * + * * @author Kohsuke Kawaguchi * @see GHRepository#getPullRequest(int) */ @SuppressWarnings({"UnusedDeclaration"}) public class GHPullRequest extends GHIssue { + private static final String COMMENTS_ACTION = "/comments"; + private static final String COMMIT_ID_FIELD = "commit_id"; + private String patch_url, diff_url, issue_url; private GHCommitPointer base; private String merged_at; @@ -245,7 +249,6 @@ public PagedIterable listReviews() { return new PagedIterable() { public PagedIterator _iterator(int pageSize) { return new PagedIterator(root.retrieve() - .withPreview(BLACK_CAT) .asIterator(String.format("%s/reviews", getApiRoute()), GHPullRequestReview[].class, pageSize)) { @Override @@ -265,7 +268,7 @@ protected void wrapUp(GHPullRequestReview[] page) { public PagedIterable listReviewComments() throws IOException { return new PagedIterable() { public PagedIterator _iterator(int pageSize) { - return new PagedIterator(root.retrieve().asIterator(getApiRoute() + "/comments", + return new PagedIterator(root.retrieve().asIterator(getApiRoute() + COMMENTS_ACTION, GHPullRequestReviewComment[].class, pageSize)) { protected void wrapUp(GHPullRequestReviewComment[] page) { for (GHPullRequestReviewComment c : page) @@ -295,37 +298,58 @@ protected void wrapUp(GHPullRequestCommitDetail[] page) { }; } - @Preview - @Deprecated - public GHPullRequestReview createReview(String body, GHPullRequestReviewComment... comments) + public GHPullRequestReview createReview(@Nullable String commitId, String body, GHPullRequestReviewEvent event, + GHPullRequestReviewComment... comments) throws IOException { + return createReview(commitId, body, event, Arrays.asList(comments)); + } + + public GHPullRequestReview createReview(@Nullable String commitId, String body, GHPullRequestReviewEvent event, + List comments) throws IOException { + List draftComments = toDraftReviewComments(comments); + return new Requester(root).method("POST") + .with(COMMIT_ID_FIELD, commitId) + .with("body", body) + .with("event", event.action()) + ._with("comments", draftComments) + .to(getApiRoute() + "/reviews", GHPullRequestReview.class) + .wrapUp(this); + } + + public GHPullRequestReviewDraft newDraftReview(@Nullable String commitId, String body, GHPullRequestReviewComment... comments) throws IOException { - return createReview(body, Arrays.asList(comments)); + return newDraftReview(commitId, body, Arrays.asList(comments)); } - @Preview - @Deprecated - public GHPullRequestReview createReview(String body, List comments) + public GHPullRequestReviewDraft newDraftReview(@Nullable String commitId, String body, List comments) throws IOException { + List draftComments = toDraftReviewComments(comments); + return new Requester(root).method("POST") + .with(COMMIT_ID_FIELD, commitId) + .with("body", body) + ._with("comments", draftComments) + .to(getApiRoute() + "/reviews", GHPullRequestReviewDraft.class) + .wrapUp(this); + } + + private static List toDraftReviewComments(List comments) { List draftComments = new ArrayList(comments.size()); for (GHPullRequestReviewComment c : comments) { Integer position = c.getPosition(); - draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), position == null ? 0 : position /*FIXME do not use GHPullRequestReviewComment for new comments*/)); + if (position == null) { + throw new IllegalArgumentException("GHPullRequestReviewComment must have a position"); + } + draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), position)); } - return new Requester(root).method("POST") - .with("body", body) - //.with("event", event.name()) - ._with("comments", draftComments) - .withPreview(BLACK_CAT) - .to(getApiRoute() + "/reviews", GHPullRequestReview.class).wrapUp(this); + return draftComments; } public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position) throws IOException { return new Requester(root).method("POST") .with("body", body) - .with("commit_id", sha) + .with(COMMIT_ID_FIELD, sha) .with("path", path) .with("position", position) - .to(getApiRoute() + "/comments", GHPullRequestReviewComment.class).wrapUp(this); + .to(getApiRoute() + COMMENTS_ACTION, GHPullRequestReviewComment.class).wrapUp(this); } /** @@ -335,7 +359,7 @@ public GHPullRequestReviewComment createReviewCommentReply(GHPullRequestReviewCo return new Requester(owner.root).method("POST") .with("body", body) .with("in_reply_to", comment.getId()) - .to(getApiRoute() + "/comments", GHPullRequestReviewComment.class) + .to(getApiRoute() + COMMENTS_ACTION, GHPullRequestReviewComment.class) .wrapUp(this); } @@ -377,10 +401,10 @@ public void merge(String msg, String sha) throws IOException { */ public void merge(String msg, String sha, MergeMethod method) throws IOException { new Requester(root).method("PUT") - .with("commit_message",msg) - .with("sha",sha) - .with("merge_method",method) - .to(getApiRoute()+"/merge"); + .with("commit_message", msg) + .with("sha", sha) + .with("merge_method", method) + .to(getApiRoute() + "/merge"); } public enum MergeMethod{ MERGE, SQUASH, REBASE } diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReview.java b/src/main/java/org/kohsuke/github/GHPullRequestReview.java index c96e8f1491..3bb155b881 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReview.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReview.java @@ -24,127 +24,34 @@ package org.kohsuke.github; import java.io.IOException; -import java.net.URL; -import javax.annotation.CheckForNull; - -import static org.kohsuke.github.Previews.*; +import java.util.List; /** * Review to the pull request * * @see GHPullRequest#listReviews() - * @see GHPullRequest#createReview(String, GHPullRequestReviewComment...) + * @see GHPullRequest#createReview(String, String, GHPullRequestReviewEvent, List) */ -public class GHPullRequestReview extends GHObject { - GHPullRequest owner; - - private String body; - private GHUser user; - private String commit_id; +public class GHPullRequestReview extends GHPullRequestReviewAbstract { private GHPullRequestReviewState state; - /*package*/ GHPullRequestReview wrapUp(GHPullRequest owner) { - this.owner = owner; - return this; - } - - /** - * Gets the pull request to which this review is associated. - */ - public GHPullRequest getParent() { - return owner; - } - - /** - * The comment itself. - */ - public String getBody() { - return body; - } - - /** - * Gets the user who posted this review. - */ - public GHUser getUser() throws IOException { - return owner.root.getUser(user.getLogin()); - } - - public String getCommitId() { - return commit_id; - } - - @CheckForNull + @Override public GHPullRequestReviewState getState() { return state; } - @Override - public URL getHtmlUrl() { - return null; - } - - protected String getApiRoute() { - return owner.getApiRoute()+"/reviews/"+id; - } - - /** - * Updates the comment. - */ - @Preview - @Deprecated - public void submit(String body, GHPullRequestReviewEvent event) throws IOException { - new Requester(owner.root).method("POST") - .with("body", body) - .with("event", event.action()) - .withPreview("application/vnd.github.black-cat-preview+json") - .to(getApiRoute()+"/events",this); - this.body = body; - } - - /** - * Deletes this review. - */ - @Preview - @Deprecated - public void delete() throws IOException { - new Requester(owner.root).method("DELETE") - .withPreview(BLACK_CAT) - .to(getApiRoute()); + GHPullRequestReview wrapUp(GHPullRequest owner) { + this.owner = owner; + return this; } /** * Dismisses this review. */ - @Preview - @Deprecated public void dismiss(String message) throws IOException { new Requester(owner.root).method("PUT") .with("message", message) - .withPreview(BLACK_CAT) - .to(getApiRoute()+"/dismissals"); + .to(getApiRoute() + "/dismissals"); state = GHPullRequestReviewState.DISMISSED; } - - /** - * Obtains all the review comments associated with this pull request review. - */ - @Preview - @Deprecated - public PagedIterable listReviewComments() throws IOException { - return new PagedIterable() { - public PagedIterator _iterator(int pageSize) { - return new PagedIterator( - owner.root.retrieve() - .withPreview(BLACK_CAT) - .asIterator(getApiRoute() + "/comments", - GHPullRequestReviewComment[].class, pageSize)) { - protected void wrapUp(GHPullRequestReviewComment[] page) { - for (GHPullRequestReviewComment c : page) - c.wrapUp(owner); - } - }; - } - }; - } - } diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewAbstract.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewAbstract.java new file mode 100644 index 0000000000..4842a62085 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewAbstract.java @@ -0,0 +1,101 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Eric Maupin + * + * 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 java.io.IOException; +import java.net.URL; + +@SuppressFBWarnings(value = {"UWF_UNWRITTEN_FIELD"}, justification = "JSON API") +public abstract class GHPullRequestReviewAbstract extends GHObject { + protected GHPullRequest owner; + + protected String body; + private GHUser user; + private String commit_id; + + public abstract GHPullRequestReviewState getState(); + + /** + * Gets the pull request to which this review is associated. + */ + public GHPullRequest getParent() { + return owner; + } + + /** + * The comment itself. + */ + public String getBody() { + return body; + } + + /** + * Gets the user who posted this review. + */ + public GHUser getUser() throws IOException { + return owner.root.getUser(user.getLogin()); + } + + public String getCommitId() { + return commit_id; + } + + @Override + public URL getHtmlUrl() { + return null; + } + + String getApiRoute() { + return owner.getApiRoute() + "/reviews/" + id; + } + + /** + * Deletes this review. + */ + public void delete() throws IOException { + new Requester(owner.root).method("DELETE") + .to(getApiRoute()); + } + + /** + * Obtains all the review comments associated with this pull request review. + */ + public PagedIterable listReviewComments() { + return new PagedIterable() { + public PagedIterator _iterator(int pageSize) { + return new PagedIterator( + owner.root.retrieve() + .asIterator(getApiRoute() + "/comments", + GHPullRequestReviewComment[].class, pageSize)) { + protected void wrapUp(GHPullRequestReviewComment[] page) { + for (GHPullRequestReviewComment c : page) + c.wrapUp(owner); + } + }; + } + }; + } +} diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewDraft.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewDraft.java new file mode 100644 index 0000000000..689d061bc0 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewDraft.java @@ -0,0 +1,53 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Eric Maupin + * + * 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 java.io.IOException; + +/** + * Draft of a Pull Request review. + * + * @see GHPullRequest#newDraftReview(String, String, GHPullRequestReviewComment...) + */ +public class GHPullRequestReviewDraft extends GHPullRequestReviewAbstract { + + GHPullRequestReviewDraft wrapUp(GHPullRequest owner) { + this.owner = owner; + return this; + } + + @Override + public GHPullRequestReviewState getState() { + return GHPullRequestReviewState.PENDING; + } + + public void submit(String body, GHPullRequestReviewEvent event) throws IOException { + new Requester(owner.root).method("POST") + .with("body", body) + .with("event", event.action()) + .to(getApiRoute() + "/events", this); + this.body = body; + } + +} diff --git a/src/test/java/org/kohsuke/github/PullRequestTest.java b/src/test/java/org/kohsuke/github/PullRequestTest.java index bf45e35c46..97826baea0 100644 --- a/src/test/java/org/kohsuke/github/PullRequestTest.java +++ b/src/test/java/org/kohsuke/github/PullRequestTest.java @@ -33,7 +33,7 @@ public void createPullRequestComment() throws Exception { public void testPullRequestReviews() throws Exception { String name = rnd.next(); GHPullRequest p = getRepository().createPullRequest(name, "stable", "master", "## test"); - GHPullRequestReview draftReview = p.createReview("Some draft review", null, + GHPullRequestReviewDraft draftReview = p.newDraftReview(null, "Some draft review", GHPullRequestReviewComment.draft("Some niggle", "changelog.html", 1) ); assertThat(draftReview.getState(), is(GHPullRequestReviewState.PENDING)); @@ -45,15 +45,15 @@ public void testPullRequestReviews() throws Exception { assertThat(review.getState(), is(GHPullRequestReviewState.PENDING)); assertThat(review.getBody(), is("Some draft review")); assertThat(review.getCommitId(), notNullValue()); - review.submit("Some review comment", GHPullRequestReviewEvent.COMMENT); + draftReview.submit("Some review comment", GHPullRequestReviewEvent.COMMENT); List comments = review.listReviewComments().asList(); assertEquals(1, comments.size()); GHPullRequestReviewComment comment = comments.get(0); assertEquals("Some niggle", comment.getBody()); - review = p.createReview("Some new review", null, + draftReview = p.newDraftReview(null, "Some new review", GHPullRequestReviewComment.draft("Some niggle", "changelog.html", 1) ); - review.delete(); + draftReview.delete(); } @Test