Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-41578 Add a system property/environment variable to specify d… #7

Merged
merged 1 commit into from
Jan 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.jenkinsci.plugins.displayurlapi;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import hudson.ExtensionPoint;
import hudson.Util;
import hudson.model.Job;
import hudson.model.Run;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.displayurlapi.actions.AbstractDisplayAction;

import static org.apache.commons.lang.StringUtils.isNotEmpty;

/**
* Generates URLs for well known UI locations for use in notifications (e.g. mailer, HipChat, Slack, IRC, etc)
* Extensible to allow plugins to override common URLs (e.g. Blue Ocean or another future secondary UI)
Expand All @@ -27,7 +31,20 @@ public static Iterable<DisplayURLProvider> all() {
}

public static DisplayURLProvider getDefault() {
return Iterables.find(all(), Predicates.instanceOf(ClassicDisplayURLProvider.class));
DisplayURLProvider defaultProvider = Iterables.find(all(), Predicates.instanceOf(ClassicDisplayURLProvider.class));

// Get the default provider from environment variable or system property
final String clazz = findClass();
if (isNotEmpty(clazz)) {
defaultProvider = Iterables.find(DisplayURLProvider.all(), new Predicate<DisplayURLProvider>() {
@Override
public boolean apply(DisplayURLProvider input) {
return input.getClass().getName().equals(clazz);
}
});
}

return defaultProvider;
}

/** Fully qualified URL for the Root display URL */
Expand Down Expand Up @@ -84,11 +101,22 @@ public String getTestUrl(hudson.tasks.test.TestResult result) {
}
}

private static String findClass() {
String clazz = System.getenv(JENKINS_DISPLAYURL_PROVIDER_ENV);
if (StringUtils.isEmpty(clazz)) {
clazz = System.getProperty(JENKINS_DISPLAYURL_PROVIDER_PROP);
}
return clazz;
}

private static Jenkins getJenkins() {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null) {
throw new IllegalStateException("Jenkins has not started");
}
return jenkins;
}

private static final String JENKINS_DISPLAYURL_PROVIDER_ENV = "JENKINS_DISPLAYURL_PROVIDER";
private static final String JENKINS_DISPLAYURL_PROVIDER_PROP = "jenkins.displayurl.provider";
}