diff options
author | S.Nishio <nishio@densan-labs.net> | 2015-06-02 01:03:40 +0900 |
---|---|---|
committer | S.Nishio <nishio@densan-labs.net> | 2015-06-02 01:03:40 +0900 |
commit | e9bb30024131d1b0f9945ee7d4b71e7b48e698b8 (patch) | |
tree | 7edf40aa72935dd85d32fb2445ab57e057efc863 | |
parent | 7feceb28cb89a27f35f62f47368cf38b2026eb00 (diff) | |
parent | 2163f513366927f2724ea38e4c4dbc3bb1f938dd (diff) | |
download | bbprb-e9bb30024131d1b0f9945ee7d4b71e7b48e698b8.tar.gz |
Merge pull request #44 from atsushi-ageet/approve-if-build-success
Implement approve if build success
6 files changed, 70 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 --- 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 --- 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 --- 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 --- 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 100644 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; + } +} diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly index ae93dcf..0eee781 100644 --- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly @@ -20,4 +20,7 @@ <f:entry title="Rebuild if destination branch changes?" field="checkDestinationCommit"> <f:checkbox /> </f:entry> + <f:entry title="Approve if build success?" field="approveIfSuccess"> + <f:checkbox /> + </f:entry> </j:jelly>
\ No newline at end of file |