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