diff options
author | atsushi <atsushi@ageet.com> | 2015-03-30 21:09:17 +0900 |
---|---|---|
committer | atsushi <atsushi@ageet.com> | 2015-03-30 21:35:53 +0900 |
commit | 71d825e6bc644fec1e3782cfae7778c5208354fd (patch) | |
tree | 2b01a4cf7cfc1e76768e765ed84f1a52aa177d9a /src/main/java/bitbucketpullrequestbuilder | |
parent | 7feceb28cb89a27f35f62f47368cf38b2026eb00 (diff) | |
download | bbprb-71d825e6bc644fec1e3782cfae7778c5208354fd.tar.gz |
Implement approve if build success
Diffstat (limited to 'src/main/java/bitbucketpullrequestbuilder')
5 files changed, 67 insertions, 1 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index e5b0fa7..3934f92 100644..100755 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -29,6 +29,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { private final String repositoryName; private final String ciSkipPhrases; private final boolean checkDestinationCommit; + private final boolean approveIfSuccess; transient private BitbucketPullRequestsBuilder bitbucketPullRequestsBuilder; @@ -44,7 +45,8 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { String repositoryOwner, String repositoryName, String ciSkipPhrases, - boolean checkDestinationCommit + boolean checkDestinationCommit, + boolean approveIfSuccess ) throws ANTLRException { super(cron); this.projectPath = projectPath; @@ -55,6 +57,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { this.repositoryName = repositoryName; this.ciSkipPhrases = ciSkipPhrases; this.checkDestinationCommit = checkDestinationCommit; + this.approveIfSuccess = approveIfSuccess; } public String getProjectPath() { @@ -89,6 +92,10 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { return checkDestinationCommit; } + public boolean getApproveIfSuccess() { + return approveIfSuccess; + } + @Override public void start(AbstractProject<?, ?> project, boolean newInstance) { try { diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java index 0fa2fd0..45e1873 100644..100755 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java @@ -58,5 +58,8 @@ public class BitbucketBuilds { } repository.deletePullRequestComment(cause.getPullRequestId(), cause.getBuildStartCommentId()); repository.postFinishedComment(cause.getPullRequestId(), cause.getSourceCommitHash(), cause.getDestinationCommitHash(), result == Result.SUCCESS, buildUrl); + if ( this.trigger.getApproveIfSuccess() && result == Result.SUCCESS ) { + this.repository.postPullRequestApproval(cause.getPullRequestId()); + } } } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index f027919..c084391 100644..100755 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -71,6 +71,9 @@ public class BitbucketRepository { public void addFutureBuildTasks(Collection<BitbucketPullRequestResponseValue> pullRequests) { for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { String commentId = postBuildStartCommentTo(pullRequest); + if ( this.trigger.getApproveIfSuccess() ) { + deletePullRequestApproval(pullRequest.getId()); + } BitbucketCause cause = new BitbucketCause( pullRequest.getSource().getBranch().getName(), pullRequest.getDestination().getBranch().getName(), @@ -101,6 +104,14 @@ public class BitbucketRepository { this.client.postPullRequestComment(pullRequestId, comment); } + public void deletePullRequestApproval(String pullRequestId) { + this.client.deletePullRequestApproval(pullRequestId); + } + + public void postPullRequestApproval(String pullRequestId) { + this.client.postPullRequestApproval(pullRequestId); + } + private boolean isBuildTarget(BitbucketPullRequestResponseValue pullRequest) { boolean shouldBuild = true; diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java index b6d3155..71fc861 100644..100755 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java @@ -80,6 +80,22 @@ public class BitbucketApiClient { 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) { @@ -175,5 +191,14 @@ public class BitbucketApiClient { 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 new file mode 100755 index 0000000..a7fe813 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestApproval.java @@ -0,0 +1,20 @@ +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; + } +} |