diff --git a/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/Constants.java b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/Constants.java index c9748e68..68461e02 100644 --- a/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/Constants.java +++ b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/Constants.java @@ -24,6 +24,8 @@ public class Constants { public static final String STASH_PUBLISHER_ID = "atlassianStashPublisher"; public static final String STASH_BASE_URL = "stashBaseUrl"; + public static final String STASH_PROJECT_KEY = "stashProjectKey"; + public static final String STASH_REPO_NAME = "stashRepoName"; public static final String STASH_USERNAME = "stashUsername"; public static final String STASH_PASSWORD = "secure:stashPassword"; @@ -91,9 +93,13 @@ public String getSshKey() { } @NotNull - public String getStashBaseUrl() { - return STASH_BASE_URL; - } + public String getStashBaseUrl() { return STASH_BASE_URL; } + + @NotNull + public String getStashProjectKey() { return STASH_PROJECT_KEY; } + + @NotNull + public String getStashRepoName() { return STASH_REPO_NAME; } @NotNull public String getStashUsername() { diff --git a/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/StashPublisher.java b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/StashPublisher.java index b832c36a..2547fc88 100644 --- a/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/StashPublisher.java +++ b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/StashPublisher.java @@ -1,21 +1,25 @@ package jetbrains.buildServer.commitPublisher.stash; +import com.google.common.collect.Iterables; import com.google.gson.*; import com.intellij.openapi.diagnostic.Logger; import jetbrains.buildServer.commitPublisher.*; +import jetbrains.buildServer.commitPublisher.stash.data.PullRequestInfo; +import jetbrains.buildServer.commitPublisher.stash.data.StashCommit; import jetbrains.buildServer.serverSide.*; import jetbrains.buildServer.serverSide.executors.ExecutorServices; import jetbrains.buildServer.serverSide.impl.LogUtil; import jetbrains.buildServer.users.User; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; +import org.apache.http.*; import org.apache.http.entity.ContentType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; class StashPublisher extends HttpBasedCommitStatusPublisher { @@ -23,6 +27,8 @@ class StashPublisher extends HttpBasedCommitStatusPublisher { private static final Logger LOG = Logger.getInstance(StashPublisher.class.getName()); + protected final Gson myGson = new Gson(); + private final WebLinks myLinks; StashPublisher(@NotNull CommitStatusPublisherSettings settings, @@ -121,7 +127,16 @@ private void vote(@NotNull SBuild build, @NotNull StashBuildStatus status, @NotNull String comment) { String msg = createMessage(status, build.getBuildPromotion().getBuildTypeExternalId(), getBuildName(build), myLinks.getViewResultsUrl(build), comment); - vote(revision.getRevision(), msg, LogUtil.describe(build)); + String commitRevision = revision.getRevision(); + try { + StashCommit commit = findPullRequestCommit(build); + if (commit != null) { + commitRevision = commit.id; + } + } catch (PublisherException e) { + e.printStackTrace(); + } + vote(commitRevision, msg, LogUtil.describe(build)); } private void vote(@NotNull SQueuedBuild build, @@ -214,4 +229,58 @@ private String getUsername() { private String getPassword() { return myParams.get(Constants.STASH_PASSWORD); } + + private StashCommit findPullRequestCommit(@NotNull SBuild build) throws PublisherException { + String apiUrl = myParams.get(Constants.STASH_BASE_URL); + String projectKey = myParams.get(Constants.STASH_PROJECT_KEY); + String repository = myParams.get(Constants.STASH_REPO_NAME); + + final List lastCommit = new ArrayList(); + + // Plugin will recognize pull request only when Branch specification parameter will be configured as below: + // +:refs/pull-requests/(*/merge) + // https://blog.jetbrains.com/teamcity/2013/02/automatically-building-pull-requests-from-github-with-teamcity + + if (build.getBranch().getName().contains("merge")) { // try to find pull request only for branches with merge in the name + String pullRequestNumber = build.getBranch().getName().replace("/merge",""); //remove merge from brache name to get only pull request number + apiUrl = apiUrl + "/rest/api/1.0/projects/" + projectKey + "/repos/" + repository + "/pull-requests/" + pullRequestNumber + "/commits/"; + LOG.debug("Bitbucket Pull Request apiUrl: " + apiUrl); + } + + if (null != apiUrl || apiUrl.length() != 0) { + try { + HttpResponseProcessor processor = new DefaultHttpResponseProcessor() { + @Override + public void processResponse(HttpResponse response) throws HttpPublisherException, IOException { + + final HttpEntity entity = response.getEntity(); + if (null == entity) { + throw new HttpPublisherException("Bitbucket publisher has received no response"); + } + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + entity.writeTo(bos); + final String json = bos.toString("utf-8"); + PullRequestInfo commitInfo = myGson.fromJson(json, PullRequestInfo.class); + if (null == commitInfo) + throw new HttpPublisherException("Bitbucket Server publisher has received a malformed response"); + if (null != commitInfo.values && !commitInfo.values.isEmpty()) { + StashCommit commit = Iterables.get(commitInfo.values, 0); + if (commit != null) { + lastCommit.add(commit); + } + } + } + }; + + HttpHelper.get(apiUrl, getUsername(), getPassword(), + Collections.singletonMap("Accept", "application/json"), BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, processor); + } catch (Exception ex) { + throw new PublisherException(String.format("Bitbucket Server publisher has failed to connect to %s repository", apiUrl), ex); + } + } + if (!lastCommit.isEmpty() && lastCommit.size() > 0) { + return lastCommit.get(0); + } + return null; + } } diff --git a/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/data/PullRequestInfo.java b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/data/PullRequestInfo.java new file mode 100644 index 00000000..5688fe9d --- /dev/null +++ b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/data/PullRequestInfo.java @@ -0,0 +1,14 @@ +package jetbrains.buildServer.commitPublisher.stash.data; + +import java.util.Collection; + +/** + * Created by rafalkasa on 2017-03-19. + */ + +/** + * This class does not represent full repository information + */ +public class PullRequestInfo { + public Collection values; +} diff --git a/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/data/StashCommit.java b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/data/StashCommit.java new file mode 100644 index 00000000..108fe0fe --- /dev/null +++ b/commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/data/StashCommit.java @@ -0,0 +1,13 @@ +package jetbrains.buildServer.commitPublisher.stash.data; + +/** + * Created by rafalkasa on 2017-03-19. + */ + +/** + * This class does not represent full repository information + */ +public class StashCommit { + public String id; + public String displayId; +} diff --git a/commit-status-publisher-server/src/main/resources/buildServerResources/stash/stashSettings.jsp b/commit-status-publisher-server/src/main/resources/buildServerResources/stash/stashSettings.jsp index 28d68a00..a9efec5e 100644 --- a/commit-status-publisher-server/src/main/resources/buildServerResources/stash/stashSettings.jsp +++ b/commit-status-publisher-server/src/main/resources/buildServerResources/stash/stashSettings.jsp @@ -21,19 +21,35 @@ - - - - - - - - - - - - - - + + Plugin will recognize Pull Request branches only when Branch specification parameter in VCS Roots will be configured as below: + +:refs/pull-requests/(*/merge) + + + + + + + + + + + + + + + + + + + + + + + + + + +