aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java
diff options
context:
space:
mode:
authorChris Somme <csomme@atlassian.com>2016-08-25 11:17:37 -0700
committerChris Somme <csomme@atlassian.com>2016-08-25 11:28:18 -0700
commit3f1ccecdf1acee11f43377a613b4943580a38ea8 (patch)
tree2c86a5a708c6c47dba54dc7116d3a810ce7e7df3 /src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java
parent37d5d42e056d5faff0bbed119115a4cbe7c4e016 (diff)
downloadbbprb-3f1ccecdf1acee11f43377a613b4943580a38ea8.tar.gz
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.
Diffstat (limited to 'src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java')
-rw-r--r--src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java39
1 files changed, 25 insertions, 14 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<Pullrequest> 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<Pullrequest.Comment> getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) {
- try {
- return parse(get(v1("/pullrequests/" + pullRequestId + "/comments")), new TypeReference<List<Pullrequest.Comment>>() {});
- } 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 <T> List<T> getAllValues(String rootUrl, int pageLen, Class<T> cls) {
+ List<T> values = new ArrayList<T>();
+ try {
+ String url = rootUrl + "?pagelen=" + pageLen;
+ do {
+ final JavaType type = TypeFactory.defaultInstance().constructParametricType(Pullrequest.Response.class, cls);
+ Pullrequest.Response<T> 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> R parse(String response, Class<R> cls) throws IOException {
return new ObjectMapper().readValue(response, cls);
}
+ private <R> R parse(String response, JavaType type) throws IOException {
+ return new ObjectMapper().readValue(response, type);
+ }
private <R> R parse(String response, TypeReference<R> ref) throws IOException {
return new ObjectMapper().readValue(response, ref);
}