Search in sources :

Example 1 with ScmServerEndpoint

use of io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpoint in project blueocean-plugin by jenkinsci.

the class GithubServerContainer method create.

@CheckForNull
public ScmServerEndpoint create(@JsonBody JSONObject request) {
    List<ErrorMessage.Error> errors = Lists.newLinkedList();
    // Validate name
    final String name = (String) request.get(GithubServer.NAME);
    if (StringUtils.isEmpty(name)) {
        errors.add(new ErrorMessage.Error(GithubServer.NAME, ErrorMessage.Error.ErrorCodes.MISSING.toString(), GithubServer.NAME + " is required"));
    } else {
        GithubServer byName = findByName(name);
        if (byName != null) {
            errors.add(new ErrorMessage.Error(GithubServer.NAME, ErrorMessage.Error.ErrorCodes.ALREADY_EXISTS.toString(), GithubServer.NAME + " already exists for server at '" + byName.getApiUrl() + "'"));
        }
    }
    // Validate url
    final String url = (String) request.get(GithubServer.API_URL);
    if (StringUtils.isEmpty(url)) {
        errors.add(new ErrorMessage.Error(GithubServer.API_URL, ErrorMessage.Error.ErrorCodes.MISSING.toString(), GithubServer.API_URL + " is required"));
    } else {
        Endpoint byUrl = GitHubConfiguration.get().findEndpoint(url);
        if (byUrl != null) {
            errors.add(new ErrorMessage.Error(GithubServer.API_URL, ErrorMessage.Error.ErrorCodes.ALREADY_EXISTS.toString(), GithubServer.API_URL + " is already registered as '" + byUrl.getName() + "'"));
        }
    }
    if (StringUtils.isNotEmpty(url)) {
        // Validate that the URL represents a Github API endpoint
        try {
            HttpURLConnection connection = HttpRequest.get(url).connect();
            if (connection.getHeaderField("X-GitHub-Request-Id") == null) {
                errors.add(new ErrorMessage.Error(GithubServer.API_URL, ErrorMessage.Error.ErrorCodes.INVALID.toString(), ERROR_MESSAGE_INVALID_SERVER));
            } else {
                boolean isGithubCloud = false;
                boolean isGithubEnterprise = false;
                try {
                    InputStream inputStream;
                    int code = connection.getResponseCode();
                    if (200 <= code && code < 300) {
                        inputStream = HttpRequest.getInputStream(connection);
                    } else {
                        inputStream = HttpRequest.getErrorStream(connection);
                    }
                    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
                    };
                    Map<String, String> responseBody = GithubScm.om.readValue(inputStream, typeRef);
                    isGithubCloud = code == 200 && responseBody.containsKey("current_user_url");
                    isGithubEnterprise = code == 401 && responseBody.containsKey("message");
                } catch (IOException ioe) {
                    LOGGER.log(Level.INFO, "Could not parse response body from Github");
                }
                if (!isGithubCloud && !isGithubEnterprise) {
                    errors.add(new ErrorMessage.Error(GithubServer.API_URL, ErrorMessage.Error.ErrorCodes.INVALID.toString(), ERROR_MESSAGE_INVALID_APIURL));
                }
            }
        } catch (Throwable e) {
            errors.add(new ErrorMessage.Error(GithubServer.API_URL, ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.toString()));
            LOGGER.log(Level.INFO, "Could not connect to Github", e);
        }
    }
    if (errors.isEmpty()) {
        SecurityContext old = null;
        try {
            // We need to escalate privilege to add user defined endpoint to
            old = ACL.impersonate(ACL.SYSTEM);
            GitHubConfiguration config = GitHubConfiguration.get();
            String sanitizedUrl = discardQueryString(url);
            Endpoint endpoint = new Endpoint(sanitizedUrl, name);
            if (!config.addEndpoint(endpoint)) {
                errors.add(new ErrorMessage.Error(GithubServer.API_URL, ErrorMessage.Error.ErrorCodes.ALREADY_EXISTS.toString(), GithubServer.API_URL + " is already registered as '" + endpoint.getName() + "'"));
            } else {
                return new GithubServer(endpoint, getLink());
            }
        } finally {
            // reset back to original privilege level
            if (old != null) {
                SecurityContextHolder.setContext(old);
            }
        }
    }
    ErrorMessage message = new ErrorMessage(400, "Failed to create Github server");
    message.addAll(errors);
    throw new ServiceException.BadRequestException(message);
}
Also used : GitHubConfiguration(org.jenkinsci.plugins.github_branch_source.GitHubConfiguration) HashMap(java.util.HashMap) InputStream(java.io.InputStream) IOException(java.io.IOException) Endpoint(org.jenkinsci.plugins.github_branch_source.Endpoint) ScmServerEndpoint(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpoint) HttpURLConnection(java.net.HttpURLConnection) Endpoint(org.jenkinsci.plugins.github_branch_source.Endpoint) ScmServerEndpoint(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpoint) SecurityContext(org.acegisecurity.context.SecurityContext) JSONObject(net.sf.json.JSONObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) CheckForNull(javax.annotation.CheckForNull)

Example 2 with ScmServerEndpoint

use of io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpoint in project blueocean-plugin by jenkinsci.

the class BitbucketServerEndpointContainer method iterator.

@Override
public Iterator<ScmServerEndpoint> iterator() {
    BitbucketEndpointConfiguration endpointConfiguration = BitbucketEndpointConfiguration.get();
    Iterator<com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint> serverEndpoints = Iterators.filter(endpointConfiguration.getEndpoints().iterator(), com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint.class);
    return Iterators.transform(serverEndpoints, new Function<AbstractBitbucketEndpoint, ScmServerEndpoint>() {

        @Override
        public ScmServerEndpoint apply(AbstractBitbucketEndpoint input) {
            return new BitbucketServerEndpoint(input, BitbucketServerEndpointContainer.this);
        }
    });
}
Also used : ScmServerEndpoint(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpoint) AbstractBitbucketEndpoint(com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint) BitbucketEndpointConfiguration(com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration)

Aggregations

ScmServerEndpoint (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpoint)2 AbstractBitbucketEndpoint (com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint)1 BitbucketEndpointConfiguration (com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 HashMap (java.util.HashMap)1 CheckForNull (javax.annotation.CheckForNull)1 JSONObject (net.sf.json.JSONObject)1 SecurityContext (org.acegisecurity.context.SecurityContext)1 Endpoint (org.jenkinsci.plugins.github_branch_source.Endpoint)1 GitHubConfiguration (org.jenkinsci.plugins.github_branch_source.GitHubConfiguration)1