Search in sources :

Example 6 with GitClient

use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.

the class GitUtils method validateCredentials.

/**
 *  Calls 'git ls-remote -h uri' to check if git uri or supplied credentials are valid
 *
 * @param uri git repo uri
 * @param credentials credential to use when accessing git
 * @return list of Errors. Empty list means success.
 */
static List<ErrorMessage.Error> validateCredentials(@Nonnull String uri, @Nullable StandardCredentials credentials) throws GitException {
    List<ErrorMessage.Error> errors = new ArrayList<>();
    Git git = new Git(TaskListener.NULL, new EnvVars());
    try {
        GitClient gitClient = git.getClient();
        if (credentials != null) {
            gitClient.addCredentials(uri, credentials);
        }
        gitClient.getRemoteReferences(uri, null, true, false);
    } catch (IOException | InterruptedException e) {
        logger.error("Error running git remote-ls: " + e.getMessage(), e);
        throw new ServiceException.UnexpectedErrorException("Failed to create pipeline due to unexpected error: " + e.getMessage(), e);
    } catch (IllegalStateException | GitException e) {
        logger.error("Error running git remote-ls: " + e.getMessage(), e);
        if (credentials != null) {
            // this turn very hackhish with upgrade of git plugin as there is more and more and more cause/layers before having the real cause...
            if (e instanceof IllegalStateException || e.getMessage().endsWith("not authorized") || e.getMessage().endsWith("not authenticated") || (e instanceof GitException && checkCauseNotAuthenticated((GitException) e))) {
                errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "Invalid credentialId: " + credentials.getId()));
            } else {
                errors.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage()));
            }
        } else if (e.getMessage().contains("Authentication is required") || e.getMessage().contains("connection is not authenticated")) {
            errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage()));
        } else {
            errors.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage()));
        }
    }
    return errors;
}
Also used : GitException(hudson.plugins.git.GitException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Git(org.jenkinsci.plugins.gitclient.Git) EnvVars(hudson.EnvVars) ServiceException(io.jenkins.blueocean.commons.ServiceException) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) GitClient(org.jenkinsci.plugins.gitclient.GitClient)

Aggregations

GitClient (org.jenkinsci.plugins.gitclient.GitClient)6 GitException (hudson.plugins.git.GitException)5 ServiceException (io.jenkins.blueocean.commons.ServiceException)4 EnvVars (hudson.EnvVars)3 IOException (java.io.IOException)3 TaskListener (hudson.model.TaskListener)2 LogTaskListener (hudson.util.LogTaskListener)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ArrayList (java.util.ArrayList)1 GitSCMFileSystem (jenkins.plugins.git.GitSCMFileSystem)1 URIish (org.eclipse.jgit.transport.URIish)1 Git (org.jenkinsci.plugins.gitclient.Git)1