public void testConnection()

in commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/stash/StashSettings.java [196:247]


  public void testConnection(@NotNull BuildTypeIdentity buildTypeOrTemplate, @NotNull VcsRoot root, @NotNull Map<String, String> params) throws PublisherException {
    String vcsRootUrl = root.getProperty("url");
    if (null == vcsRootUrl) {
      throw new PublisherException("Missing VCS root URL");
    }
    final Repository repository = VCS_URL_PARSER.parseRepositoryUrl(vcsRootUrl);
    if (null == repository)
      throw new PublisherException("Cannot parse repository URL from VCS root " + root.getName());
    String apiUrl = params.getOrDefault(Constants.STASH_BASE_URL, guessApiURL(root.getProperty("url")));
    if (null == apiUrl || apiUrl.length() == 0)
      throw new PublisherException("Missing Bitbucket Server API URL parameter");
    String url = apiUrl + "/rest/api/1.0/projects/" + repository.owner() + "/repos/" + repository.repositoryName();

    final Map<String, String> headers = ImmutableMap.of(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);

    final HttpCredentials credentials = getCredentials(buildTypeOrTemplate.getProject(), root, params);

    HttpResponseProcessor<HttpPublisherException> processor = new DefaultHttpResponseProcessor() {
      @Override
      public void processResponse(HttpHelper.HttpResponse response) throws HttpPublisherException, IOException {

        super.processResponse(response);

        final String json = response.getContent();
        if (null == json) {
          throw new HttpPublisherException("Stash publisher has received no response");
        }
        StashRepoInfo repoInfo = myGson.fromJson(json, StashRepoInfo.class);
        if (null == repoInfo)
          throw new HttpPublisherException("Bitbucket Server publisher has received a malformed response");
        if (null != repoInfo.errors && !repoInfo.errors.isEmpty()) {
          StringBuilder sb = new StringBuilder();
          for (StashError err: repoInfo.errors) {
            sb.append("\n");
            sb.append(err.message);
          }
          String pluralS = "";
          if (repoInfo.errors.size() > 1)
            pluralS = "s";
          throw new HttpPublisherException(String.format("Bitbucket Server publisher error%s:%s", pluralS, sb.toString()));
        }
      }
    };
    try {
      IOGuard.allowNetworkCall(() -> {
        HttpHelper.get(url, credentials, headers,
                       BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(), processor);
      });
    } catch (Exception ex) {
      throw new PublisherException(String.format("Bitbucket Server publisher has failed to connect to \"%s\" repository", repository.url()), ex);
    }
  }