public GitHubApi openGitHubForStoredToken()

in commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/github/api/impl/GitHubApiFactoryImpl.java [89:147]


  public GitHubApi openGitHubForStoredToken(@NotNull final String url,
                                            @NotNull final String tokenId,
                                            @NotNull final SProject project) {

    return new GitHubApiImpl(myWrapper, new GitHubApiPaths(url)){
      @Override
      protected SimpleCredentials authenticationCredentials() throws IOException {
        final OAuthToken gitHubOAuthToken = myOAuthTokensStorage.getToken(project, tokenId, true, true);
        if (gitHubOAuthToken != null) {
          //todo: must be refactored to use Bearer token TW-76091
          return new SimpleCredentials("oauth2", gitHubOAuthToken.getAccessToken());
        }
        else {
          throw new IOException("Failed to get installation token for GitHub App connection (tokenId: " + tokenId + ")");
        }
      }

      @Override
      protected void checkPermissions(@NotNull Repository repo, @NotNull RepoInfo repoInfo) throws PublisherException {
        if (null == repoInfo.name || null == repoInfo.permissions) {
          throw new PublisherException(String.format("Repository \"%s\" is inaccessible", repo.url()));
        }

        final OAuthToken gitHubOAuthToken = myOAuthTokensStorage.getToken(project, tokenId, true, true);
        if (gitHubOAuthToken == null) {
          throw new PublisherException("Failed to retrieve configured token from storage (tokenId: " + tokenId + ")");
        }

        if (SUser.UKNOWN_USER_ID == gitHubOAuthToken.getTeamCityUserId()) { // GitHub App Connection
          final OAuthConnectionDescriptor connection = getConnection(project, tokenId);
          final TokenIntent intent = new TokenIntent(TokenIntentType.PUBLISH_STATUS, repo.url());
          if (!connection.getOauthProvider().isSuitableToken(gitHubOAuthToken, intent)) {
            throw new PublisherException(String.format("The stored token doesn't have push access to the repository \"%s\"", repo.url()));
          }

        } else { // OAuth connection
          if (!repoInfo.permissions.push) {
            throw new PublisherException(String.format("There is no push access to the repository \"%s\"", repo.url()));
          }
        }
      }


      @NotNull
      private OAuthConnectionDescriptor getConnection(@NotNull SProject project, @NotNull String tokenId) throws PublisherException {
        final TokenFullIdComponents components = OAuthTokensStorage.parseFullTokenId(tokenId);
        if (components == null) {
          throw new PublisherException("Unable to parse token id " + tokenId);
        }

        final OAuthConnectionDescriptor connection = myConnectionsManager.findConnectionByTokenStorageId(project, components.getTokenStorageId());
        if (connection == null) {
          throw new PublisherException("Unable to find connection by token id " + tokenId);
        }

        return connection;
      }
    };
  }