aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
diff options
context:
space:
mode:
authorJoel Baranick <joel.baranick@ensighten.com>2015-12-17 08:50:45 -0800
committerJoel Baranick <joel.baranick@ensighten.com>2015-12-17 08:50:45 -0800
commitf0588fb15ee687619695bccfc7553dbcd43b948d (patch)
tree54cf3ba9d17db5de413553d238949cc0db286954 /src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
parentf2294ad4c37592d4046fd02e35439340278b09a0 (diff)
parent810d15cd76fe5869730ab6610de457ae52a6433e (diff)
downloadbbprb-f0588fb15ee687619695bccfc7553dbcd43b948d.tar.gz
Merge branch 'master_k' into CredentialsSupport
# Conflicts: # src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
Diffstat (limited to 'src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java')
-rw-r--r--src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java111
1 files changed, 36 insertions, 75 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
index fb43f12..309ceec 100644
--- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
+++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
@@ -5,16 +5,17 @@ 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.ApiClient;
+import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BuildState;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
+import jenkins.model.Jenkins;
+
import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf;
/**
@@ -22,17 +23,9 @@ import static com.cloudbees.plugins.credentials.CredentialsMatchers.instanceOf;
*/
public class BitbucketRepository {
private static final Logger logger = Logger.getLogger(BitbucketRepository.class.getName());
- 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";
+ private static final String BUILD_DESCRIPTION = "%s: %s into %s";
+ private static final String BUILD_REQUEST_MARKER = "test this please";
- 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;
@@ -52,12 +45,14 @@ public class BitbucketRepository {
username = credentials.getUsername();
password = credentials.getPassword().getPlainText();
}
-
client = new ApiClient(
username,
password,
trigger.getRepositoryOwner(),
- trigger.getRepositoryName());
+ trigger.getRepositoryName(),
+ trigger.getCiKey(),
+ trigger.getCiName()
+ );
}
public Collection<Pullrequest> getTargetPullRequests() {
@@ -72,17 +67,8 @@ public class BitbucketRepository {
return targetPullRequests;
}
- public String postBuildStartCommentTo(Pullrequest pullRequest) {
- String sourceCommit = pullRequest.getSource().getCommit().getHash();
- String destinationCommit = pullRequest.getDestination().getCommit().getHash();
- String comment = String.format(BUILD_START_MARKER, builder.getProject().getDisplayName(), sourceCommit, destinationCommit);
- Pullrequest.Comment commentResponse = this.client.postPullRequestComment(pullRequest.getId(), comment);
- return commentResponse.getId().toString();
- }
-
public void addFutureBuildTasks(Collection<Pullrequest> pullRequests) {
for(Pullrequest pullRequest : pullRequests) {
- String commentId = postBuildStartCommentTo(pullRequest);
if ( this.trigger.getApproveIfSuccess() ) {
deletePullRequestApproval(pullRequest.getId());
}
@@ -96,24 +82,26 @@ public class BitbucketRepository {
pullRequest.getDestination().getRepository().getRepositoryName(),
pullRequest.getTitle(),
pullRequest.getSource().getCommit().getHash(),
- pullRequest.getDestination().getCommit().getHash(),
- commentId);
+ pullRequest.getDestination().getCommit().getHash());
+ setBuildStatus(cause, BuildState.INPROGRESS, Jenkins.getInstance().getRootUrl());
this.builder.getTrigger().startJob(cause);
}
}
- public void deletePullRequestComment(String pullRequestId, String commentId) {
- this.client.deletePullRequestComment(pullRequestId,commentId);
- }
+ public void setBuildStatus(BitbucketCause cause, BuildState state, String buildUrl) {
+ String comment = null;
+ String sourceCommit = cause.getSourceCommitHash();
+ String owner = cause.getRepositoryOwner();
+ String repository = cause.getRepositoryName();
+ String destinationBranch = cause.getTargetBranch();
+
+ logger.info("setBuildStatus " + state + " for commit: " + sourceCommit + " with url " + 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;
+ if (state == BuildState.FAILED || state == BuildState.SUCCESSFUL) {
+ comment = String.format(BUILD_DESCRIPTION, builder.getProject().getDisplayName(), sourceCommit, destinationBranch);
}
- String comment = String.format(BUILD_FINISH_SENTENCE, builder.getProject().getDisplayName(), sourceCommit, destinationCommit, message, buildUrl);
- this.client.postPullRequestComment(pullRequestId, comment);
+ this.client.setBuildStatus(owner, repository, sourceCommit, state, buildUrl, comment);
}
public void deletePullRequestApproval(String pullRequestId) {
@@ -125,19 +113,16 @@ public class BitbucketRepository {
}
private boolean isBuildTarget(Pullrequest pullRequest) {
-
- boolean shouldBuild = true;
if (pullRequest.getState() != null && pullRequest.getState().equals("OPEN")) {
if (isSkipBuild(pullRequest.getTitle())) {
return false;
}
- String sourceCommit = pullRequest.getSource().getCommit().getHash();
-
+ Pullrequest.Revision source = pullRequest.getSource();
+ String sourceCommit = source.getCommit().getHash();
Pullrequest.Revision destination = pullRequest.getDestination();
String owner = destination.getRepository().getOwnerName();
String repositoryName = destination.getRepository().getRepositoryName();
- String destinationCommit = destination.getCommit().getHash();
String id = pullRequest.getId();
List<Pullrequest.Comment> comments = client.getPullRequestComments(owner, repositoryName, id);
@@ -151,47 +136,23 @@ public class BitbucketRepository {
continue;
}
- //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;
+ return true;
}
}
}
+
+ Pullrequest.Repository sourceRepository = source.getRepository();
+
+ if (this.client.hasBuildStatus(sourceRepository.getOwnerName(), sourceRepository.getRepositoryName(), sourceCommit)) {
+ logger.info("Commit " + sourceCommit + " has already been processed");
+ return false;
+ }
+
+ return true;
}
- return shouldBuild;
+
+ return false;
}
private boolean isSkipBuild(String pullRequestTitle) {