aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java')
-rw-r--r--src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java93
1 files changed, 71 insertions, 22 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
index 61d3702..43e4ddd 100644
--- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
+++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
@@ -1,28 +1,34 @@
package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder;
-import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketApiClient;
-import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketPullRequestComment;
-import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketPullRequestResponseValue;
-import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BitbucketPullRequestResponseValueRepository;
-
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
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;
/**
* Created by nishio
*/
public class BitbucketRepository {
private static final Logger logger = Logger.getLogger(BitbucketRepository.class.getName());
- public static final String BUILD_START_MARKER = "[*BuildStarted*] %s";
- public static final String BUILD_FINISH_MARKER = "[*BuildFinished*] %s";
+ public static final String BUILD_START_MARKER = "[*BuildStarted* **%s**] %s into %s";
+ public static final String BUILD_FINISH_MARKER = "[*BuildFinished* **%s**] %s into %s";
+
+ public static final String BUILD_START_REGEX = "\\[\\*BuildStarted\\* \\*\\*%s\\*\\*\\] ([0-9a-fA-F]+) into ([0-9a-fA-F]+)";
+ public static final String BUILD_FINISH_REGEX = "\\[\\*BuildFinished\\* \\*\\*%s\\*\\*\\] ([0-9a-fA-F]+) into ([0-9a-fA-F]+)";
+
public static final String BUILD_FINISH_SENTENCE = BUILD_FINISH_MARKER + " \n\n **%s** - %s";
public static final String BUILD_REQUEST_MARKER = "test this please";
- public static final String BUILD_SUCCESS_COMMENT = ":star:SUCCESS";
- public static final String BUILD_FAILURE_COMMENT = ":x:FAILURE";
+ public static final String BUILD_SUCCESS_COMMENT = "✔ SUCCESS";
+ public static final String BUILD_FAILURE_COMMENT = "✖ FAILURE";
private String projectPath;
private BitbucketPullRequestsBuilder builder;
private BitbucketBuildTrigger trigger;
@@ -55,8 +61,9 @@ public class BitbucketRepository {
}
public String postBuildStartCommentTo(BitbucketPullRequestResponseValue pullRequest) {
- String commit = pullRequest.getSource().getCommit().getHash();
- String comment = String.format(BUILD_START_MARKER, commit);
+ 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();
}
@@ -64,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(),
@@ -74,6 +84,7 @@ public class BitbucketRepository {
pullRequest.getDestination().getRepository().getRepositoryName(),
pullRequest.getTitle(),
pullRequest.getSource().getCommit().getHash(),
+ pullRequest.getDestination().getCommit().getHash(),
commentId);
this.builder.getTrigger().startJob(cause);
}
@@ -83,46 +94,84 @@ public class BitbucketRepository {
this.client.deletePullRequestComment(pullRequestId,commentId);
}
- public void postFinishedComment(String pullRequestId, String commit, boolean success, String buildUrl) {
+ public void postFinishedComment(String pullRequestId, String sourceCommit, String destinationCommit, boolean success, String buildUrl) {
String message = BUILD_FAILURE_COMMENT;
if (success){
message = BUILD_SUCCESS_COMMENT;
}
- String comment = String.format(BUILD_FINISH_SENTENCE, commit, message, buildUrl);
+ String comment = String.format(BUILD_FINISH_SENTENCE, builder.getProject().getDisplayName(), sourceCommit, destinationCommit, message, buildUrl);
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;
if (pullRequest.getState() != null && pullRequest.getState().equals("OPEN")) {
if (isSkipBuild(pullRequest.getTitle())) {
return false;
}
- String commit = pullRequest.getSource().getCommit().getHash();
+ String sourceCommit = pullRequest.getSource().getCommit().getHash();
+
BitbucketPullRequestResponseValueRepository 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);
- String searchStartMarker = String.format(BUILD_START_MARKER, commit).toLowerCase();
- String searchFinishMarker = String.format(BUILD_FINISH_MARKER, commit).toLowerCase();
if (comments != null) {
Collections.sort(comments);
Collections.reverse(comments);
- for(BitbucketPullRequestComment comment : comments) {
+ for (BitbucketPullRequestComment comment : comments) {
String content = comment.getContent();
if (content == null || content.isEmpty()) {
continue;
}
- content = content.toLowerCase();
- if (content.contains(searchStartMarker) ||
- content.contains(searchFinishMarker)) {
- shouldBuild = false;
- break;
+
+ //These will match any start or finish message -- need to check commits
+ String project_build_start = String.format(BUILD_START_REGEX, builder.getProject().getDisplayName());
+ String project_build_finished = String.format(BUILD_FINISH_REGEX, builder.getProject().getDisplayName());
+ Matcher startMatcher = Pattern.compile(project_build_start, Pattern.CASE_INSENSITIVE).matcher(content);
+ Matcher finishMatcher = Pattern.compile(project_build_finished, Pattern.CASE_INSENSITIVE).matcher(content);
+
+ if (startMatcher.find() ||
+ finishMatcher.find()) {
+
+ String sourceCommitMatch;
+ String destinationCommitMatch;
+
+ if (startMatcher.find(0)) {
+ sourceCommitMatch = startMatcher.group(1);
+ destinationCommitMatch = startMatcher.group(2);
+ } else {
+ sourceCommitMatch = finishMatcher.group(1);
+ destinationCommitMatch = finishMatcher.group(2);
+ }
+
+ //first check source commit -- if it doesn't match, just move on. If it does, investigate further.
+ if (sourceCommitMatch.equalsIgnoreCase(sourceCommit)) {
+ // if we're checking destination commits, and if this doesn't match, then move on.
+ if (this.trigger.getCheckDestinationCommit()
+ && (!destinationCommitMatch.equalsIgnoreCase(destinationCommit))) {
+ continue;
+ }
+
+ shouldBuild = false;
+ break;
+ }
}
+
if (content.contains(BUILD_REQUEST_MARKER.toLowerCase())) {
shouldBuild = true;
break;