diff options
author | S.Nishio <nishio@densan-labs.net> | 2015-09-06 14:28:48 +0900 |
---|---|---|
committer | S.Nishio <nishio@densan-labs.net> | 2015-09-06 14:28:48 +0900 |
commit | d002e0b758c1907c2d4963a2b9e556177e05ab03 (patch) | |
tree | d5c5c8edf32b08f3e4b3ab90bbe4dacd56bb81e2 /src/main/java | |
parent | 636cb7acb982f351f36005cbb71292e1c92ff52a (diff) | |
parent | 733e87f9ce3bd72eab48de2e55b4bb59644c9237 (diff) | |
download | bbprb-d002e0b758c1907c2d4963a2b9e556177e05ab03.tar.gz |
Merge pull request #58 from b-studios/refactor
Refactorings
Diffstat (limited to 'src/main/java')
14 files changed, 541 insertions, 672 deletions
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/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<BitbucketPullRequestResponseValue> targetPullRequests = this.repository.getTargetPullRequests(); + Collection<Pullrequest> 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<BitbucketPullRequestResponseValue> getTargetPullRequests() { + public Collection<Pullrequest> getTargetPullRequests() { logger.info("Fetch PullRequests."); - List<BitbucketPullRequestResponseValue> pullRequests = client.getPullRequests(); - List<BitbucketPullRequestResponseValue> targetPullRequests = new ArrayList<BitbucketPullRequestResponseValue>(); - for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { + List<Pullrequest> pullRequests = client.getPullRequests(); + List<Pullrequest> targetPullRequests = new ArrayList<Pullrequest>(); + 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<BitbucketPullRequestResponseValue> pullRequests) { - for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { + public void addFutureBuildTasks(Collection<Pullrequest> 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<BitbucketPullRequestComment> comments = client.getPullRequestComments(owner, repositoryName, id); + List<Pullrequest.Comment> 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..713a061 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -0,0 +1,157 @@ +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<Pullrequest> getPullRequests() { + try { + return parse(get(v2("/pullrequests/")), Pullrequest.Response.class).getPullrequests(); + } catch(Exception e) { + logger.log(Level.WARNING, "invalid pull request response.", e); + } + return Collections.EMPTY_LIST; + } + + 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); + } + 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} + delete(path); + } + + + public Pullrequest.Comment postPullRequestComment(String pullRequestId, String comment) { + try { + String response = post( + v1("/pullrequests/" + pullRequestId + "/comments"), + new NameValuePair[]{ new NameValuePair("content", comment) }); + return parse(response, Pullrequest.Comment.class); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public void deletePullRequestApproval(String pullRequestId) { + delete(v2("/pullrequests/" + pullRequestId + "/approve")); + } + + public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { + try { + return parse(post(v2("/pullrequests/" + pullRequestId + "/approve"), + new NameValuePair[]{}), Pullrequest.Participant.class); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private HttpClient getHttpClient() { + HttpClient client = new HttpClient(); + 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; + } + + 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)); + } + + 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 send(HttpMethodBase req) { + HttpClient client = getHttpClient(); + client.getState().setCredentials(AuthScope.ANY, credentials); + client.getParams().setAuthenticationPreemptive(true); + try { + client.executeMethod(req); + return req.getResponseBodyAsString(); + } catch (HttpException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private <R> R parse(String response, Class<R> cls) throws IOException { + return new ObjectMapper().readValue(response, cls); + } + 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/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<BitbucketPullRequestResponseValue> 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<BitbucketPullRequestComment> 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<BitbucketPullRequestComment> parseCommentJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - List<BitbucketPullRequestComment> parsedResponse; - parsedResponse = mapper.readValue( - response, - new TypeReference<List<BitbucketPullRequestComment>>() { - }); - 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<BitbucketPullRequestComment> { - 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<BitbucketPullRequestResponseValue> 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<BitbucketPullRequestResponseValue> getPrValues() { - return prValues; - } - - @JsonProperty("values") - public void setPrValues(List<BitbucketPullRequestResponseValue> 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<Pullrequest> 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<Pullrequest> getPullrequests() { + return pullrequests; + } + @JsonProperty("values") + public void setPullrequests(List<Pullrequest> 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<Comment> { + 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; + } + +} |