public void testConnection()

in commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/gitlab/GitlabSettings.java [130:176]


  public void testConnection(@NotNull BuildTypeIdentity buildTypeOrTemplate, @NotNull VcsRoot root, @NotNull Map<String, String> params) throws PublisherException {
    String apiUrl = params.getOrDefault(Constants.GITLAB_API_URL, guessApiURL(root.getProperty("url")));
    if (StringUtil.isEmptyOrSpaces(apiUrl))
      throw new PublisherException("Missing GitLab API URL parameter");
    String pathPrefix = getPathPrefix(apiUrl);
    Repository repository = GitlabPublisher.parseRepository(root, pathPrefix);
    if (null == repository)
      throw new PublisherException("Cannot parse repository URL from VCS root " + root.getName());

    HttpCredentials credentials = getCredentials(buildTypeOrTemplate.getProject(), root, params);
    try {
      IOGuard.allowNetworkCall(() -> {
        ProjectInfoResponseProcessor processorPrj = new ProjectInfoResponseProcessor();
        HttpHelper.get(getProjectsUrl(apiUrl, repository.owner(), repository.repositoryName()),
                       credentials, Collections.emptyMap(),
                       BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(), processorPrj);
        boolean tokenHasEnoughRights = true;
        if (processorPrj.isEmptyPermissions()) {
          MultipleProjectsInfoResponseProcessor mulProjectProcessorPrj = new MultipleProjectsInfoResponseProcessor();
          HttpHelper.get(getAuthorizedProjectsUrl(apiUrl, repository.repositoryName()),
                         credentials, Collections.emptyMap(),
                         BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(), mulProjectProcessorPrj);
          tokenHasEnoughRights = mulProjectProcessorPrj.getInfo().contains(processorPrj.getInfo());
        }
        else if (processorPrj.getAccessLevel() < 30) {
          UserInfoResponseProcessor processorUser = new UserInfoResponseProcessor();
          HttpHelper.get(getUserUrl(apiUrl), credentials, Collections.emptyMap(),
                         BaseCommitStatusPublisher.DEFAULT_CONNECTION_TIMEOUT, trustStore(), processorUser);
          tokenHasEnoughRights = processorUser.isAdmin();
        }
        if (!tokenHasEnoughRights) {
          throw new HttpPublisherException("GitLab does not grant enough permissions to publish a commit status");
        }
      });
    } catch (HttpPublisherException pe) {
      Integer statusCode = pe.getStatusCode();
      if (Objects.equals(statusCode, 404)) {
        throw new PublisherException(String.format("Repository \"%s\" can not be found. Please check if it was renamed or moved to another namespace", repository.repositoryName()));
      } else if (Objects.equals(statusCode, 401)) {
        throw new PublisherException(String.format("Cannot access the \"%s\" repository. Ensure you are using a valid access token instead of a password (authentication via password is no longer available)", repository.repositoryName()));
      } else {
        throw new PublisherException("Request was failed with error", pe);
      }
    } catch (Exception ex) {
      throw new PublisherException(String.format("GitLab publisher has failed to connect to \"%s\" repository", repository.url()), ex);
    }
  }