Search in sources :

Example 1 with GitException

use of hudson.plugins.git.GitException 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) {
            //      appended to the message.
            if (e instanceof IllegalStateException || e.getMessage().endsWith("not authorized")) {
                errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "Invalid credentialId: " + credentials.getId()));
            }
        } 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

EnvVars (hudson.EnvVars)1 GitException (hudson.plugins.git.GitException)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Git (org.jenkinsci.plugins.gitclient.Git)1 GitClient (org.jenkinsci.plugins.gitclient.GitClient)1