diff options
Diffstat (limited to 'src/main/java')
3 files changed, 55 insertions, 34 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java index d337796..899edba 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java @@ -69,10 +69,8 @@ public class BitbucketPullRequestsBuilder { return new String(Hex.encodeHex(MD5.digest(this.project.getFullName().getBytes("UTF-8")))); } catch (NoSuchAlgorithmException exc) { logger.log(Level.WARNING, "Failed to produce hash", exc); - exc.printStackTrace(); } catch (UnsupportedEncodingException exc) { logger.log(Level.WARNING, "Failed to produce hash", exc); - exc.printStackTrace(); } return this.project.getFullName(); } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index c8dfeb2..0c87478 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -5,10 +5,14 @@ import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.params.HttpClientParams; 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; @@ -42,10 +46,16 @@ public class ApiClient { public static final byte MAX_KEY_SIZE_BB_API = 40; public static class HttpClientFactory { - public static final HttpClientFactory INSTANCE = new HttpClientFactory(); + public static final HttpClientFactory INSTANCE = new HttpClientFactory(); + private static final int DEFAULT_TIMEOUT = 60000; public HttpClient getInstanceHttpClient() { HttpClient client = new HttpClient(); + + HttpClientParams params = client.getParams(); + params.setConnectionManagerTimeout(DEFAULT_TIMEOUT); + params.setSoTimeout(DEFAULT_TIMEOUT); + if (Jenkins.getInstance() == null) return client; ProxyConfiguration proxy = Jenkins.getInstance().proxy; @@ -82,23 +92,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() { @@ -121,10 +119,8 @@ public class ApiClient { return new String(Hex.encodeHex(SHA1.digest(computedKey.getBytes("UTF-8")))); } catch(NoSuchAlgorithmException e) { logger.log(Level.WARNING, "Failed to create hash provider", e); - e.printStackTrace(); } catch (UnsupportedEncodingException e) { logger.log(Level.WARNING, "Failed to create hash provider", e); - e.printStackTrace(); } } return (computedKey.length() <= MAX_KEY_SIZE_BB_API) ? computedKey : computedKey.substring(0, MAX_KEY_SIZE_BB_API); @@ -175,7 +171,6 @@ public class ApiClient { new NameValuePair[]{}), Pullrequest.Participant.class); } catch (IOException e) { logger.log(Level.WARNING, "Invalid pull request approval response.", e); - e.printStackTrace(); } return null; } @@ -188,11 +183,26 @@ public class ApiClient { return parse(post(v1("/pullrequests/" + pullRequestId + "/comments"), data), new TypeReference<Pullrequest.Comment>() {}); } catch(Exception e) { logger.log(Level.WARNING, "Invalid pull request comment response.", e); - e.printStackTrace(); } 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); + } + return values; + } + private HttpClient getHttpClient() { return this.factory.getInstanceHttpClient(); } @@ -240,10 +250,8 @@ public class ApiClient { return req.getResponseBodyAsString(); } catch (HttpException e) { logger.log(Level.WARNING, "Failed to send request.", e); - e.printStackTrace(); } catch (IOException e) { logger.log(Level.WARNING, "Failed to send request.", e); - e.printStackTrace(); } finally { req.releaseConnection(); } @@ -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); } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java index 140c213..25755a4 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<T> { private int pageLength; - private List<Pullrequest> pullrequests; + private List<T> 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<Pullrequest> getPullrequests() { - return pullrequests; + public List<T> getValues() { + return values; } - @JsonProperty("values") - public void setPullrequests(List<Pullrequest> pullrequests) { - this.pullrequests = pullrequests; + public void setValues(List<T> 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; @@ -107,7 +114,7 @@ public class Pullrequest { @JsonProperty("full_name") public void setFullName(String fullName) { // Also extract owner- and reponame - if (name != null) { + if (fullName != null) { this.ownerName = fullName.split("/")[0]; this.repositoryName = fullName.split("/")[1]; } @@ -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() { |