From 3f1ccecdf1acee11f43377a613b4943580a38ea8 Mon Sep 17 00:00:00 2001 From: Chris Somme Date: Thu, 25 Aug 2016 11:17:37 -0700 Subject: Use the V2 Bitbucket pull request comment API. The V1 pull request comment API is deprecated. Replace with a call to the V2 API. The V2 API is paginated and may need to be called multiple times to get the entire set of comments. While fixing this I noticed that while fetching pull requests the plugin is using the v2 API there, which is also paginated. So not all open pull requests were being retrieved. --- .../bitbucket/ApiClient.java | 39 ++++++++++++++-------- .../bitbucket/Pullrequest.java | 32 ++++++++++++------ 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index c8dfeb2..7025396 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -6,9 +6,12 @@ import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.DeleteMethod; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.type.TypeFactory; +import org.codehaus.jackson.type.JavaType; import org.codehaus.jackson.type.TypeReference; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Level; @@ -82,23 +85,11 @@ public class ApiClient { } public List getPullRequests() { - try { - return parse(get(v2("/pullrequests/")), Pullrequest.Response.class).getPullrequests(); - } catch(Exception e) { - logger.log(Level.WARNING, "invalid pull request response.", e); - e.printStackTrace(); - } - return Collections.EMPTY_LIST; + return getAllValues(v2("/pullrequests/"), 50, Pullrequest.class); } public List getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { - try { - return parse(get(v1("/pullrequests/" + pullRequestId + "/comments")), new TypeReference>() {}); - } catch(Exception e) { - logger.log(Level.WARNING, "invalid pull request response.", e); - e.printStackTrace(); - } - return Collections.EMPTY_LIST; + return getAllValues(v2("/pullrequests/" + pullRequestId + "/comments"), 100, Pullrequest.Comment.class); } public String getName() { @@ -193,6 +184,23 @@ public class ApiClient { return null; } + private List getAllValues(String rootUrl, int pageLen, Class cls) { + List values = new ArrayList(); + try { + String url = rootUrl + "?pagelen=" + pageLen; + do { + final JavaType type = TypeFactory.defaultInstance().constructParametricType(Pullrequest.Response.class, cls); + Pullrequest.Response response = parse(get(url), type); + values.addAll(response.getValues()); + url = response.getNext(); + } while (url != null); + } catch (Exception e) { + logger.log(Level.WARNING, "invalid response.", e); + e.printStackTrace(); + } + return values; + } + private HttpClient getHttpClient() { return this.factory.getInstanceHttpClient(); } @@ -253,6 +261,9 @@ public class ApiClient { private R parse(String response, Class cls) throws IOException { return new ObjectMapper().readValue(response, cls); } + private R parse(String response, JavaType type) throws IOException { + return new ObjectMapper().readValue(response, type); + } private R parse(String response, TypeReference ref) throws IOException { return new ObjectMapper().readValue(response, ref); } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java index 140c213..96412b2 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java @@ -2,6 +2,7 @@ package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; import java.util.List; import java.util.Comparator; +import java.util.Map; import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; @@ -31,11 +32,12 @@ public class Pullrequest { private Author author; @JsonIgnoreProperties(ignoreUnknown = true) - public static class Response { + public static class Response { private int pageLength; - private List pullrequests; + private List values; private int page; private int size; + private String next; @JsonProperty("pagelen") public int getPageLength() { @@ -45,13 +47,11 @@ public class Pullrequest { public void setPageLength(int pageLength) { this.pageLength = pageLength; } - @JsonProperty("values") - public List getPullrequests() { - return pullrequests; + public List getValues() { + return values; } - @JsonProperty("values") - public void setPullrequests(List pullrequests) { - this.pullrequests = pullrequests; + public void setValues(List values) { + this.values = values; } public int getPage() { return page; @@ -65,8 +65,15 @@ public class Pullrequest { public void setSize(int size) { this.size = size; } + public String getNext() { + return next; + } + public void setNext(String next) { + this.next = next; + } } + @JsonIgnoreProperties(ignoreUnknown = true) public static class Revision { private Repository repository; @@ -214,8 +221,13 @@ public class Pullrequest { return content; } - public void setContent(String content) { - this.content = content; + public void setContent(Object content) { + if (content instanceof String) { + this.content = (String)content; + } else if (content instanceof Map){ + this.content = (String)((Map)content).get("raw"); + } + return; } @JsonProperty("utc_last_updated") public String getUpdatedOn() { -- cgit v1.2.3