Search in sources :

Example 1 with SSHCapableUserCredentialsProvider

use of com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method doListRemoteBranchesInternal.

/**
 * Do list remote branches internal.
 *
 * @param repoURL The repository URL.
 * @param excMessPresenter  Exception message presenter.
 *
 * @return the remote branches or an empty list.
 */
public Collection<Ref> doListRemoteBranchesInternal(URIish repoURL, AuthExceptionMessagePresenter excMessPresenter) {
    Collection<Ref> remoteRefs = Collections.emptySet();
    String host = repoURL.getHost();
    boolean shouldStopTryingLogin = false;
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("START LISTING REMOTE BRANCHES FOR: " + repoURL);
    }
    do {
        SSHCapableUserCredentialsProvider credentialsProvider = AuthUtil.getCredentialsProvider(host);
        try {
            LOGGER.debug("Now do list the remote branches...");
            remoteRefs = Git.lsRemoteRepository().setHeads(true).setRemote(repoURL.toString()).setCredentialsProvider(credentialsProvider).call();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("BRANCHES: " + remoteRefs);
            }
            shouldStopTryingLogin = true;
        } catch (TransportException ex) {
            LOGGER.debug(ex.getMessage(), ex);
            boolean retryLogin = AuthUtil.handleAuthException(ex, host, excMessPresenter, !credentialsProvider.wasResetCalled());
            if (!retryLogin || credentialsProvider.shouldCancelLogin()) {
                LOGGER.debug("STOP TRYING TO LOGIN!");
                shouldStopTryingLogin = true;
            }
        } catch (GitAPIException e) {
            LOGGER.error(e.getMessage(), e);
        }
    } while (!shouldStopTryingLogin);
    return remoteRefs;
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref) SSHCapableUserCredentialsProvider(com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider) TransportException(org.eclipse.jgit.api.errors.TransportException)

Example 2 with SSHCapableUserCredentialsProvider

use of com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider in project oxygen-git-client-addon by oxygenxml.

the class GitAccess method fetch.

/**
 * Brings all the commits to the local repository but does not merge them.
 *
 * @throws SSHPassphraseRequiredException
 * @throws PrivateRepositoryException
 * @throws RepositoryUnavailableException
 */
public void fetch() throws SSHPassphraseRequiredException, PrivateRepositoryException, RepositoryUnavailableException {
    LOGGER.debug("Begin fetch");
    if (git == null) {
        throw new RepositoryUnavailableException(new NoRepositorySelected("Repository is empty"));
    }
    AuthenticationInterceptor.install();
    SSHCapableUserCredentialsProvider credentialsProvider = AuthUtil.getCredentialsProvider(getHostName());
    try {
        StoredConfig config = git.getRepository().getConfig();
        Set<String> sections = config.getSections();
        if (sections.contains(ConfigConstants.CONFIG_KEY_REMOTE)) {
            git.fetch().setRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/" + getRemoteFromCurrentBranch() + "/*")).setCheckFetchedObjects(true).setRemote(getRemoteFromCurrentBranch()).setRemoveDeletedRefs(true).setCredentialsProvider(credentialsProvider).call();
        }
    } catch (TransportException e) {
        LOGGER.debug(e.getMessage(), e);
        Throwable cause = e;
        while (cause.getCause() != null) {
            cause = cause.getCause();
        }
        String message = e.getMessage();
        if (message != null && (message.contains("Authentication is required but no CredentialsProvider has been registered") || message.contains(AuthUtil.NOT_AUTHORIZED))) {
            throw new PrivateRepositoryException(e);
        } else if (message != null && message.toLowerCase().contains(AuthUtil.AUTH_FAIL) && credentialsProvider.isPassphaseRequested() || (cause instanceof SshException) && ((SshException) cause).getDisconnectCode() == SshConstants.SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE) {
            throw new SSHPassphraseRequiredException(e);
        } else {
            throw new RepositoryUnavailableException(e);
        }
    } catch (GitAPIException | RevisionSyntaxException e) {
        LOGGER.error(e.getMessage(), e);
    }
    LOGGER.debug("End fetch");
}
Also used : SSHCapableUserCredentialsProvider(com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider) RevisionSyntaxException(org.eclipse.jgit.errors.RevisionSyntaxException) SshException(org.apache.sshd.common.SshException) TransportException(org.eclipse.jgit.api.errors.TransportException) StoredConfig(org.eclipse.jgit.lib.StoredConfig) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefSpec(org.eclipse.jgit.transport.RefSpec)

Example 3 with SSHCapableUserCredentialsProvider

use of com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider in project oxygen-git-client-addon by oxygenxml.

the class GitTestBase method pushOneFileToRemote.

/**
 * Loads the repository and pushes one file to the remote.
 *
 * @throws Exception If it fails.
 */
protected final void pushOneFileToRemote(String repository, String fileName, String fileContent) throws Exception {
    commitOneFile(repository, fileName, fileContent);
    GitAccess.getInstance().push(new SSHCapableUserCredentialsProvider("", "", "", GitAccess.getInstance().getHostName()));
}
Also used : SSHCapableUserCredentialsProvider(com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider)

Aggregations

SSHCapableUserCredentialsProvider (com.oxygenxml.git.auth.SSHCapableUserCredentialsProvider)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 TransportException (org.eclipse.jgit.api.errors.TransportException)2 SshException (org.apache.sshd.common.SshException)1 RevisionSyntaxException (org.eclipse.jgit.errors.RevisionSyntaxException)1 Ref (org.eclipse.jgit.lib.Ref)1 StoredConfig (org.eclipse.jgit.lib.StoredConfig)1 RefSpec (org.eclipse.jgit.transport.RefSpec)1