Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Pull Request review and comments support #406

Merged
merged 5 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 61 additions & 31 deletions src/main/java/org/kohsuke/github/GHPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package org.kohsuke.github;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
Expand All @@ -36,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;
Expand Down Expand Up @@ -91,7 +94,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
Expand All @@ -114,7 +117,7 @@ public GHCommitPointer getBase() {
public GHCommitPointer getHead() {
return head;
}

@Deprecated
public Date getIssueUpdatedAt() throws IOException {
return super.getUpdatedAt();
Expand Down Expand Up @@ -246,7 +249,6 @@ public PagedIterable<GHPullRequestReview> listReviews() {
return new PagedIterable<GHPullRequestReview>() {
public PagedIterator<GHPullRequestReview> _iterator(int pageSize) {
return new PagedIterator<GHPullRequestReview>(root.retrieve()
.withPreview(BLACK_CAT)
.asIterator(String.format("%s/reviews", getApiRoute()),
GHPullRequestReview[].class, pageSize)) {
@Override
Expand All @@ -266,7 +268,7 @@ protected void wrapUp(GHPullRequestReview[] page) {
public PagedIterable<GHPullRequestReviewComment> listReviewComments() throws IOException {
return new PagedIterable<GHPullRequestReviewComment>() {
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
return new PagedIterator<GHPullRequestReviewComment>(root.retrieve().asIterator(getApiRoute() + "/comments",
return new PagedIterator<GHPullRequestReviewComment>(root.retrieve().asIterator(getApiRoute() + COMMENTS_ACTION,
GHPullRequestReviewComment[].class, pageSize)) {
protected void wrapUp(GHPullRequestReviewComment[] page) {
for (GHPullRequestReviewComment c : page)
Expand Down Expand Up @@ -296,41 +298,69 @@ protected void wrapUp(GHPullRequestCommitDetail[] page) {
};
}

@Preview
@Deprecated
public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event,
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<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 createReview(body, event, Arrays.asList(comments));
return newDraftReview(commitId, body, Arrays.asList(comments));
}

@Preview
@Deprecated
public GHPullRequestReview createReview(String body, @CheckForNull GHPullRequestReviewState event,
List<GHPullRequestReviewComment> comments)
public GHPullRequestReviewDraft newDraftReview(@Nullable String commitId, String body, List<GHPullRequestReviewComment> comments)
throws IOException {
// if (event == null) {
// event = GHPullRequestReviewState.PENDING;
// }
List<DraftReviewComment> draftComments = new ArrayList<DraftReviewComment>(comments.size());
for (GHPullRequestReviewComment c : comments) {
draftComments.add(new DraftReviewComment(c.getBody(), c.getPath(), c.getPosition()));
}
List<DraftReviewComment> draftComments = toDraftReviewComments(comments);
return new Requester(root).method("POST")
.with(COMMIT_ID_FIELD, commitId)
.with("body", body)
//.with("event", event.name())
._with("comments", draftComments)
.withPreview(BLACK_CAT)
.to(getApiRoute() + "/reviews", GHPullRequestReview.class).wrapUp(this);
.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 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);
}

/**
* 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);
}

/**
Expand Down Expand Up @@ -371,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 }
Expand Down
108 changes: 8 additions & 100 deletions src/main/java/org/kohsuke/github/GHPullRequestReview.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,126 +24,34 @@
package org.kohsuke.github;

import java.io.IOException;
import java.net.URL;

import static org.kohsuke.github.Previews.*;
import java.util.List;

/**
* Review to the pull request
*
* @see GHPullRequest#listReviews()
* @see GHPullRequest#createReview(String, GHPullRequestReviewState, 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;
}

@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, GHPullRequestReviewState 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;
}

/**
* 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<GHPullRequestReviewComment> listReviewComments() throws IOException {
return new PagedIterable<GHPullRequestReviewComment>() {
public PagedIterator<GHPullRequestReviewComment> _iterator(int pageSize) {
return new PagedIterator<GHPullRequestReviewComment>(
owner.root.retrieve()
.withPreview(BLACK_CAT)
.asIterator(getApiRoute() + "/comments",
GHPullRequestReviewComment[].class, pageSize)) {
protected void wrapUp(GHPullRequestReviewComment[] page) {
for (GHPullRequestReviewComment c : page)
c.wrapUp(owner);
}
};
}
};
}

}
Loading