diff options
Diffstat (limited to 'src/main')
5 files changed, 53 insertions, 21 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java index eb80d86..230432d 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java @@ -85,22 +85,23 @@ public class BitbucketBuildFilter { } public BitbucketBuildFilter(String f) { - this.filter = f; + this.filter = f != null ? f : ""; this.buildFilter(this.filter); } private void buildFilter(String filter) { + logger.log(Level.INFO, "Build filter by phrase: {0}", filter); for(Filter f : AvailableFilters) { if (f.check(filter)) { this.currFilter = f; logger.log(Level.INFO, "Using filter: {0}", f.getClass().getSimpleName()); break; } - } - logger.warning("No available filters to use ..."); + } } public boolean approved(BitbucketCause cause) { + logger.log(Level.INFO, "Approve cause: {0}", cause.toString()); return this.currFilter.apply(this.filter, cause); } } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java index 3671ea6..c5d4159 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketPullRequestsBuilder.java @@ -2,9 +2,11 @@ package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; import hudson.model.AbstractProject; +import java.security.MessageDigest; import java.util.Collection; import java.util.logging.Logger; +import org.apache.commons.codec.binary.Hex; /** * Created by nishio @@ -50,6 +52,16 @@ public class BitbucketPullRequestsBuilder { public AbstractProject<?, ?> getProject() { return this.project; + } + + public String getProjectId() { + try { + final MessageDigest MD5 = MessageDigest.getInstance("MD5"); + return new String(Hex.encodeHex(MD5.digest(this.project.getFullName().getBytes("UTF-8")))); + } catch (Exception exc) { + logger.severe(exc.toString()); + } + return this.project.getFullName(); } public BitbucketBuildTrigger getTrigger() { diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index e0a4b19..2686ffd 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -9,6 +9,7 @@ import java.util.logging.Logger; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClient; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BuildState; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; +import java.util.logging.Level; import jenkins.model.Jenkins; /** @@ -87,7 +88,7 @@ public class BitbucketRepository { comment = String.format(BUILD_DESCRIPTION, builder.getProject().getDisplayName(), sourceCommit, destinationBranch); } - this.client.setBuildStatus(owner, repository, sourceCommit, state, buildUrl, comment); + this.client.setBuildStatus(owner, repository, sourceCommit, state, buildUrl, comment, this.builder.getProjectId()); } public void deletePullRequestApproval(String pullRequestId) { @@ -110,9 +111,20 @@ public class BitbucketRepository { String owner = destination.getRepository().getOwnerName(); String repositoryName = destination.getRepository().getRepositoryName(); + Pullrequest.Repository sourceRepository = source.getRepository(); + + boolean commitAlreadyBeenProcessed = this.client.hasBuildStatus( + sourceRepository.getOwnerName(), sourceRepository.getRepositoryName(), sourceCommit, this.builder.getProjectId() + ); + if (commitAlreadyBeenProcessed) logger.log(Level.INFO, + "Commit {0}#{1} has already been processed", + new Object[]{ sourceCommit, this.builder.getProjectId() } + ); + String id = pullRequest.getId(); List<Pullrequest.Comment> comments = client.getPullRequestComments(owner, repositoryName, id); + boolean rebuildCommentAvailable = false; if (comments != null) { Collections.sort(comments); Collections.reverse(comments); @@ -123,19 +135,18 @@ public class BitbucketRepository { } if (content.contains(BUILD_REQUEST_MARKER.toLowerCase())) { - return true; + rebuildCommentAvailable = true; + logger.log(Level.INFO, + "Rebuild comment available for commit {0} and comment #{1}", + new Object[]{ sourceCommit, comment.getId() } + ); + this.client.deleteComment(id, Integer.toString(comment.getId())); } } - } - - 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; + logger.log(Level.INFO, "Build target? {0}", rebuildCommentAvailable || !commitAlreadyBeenProcessed); + return rebuildCommentAvailable || !commitAlreadyBeenProcessed; } return false; diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index c8f1818..18ca86f 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -24,6 +24,7 @@ public class ApiClient { private static final Logger logger = Logger.getLogger(ApiClient.class.getName()); private static final String V1_API_BASE_URL = "https://bitbucket.org/api/1.0/repositories/"; private static final String V2_API_BASE_URL = "https://bitbucket.org/api/2.0/repositories/"; + private static final String COMPUTED_KEY_FORMAT = "%s-%s"; private String owner; private String repositoryName; private Credentials credentials; @@ -56,27 +57,33 @@ public class ApiClient { return Collections.EMPTY_LIST; } - public boolean hasBuildStatus(String owner, String repositoryName, String revision) { - String url = v2(owner, repositoryName, "/commit/" + revision + "/statuses/build/" + this.key); + public boolean hasBuildStatus(String owner, String repositoryName, String revision, String keyEx) { + String url = v2(owner, repositoryName, "/commit/" + revision + "/statuses/build/" + String.format(COMPUTED_KEY_FORMAT, this.key, keyEx)); return get(url).contains("\"state\""); } - public void setBuildStatus(String owner, String repositoryName, String revision, BuildState state, String buildUrl, String comment) { + public void setBuildStatus(String owner, String repositoryName, String revision, BuildState state, String buildUrl, String comment, String keyEx) { String url = v2(owner, repositoryName, "/commit/" + revision + "/statuses/build"); + String computedKey = String.format(COMPUTED_KEY_FORMAT, this.key, keyEx); NameValuePair[] data = new NameValuePair[]{ new NameValuePair("description", comment), - new NameValuePair("key", this.key), + new NameValuePair("key", computedKey), new NameValuePair("name", this.name), new NameValuePair("state", state.toString()), new NameValuePair("url", buildUrl), }; - logger.info("POST state " + state + " to " + url); - post(url, data); + logger.log(Level.INFO, "POST state {0} to {1} with key {2} with response {3}", new Object[]{ + state, url, computedKey, post(url, data)} + ); } public void deletePullRequestApproval(String pullRequestId) { delete(v2("/pullrequests/" + pullRequestId + "/approve")); } + + public void deleteComment(String pullRequestId, String commentId) { + delete(v1("/pullrequests/" + pullRequestId + "/comments/" + commentId + "/")); + } public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { try { @@ -128,6 +135,7 @@ public class ApiClient { private String post(String path, NameValuePair[] data) { PostMethod req = new PostMethod(path); req.setRequestBody(data); + req.getParams().setContentCharset("utf-8"); return send(req); } diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html index 8b801db..f49da40 100644 --- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html @@ -2,4 +2,4 @@ Filter option in custom format. Default value is empty or "any". Available formats: * any pull requests applied for this project: "any", "*" or empty string. * filtered by destination branch: "my-branch" or more complex reg-ex filter "r:^master" (must be started with "r:" and case insensitive match). -* filtered by source and destination branches: "s:source-branch d:dest-branch"
\ No newline at end of file +* filtered by source and destination branches: "s:source-branch d:dest-branch" |