From be3e57315173936cd23c135b6c9eb86e73d1700e Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Tue, 13 May 2014 16:37:58 +1200 Subject: adding bitbucket pull request hash into BuildStart and BuildFinish messages, so when a pull request is updated a new build will be made. --- .../BitbucketBuilds.java | 4 +-- .../BitbucketCause.java | 7 ++++- .../BitbucketRepository.java | 30 ++++++++++++++-------- ...itbucketPullRequestResponseValueRepository.java | 11 ++++++++ ...etPullRequestResponseValueRepositoryCommit.java | 16 ++++++++++++ 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryCommit.java (limited to 'src/main/java') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java index a940ca9..8052cc6 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuilds.java @@ -57,9 +57,9 @@ public class BitbucketBuilds { buildUrl = rootUrl + build.getUrl(); } if(result == Result.SUCCESS) { - repository.postFinishedComment(cause.getPullRequestId(), true, buildUrl); + repository.postFinishedComment(cause.getPullRequestId(), cause.getCommitHash(), true, buildUrl); } else if (result == Result.FAILURE || result == Result.UNSTABLE) { - repository.postFinishedComment(cause.getPullRequestId(), false, buildUrl); + repository.postFinishedComment(cause.getPullRequestId(), cause.getCommitHash(), false, buildUrl); } } } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java index 1cb5da4..66a0ad8 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketCause.java @@ -14,6 +14,7 @@ public class BitbucketCause extends Cause { private final String destinationRepositoryOwner; private final String destinationRepositoryName; private final String pullRequestTitle; + private final String commitHash; public static final String BITBUCKET_URL = "https://bitbucket.org/"; public BitbucketCause(String sourceBranch, @@ -23,7 +24,8 @@ public class BitbucketCause extends Cause { String pullRequestId, String destinationRepositoryOwner, String destinationRepositoryName, - String pullRequestTitle) { + String pullRequestTitle, + String commitHash) { this.sourceBranch = sourceBranch; this.targetBranch = targetBranch; this.repositoryOwner = repositoryOwner; @@ -32,6 +34,7 @@ public class BitbucketCause extends Cause { this.destinationRepositoryOwner = destinationRepositoryOwner; this.destinationRepositoryName = destinationRepositoryName; this.pullRequestTitle = pullRequestTitle; + this.commitHash = commitHash; } public String getSourceBranch() { @@ -66,6 +69,8 @@ public class BitbucketCause extends Cause { return pullRequestTitle; } + public String getCommitHash() { return commitHash; } + @Override public String getShortDescription() { String description = " pullRequests) { for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { - String comment = BUILD_START_MARKER; + String commit = pullRequest.getSource().getCommit().getHash(); + String comment = String.format(BUILD_START_MARKER, commit); comment += " Build Triggered. Waiting to hear about " + pullRequest.getSource().getRepository().getFullName(); this.client.postPullRequestComment(pullRequest.getId(), comment); } @@ -70,17 +72,19 @@ public class BitbucketRepository { pullRequest.getId(), pullRequest.getDestination().getRepository().getOwnerName(), pullRequest.getDestination().getRepository().getRepositoryName(), - pullRequest.getTitle()); + pullRequest.getTitle(), + pullRequest.getSource().getCommit().getHash()); this.builder.getTrigger().startJob(cause); } } - public void postFinishedComment(String pullRequestId, boolean success, String buildUrl) { - String comment = BUILD_FINISH_MARKER; + public void postFinishedComment(String pullRequestId, String commit, boolean success, String buildUrl) { + + String comment = String.format(BUILD_FINISH_MARKER, commit); if (success) { - comment += " Test PASSed. Refer to this link for build results."; + comment += BUILD_SUCCESS_COMMENT; } else { - comment += " Test FAILed. Refer to this link for build results."; + comment += BUILD_FAILURE_COMMENT; } comment += buildUrl; this.client.postPullRequestComment(pullRequestId, comment); @@ -92,11 +96,15 @@ public class BitbucketRepository { if (isSkipBuild(pullRequest.getTitle())) { return false; } + + String commit = pullRequest.getSource().getCommit().getHash(); BitbucketPullRequestResponseValueRepository destination = pullRequest.getDestination(); String owner = destination.getRepository().getOwnerName(); String repositoryName = destination.getRepository().getRepositoryName(); String id = pullRequest.getId(); List comments = client.getPullRequestComments(owner, repositoryName, id); + String searchString = String.format(BUILD_START_MARKER, commit).toLowerCase(); + if (comments != null) { Collections.sort(comments); Collections.reverse(comments); @@ -106,7 +114,7 @@ public class BitbucketRepository { continue; } content = content.toLowerCase(); - if (content.contains(BUILD_START_MARKER.toLowerCase())) { + if (content.contains(searchString)) { shouldBuild = false; break; } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java index 76eeaf2..f97c010 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepository.java @@ -10,6 +10,7 @@ import org.codehaus.jackson.annotate.JsonProperty; public class BitbucketPullRequestResponseValueRepository { private BitbucketPullRequestResponseValueRepositoryRepository repository; private BitbucketPullRequestResponseValueRepositoryBranch branch; + private BitbucketPullRequestResponseValueRepositoryCommit commit; @JsonProperty("repository") public BitbucketPullRequestResponseValueRepositoryRepository getRepository() { @@ -30,6 +31,16 @@ public class BitbucketPullRequestResponseValueRepository { 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/BitbucketPullRequestResponseValueRepositoryCommit.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryCommit.java new file mode 100644 index 0000000..5846d71 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketPullRequestResponseValueRepositoryCommit.java @@ -0,0 +1,16 @@ +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 -- cgit v1.2.3 From 3a8d79bdf929ca07e4692dcaacca54e207eb3ff3 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Wed, 14 May 2014 14:26:22 +1200 Subject: modify the comments to use bitbucket's markdown --- .../BitbucketRepository.java | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index 362a65a..b25a8fe 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -16,12 +16,12 @@ import java.util.logging.Logger; */ public class BitbucketRepository { private static final Logger logger = Logger.getLogger(BitbucketRepository.class.getName()); - public static final String BUILD_START_MARKER = "[Build:%s Start]"; - public static final String BUILD_FINISH_MARKER = "[Build:%s Finished]"; + public static final String BUILD_START_MARKER = "[*BuildStarted*] %s"; + public static final String BUILD_FINISH_MARKER = "[*BuildFinished*] %s \n\n **%s** - %s"; public static final String BUILD_REQUEST_MARKER = "test this please"; - public static final String BUILD_SUCCESS_COMMENT = " Test PASSED.\n\t"; - public static final String BUILD_FAILURE_COMMENT = " Test FAILED.\n\t"; + public static final String BUILD_SUCCESS_COMMENT = ":star:SUCCESS"; + public static final String BUILD_FAILURE_COMMENT = ":x:FAILURE"; private String projectPath; private BitbucketPullRequestsBuilder builder; private BitbucketBuildTrigger trigger; @@ -57,7 +57,7 @@ public class BitbucketRepository { for(BitbucketPullRequestResponseValue pullRequest : pullRequests) { String commit = pullRequest.getSource().getCommit().getHash(); String comment = String.format(BUILD_START_MARKER, commit); - comment += " Build Triggered. Waiting to hear about " + pullRequest.getSource().getRepository().getFullName(); + this.client.postPullRequestComment(pullRequest.getId(), comment); } } @@ -79,14 +79,12 @@ public class BitbucketRepository { } public void postFinishedComment(String pullRequestId, String commit, boolean success, String buildUrl) { - - String comment = String.format(BUILD_FINISH_MARKER, commit); - if (success) { - comment += BUILD_SUCCESS_COMMENT; - } else { - comment += BUILD_FAILURE_COMMENT; + String message = BUILD_FAILURE_COMMENT; + if (success){ + message = BUILD_SUCCESS_COMMENT; } - comment += buildUrl; + String comment = String.format(BUILD_FINISH_MARKER, commit, message, buildUrl); + this.client.postPullRequestComment(pullRequestId, comment); } -- cgit v1.2.3