Java Code Examples for jenkins.model.JenkinsLocationConfiguration#get()

The following examples show how to use jenkins.model.JenkinsLocationConfiguration#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KafkaComputerLauncher.java    From remoting-kafka-plugin with MIT License 6 votes vote down vote up
/**
 * Returns Jenkins URL to be used by agents launched by this cloud. Always ends with a trailing slash.
 *
 * Uses in order:
 * * Cloud configuration (if launched by Cloud)
 * * Jenkins Location URL
 */
private URL retrieveJenkinsURL(Computer computer) throws RemotingKafkaConfigurationException {
    JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
    String cloudJenkinsUrl = null;
    if (computer instanceof KafkaCloudComputer) {
        KafkaCloudSlave agent = (KafkaCloudSlave) computer.getNode();
        if (agent == null) {
            LOGGER.warning("Cannot find node for computer " + computer.getName());
        } else {
            cloudJenkinsUrl = agent.getCloud().getJenkinsUrl();
        }
    }

    String url = StringUtils.defaultIfBlank(cloudJenkinsUrl, locationConfiguration.getUrl());
    try {
        if (url == null)
            throw new RemotingKafkaConfigurationException(Messages.KafkaComputerLauncher_MalformedJenkinsURL());
        return new URL(url);
    } catch (MalformedURLException e) {
        throw new RemotingKafkaConfigurationException(Messages.KafkaComputerLauncher_MalformedJenkinsURL());
    }
}
 
Example 2
Source File: KubernetesCloud.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Returns Jenkins URL to be used by agents launched by this cloud. Always ends with a trailing slash.
 *
 * Uses in order:
 * * cloud configuration
 * * environment variable <b>KUBERNETES_JENKINS_URL</b>
 * * Jenkins Location URL
 *
 * @return Jenkins URL to be used by agents launched by this cloud. Always ends with a trailing slash.
 *         Null if no Jenkins URL could be computed.
 */
@CheckForNull
public String getJenkinsUrlOrNull() {
    JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
    String url = StringUtils.defaultIfBlank(
            getJenkinsUrl(),
            StringUtils.defaultIfBlank(
                    System.getProperty("KUBERNETES_JENKINS_URL",System.getenv("KUBERNETES_JENKINS_URL")),
                    locationConfiguration.getUrl()
            )
    );
    if (url == null) {
        return null;
    }
    url = url.endsWith("/") ? url : url + "/";
    return url;
}
 
Example 3
Source File: BitbucketPipelineCreateRequest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
@Override
protected SCMSource createSource(@Nonnull MultiBranchProject project, @Nonnull BlueScmConfig scmConfig) {
    /* scmConfig.uri presence is already validated in {@link #validate} but lets check just in case */
    if(StringUtils.isBlank(scmConfig.getUri())){
        throw new ServiceException.BadRequestException("scmConfig.uri must be present");
    }

    Set<ChangeRequestCheckoutStrategy> strategies = new HashSet<>();
    strategies.add(ChangeRequestCheckoutStrategy.MERGE);

    BitbucketSCMSource bitbucketSCMSource = new BitbucketSCMSourceBuilder(null, scmConfig.getUri(), computeCredentialId(scmConfig),
            (String)scmConfig.getConfig().get("repoOwner"),
            (String)scmConfig.getConfig().get("repository"))
            .withTrait(new BranchDiscoveryTrait(true, true)) //take all branches
            .withTrait(new ForkPullRequestDiscoveryTrait(strategies, new ForkPullRequestDiscoveryTrait.TrustTeamForks()))
            .withTrait(new OriginPullRequestDiscoveryTrait(strategies))
            .withTrait(new CleanBeforeCheckoutTrait())
            .withTrait(new CleanAfterCheckoutTrait())
            .withTrait(new LocalBranchTrait())
            .build();

    //Setup Jenkins root url, if not set bitbucket cloud notification will fail
    JenkinsLocationConfiguration jenkinsLocationConfiguration = JenkinsLocationConfiguration.get();
    if(jenkinsLocationConfiguration != null) {
        String url = jenkinsLocationConfiguration.getUrl();
        if (url == null) {
            url = Jenkins.getInstance().getRootUrl();
            if (url != null) {
                jenkinsLocationConfiguration.setUrl(url);
            }
        }
    }
    return bitbucketSCMSource;
}
 
Example 4
Source File: MattermostNotifier.java    From jenkins-mattermost-plugin with MIT License 5 votes vote down vote up
public String getBuildServerUrl() {
  if (buildServerUrl == null || buildServerUrl.equals("")) {
    // Note: current code should no longer use "new JenkinsLocationConfiguration()"
    // as only one instance per runtime is really supported by the current core.
    JenkinsLocationConfiguration jenkinsConfig = JenkinsLocationConfiguration.get();
    return jenkinsConfig.getUrl();
  } else {
    return buildServerUrl;
  }
}
 
Example 5
Source File: MattermostNotifier.java    From jenkins-mattermost-plugin with MIT License 5 votes vote down vote up
public String getBuildServerUrl() {
  if (buildServerUrl == null || buildServerUrl.equals("")) {
    // Note: current code should no longer use "new JenkinsLocationConfiguration()"
    // as only one instance per runtime is really supported by the current core.
    JenkinsLocationConfiguration jenkinsConfig = JenkinsLocationConfiguration.get();
    return jenkinsConfig.getUrl();
  } else {
    return buildServerUrl;
  }
}
 
Example 6
Source File: SetCommitStatusExecution.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Override
protected Void run() throws Exception {
    // Figure out which GitHub repository to send the request to
    checkArgument(nonNull(config.getState()), "Missing required parameter 'state'");

    GHRepository repository = resolveRepository();
    String statusContext = resolveContext();

    final GitHubPRCause cause = run.getCause(GitHubPRCause.class);
    if (isNull(cause)) {
        throw new AbortException(FUNC_NAME + " requires run to be triggered by GitHub Pull Request");
    }

    // Update the commit status
    log.getLogger().printf("Setting pull request status %s to %s with message: %s%n",
            statusContext,
            config.getState(),
            config.getMessage()
    );
    String buildUrl = null;
    final JenkinsLocationConfiguration globalConfig = JenkinsLocationConfiguration.get();
    if (nonNull(globalConfig)) {
        buildUrl = globalConfig.getUrl();
    }
    if (isEmpty(buildUrl)) {
        log.error("Jenkins Location is not configured in system settings. Cannot create a 'details' link.");
        buildUrl = null;
    } else {
        buildUrl += run.getUrl();
    }

    repository.createCommitStatus(cause.getHeadSha(),
            config.getState(),
            buildUrl,
            config.getMessage(),
            statusContext);
    return null;
}
 
Example 7
Source File: ECSCloud.java    From amazon-ecs-plugin with MIT License 5 votes vote down vote up
@DataBoundSetter
public void setJenkinsUrl(String jenkinsUrl) {
    if(StringUtils.isNotBlank(jenkinsUrl)) {
        this.jenkinsUrl = jenkinsUrl;
    } else {
        JenkinsLocationConfiguration config = JenkinsLocationConfiguration.get();
        if (config != null) {
            this.jenkinsUrl = config.getUrl();
        }
    }
}
 
Example 8
Source File: GiteaWebhookListener.java    From gitea-plugin with MIT License 4 votes vote down vote up
public static void register(SCMNavigatorOwner owner, GiteaSCMNavigator navigator,
                            WebhookRegistration mode, String credentialsId) {
    StandardCredentials credentials;
    String serverUrl = navigator.getServerUrl();
    switch (mode) {
        case SYSTEM:
            GiteaServer server = GiteaServers.get().findServer(serverUrl);
            if (server == null || !server.isManageHooks()) {
                return;
            }
            credentials = server.credentials();
            break;
        case ITEM:
            credentials = navigator.credentials(owner);
            break;
        case DISABLE:
        default:
            return;
    }
    if (credentials == null) {
        return;
    }
    JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
    String rootUrl = locationConfiguration.getUrl();
    if (StringUtils.isBlank(rootUrl) || rootUrl.startsWith("http://localhost:")) {
        // Jenkins URL not configured, can't register hooks
        LOGGER.log(Level.FINE, "JENKINS_URL is not defined. Cannot register a WebHook.");
        return;
    }
    String hookUrl =
            UriTemplate.buildFromTemplate(rootUrl).literal("gitea-webhook").literal("/post").build().expand();
    try (GiteaConnection c = connect(serverUrl, credentials)) {
        GiteaUser user = c.fetchUser(navigator.getRepoOwner());
        if (StringUtils.isNotBlank(user.getEmail())) {
            // it's a user not an org
            return;
        }
        GiteaOrganization org = c.fetchOrganization(navigator.getRepoOwner());
        if (org == null) {
            return;
        }
        List<GiteaHook> hooks = c.fetchHooks(org);
        GiteaHook hook = null;
        for (GiteaHook h : hooks) {
            if (hookUrl.equals(h.getConfig().getUrl())) {
                if (hook == null
                        && h.getType() == GiteaHookType.GITEA
                        && h.getConfig().getContentType() == GiteaPayloadType.JSON
                        && h.isActive()
                        && EnumSet.allOf(GiteaEventType.class).equals(h.getEvents())) {
                    hook = h;
                } else {
                    c.deleteHook(org, h);
                }
            }
        }
        if (hook == null) {
            hook = new GiteaHook();
            GiteaHook.Configuration configuration = new GiteaHook.Configuration();
            configuration.setContentType(GiteaPayloadType.JSON);
            configuration.setUrl(hookUrl);
            hook.setType(GiteaHookType.GITEA);
            hook.setConfig(configuration);
            hook.setEvents(EnumSet.allOf(GiteaEventType.class));
            hook.setActive(true);
            c.createHook(org, hook);
        }
    } catch (IOException | InterruptedException e) {
        LOGGER.log(Level.WARNING,
                "Could not manage organization hooks for " + navigator.getRepoOwner() + " on " + serverUrl, e);
    }
}
 
Example 9
Source File: GiteaWebhookListener.java    From gitea-plugin with MIT License 4 votes vote down vote up
public static void register(SCMSourceOwner owner, GiteaSCMSource source,
                            WebhookRegistration mode, String credentialsId) {
    StandardCredentials credentials;
    String serverUrl = source.getServerUrl();
    switch (mode) {
        case SYSTEM:
            GiteaServer server = GiteaServers.get().findServer(serverUrl);
            if (server == null || !server.isManageHooks()) {
                return;
            }
            credentials = server.credentials();
            break;
        case ITEM:
            credentials = source.credentials();
            break;
        case DISABLE:
        default:
            return;
    }
    if (credentials == null) {
        return;
    }
    JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
    String rootUrl = locationConfiguration.getUrl();
    if (StringUtils.isBlank(rootUrl) || rootUrl.startsWith("http://localhost:")) {
        // Jenkins URL not configured, can't register hooks
        LOGGER.log(Level.FINE, "JENKINS_URL is not defined. Cannot register a WebHook.");
        return;
    }
    String hookUrl =
            UriTemplate.buildFromTemplate(rootUrl).literal("gitea-webhook").literal("/post").build().expand();
    try (GiteaConnection c = connect(serverUrl, credentials)) {
        GiteaRepository repo = c.fetchRepository(source.getRepoOwner(), source.getRepository());
        if (repo == null) {
            return;
        }
        List<GiteaHook> hooks = c.fetchHooks(repo);
        GiteaHook hook = null;
        for (GiteaHook h : hooks) {
            if (hookUrl.equals(h.getConfig().getUrl())) {
                if (hook == null
                        && h.getType() == GiteaHookType.GITEA
                        && h.getConfig().getContentType() == GiteaPayloadType.JSON
                        && h.isActive()
                        && EnumSet.allOf(GiteaEventType.class).equals(h.getEvents())) {
                    hook = h;
                } else {
                    c.deleteHook(repo, h);
                }
            }
        }
        if (hook == null) {
            hook = new GiteaHook();
            GiteaHook.Configuration configuration = new GiteaHook.Configuration();
            configuration.setContentType(GiteaPayloadType.JSON);
            configuration.setUrl(hookUrl);
            hook.setType(GiteaHookType.GITEA);
            hook.setConfig(configuration);
            hook.setEvents(EnumSet.allOf(GiteaEventType.class));
            hook.setActive(true);
            c.createHook(repo, hook);
        }
    } catch (IOException | InterruptedException e) {
        LOGGER.log(Level.WARNING,
                "Could not manage repository hooks for " + source.getRepoOwner() + "/" + source.getRepository()
                        + " on " + serverUrl, e);
    }

}