Search in sources :

Example 1 with ServiceException

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

the class GithubPipelineCreateRequest method validateGithubAccessToken.

private static void validateGithubAccessToken(String accessToken, String apiUrl) throws IOException {
    try {
        HttpURLConnection connection = GithubScm.connect(apiUrl + "/user", accessToken);
        GithubScm.validateAccessTokenScopes(connection);
    } catch (Exception e) {
        if (e instanceof ServiceException) {
            throw e;
        }
        throw new ServiceException.UnexpectedErrorException("Failure validating github access token: " + e.getMessage(), e);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ServiceException(io.jenkins.blueocean.commons.ServiceException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException)

Example 2 with ServiceException

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

the class GithubPipelineCreateRequest method create.

@SuppressWarnings("unchecked")
@Override
public BluePipeline create(Reachable parent) throws IOException {
    String apiUrl = null;
    //default
    String orgName = getName();
    String credentialId = null;
    StringBuilder sb = new StringBuilder();
    List<String> repos = new ArrayList<>();
    if (scmConfig != null) {
        apiUrl = StringUtils.defaultIfBlank(scmConfig.getUri(), GithubScm.DEFAULT_API_URI);
        if (scmConfig.getConfig().get("orgName") instanceof String) {
            orgName = (String) scmConfig.getConfig().get("orgName");
        }
        credentialId = scmConfig.getCredentialId();
        if (scmConfig != null && scmConfig.getConfig().get("repos") instanceof List) {
            for (String r : (List<String>) scmConfig.getConfig().get("repos")) {
                sb.append(String.format("(%s\\b)?", r));
                repos.add(r);
            }
        }
    }
    User authenticatedUser = User.current();
    if (authenticatedUser == null) {
        throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
    }
    TopLevelItem item = null;
    try {
        if (credentialId != null) {
            validateCredentialId(credentialId, apiUrl);
        }
        item = create(Jenkins.getInstance(), getName(), DESCRIPTOR, CustomOrganizationFolderDescriptor.class);
        if (item instanceof OrganizationFolder) {
            if (credentialId != null) {
                //Find domain attached to this credentialId, if present check if it's BlueOcean specific domain then
                //add the properties otherwise simply use it
                Domain domain = CredentialsUtils.findDomain(credentialId, authenticatedUser);
                if (domain == null) {
                    //this should not happen since validateCredentialId found the credential
                    throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create pipeline").add(new ErrorMessage.Error("scm.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "No domain in user credentials found for credentialId: " + scmConfig.getCredentialId())));
                }
                if (domain.test(new BlueOceanDomainRequirement())) {
                    ((OrganizationFolder) item).addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(apiUrl)));
                }
            }
            GitHubSCMNavigator gitHubSCMNavigator = new GitHubSCMNavigator(apiUrl, orgName, credentialId, credentialId);
            if (sb.length() > 0) {
                gitHubSCMNavigator.setPattern(sb.toString());
            }
            // cick of github scan build
            OrganizationFolder organizationFolder = (OrganizationFolder) item;
            organizationFolder.getNavigators().replace(gitHubSCMNavigator);
            if (repos.size() == 1) {
                SCMSourceEvent.fireNow(new SCMSourceEventImpl(repos.get(0), item, apiUrl, gitHubSCMNavigator));
            } else {
                organizationFolder.scheduleBuild(new Cause.UserIdCause());
            }
            return new GithubOrganizationFolder(organizationFolder, parent.getLink());
        }
    } catch (Exception e) {
        String msg = String.format("Error creating pipeline %s: %s", getName(), e.getMessage());
        logger.error(msg, e);
        if (item != null) {
            try {
                item.delete();
            } catch (InterruptedException e1) {
                logger.error(String.format("Error creating pipeline %s: %s", getName(), e1.getMessage()), e1);
                throw new ServiceException.UnexpectedErrorException("Error cleaning up pipeline " + getName() + " due to error: " + e.getMessage(), e);
            }
        }
        if (e instanceof ServiceException) {
            throw e;
        }
        throw new ServiceException.UnexpectedErrorException(msg, e);
    }
    return null;
}
Also used : User(hudson.model.User) ArrayList(java.util.ArrayList) TopLevelItem(hudson.model.TopLevelItem) Cause(hudson.model.Cause) ArrayList(java.util.ArrayList) List(java.util.List) CustomOrganizationFolderDescriptor(jenkins.branch.CustomOrganizationFolderDescriptor) OrganizationFolder(jenkins.branch.OrganizationFolder) BlueOceanCredentialsProvider(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider) GitHubSCMNavigator(org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) Domain(com.cloudbees.plugins.credentials.domains.Domain) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage)

Example 3 with ServiceException

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

the class ApiHead method getDynamic.

/**
     * Exposes all {@link ApiRoutable}s to URL space.
     *
     * @param route current URL route handled by ApiHead
     * @return {@link ApiRoutable} object
     */
public ApiRoutable getDynamic(String route) {
    setApis();
    StaplerRequest request = Stapler.getCurrentRequest();
    String m = request.getMethod();
    if (m.equalsIgnoreCase("POST") || m.equalsIgnoreCase("PUT") || m.equalsIgnoreCase("PATCH")) {
        String header = request.getHeader("Content-Type");
        if (header == null || !header.contains("application/json")) {
            throw new ServiceException(415, "Content-Type: application/json required");
        }
    }
    return apis.get(route);
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) StaplerRequest(org.kohsuke.stapler.StaplerRequest)

Aggregations

ServiceException (io.jenkins.blueocean.commons.ServiceException)3 IOException (java.io.IOException)2 Domain (com.cloudbees.plugins.credentials.domains.Domain)1 Cause (hudson.model.Cause)1 TopLevelItem (hudson.model.TopLevelItem)1 User (hudson.model.User)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 BlueOceanCredentialsProvider (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanCredentialsProvider)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1 HttpURLConnection (java.net.HttpURLConnection)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CustomOrganizationFolderDescriptor (jenkins.branch.CustomOrganizationFolderDescriptor)1 OrganizationFolder (jenkins.branch.OrganizationFolder)1 GitHubSCMNavigator (org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator)1 StaplerRequest (org.kohsuke.stapler.StaplerRequest)1