use of jenkins.plugins.git.AbstractGitSCMSource in project blueocean-plugin by jenkinsci.
the class GitScm method validateAndCreate.
@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
boolean requirePush = request.has("requirePush");
// --[ Grab repo url and SCMSource ]----------------------------------------------------------
final String repositoryUrl;
final AbstractGitSCMSource scmSource;
if (request.has("repositoryUrl")) {
repositoryUrl = request.getString("repositoryUrl");
scmSource = new GitSCMSource(repositoryUrl);
} else {
try {
String fullName = request.getJSONObject("pipeline").getString("fullName");
SCMSourceOwner item = Jenkins.get().getItemByFullName(fullName, SCMSourceOwner.class);
if (item != null) {
scmSource = (AbstractGitSCMSource) item.getSCMSources().iterator().next();
repositoryUrl = scmSource.getRemote();
} else {
return HttpResponses.errorJSON("No repository found for: " + fullName);
}
} catch (JSONException e) {
return HttpResponses.errorJSON("No repositoryUrl or pipeline.fullName specified in request.");
} catch (RuntimeException e) {
return HttpResponses.errorWithoutStack(ServiceException.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
// --[ Grab user ]-------------------------------------------------------------------------------------
User user = User.current();
if (user == null) {
throw new ServiceException.UnauthorizedException("Not authenticated");
}
// --[ Get credential id from request or create from repo url ]----------------------------------------
String credentialId = null;
if (request.has("credentialId")) {
credentialId = request.getString("credentialId");
}
if (credentialId == null) {
credentialId = makeCredentialId(repositoryUrl);
}
if (credentialId == null) {
// Still null? Must be a bad repoURL
throw new ServiceException.BadRequestException("Invalid URL \"" + repositoryUrl + "\"");
}
// Create new is only for username + password
if (request.has("userName") || request.has("password")) {
createPWCredentials(credentialId, user, request, repositoryUrl);
}
final StandardCredentials creds = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, Jenkins.get(), Jenkins.getAuthentication(), (List<DomainRequirement>) null), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialId)));
if (creds == null) {
throw new ServiceException.NotFoundException("No credentials found for: " + credentialId);
}
try {
if (requirePush) {
String branch = request.getString("branch");
if (repositoryUrl != null) {
((GitSCMSource) scmSource).setCredentialsId(credentialId);
}
new GitBareRepoReadSaveRequest(scmSource, branch, null, branch, null, null).invokeOnScm((GitSCMFileSystem.FSFunction<Void>) repository -> {
GitUtils.validatePushAccess(repository, repositoryUrl, creds);
return null;
});
} else {
List<ErrorMessage.Error> errors = GitUtils.validateCredentials(repositoryUrl, creds);
if (!errors.isEmpty()) {
throw new ServiceException.UnauthorizedException(errors.get(0).getMessage());
}
}
} catch (Exception e) {
String message = e.getMessage();
if (message != null && message.contains("TransportException")) {
throw new ServiceException.PreconditionRequired("Repository URL unreachable: " + repositoryUrl);
}
return HttpResponses.errorWithoutStack(ServiceException.PRECONDITION_REQUIRED, message);
}
return HttpResponses.okJSON();
}
Aggregations