diff options
Diffstat (limited to 'src')
6 files changed, 61 insertions, 18 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java index 8052cc6..9600e26 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java @@ -56,10 +56,7 @@ public class BitbucketBuilds { else { buildUrl = rootUrl + build.getUrl(); } - if(result == Result.SUCCESS) { - repository.postFinishedComment(cause.getPullRequestId(), cause.getCommitHash(), true, buildUrl); - } else if (result == Result.FAILURE || result == Result.UNSTABLE) { - repository.postFinishedComment(cause.getPullRequestId(), cause.getCommitHash(), false, buildUrl); - } + repository.deletePullRequestComment(cause.getPullRequestId(), cause.getBuildStartCommentId()); + repository.postFinishedComment(cause.getPullRequestId(), cause.getCommitHash(), result == Result.SUCCESS, buildUrl); } } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java index 66a0ad8..2bdc558 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java @@ -15,6 +15,7 @@ public class BitbucketCause extends Cause { private final String destinationRepositoryName; private final String pullRequestTitle; private final String commitHash; + private final String buildStartCommentId; public static final String BITBUCKET_URL = "https://bitbucket.org/"; public BitbucketCause(String sourceBranch, @@ -25,7 +26,8 @@ public class BitbucketCause extends Cause { String destinationRepositoryOwner, String destinationRepositoryName, String pullRequestTitle, - String commitHash) { + String commitHash, + String buildStartCommentId) { this.sourceBranch = sourceBranch; this.targetBranch = targetBranch; this.repositoryOwner = repositoryOwner; @@ -35,6 +37,7 @@ public class BitbucketCause extends Cause { this.destinationRepositoryName = destinationRepositoryName; this.pullRequestTitle = pullRequestTitle; this.commitHash = commitHash; + this.buildStartCommentId = buildStartCommentId; } public String getSourceBranch() { @@ -71,6 +74,8 @@ public class BitbucketCause extends Cause { public String getCommitHash() { return commitHash; } + public String getBuildStartCommentId() { return buildStartCommentId; } + @Override public String getShortDescription() { String description = "<a href=" + BITBUCKET_URL + this.getDestinationRepositoryOwner() + "/"; diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java index 2f0a018..1838607 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java @@ -28,7 +28,6 @@ public class BitbucketPullRequestsBuilder { logger.info("Build Start."); this.repository.init(); Collection<BitbucketPullRequestResponseValue> targetPullRequests = this.repository.getTargetPullRequests(); - this.repository.postBuildStartCommentTo(targetPullRequests); this.repository.addFutureBuildTasks(targetPullRequests); } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index 8cee9d0..904892e 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -53,17 +53,16 @@ public class BitbucketRepository { return targetPullRequests; } - public void postBuildStartCommentTo(Collection<BitbucketPullRequestResponseValue> pullRequests) { - for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { + public String postBuildStartCommentTo(BitbucketPullRequestResponseValue pullRequest) { String commit = pullRequest.getSource().getCommit().getHash(); String comment = String.format(BUILD_START_MARKER, commit); - - this.client.postPullRequestComment(pullRequest.getId(), comment); - } + BitbucketPullRequestComment commentResponse = this.client.postPullRequestComment(pullRequest.getId(), comment); + return commentResponse.getCommentId().toString(); } public void addFutureBuildTasks(Collection<BitbucketPullRequestResponseValue> pullRequests) { for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { + String commentId = postBuildStartCommentTo(pullRequest); BitbucketCause cause = new BitbucketCause( pullRequest.getSource().getBranch().getName(), pullRequest.getDestination().getBranch().getName(), @@ -73,11 +72,16 @@ public class BitbucketRepository { pullRequest.getDestination().getRepository().getOwnerName(), pullRequest.getDestination().getRepository().getRepositoryName(), pullRequest.getTitle(), - pullRequest.getSource().getCommit().getHash()); + pullRequest.getSource().getCommit().getHash(), + commentId); this.builder.getTrigger().startJob(cause); } } + public void deletePullRequestComment(String pullRequestId, String commentId) { + this.client.deletePullRequestComment(pullRequestId,commentId); + } + public void postFinishedComment(String pullRequestId, String commit, boolean success, String buildUrl) { String message = BUILD_FAILURE_COMMENT; if (success){ diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java index 200b81e..af43408 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java @@ -4,6 +4,7 @@ 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; @@ -52,14 +53,27 @@ public class BitbucketApiClient { return null; } - public void postPullRequestComment(String pullRequestId, String comment) { + 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); - postRequest(path, new NameValuePair[]{ content }); + String response = postRequest(path, new NameValuePair[]{ content }); + return parseSingleCommentJson(response); + } catch (UnsupportedEncodingException e) { e.printStackTrace(); } + catch (IOException e) { + e.printStackTrace(); + } + return null; } private String getRequest(String path) { @@ -79,21 +93,37 @@ public class BitbucketApiClient { return response; } - private void postRequest(String path, NameValuePair[] params) throws UnsupportedEncodingException { + public void deleteRequest(String path) { + HttpClient client = new HttpClient(); + 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 = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, credentials); PostMethod httppost = new PostMethod(path); httppost.setRequestBody(params); client.getParams().setAuthenticationPreemptive(true); + String response = ""; try { client.executeMethod(httppost); - String response = httppost.getResponseBodyAsString(); + 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 { @@ -113,5 +143,13 @@ public class BitbucketApiClient { return parsedResponse; } + private BitbucketPullRequestComment parseSingleCommentJson(String response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + BitbucketPullRequestComment parsedResponse; + parsedResponse = mapper.readValue( + response, + BitbucketPullRequestComment.class); + return parsedResponse; + } } diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly index 5117011..da12a64 100644 --- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly @@ -11,7 +11,7 @@ <f:entry title="RepositoryOwner" field="repositoryOwner"> <f:textbox /> </f:entry> - <f:entry title="RespositoryName" field="repositoryName"> + <f:entry title="RepositoryName" field="repositoryName"> <f:textbox /> </f:entry> <f:entry title="CI Skip Phrases" field="ciSkipPhrases"> |