public void testConnection()

in commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/upsource/UpsourceSettings.java [135:168]


  public void testConnection(@NotNull BuildTypeIdentity buildTypeOrTemplate, @NotNull VcsRoot root, @NotNull Map<String, String> params) throws PublisherException {
    String apiUrl = HttpHelper.stripTrailingSlash(params.get(Constants.UPSOURCE_SERVER_URL));
    if (null == apiUrl || apiUrl.length() == 0)
      throw new PublisherException("Missing Upsource Server URL parameter");
    String username = params.get(Constants.UPSOURCE_USERNAME);
    String password = params.get(Constants.UPSOURCE_PASSWORD);
    if (null == username || null == password)
      throw new PublisherException("Missing Upsource credentials");
    final HttpCredentials credentials = new UsernamePasswordCredentials(username, password);
    final String projectId = params.get(Constants.UPSOURCE_PROJECT_ID);
    String urlPost = apiUrl + "/" + ENDPOINT_TEST_CONNECTION;
    String urlGet = apiUrl + "/" + ENDPOINT_RPC + "/" + QUERY_GET_CURRENT_USER;
    try {
      Map<String, String> data = new HashMap<String, String>();
      data.put(UpsourceSettings.PROJECT_FIELD, projectId);
      // Newer versions of Upsource support special test connection call, that works correctly for their CI-specific authentication
      IOGuard.allowNetworkCall(() -> {
        HttpHelper.post(urlPost, credentials, myGson.toJson(data), ContentType.APPLICATION_JSON, null,
                        BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(),
                        new DefaultHttpResponseProcessor());
      });
    } catch (Exception ex) {
      try {
        // If the newer method fails, we assume it may be an older version of Upsource, and test connection in a regular way
        IOGuard.allowNetworkCall(() -> {
          HttpHelper.get(urlGet, credentials, null,
                         BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(),
                         new TestConnectionResponseProcessor(projectId));
        });
      } catch (Exception ex2) {
        throw new PublisherException(String.format("Upsource publisher has failed to connect to project '%s'", projectId), ex2);
      }
    }
  }