From 69baef38cbde57293451dc841b1b370ec0708e88 Mon Sep 17 00:00:00 2001 From: Jonathan Brachthäuser Date: Thu, 3 Sep 2015 22:37:59 +0200 Subject: Refactored: Only have one file for JSON POJOs --- .../BitbucketPullRequestsBuilder.java | 4 +- .../BitbucketRepository.java | 36 +-- .../bitbucket/ApiClient.java | 204 ++++++++++++ .../bitbucket/BitbucketApiClient.java | 204 ------------ .../bitbucket/BitbucketPullRequestApproval.java | 20 -- .../bitbucket/BitbucketPullRequestComment.java | 118 ------- .../bitbucket/BitbucketPullRequestResponse.java | 58 ---- .../BitbucketPullRequestResponseValue.java | 130 -------- ...itbucketPullRequestResponseValueRepository.java | 46 --- ...etPullRequestResponseValueRepositoryBranch.java | 16 - ...etPullRequestResponseValueRepositoryCommit.java | 16 - ...llRequestResponseValueRepositoryRepository.java | 43 --- .../bitbucket/Pullrequest.java | 345 +++++++++++++++++++++ 13 files changed, 568 insertions(+), 672 deletions(-) create mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestApproval.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestComment.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponse.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValue.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryBranch.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryCommit.java delete mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryRepository.java create mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java index 1838607..3671ea6 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java @@ -1,6 +1,6 @@ package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketPullRequestResponseValue; +import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; import hudson.model.AbstractProject; import java.util.Collection; @@ -27,7 +27,7 @@ public class BitbucketPullRequestsBuilder { public void run() { logger.info("Build Start."); this.repository.init(); - Collection targetPullRequests = this.repository.getTargetPullRequests(); + Collection targetPullRequests = this.repository.getTargetPullRequests(); this.repository.addFutureBuildTasks(targetPullRequests); } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index f988d23..bdf5d7c 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -8,10 +8,8 @@ import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketApiClient; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketPullRequestComment; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketPullRequestResponseValue; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketPullRequestResponseValueRepository; +import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClient; +import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; /** * Created by nishio @@ -32,7 +30,7 @@ public class BitbucketRepository { private String projectPath; private BitbucketPullRequestsBuilder builder; private BitbucketBuildTrigger trigger; - private BitbucketApiClient client; + private ApiClient client; public BitbucketRepository(String projectPath, BitbucketPullRequestsBuilder builder) { this.projectPath = projectPath; @@ -41,18 +39,18 @@ public class BitbucketRepository { public void init() { trigger = this.builder.getTrigger(); - client = new BitbucketApiClient( + client = new ApiClient( trigger.getUsername(), trigger.getPassword(), trigger.getRepositoryOwner(), trigger.getRepositoryName()); } - public Collection getTargetPullRequests() { + public Collection getTargetPullRequests() { logger.info("Fetch PullRequests."); - List pullRequests = client.getPullRequests(); - List targetPullRequests = new ArrayList(); - for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { + List pullRequests = client.getPullRequests(); + List targetPullRequests = new ArrayList(); + for(Pullrequest pullRequest : pullRequests) { if (isBuildTarget(pullRequest)) { targetPullRequests.add(pullRequest); } @@ -60,16 +58,16 @@ public class BitbucketRepository { return targetPullRequests; } - public String postBuildStartCommentTo(BitbucketPullRequestResponseValue pullRequest) { + public String postBuildStartCommentTo(Pullrequest pullRequest) { String sourceCommit = pullRequest.getSource().getCommit().getHash(); String destinationCommit = pullRequest.getDestination().getCommit().getHash(); String comment = String.format(BUILD_START_MARKER, builder.getProject().getDisplayName(), sourceCommit, destinationCommit); - BitbucketPullRequestComment commentResponse = this.client.postPullRequestComment(pullRequest.getId(), comment); - return commentResponse.getCommentId().toString(); + Pullrequest.Comment commentResponse = this.client.postPullRequestComment(pullRequest.getId(), comment); + return commentResponse.getId().toString(); } - public void addFutureBuildTasks(Collection pullRequests) { - for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { + public void addFutureBuildTasks(Collection pullRequests) { + for(Pullrequest pullRequest : pullRequests) { String commentId = postBuildStartCommentTo(pullRequest); if ( this.trigger.getApproveIfSuccess() ) { deletePullRequestApproval(pullRequest.getId()); @@ -112,7 +110,7 @@ public class BitbucketRepository { this.client.postPullRequestApproval(pullRequestId); } - private boolean isBuildTarget(BitbucketPullRequestResponseValue pullRequest) { + private boolean isBuildTarget(Pullrequest pullRequest) { boolean shouldBuild = true; if (pullRequest.getState() != null && pullRequest.getState().equals("OPEN")) { @@ -122,18 +120,18 @@ public class BitbucketRepository { String sourceCommit = pullRequest.getSource().getCommit().getHash(); - BitbucketPullRequestResponseValueRepository destination = pullRequest.getDestination(); + Pullrequest.Revision destination = pullRequest.getDestination(); String owner = destination.getRepository().getOwnerName(); String repositoryName = destination.getRepository().getRepositoryName(); String destinationCommit = destination.getCommit().getHash(); String id = pullRequest.getId(); - List comments = client.getPullRequestComments(owner, repositoryName, id); + List comments = client.getPullRequestComments(owner, repositoryName, id); if (comments != null) { Collections.sort(comments); Collections.reverse(comments); - for (BitbucketPullRequestComment comment : comments) { + for (Pullrequest.Comment comment : comments) { String content = comment.getContent(); if (content == null || content.isEmpty()) { continue; diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java new file mode 100644 index 0000000..ceec1a8 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -0,0 +1,204 @@ +package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; + +import org.apache.commons.httpclient.*; +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.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Collections; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import jenkins.model.Jenkins; +import hudson.ProxyConfiguration; + +/** + * Created by nishio + */ +public class ApiClient { + private static final Logger logger = Logger.getLogger(ApiClient.class.getName()); + private static final String BITBUCKET_HOST = "bitbucket.org"; + private static final String V1_API_BASE_URL = "https://bitbucket.org/api/1.0/repositories/"; + private static final String V2_API_BASE_URL = "https://bitbucket.org/api/2.0/repositories/"; + private String owner; + private String repositoryName; + private Credentials credentials; + + public ApiClient(String username, String password, String owner, String repositoryName) { + this.credentials = new UsernamePasswordCredentials(username, password); + this.owner = owner; + this.repositoryName = repositoryName; + } + + public List getPullRequests() { + String response = getRequest(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); + try { + return parsePullRequestJson(response).getPullrequests(); + } catch(Exception e) { + logger.log(Level.WARNING, "invalid pull request response.", e); + } + return Collections.EMPTY_LIST; + } + + public List getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { + String response = getRequest( + V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); + try { + return parseCommentJson(response); + } catch(Exception e) { + logger.log(Level.WARNING, "invalid pull request response.", e); + } + return Collections.EMPTY_LIST; + } + + public void deletePullRequestComment(String pullRequestId, String commentId) { + String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments/" + commentId; + //https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/pullrequests/{pull_request_id}/comments/{comment_id} + deleteRequest(path); + } + + + public Pullrequest.Comment postPullRequestComment(String pullRequestId, String comment) { + String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments"; + try { + NameValuePair content = new NameValuePair("content", comment); + String response = postRequest(path, new NameValuePair[]{ content }); + return parseSingleCommentJson(response); + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public void deletePullRequestApproval(String pullRequestId) { + String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; + deleteRequest(path); + } + + public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { + String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; + try { + String response = postRequest(path, new NameValuePair[]{}); + return parseApprovalJson(response); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private HttpClient getHttpClient() { + HttpClient client = new HttpClient(); + if (Jenkins.getInstance() != null) { + ProxyConfiguration proxy = Jenkins.getInstance().proxy; + if (proxy != null) { + logger.info("Jenkins proxy: " + proxy.name + ":" + proxy.port); + client.getHostConfiguration().setProxy(proxy.name, proxy.port); + String username = proxy.getUserName(); + String password = proxy.getPassword(); + // Consider it to be passed if username specified. Sufficient? + if (username != null && !"".equals(username.trim())) { + logger.info("Using proxy authentication (user=" + username + ")"); + client.getState().setProxyCredentials(AuthScope.ANY, + new UsernamePasswordCredentials(username, password)); + } + } + } + return client; + } + + private String getRequest(String path) { + HttpClient client = getHttpClient(); + client.getState().setCredentials(AuthScope.ANY, credentials); + GetMethod httpget = new GetMethod(path); + client.getParams().setAuthenticationPreemptive(true); + String response = null; + try { + client.executeMethod(httpget); + response = httpget.getResponseBodyAsString(); + } catch (HttpException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return response; + } + + public void deleteRequest(String path) { + HttpClient client = getHttpClient(); + client.getState().setCredentials(AuthScope.ANY, credentials); + DeleteMethod httppost = new DeleteMethod(path); + client.getParams().setAuthenticationPreemptive(true); + String response = ""; + try { + client.executeMethod(httppost); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private String postRequest(String path, NameValuePair[] params) throws UnsupportedEncodingException { + HttpClient client = getHttpClient(); + client.getState().setCredentials(AuthScope.ANY, credentials); + PostMethod httppost = new PostMethod(path); + httppost.setRequestBody(params); + client.getParams().setAuthenticationPreemptive(true); + String response = ""; + try { + client.executeMethod(httppost); + response = httppost.getResponseBodyAsString(); + logger.info("API Request Response: " + response); + } catch (HttpException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return response; + + } + + private Pullrequest.Response parsePullRequestJson(String response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + Pullrequest.Response parsedResponse; + parsedResponse = mapper.readValue(response, Pullrequest.Response.class); + return parsedResponse; + } + + private List parseCommentJson(String response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + List parsedResponse; + parsedResponse = mapper.readValue( + response, + new TypeReference>() { + }); + return parsedResponse; + } + + private Pullrequest.Comment parseSingleCommentJson(String response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + Pullrequest.Comment parsedResponse; + parsedResponse = mapper.readValue( + response, + Pullrequest.Comment.class); + return parsedResponse; + } + + private Pullrequest.Participant parseApprovalJson(String response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + Pullrequest.Participant parsedResponse; + parsedResponse = mapper.readValue( + response, + Pullrequest.Participant.class); + return parsedResponse; + } +} + diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java deleted file mode 100644 index 71fc861..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java +++ /dev/null @@ -1,204 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.apache.commons.httpclient.*; -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.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.type.TypeReference; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.util.Collections; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import jenkins.model.Jenkins; -import hudson.ProxyConfiguration; - -/** - * Created by nishio - */ -public class BitbucketApiClient { - private static final Logger logger = Logger.getLogger(BitbucketApiClient.class.getName()); - private static final String BITBUCKET_HOST = "bitbucket.org"; - private static final String V1_API_BASE_URL = "https://bitbucket.org/api/1.0/repositories/"; - private static final String V2_API_BASE_URL = "https://bitbucket.org/api/2.0/repositories/"; - private String owner; - private String repositoryName; - private Credentials credentials; - - public BitbucketApiClient(String username, String password, String owner, String repositoryName) { - this.credentials = new UsernamePasswordCredentials(username, password); - this.owner = owner; - this.repositoryName = repositoryName; - } - - public List getPullRequests() { - String response = getRequest(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); - try { - return parsePullRequestJson(response).getPrValues(); - } catch(Exception e) { - logger.log(Level.WARNING, "invalid pull request response.", e); - } - return Collections.EMPTY_LIST; - } - - public List getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { - String response = getRequest( - V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); - try { - return parseCommentJson(response); - } catch(Exception e) { - logger.log(Level.WARNING, "invalid pull request response.", e); - } - return Collections.EMPTY_LIST; - } - - public void deletePullRequestComment(String pullRequestId, String commentId) { - String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments/" + commentId; - //https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/pullrequests/{pull_request_id}/comments/{comment_id} - deleteRequest(path); - } - - - public BitbucketPullRequestComment postPullRequestComment(String pullRequestId, String comment) { - String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments"; - try { - NameValuePair content = new NameValuePair("content", comment); - String response = postRequest(path, new NameValuePair[]{ content }); - return parseSingleCommentJson(response); - - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - public void deletePullRequestApproval(String pullRequestId) { - String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; - deleteRequest(path); - } - - public BitbucketPullRequestApproval postPullRequestApproval(String pullRequestId) { - String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; - try { - String response = postRequest(path, new NameValuePair[]{}); - return parseApprovalJson(response); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - private HttpClient getHttpClient() { - HttpClient client = new HttpClient(); - if (Jenkins.getInstance() != null) { - ProxyConfiguration proxy = Jenkins.getInstance().proxy; - if (proxy != null) { - logger.info("Jenkins proxy: " + proxy.name + ":" + proxy.port); - client.getHostConfiguration().setProxy(proxy.name, proxy.port); - String username = proxy.getUserName(); - String password = proxy.getPassword(); - // Consider it to be passed if username specified. Sufficient? - if (username != null && !"".equals(username.trim())) { - logger.info("Using proxy authentication (user=" + username + ")"); - client.getState().setProxyCredentials(AuthScope.ANY, - new UsernamePasswordCredentials(username, password)); - } - } - } - return client; - } - - private String getRequest(String path) { - HttpClient client = getHttpClient(); - client.getState().setCredentials(AuthScope.ANY, credentials); - GetMethod httpget = new GetMethod(path); - client.getParams().setAuthenticationPreemptive(true); - String response = null; - try { - client.executeMethod(httpget); - response = httpget.getResponseBodyAsString(); - } catch (HttpException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return response; - } - - public void deleteRequest(String path) { - HttpClient client = getHttpClient(); - client.getState().setCredentials(AuthScope.ANY, credentials); - DeleteMethod httppost = new DeleteMethod(path); - client.getParams().setAuthenticationPreemptive(true); - String response = ""; - try { - client.executeMethod(httppost); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private String postRequest(String path, NameValuePair[] params) throws UnsupportedEncodingException { - HttpClient client = getHttpClient(); - client.getState().setCredentials(AuthScope.ANY, credentials); - PostMethod httppost = new PostMethod(path); - httppost.setRequestBody(params); - client.getParams().setAuthenticationPreemptive(true); - String response = ""; - try { - client.executeMethod(httppost); - response = httppost.getResponseBodyAsString(); - logger.info("API Request Response: " + response); - } catch (HttpException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return response; - - } - - private BitbucketPullRequestResponse parsePullRequestJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - BitbucketPullRequestResponse parsedResponse; - parsedResponse = mapper.readValue(response, BitbucketPullRequestResponse.class); - return parsedResponse; - } - - private List parseCommentJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - List parsedResponse; - parsedResponse = mapper.readValue( - response, - new TypeReference>() { - }); - return parsedResponse; - } - - private BitbucketPullRequestComment parseSingleCommentJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - BitbucketPullRequestComment parsedResponse; - parsedResponse = mapper.readValue( - response, - BitbucketPullRequestComment.class); - return parsedResponse; - } - - private BitbucketPullRequestApproval parseApprovalJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - BitbucketPullRequestApproval parsedResponse; - parsedResponse = mapper.readValue( - response, - BitbucketPullRequestApproval.class); - return parsedResponse; - } -} - diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestApproval.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestApproval.java deleted file mode 100644 index a7fe813..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestApproval.java +++ /dev/null @@ -1,20 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.annotate.JsonProperty; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestApproval { - private String role; - private Boolean approved; - - @JsonProperty("role") - public String getRole() { - return role; - } - - @JsonProperty("approved") - public Boolean getApproved() { - return approved; - } -} diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestComment.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestComment.java deleted file mode 100644 index 2e1e18e..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestComment.java +++ /dev/null @@ -1,118 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.annotate.JsonProperty; - -import java.util.Comparator; - -/** - * Created by nishio - */ -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestComment implements Comparable { - private Boolean isEntityAuthor; - private Integer pullRequestId; - private String contentRendered; - private Boolean deleted; - private String UtcLastUpdated; - private Integer commentId; - private String content; - private String UtcCreatedOn; - private Boolean isSpam; - - @JsonProperty("is_entity_author") - public Boolean getIsEntityAuthor() { - return isEntityAuthor; - } - - @JsonProperty("is_entity_author") - public void setIsEntityAuthor(Boolean isEntityAuthor) { - this.isEntityAuthor = isEntityAuthor; - } - - @JsonProperty("pull_request_id") - public Integer getPullRequestId() { - return pullRequestId; - } - - @JsonProperty("pull_request_id") - public void setPullRequestId(Integer pullRequestId) { - this.pullRequestId = pullRequestId; - } - - @JsonProperty("content_rendered") - public String getContentRendered() { - return contentRendered; - } - - @JsonProperty("content_rendered") - public void setContentRendered(String contentRendered) { - this.contentRendered = contentRendered; - } - - public Boolean getDeleted() { - return deleted; - } - - public void setDeleted(Boolean deleted) { - this.deleted = deleted; - } - - @JsonProperty("utc_last_updated") - public String getUtcLastUpdated() { - return UtcLastUpdated; - } - - @JsonProperty("utc_last_updated") - public void setUtcLastUpdated(String utcLastUpdated) { - UtcLastUpdated = utcLastUpdated; - } - - @JsonProperty("comment_id") - public Integer getCommentId() { - return commentId; - } - - @JsonProperty("comment_id") - public void setCommentId(Integer commentId) { - this.commentId = commentId; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - @JsonProperty("utc_created_on") - public String getUtcCreatedOn() { - return UtcCreatedOn; - } - - @JsonProperty("utc_created_on") - public void setUtcCreatedOn(String utcCreatedOn) { - UtcCreatedOn = utcCreatedOn; - } - - @JsonProperty("is_spam") - public Boolean getIsSpam() { - return isSpam; - } - - @JsonProperty("is_spam") - public void setIsSpam(Boolean isSpam) { - this.isSpam = isSpam; - } - - public int compareTo(BitbucketPullRequestComment target) { - if (this.getCommentId() > target.getCommentId()) { - return 1; - } else if (this.getCommentId().equals(target.getCommentId())) { - return 0; - } else { - return -1; - } - } -} diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponse.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponse.java deleted file mode 100644 index 82e6009..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponse.java +++ /dev/null @@ -1,58 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.annotate.JsonProperty; - -import java.util.List; - -/** - * Created by nishio - */ -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestResponse { - private int pageLength; - private List prValues; - private int page; - private int size; - - @JsonProperty("pagelen") - public int getPageLength() { - return pageLength; - } - - @JsonProperty("pagelen") - public void setPageLength(int pageLength) { - this.pageLength = pageLength; - } - - @JsonProperty("values") - public List getPrValues() { - return prValues; - } - - @JsonProperty("values") - public void setPrValues(List prValues) { - this.prValues = prValues; - } - - @JsonProperty("page") - public int getPage() { - return page; - } - - @JsonProperty("page") - public void setPage(int page) { - this.page = page; - } - - @JsonProperty("size") - public int getSize() { - return size; - } - - @JsonProperty("size") - public void setSize(int size) { - this.size = size; - } - -} diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValue.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValue.java deleted file mode 100644 index e8be708..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValue.java +++ /dev/null @@ -1,130 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.annotate.JsonProperty; - -/** - * Created by nishio - */ -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestResponseValue { - private String description; - private Boolean closeSourceBranch; - private String title; - private BitbucketPullRequestResponseValueRepository destination; - private String reason; - private String closedBy; - private BitbucketPullRequestResponseValueRepository source; - private String state; - private String createdOn; - private String updatedOn; - private String mergeCommit; - private String id; - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - @JsonProperty("close_source_branch") - public Boolean getCloseSourceBranch() { - return closeSourceBranch; - } - - @JsonProperty("close_source_branch") - public void setCloseSourceBranch(Boolean closeSourceBranch) { - this.closeSourceBranch = closeSourceBranch; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public BitbucketPullRequestResponseValueRepository getDestination() { - return destination; - } - - public void setDestination(BitbucketPullRequestResponseValueRepository destination) { - this.destination = destination; - } - - public String getReason() { - return reason; - } - - public void setReason(String reason) { - this.reason = reason; - } - - @JsonProperty("closed_by") - public String getClosedBy() { - return closedBy; - } - - @JsonProperty("closed_by") - public void setClosedBy(String closedBy) { - this.closedBy = closedBy; - } - - public BitbucketPullRequestResponseValueRepository getSource() { - return source; - } - - public void setSource(BitbucketPullRequestResponseValueRepository source) { - this.source = source; - } - - public String getState() { - return state; - } - - public void setState(String state) { - this.state = state; - } - - @JsonProperty("created_on") - public String getCreatedOn() { - return createdOn; - } - - @JsonProperty("created_on") - public void setCreatedOn(String createdOn) { - this.createdOn = createdOn; - } - - @JsonProperty("updated_on") - public String getUpdatedOn() { - return updatedOn; - } - - @JsonProperty("updated_on") - public void setUpdatedOn(String updatedOn) { - this.updatedOn = updatedOn; - } - - @JsonProperty("merge_commit") - public String getMergeCommit() { - return mergeCommit; - } - - @JsonProperty("merge_commit") - public void setMergeCommit(String mergeCommit) { - this.mergeCommit = mergeCommit; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - -} diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java deleted file mode 100644 index f97c010..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java +++ /dev/null @@ -1,46 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.annotate.JsonProperty; - -/** - * Created by nishio - */ -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestResponseValueRepository { - private BitbucketPullRequestResponseValueRepositoryRepository repository; - private BitbucketPullRequestResponseValueRepositoryBranch branch; - private BitbucketPullRequestResponseValueRepositoryCommit commit; - - @JsonProperty("repository") - public BitbucketPullRequestResponseValueRepositoryRepository getRepository() { - return repository; - } - - @JsonProperty("repository") - public void setRepository(BitbucketPullRequestResponseValueRepositoryRepository repository) { - this.repository = repository; - } - - @JsonProperty("branch") - public BitbucketPullRequestResponseValueRepositoryBranch getBranch() { - return branch; - } - - @JsonProperty("branch") - public void setBranch(BitbucketPullRequestResponseValueRepositoryBranch branch) { - this.branch = branch; - } - - @JsonProperty("commit") - public BitbucketPullRequestResponseValueRepositoryCommit getCommit() { - return commit; - } - - @JsonProperty("commit") - public void setCommit(BitbucketPullRequestResponseValueRepositoryCommit commit) { - this.commit = commit; - } -} - - diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryBranch.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryBranch.java deleted file mode 100644 index 2fa2eee..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryBranch.java +++ /dev/null @@ -1,16 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestResponseValueRepositoryBranch { - private String Name; - - public String getName() { - return Name; - } - - public void setName(String name) { - Name = name; - } -} \ No newline at end of file diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryCommit.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryCommit.java deleted file mode 100644 index 5846d71..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryCommit.java +++ /dev/null @@ -1,16 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestResponseValueRepositoryCommit { - private String Hash; - - public String getHash() { - return Hash; - } - - public void setHash(String hash) { - Hash = hash; - } -} \ No newline at end of file diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryRepository.java deleted file mode 100644 index d8ff67e..0000000 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryRepository.java +++ /dev/null @@ -1,43 +0,0 @@ -package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; - -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.annotate.JsonProperty; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class BitbucketPullRequestResponseValueRepositoryRepository { - private String fullName; - private String name; - - @JsonProperty("full_name") - public String getFullName() { - return fullName; - } - - @JsonProperty("full_name") - public void setFullName(String fullName) { - this.fullName = fullName; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getOwnerName() { - if (this.fullName != null) { - return this.fullName.split("/")[0]; - } - return null; - } - - public String getRepositoryName() { - if (this.fullName != null) { - return this.fullName.split("/")[1]; - } - return null; - } -} - diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java new file mode 100644 index 0000000..1e8bf5c --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/Pullrequest.java @@ -0,0 +1,345 @@ +package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; + +import java.util.List; +import java.util.Comparator; + +import org.codehaus.jackson.annotate.JsonIgnoreProperties; +import org.codehaus.jackson.annotate.JsonProperty; + +/** + * POJOs representing the pull-requests extracted from the + * JSON response of the Bitbucket API V2. + * + * @see https://confluence.atlassian.com/bitbucket/pullrequests-resource-423626332.html#pullrequestsResource-GETaspecificpullrequest + */ + +@JsonIgnoreProperties(ignoreUnknown = true) +public class Pullrequest { + + private String description; + private Boolean closeSourceBranch; + private String title; + private Revision destination; + private String reason; + private String closedBy; + private Revision source; + private String state; + private String createdOn; + private String updatedOn; + private String mergeCommit; + private String id; + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Response { + private int pageLength; + private List pullrequests; + private int page; + private int size; + + @JsonProperty("pagelen") + public int getPageLength() { + return pageLength; + } + @JsonProperty("pagelen") + public void setPageLength(int pageLength) { + this.pageLength = pageLength; + } + @JsonProperty("values") + public List getPullrequests() { + return pullrequests; + } + @JsonProperty("values") + public void setPullrequests(List pullrequests) { + this.pullrequests = pullrequests; + } + public int getPage() { + return page; + } + public void setPage(int page) { + this.page = page; + } + public int getSize() { + return size; + } + public void setSize(int size) { + this.size = size; + } + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Revision { + private Repository repository; + private Branch branch; + private Commit commit; + + public Repository getRepository() { + return repository; + } + public void setRepository(Repository repository) { + this.repository = repository; + } + public Branch getBranch() { + return branch; + } + public void setBranch(Branch branch) { + this.branch = branch; + } + public Commit getCommit() { + return commit; + } + public void setCommit(Commit commit) { + this.commit = commit; + } + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Repository { + private String fullName; + private String name; + private String ownerName; + private String repositoryName; + + @JsonProperty("full_name") + public String getFullName() { + return fullName; + } + @JsonProperty("full_name") + public void setFullName(String fullName) { + // Also extract owner- and reponame + if (name != null) { + this.ownerName = fullName.split("/")[0]; + this.repositoryName = fullName.split("/")[1]; + } + this.fullName = fullName; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getOwnerName() { + return ownerName; + } + public String getRepositoryName() { + return repositoryName; + } + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Branch { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Commit { + private String hash; + + public String getHash() { + return hash; + } + + public void setHash(String hash) { + this.hash = hash; + } + } + + // Was: Approval + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Participant { + private String role; + private Boolean approved; + + public String getRole() { + return role; + } + public void setRole(String role) { + this.role = role; + } + public Boolean getApproved() { + return approved; + } + public void setApproved(Boolean approved) { + this.approved = approved; + } + } + + // https://confluence.atlassian.com/bitbucket/pullrequests-resource-1-0-296095210.html#pullrequestsResource1.0-POSTanewcomment + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Comment implements Comparable { + private Integer id; + private String filename; + private String content; + private String updatedOn; + private String createdOn; + + public int compareTo(Comment target) { + if (this.getId() > target.getId()) { + return 1; + } else if (this.getId().equals(target.getId())) { + return 0; + } else { + return -1; + } + } + + @JsonProperty("comment_id") + public Integer getId() { + return id; + } + + @JsonProperty("comment_id") + public void setId(Integer id) { + this.id = id; + } + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + @JsonProperty("utc_last_updated") + public String getUpdatedOn() { + return updatedOn; + } + @JsonProperty("utc_last_updated") + public void setUpdatedOn(String updatedOn) { + this.updatedOn = updatedOn; + } + @JsonProperty("utc_created_on") + public String getCreatedOn() { + return createdOn; + } + @JsonProperty("utc_created_on") + public void setCreatedOn(String createdOn) { + this.createdOn = createdOn; + } + } + + //-------------------- only getters and setters follow ----------------- + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @JsonProperty("close_source_branch") + public Boolean getCloseSourceBranch() { + return closeSourceBranch; + } + + @JsonProperty("close_source_branch") + public void setCloseSourceBranch(Boolean closeSourceBranch) { + this.closeSourceBranch = closeSourceBranch; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Revision getDestination() { + return destination; + } + + public void setDestination(Revision destination) { + this.destination = destination; + } + + public String getReason() { + return reason; + } + + public void setReason(String reason) { + this.reason = reason; + } + + @JsonProperty("closed_by") + public String getClosedBy() { + return closedBy; + } + + @JsonProperty("closed_by") + public void setClosedBy(String closedBy) { + this.closedBy = closedBy; + } + + public Revision getSource() { + return source; + } + + public void setSource(Revision source) { + this.source = source; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + @JsonProperty("created_on") + public String getCreatedOn() { + return createdOn; + } + + @JsonProperty("created_on") + public void setCreatedOn(String createdOn) { + this.createdOn = createdOn; + } + + @JsonProperty("updated_on") + public String getUpdatedOn() { + return updatedOn; + } + + @JsonProperty("updated_on") + public void setUpdatedOn(String updatedOn) { + this.updatedOn = updatedOn; + } + + @JsonProperty("merge_commit") + public String getMergeCommit() { + return mergeCommit; + } + + @JsonProperty("merge_commit") + public void setMergeCommit(String mergeCommit) { + this.mergeCommit = mergeCommit; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + +} -- cgit v1.2.3 From db67f5bd46fe70073548e9acfc590ecd35b72272 Mon Sep 17 00:00:00 2001 From: Jonathan Brachthäuser Date: Thu, 3 Sep 2015 23:16:16 +0200 Subject: Refactor: Have only one generic parse method --- .gitignore | 20 ---------- .../bitbucketpullrequestbuilder/.gitignore | 20 ++++++++++ .../bitbucket/ApiClient.java | 45 ++++------------------ 3 files changed, 28 insertions(+), 57 deletions(-) delete mode 100644 .gitignore create mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 21e5893..0000000 --- a/.gitignore +++ /dev/null @@ -1,20 +0,0 @@ -target/ -work/ - -# -# Eclipse metadata. -# -.project -.classpath -.settings/ - -# -# Eclipse and Maven build results -# -bin/ - -# IntelliJ metadata. -*.iml -*.ipr -*.iws -.idea/ diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore new file mode 100644 index 0000000..21e5893 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore @@ -0,0 +1,20 @@ +target/ +work/ + +# +# Eclipse metadata. +# +.project +.classpath +.settings/ + +# +# Eclipse and Maven build results +# +bin/ + +# IntelliJ metadata. +*.iml +*.ipr +*.iws +.idea/ diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index ceec1a8..ad9b16b 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -39,7 +39,7 @@ public class ApiClient { public List getPullRequests() { String response = getRequest(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); try { - return parsePullRequestJson(response).getPullrequests(); + return parse(response, Pullrequest.Response.class).getPullrequests(); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -50,7 +50,7 @@ public class ApiClient { String response = getRequest( V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); try { - return parseCommentJson(response); + return parse(response, new TypeReference>() {}); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -69,8 +69,7 @@ public class ApiClient { try { NameValuePair content = new NameValuePair("content", comment); String response = postRequest(path, new NameValuePair[]{ content }); - return parseSingleCommentJson(response); - + return parse(response, Pullrequest.Comment.class); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } @@ -89,7 +88,7 @@ public class ApiClient { String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; try { String response = postRequest(path, new NameValuePair[]{}); - return parseApprovalJson(response); + return parse(response, Pullrequest.Participant.class); } catch (IOException e) { e.printStackTrace(); } @@ -166,39 +165,11 @@ public class ApiClient { } - private Pullrequest.Response parsePullRequestJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Pullrequest.Response parsedResponse; - parsedResponse = mapper.readValue(response, Pullrequest.Response.class); - return parsedResponse; - } - - private List parseCommentJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - List parsedResponse; - parsedResponse = mapper.readValue( - response, - new TypeReference>() { - }); - return parsedResponse; + private R parse(String response, Class cls) throws IOException { + return new ObjectMapper().readValue(response, cls); } - - private Pullrequest.Comment parseSingleCommentJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Pullrequest.Comment parsedResponse; - parsedResponse = mapper.readValue( - response, - Pullrequest.Comment.class); - return parsedResponse; - } - - private Pullrequest.Participant parseApprovalJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Pullrequest.Participant parsedResponse; - parsedResponse = mapper.readValue( - response, - Pullrequest.Participant.class); - return parsedResponse; + private R parse(String response, TypeReference ref) throws IOException { + return new ObjectMapper().readValue(response, ref); } } -- cgit v1.2.3 From 31ed45e7bf69505c0dedb6ddf19b69bb6c37ffaa Mon Sep 17 00:00:00 2001 From: Jonathan Brachthäuser Date: Thu, 3 Sep 2015 23:24:47 +0200 Subject: Refactor http requests --- .../bitbucket/ApiClient.java | 61 +++++++--------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index ad9b16b..7b15069 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -37,7 +37,7 @@ public class ApiClient { } public List getPullRequests() { - String response = getRequest(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); + String response = get(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); try { return parse(response, Pullrequest.Response.class).getPullrequests(); } catch(Exception e) { @@ -47,7 +47,7 @@ public class ApiClient { } public List getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { - String response = getRequest( + String response = get( V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); try { return parse(response, new TypeReference>() {}); @@ -60,7 +60,7 @@ public class ApiClient { public void deletePullRequestComment(String pullRequestId, String commentId) { String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments/" + commentId; //https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/pullrequests/{pull_request_id}/comments/{comment_id} - deleteRequest(path); + delete(path); } @@ -68,7 +68,7 @@ public class ApiClient { String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments"; try { NameValuePair content = new NameValuePair("content", comment); - String response = postRequest(path, new NameValuePair[]{ content }); + String response = post(path, new NameValuePair[]{ content }); return parse(response, Pullrequest.Comment.class); } catch (UnsupportedEncodingException e) { e.printStackTrace(); @@ -81,13 +81,13 @@ public class ApiClient { public void deletePullRequestApproval(String pullRequestId) { String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; - deleteRequest(path); + delete(path); } public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; try { - String response = postRequest(path, new NameValuePair[]{}); + String response = post(path, new NameValuePair[]{}); return parse(response, Pullrequest.Participant.class); } catch (IOException e) { e.printStackTrace(); @@ -115,54 +115,33 @@ public class ApiClient { return client; } - private String getRequest(String path) { - HttpClient client = getHttpClient(); - client.getState().setCredentials(AuthScope.ANY, credentials); - GetMethod httpget = new GetMethod(path); - client.getParams().setAuthenticationPreemptive(true); - String response = null; - try { - client.executeMethod(httpget); - response = httpget.getResponseBodyAsString(); - } catch (HttpException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return response; + private String get(String path) { + return send(new GetMethod(path)); } - public void deleteRequest(String path) { - HttpClient client = getHttpClient(); - client.getState().setCredentials(AuthScope.ANY, credentials); - DeleteMethod httppost = new DeleteMethod(path); - client.getParams().setAuthenticationPreemptive(true); - String response = ""; - try { - client.executeMethod(httppost); - } catch (IOException e) { - e.printStackTrace(); - } + private String post(String path, NameValuePair[] data) { + PostMethod req = new PostMethod(path); + req.setRequestBody(data); + return send(req); + } + + private void delete(String path) { + send(new DeleteMethod(path)); } - private String postRequest(String path, NameValuePair[] params) throws UnsupportedEncodingException { + private String send(HttpMethodBase req) { HttpClient client = getHttpClient(); client.getState().setCredentials(AuthScope.ANY, credentials); - PostMethod httppost = new PostMethod(path); - httppost.setRequestBody(params); client.getParams().setAuthenticationPreemptive(true); - String response = ""; try { - client.executeMethod(httppost); - response = httppost.getResponseBodyAsString(); - logger.info("API Request Response: " + response); + client.executeMethod(req); + return req.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } - return response; - + return null; } private R parse(String response, Class cls) throws IOException { -- cgit v1.2.3 From e2b7778e347b787707dfe4dcb46fa7e515dce643 Mon Sep 17 00:00:00 2001 From: Jonathan Brachthäuser Date: Thu, 3 Sep 2015 23:41:17 +0200 Subject: Factor out url construction to v1 and v2 --- .../bitbucket/ApiClient.java | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index 7b15069..173ae1c 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -37,9 +37,8 @@ public class ApiClient { } public List getPullRequests() { - String response = get(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); try { - return parse(response, Pullrequest.Response.class).getPullrequests(); + return parse(get(v2("/pullrequests/")), Pullrequest.Response.class).getPullrequests(); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -47,10 +46,8 @@ public class ApiClient { } public List getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { - String response = get( - V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); try { - return parse(response, new TypeReference>() {}); + return parse(get(v1("/pullrequests/" + pullRequestId + "/comments")), new TypeReference>() {}); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -65,10 +62,10 @@ public class ApiClient { public Pullrequest.Comment postPullRequestComment(String pullRequestId, String comment) { - String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments"; try { - NameValuePair content = new NameValuePair("content", comment); - String response = post(path, new NameValuePair[]{ content }); + String response = post( + v1("/pullrequests/" + pullRequestId + "/comments"), + new NameValuePair[]{ new NameValuePair("content", comment) }); return parse(response, Pullrequest.Comment.class); } catch (UnsupportedEncodingException e) { e.printStackTrace(); @@ -80,15 +77,13 @@ public class ApiClient { } public void deletePullRequestApproval(String pullRequestId) { - String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; - delete(path); + delete(v2("/pullrequests/" + pullRequestId + "/approve")); } public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { - String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; try { - String response = post(path, new NameValuePair[]{}); - return parse(response, Pullrequest.Participant.class); + return parse(post(v2("/pullrequests/" + pullRequestId + "/approve"), + new NameValuePair[]{}), Pullrequest.Participant.class); } catch (IOException e) { e.printStackTrace(); } @@ -115,6 +110,14 @@ public class ApiClient { return client; } + private String v1(String url) { + return V1_API_BASE_URL + this.owner + "/" + this.repositoryName + url; + } + + private String v2(String url) { + return V2_API_BASE_URL + this.owner + "/" + this.repositoryName + url; + } + private String get(String path) { return send(new GetMethod(path)); } -- cgit v1.2.3 From 733e87f9ce3bd72eab48de2e55b4bb59644c9237 Mon Sep 17 00:00:00 2001 From: Jonathan Brachthäuser Date: Thu, 3 Sep 2015 23:44:28 +0200 Subject: :lipstick: Only some cosmetiques --- .../bitbucket/ApiClient.java | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index 173ae1c..713a061 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -92,20 +92,21 @@ public class ApiClient { private HttpClient getHttpClient() { HttpClient client = new HttpClient(); - if (Jenkins.getInstance() != null) { - ProxyConfiguration proxy = Jenkins.getInstance().proxy; - if (proxy != null) { - logger.info("Jenkins proxy: " + proxy.name + ":" + proxy.port); - client.getHostConfiguration().setProxy(proxy.name, proxy.port); - String username = proxy.getUserName(); - String password = proxy.getPassword(); - // Consider it to be passed if username specified. Sufficient? - if (username != null && !"".equals(username.trim())) { - logger.info("Using proxy authentication (user=" + username + ")"); - client.getState().setProxyCredentials(AuthScope.ANY, - new UsernamePasswordCredentials(username, password)); - } - } + if (Jenkins.getInstance() == null) return client; + + ProxyConfiguration proxy = Jenkins.getInstance().proxy; + if (proxy == null) return client; + + logger.info("Jenkins proxy: " + proxy.name + ":" + proxy.port); + client.getHostConfiguration().setProxy(proxy.name, proxy.port); + String username = proxy.getUserName(); + String password = proxy.getPassword(); + + // Consider it to be passed if username specified. Sufficient? + if (username != null && !"".equals(username.trim())) { + logger.info("Using proxy authentication (user=" + username + ")"); + client.getState().setProxyCredentials(AuthScope.ANY, + new UsernamePasswordCredentials(username, password)); } return client; } @@ -154,4 +155,3 @@ public class ApiClient { return new ObjectMapper().readValue(response, ref); } } - -- cgit v1.2.3