Skip to content

Commit

Permalink
Massaged the changes
Browse files Browse the repository at this point in the history
- Restored binary compatibility. The draft API has changed in some
  significant way when it became public, and the PR took the liberty to
  delete removed constants and method signatures. I brought them back so
  that older clients can keep functioning.

- Introduced a builder pattern to create PR review

- replying to a review comment should be an instance method, not a
  static method.

- GHPullRequestReview and GHPullRequestReviewDraft was not making sense
  to me, since GitHub API doesn't differentiate them. It creates a
  practical problem of not being able to submit a review comment unless
  you created the review comment in the same JVM session. That said, I
  don't understand the point of this two phase approach in the GitHub
  API to begin with!
  • Loading branch information
kohsuke committed Jan 13, 2018
1 parent e74346f commit acbf286
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 269 deletions.
89 changes: 3 additions & 86 deletions src/main/java/org/kohsuke/github/GHPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@
*/
package org.kohsuke.github;

import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static org.kohsuke.github.Previews.*;

/**
* A pull request.
Expand All @@ -44,7 +38,6 @@
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;
Expand Down Expand Up @@ -298,71 +291,19 @@ protected void wrapUp(GHPullRequestCommitDetail[] page) {
};
}

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<GHPullRequestReviewComment> comments) throws IOException {
List<DraftReviewComment> 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 newDraftReview(commitId, body, Arrays.asList(comments));
}

public GHPullRequestReviewDraft newDraftReview(@Nullable String commitId, String body, List<GHPullRequestReviewComment> comments)
throws IOException {
List<DraftReviewComment> 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<DraftReviewComment> toDraftReviewComments(List<GHPullRequestReviewComment> comments) {
List<DraftReviewComment> draftComments = new ArrayList<DraftReviewComment>(comments.size());
for (GHPullRequestReviewComment c : comments) {
Integer position = c.getPosition();
if (position == null) {
throw new IllegalArgumentException("GHPullRequestReviewComment must have a position");
}
draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), position));
}
return draftComments;
public GHPullRequestReviewBuilder createReview() {
return new GHPullRequestReviewBuilder(this);
}

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_FIELD, sha)
.with("commit_id", sha)
.with("path", path)
.with("position", position)
.to(getApiRoute() + COMMENTS_ACTION, 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_ACTION, GHPullRequestReviewComment.class)
.wrapUp(this);
}

/**
* Merge this pull request.
*
Expand Down Expand Up @@ -415,28 +356,4 @@ private void fetchIssue() throws IOException {
fetchedIssueDetails = true;
}
}

private static class DraftReviewComment {
private String body;
private String path;
private int position;

public DraftReviewComment(String body, String path, int position) {
this.body = body;
this.path = path;
this.position = position;
}

public String getBody() {
return body;
}

public String getPath() {
return path;
}

public int getPosition() {
return position;
}
}
}
109 changes: 100 additions & 9 deletions src/main/java/org/kohsuke/github/GHPullRequestReview.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,98 @@
*/
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.util.List;
import java.net.URL;

/**
* Review to the pull request
* Review to a pull request.
*
* @see GHPullRequest#listReviews()
* @see GHPullRequest#createReview(String, String, GHPullRequestReviewEvent, List)
* @see GHPullRequestReviewBuilder
*/
public class GHPullRequestReview extends GHPullRequestReviewAbstract {
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_FIELD"}, justification = "JSON API")
public class GHPullRequestReview extends GHObject {
GHPullRequest owner;

private String body;
private GHUser user;
private String commit_id;
private GHPullRequestReviewState state;

@Override
/*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
public GHPullRequestReviewState getState() {
return state;
}

GHPullRequestReview wrapUp(GHPullRequest owner) {
this.owner = owner;
return this;
@Override
public URL getHtmlUrl() {
return null;
}

protected String getApiRoute() {
return owner.getApiRoute()+"/reviews/"+id;
}

/**
* @deprecated
* Former preview method that changed when it got public. Left here for backward compatibility.
* Use {@link #submit(String, GHPullRequestReviewEvent)}
*/
public void submit(String body, GHPullRequestReviewState state) throws IOException {
submit(body,state.toEvent());
}

/**
* Updates the comment.
*/
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;
this.state = event.toState();
}

/**
* Deletes this review.
*/
public void delete() throws IOException {
new Requester(owner.root).method("DELETE")
.to(getApiRoute());
}

/**
Expand All @@ -51,7 +123,26 @@ GHPullRequestReview wrapUp(GHPullRequest owner) {
public void dismiss(String message) throws IOException {
new Requester(owner.root).method("PUT")
.with("message", message)
.to(getApiRoute() + "/dismissals");
.to(getApiRoute()+"/dismissals");
state = GHPullRequestReviewState.DISMISSED;
}

/**
* Obtains all the review comments associated with this pull request review.
*/
public PagedIterable<GHPullRequestReviewComment> listReviewComments() throws IOException {
return new PagedIterable<GHPullRequestReviewComment>() {
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
return new PagedIterator<GHPullRequestReviewComment>(
owner.root.retrieve()
.asIterator(getApiRoute() + "/comments",
GHPullRequestReviewComment[].class, pageSize)) {
protected void wrapUp(GHPullRequestReviewComment[] page) {
for (GHPullRequestReviewComment c : page)
c.wrapUp(owner);
}
};
}
};
}
}
101 changes: 0 additions & 101 deletions src/main/java/org/kohsuke/github/GHPullRequestReviewAbstract.java

This file was deleted.

Loading

0 comments on commit acbf286

Please sign in to comment.