private String publishPullRequestStatus()

in commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/tfs/TfsStatusPublisher.java [484:539]


  private String publishPullRequestStatus(@NotNull TfsRepositoryInfo info,
                                           @NotNull BuildRevision revision,
                                           @NotNull String data,
                                           @NotNull String description) throws PublisherException {
    // Check whether pull requests status publishing enabled
    String commitId = revision.getRevision();
    final String publishPullRequest = StringUtil.emptyIfNull(myParams.get(TfsConstants.PUBLISH_PULL_REQUESTS)).trim();
    if (!Boolean.parseBoolean(publishPullRequest)) {
      return commitId;
    }

    // Get branch and try to find pull request id
    final String branch = revision.getRepositoryVersion().getVcsBranch();
    if (StringUtil.isEmptyOrSpaces(branch)) {
      LOG.debug(String.format("Branch was not specified for commit %s, pull request status would not be published", commitId));
      return commitId;
    }

    final Matcher matcher = TFS_GIT_PULL_REQUEST_PATTERN.matcher(branch);
    if (!matcher.find()) {
      LOG.debug(String.format("Branch %s for commit %s does not contain info about pull request, status would not be published", branch, commitId));
      return commitId;
    }

    final String pullRequestId = matcher.group(1);

    // Since it's a merge request we need to get parent commit for it
    final Set<String> commits = getParentCommits(info, commitId, myParams, revision.getRoot());

    // Then we need to get pull request iteration where this commit present
    final Iteration iteration = getPullRequestIteration(info, pullRequestId, commits, myParams, revision.getRoot());
    final String pullRequestStatusUrl;

    if (iteration == null || StringUtil.isEmptyOrSpaces(iteration.id)) {
      // Publish status for pull request
      pullRequestStatusUrl = MessageFormat.format(PULL_REQUEST_STATUS_URL_FORMAT, info.getServer(), info.getProject(), info.getRepository(), pullRequestId);
    } else {
      if (iteration.sourceRefCommit != null && !StringUtil.isEmptyOrSpaces(iteration.sourceRefCommit.commitId))
        commitId = iteration.sourceRefCommit.commitId;
      // Publish status for pull request iteration
      pullRequestStatusUrl = MessageFormat.format(PULL_REQUEST_ITERATION_STATUS_URL_FORMAT,
                                                  info.getServer(), info.getProject(), info.getRepository(), pullRequestId, iteration.id);
    }

    try {
      postJson(pullRequestStatusUrl, getCredentials(revision.getRoot(), myParams),
               data,
               Collections.singletonMap("Accept", "application/json"),
               description
      );
    } catch (PublisherException e) {
      final String message = String.format("Failed to publish status to pull request %s in repository %s", pullRequestId, info);
      throw new PublisherException(message, e);
    }
    return commitId;
  }