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;
}
Aggregations