Search in sources :

Example 1 with ErrorMessage

use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.

the class GithubScmSaveFileRequest method save.

public Object save(@Nonnull String apiUrl, @Nullable String owner, @Nullable String repoName, @Nullable String accessToken) {
    List<ErrorMessage.Error> errors = new ArrayList<>();
    if (this.content == null) {
        errors.add(new ErrorMessage.Error("content", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "content is required parameter"));
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to save file to scm").addAll(errors));
    } else {
        errors.addAll(content.validate());
    }
    //if no owner given then check if its there in request
    if (owner == null) {
        owner = content.getOwner();
    }
    if (repoName == null) {
        repoName = content.getRepo();
    }
    if (owner == null) {
        errors.add(new ErrorMessage.Error("content.owner", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "No scm owner found with pipeline %s, please provide content.owner parameter"));
    }
    if (repoName == null) {
        errors.add(new ErrorMessage.Error("content.repo", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "No scm repo found with pipeline %s, please provide content.repo parameter"));
    }
    if (errors.size() > 0) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to save content").addAll(errors));
    }
    if (!errors.isEmpty()) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to save file to scm").addAll(errors));
    }
    try {
        String sha = createBranchIfNotPresent(apiUrl, owner, repoName, accessToken);
        //sha in request overrides the one we computed
        if (!StringUtils.isBlank(content.getSha())) {
            sha = content.getSha();
        }
        Map<String, Object> body = new HashMap<>();
        body.put("message", content.getMessage());
        body.put("content", content.getBase64Data());
        if (!StringUtils.isBlank(content.getBranch())) {
            body.put("branch", content.getBranch());
        }
        if (!StringUtils.isBlank(sha)) {
            body.put("sha", sha);
        }
        final Map ghResp = HttpRequest.put(String.format("%s/repos/%s/%s/contents/%s", apiUrl, owner, repoName, content.getPath())).withAuthorization("token " + accessToken).withBody(body).to(Map.class);
        if (ghResp == null) {
            throw new ServiceException.UnexpectedErrorException("Failed to save file to Github: " + content.getPath());
        }
        final Map ghContent = (Map) ghResp.get("content");
        if (ghContent == null) {
            throw new ServiceException.UnexpectedErrorException("Failed to save file: " + content.getPath());
        }
        return new GithubFile(new GithubContent.Builder().sha((String) ghContent.get("sha")).name((String) ghContent.get("name")).repo(repoName).owner(owner).path((String) ghContent.get("path")).build());
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException("Failed to save file: " + e.getMessage(), e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ErrorMessage

use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.

the class GithubPipelineCreateRequest method validateCredentialId.

static void validateCredentialId(String credentialId, String apiUrl) throws IOException {
    if (credentialId != null && !credentialId.trim().isEmpty()) {
        StandardUsernamePasswordCredentials credentials = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
        if (credentials == null) {
            throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Github pipeline").add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), "No Credentials instance found for credentialId: " + credentialId)));
        } else {
            String accessToken = credentials.getPassword().getPlainText();
            validateGithubAccessToken(accessToken, apiUrl);
        }
    }
}
Also used : StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage)

Example 3 with ErrorMessage

use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.

the class BluePipelineContainer method create.

/**
     * Create new pipeline.
     *
     * @param body {@link BluePipelineCreateRequest} request object
     * @return {@link CreateResponse} response
     */
@POST
@WebMethod(name = "")
public CreateResponse create(@JsonBody JSONObject body, StaplerRequest staplerRequest) throws IOException {
    ErrorMessage err = new ErrorMessage(400, "Failed to create Git pipeline");
    if (body.get("name") == null) {
        err.add(new ErrorMessage.Error("name", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "name is required"));
    }
    if (body.get("$class") == null) {
        err.add(new ErrorMessage.Error("$class", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "$class is required"));
    }
    if (!err.getErrors().isEmpty()) {
        throw new ServiceException.BadRequestExpception(err);
    }
    BluePipelineCreateRequest request = staplerRequest.bindJSON(BluePipelineCreateRequest.class, body);
    return create(request);
}
Also used : ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) WebMethod(org.kohsuke.stapler.WebMethod) POST(org.kohsuke.stapler.verb.POST)

Example 4 with ErrorMessage

use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.

the class GitPipelineCreateRequest method validate.

private void validate(String name, BlueScmConfig scmConfig) {
    if (scmConfig == null) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline").add(new ErrorMessage.Error("scmConfig", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "scmConfig is required")));
    }
    List<ErrorMessage.Error> errors = new ArrayList<>();
    String sourceUri = scmConfig.getUri();
    if (sourceUri == null) {
        errors.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "uri is required"));
    } else {
        StandardCredentials credentials = null;
        if (scmConfig.getCredentialId() != null) {
            credentials = GitUtils.getCredentials(Jenkins.getInstance(), sourceUri, scmConfig.getCredentialId());
            if (credentials == null) {
                errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), String.format("credentialId: %s not found", scmConfig.getCredentialId())));
            }
        }
        //validate credentials if no credential id (perhaps git repo doesn't need auth or credentials is present)
        if (scmConfig.getCredentialId() == null || credentials != null) {
            errors.addAll(GitUtils.validateCredentials(sourceUri, credentials));
        }
    }
    try {
        Jenkins.getInstance().getProjectNamingStrategy().checkName(getName());
    } catch (Failure f) {
        errors.add(new ErrorMessage.Error("scmConfig.name", ErrorMessage.Error.ErrorCodes.INVALID.toString(), name + "in not a valid name"));
    }
    if (Jenkins.getInstance().getItem(name) != null) {
        errors.add(new ErrorMessage.Error("name", ErrorMessage.Error.ErrorCodes.ALREADY_EXISTS.toString(), name + " already exists"));
    }
    if (!errors.isEmpty()) {
        throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline:" + name).addAll(errors));
    }
}
Also used : ArrayList(java.util.ArrayList) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) Failure(hudson.model.Failure)

Example 5 with ErrorMessage

use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.

the class GitPipelineUpdateRequest method getGitScmSource.

@SuppressWarnings("unchecked")
private BranchSource getGitScmSource(MultiBranchProject mbp) {
    String sourceUri = null;
    String credentialId = null;
    if (scmConfig != null) {
        sourceUri = scmConfig.getUri();
        List<ErrorMessage.Error> errors = new ArrayList<>();
        StandardCredentials credentials = null;
        if (scmConfig.getCredentialId() != null) {
            credentials = GitUtils.getCredentials(Jenkins.getInstance(), sourceUri, scmConfig.getCredentialId());
            if (credentials == null) {
                errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), String.format("credentialId: %s not found", scmConfig.getCredentialId())));
            }
        }
        if (sourceUri != null) {
            errors.addAll(GitUtils.validateCredentials(sourceUri, credentials));
        }
        credentialId = scmConfig.getCredentialId() == null ? "" : scmConfig.getCredentialId();
        if (!errors.isEmpty()) {
            throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline").addAll(errors));
        }
    }
    PersistedList<BranchSource> sources = mbp.getSourcesList();
    for (BranchSource source : sources) {
        if (source.getSource() instanceof GitSCMSource) {
            GitSCMSource gitSCMSource = (GitSCMSource) source.getSource();
            String remote = gitSCMSource.getRemote();
            if (sourceUri != null && !sourceUri.equals(gitSCMSource.getRemote())) {
                remote = sourceUri;
            }
            String cred = gitSCMSource.getCredentialsId();
            if (!gitSCMSource.getCredentialsId().equals(credentialId)) {
                cred = credentialId;
            }
            GitSCMSource s = new GitSCMSource(gitSCMSource.getId(), remote, cred, gitSCMSource.getIncludes(), gitSCMSource.getExcludes(), gitSCMSource.isIgnoreOnPushNotifications());
            s.setOwner(mbp);
            return new BranchSource(s);
        }
    }
    if (sourceUri != null) {
        //if no scm sources in this MBP project, add a new one using passed sourceUri
        return new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false));
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) GitSCMSource(jenkins.plugins.git.GitSCMSource) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) BranchSource(jenkins.branch.BranchSource) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials)

Aggregations

ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)10 ServiceException (io.jenkins.blueocean.commons.ServiceException)6 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)3 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)2 Domain (com.cloudbees.plugins.credentials.domains.Domain)2 Cause (hudson.model.Cause)2 TopLevelItem (hudson.model.TopLevelItem)2 User (hudson.model.User)2 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)2 Map (java.util.Map)2 BranchSource (jenkins.branch.BranchSource)2 GitSCMSource (jenkins.plugins.git.GitSCMSource)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Failure (hudson.model.Failure)1 MultiBranchPipelineImpl (io.jenkins.blueocean.rest.impl.pipeline.MultiBranchPipelineImpl)1 ScmContentProvider (io.jenkins.blueocean.rest.impl.pipeline.ScmContentProvider)1 URI (java.net.URI)1