Search in sources :

Example 1 with AbstractGitSCMSource

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();
}
Also used : BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) StaplerRequest(org.kohsuke.stapler.StaplerRequest) URISyntaxException(java.net.URISyntaxException) JsonBody(org.kohsuke.stapler.json.JsonBody) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) ScmOrganization(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmOrganization) CredentialsMatchers(com.cloudbees.plugins.credentials.CredentialsMatchers) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) Locale(java.util.Locale) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Extension(hudson.Extension) Container(io.jenkins.blueocean.rest.model.Container) User(hudson.model.User) URI(java.net.URI) CredentialsScope(com.cloudbees.plugins.credentials.CredentialsScope) DomainRequirement(com.cloudbees.plugins.credentials.domains.DomainRequirement) CredentialsUtils(io.jenkins.blueocean.credential.CredentialsUtils) JSONException(net.sf.json.JSONException) GitSCMFileSystem(jenkins.plugins.git.GitSCMFileSystem) ScmServerEndpointContainer(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpointContainer) HttpResponse(org.kohsuke.stapler.HttpResponse) AbstractGitSCMSource(jenkins.plugins.git.AbstractGitSCMSource) Jenkins(jenkins.model.Jenkins) Reachable(io.jenkins.blueocean.rest.Reachable) SCMSourceOwner(jenkins.scm.api.SCMSourceOwner) IOException(java.io.IOException) Scm(io.jenkins.blueocean.rest.impl.pipeline.scm.Scm) ServiceException(io.jenkins.blueocean.commons.ServiceException) HttpResponses(hudson.util.HttpResponses) ScmFactory(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmFactory) Objects(java.util.Objects) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) List(java.util.List) Stapler(org.kohsuke.stapler.Stapler) CredentialsProvider(com.cloudbees.plugins.credentials.CredentialsProvider) DigestUtils(io.jenkins.blueocean.commons.DigestUtils) JSONObject(net.sf.json.JSONObject) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) AbstractScm(io.jenkins.blueocean.rest.impl.pipeline.scm.AbstractScm) Collections(java.util.Collections) Link(io.jenkins.blueocean.rest.hal.Link) GitSCMSource(jenkins.plugins.git.GitSCMSource) User(hudson.model.User) SCMSourceOwner(jenkins.scm.api.SCMSourceOwner) GitSCMFileSystem(jenkins.plugins.git.GitSCMFileSystem) JSONException(net.sf.json.JSONException) AbstractGitSCMSource(jenkins.plugins.git.AbstractGitSCMSource) GitSCMSource(jenkins.plugins.git.GitSCMSource) URISyntaxException(java.net.URISyntaxException) JSONException(net.sf.json.JSONException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) AbstractGitSCMSource(jenkins.plugins.git.AbstractGitSCMSource) ServiceException(io.jenkins.blueocean.commons.ServiceException) List(java.util.List) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Aggregations

CredentialsMatchers (com.cloudbees.plugins.credentials.CredentialsMatchers)1 CredentialsProvider (com.cloudbees.plugins.credentials.CredentialsProvider)1 CredentialsScope (com.cloudbees.plugins.credentials.CredentialsScope)1 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 DomainRequirement (com.cloudbees.plugins.credentials.domains.DomainRequirement)1 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Extension (hudson.Extension)1 User (hudson.model.User)1 HttpResponses (hudson.util.HttpResponses)1 DigestUtils (io.jenkins.blueocean.commons.DigestUtils)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 CredentialsUtils (io.jenkins.blueocean.credential.CredentialsUtils)1 Reachable (io.jenkins.blueocean.rest.Reachable)1 Link (io.jenkins.blueocean.rest.hal.Link)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1 BlueOceanDomainSpecification (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification)1 AbstractScm (io.jenkins.blueocean.rest.impl.pipeline.scm.AbstractScm)1