Search in sources :

Example 1 with AbstractBitbucketEndpoint

use of com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint in project blueocean-plugin by jenkinsci.

the class BitbucketServerEndpointContainer method create.

@Override
public ScmServerEndpoint create(JSONObject request) {
    List<ErrorMessage.Error> errors = Lists.newLinkedList();
    // Validate name
    final String name = (String) request.get(ScmServerEndpoint.NAME);
    if (StringUtils.isBlank(name)) {
        errors.add(new ErrorMessage.Error(ScmServerEndpoint.NAME, ErrorMessage.Error.ErrorCodes.MISSING.toString(), ScmServerEndpoint.NAME + " is required"));
    }
    String url = (String) request.get(ScmServerEndpoint.API_URL);
    final BitbucketEndpointConfiguration endpointConfiguration = BitbucketEndpointConfiguration.get();
    if (StringUtils.isBlank(url)) {
        errors.add(new ErrorMessage.Error(ScmServerEndpoint.API_URL, ErrorMessage.Error.ErrorCodes.MISSING.toString(), ScmServerEndpoint.API_URL + " is required"));
    } else {
        try {
            String version = BitbucketServerApi.getVersion(url);
            if (!BitbucketServerApi.isSupportedVersion(version)) {
                errors.add(new ErrorMessage.Error(BitbucketServerEndpoint.API_URL, ErrorMessage.Error.ErrorCodes.INVALID.toString(), Messages.bbserver_version_validation_error(version, BitbucketServerApi.MINIMUM_SUPPORTED_VERSION)));
            } else {
                // validate presence of endpoint with same name
                url = BitbucketEndpointConfiguration.normalizeServerUrl(url);
                for (AbstractBitbucketEndpoint endpoint : endpointConfiguration.getEndpoints()) {
                    if (url.equals(endpoint.getServerUrl())) {
                        errors.add(new ErrorMessage.Error(ScmServerEndpoint.API_URL, ErrorMessage.Error.ErrorCodes.ALREADY_EXISTS.toString(), ScmServerEndpoint.API_URL + " already exists"));
                        break;
                    }
                }
            }
        } catch (ServiceException e) {
            errors.add(new ErrorMessage.Error(BitbucketServerEndpoint.API_URL, ErrorMessage.Error.ErrorCodes.INVALID.toString(), StringUtils.isBlank(e.getMessage()) ? "Invalid URL" : e.getMessage()));
        }
    }
    if (!errors.isEmpty()) {
        throw new ServiceException.BadRequestException(new ErrorMessage(400, "Failed to create Bitbucket server endpoint").addAll(errors));
    }
    final com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint endpoint = new com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint(name, url, false, null);
    SecurityContext old = null;
    try {
        // We need to escalate privilege to add user defined endpoint to
        old = ACL.impersonate(ACL.SYSTEM);
        endpointConfiguration.addEndpoint(endpoint);
    } finally {
        // reset back to original privilege level
        if (old != null) {
            SecurityContextHolder.setContext(old);
        }
    }
    return new BitbucketServerEndpoint(endpoint, this);
}
Also used : AbstractBitbucketEndpoint(com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint) ServiceException(io.jenkins.blueocean.commons.ServiceException) SecurityContext(org.acegisecurity.context.SecurityContext) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) BitbucketEndpointConfiguration(com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration)

Example 2 with AbstractBitbucketEndpoint

use of com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint 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

AbstractBitbucketEndpoint (com.cloudbees.jenkins.plugins.bitbucket.endpoints.AbstractBitbucketEndpoint)2 BitbucketEndpointConfiguration (com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration)2 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 ScmServerEndpoint (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmServerEndpoint)1 SecurityContext (org.acegisecurity.context.SecurityContext)1