Skip to content
Permalink
Browse files

JENKINS-39580 Allow the user to configure a preferred provider (#5)

  • Loading branch information
i386 committed Dec 9, 2016
1 parent ffadcca commit 4f280c227750f37d6c8be8c5532c9701758c55ad
@@ -13,6 +13,12 @@
*/
@Extension
public class ClassicDisplayURLProvider extends DisplayURLProvider {

@Override
public String getDisplayName() {
return Messages.classic_name();
}

@Override
public String getRunURL(Run<?, ?> run) {
return getRoot() + Util.encode(run.getUrl());
@@ -1,5 +1,7 @@
package org.jenkinsci.plugins.displayurlapi;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import hudson.ExtensionPoint;
import hudson.Util;
import hudson.model.Job;
@@ -24,6 +26,10 @@ public static DisplayURLProvider get() {
return getJenkins().getExtensionList(DisplayURLProvider.class);
}

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

/** Fully qualified URL for the Root display URL */
public String getRoot() {
String root = getJenkins().getRootUrl();
@@ -33,6 +39,11 @@ public String getRoot() {
return Util.encode(root);
}

/** Display name of this provider e.g. "Jenkins Classic", "Blue Ocean", etc */
public String getDisplayName() {
return this.getClass().getSimpleName();
}

/** Fully qualified URL for a Run */
public abstract String getRunURL(Run<?, ?> run);

@@ -3,8 +3,10 @@
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import hudson.model.Action;
import hudson.model.User;
import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.jenkinsci.plugins.displayurlapi.user.PreferredProviderUserProperty;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

@@ -39,9 +41,13 @@ public final Object doRedirect(StaplerRequest req, StaplerResponse rsp) throws I
protected abstract String getRedirectURL(DisplayURLProvider provider);

DisplayURLProvider lookupProvider() {
Iterable<DisplayURLProvider> all = DisplayURLProvider.all();
DisplayURLProvider defaultProvider = Iterables.find(all, Predicates.instanceOf(ClassicDisplayURLProvider.class));
Iterable<DisplayURLProvider> availableProviders = Iterables.filter(all, Predicates.not(Predicates.instanceOf(ClassicDisplayURLProvider.class)));
return Iterables.getFirst(availableProviders, defaultProvider);
User current = User.current();
DisplayURLProvider provider = current.getProperty(PreferredProviderUserProperty.class).getConfiguredProvider();
if (provider == null) {
Iterable<DisplayURLProvider> all = DisplayURLProvider.all();
Iterable<DisplayURLProvider> availableProviders = Iterables.filter(all, Predicates.not(Predicates.instanceOf(ClassicDisplayURLProvider.class)));
provider = Iterables.getFirst(availableProviders, DisplayURLProvider.getDefault());
}
return provider;
}
}
@@ -0,0 +1,78 @@
package org.jenkinsci.plugins.displayurlapi.user;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import hudson.Extension;
import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nullable;
import java.util.List;

public class PreferredProviderUserProperty extends UserProperty {

@Extension
public static final UserPropertyDescriptor DESCRIPTOR = new PreferredProviderUserPropertyDescriptor();

@Nullable
private String providerId;

@DataBoundConstructor
public PreferredProviderUserProperty(@Nullable String providerId) {
this.providerId = providerId;
}

public ProviderOption getProvider() {
final DisplayURLProvider provider = getConfiguredProvider();
return provider == null ? ProviderOption.DEFAULT_OPTION : new ProviderOption(provider.getClass().getName(), provider.getDisplayName());
}

public DisplayURLProvider getConfiguredProvider() {
return Iterables.find(DisplayURLProvider.all(), new Predicate<DisplayURLProvider>() {
@Override
public boolean apply(DisplayURLProvider input) {
return input.getClass().getName().equals(providerId);
}
}, null);
}

public List<ProviderOption> getAll() {
Iterable<ProviderOption> options = Iterables.transform(DisplayURLProvider.all(), new Function<DisplayURLProvider, ProviderOption>() {
@Override
public ProviderOption apply(DisplayURLProvider input) {
return new ProviderOption(input.getClass().getName(), input.getDisplayName());
}
});
return ImmutableList.copyOf(Iterables.concat(Lists.newArrayList(ProviderOption.DEFAULT_OPTION), options));
}

public boolean isSelected(String providerId) {
return getProvider().getId().equals(providerId);
}

public static class ProviderOption {

public static final ProviderOption DEFAULT_OPTION = new ProviderOption("default", "Default");

private final String id;
private final String name;

public ProviderOption(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public String getName() {
return name;
}
}
}
@@ -0,0 +1,24 @@
package org.jenkinsci.plugins.displayurlapi.user;

import hudson.model.User;
import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;
import org.jenkinsci.plugins.displayurlapi.Messages;
import org.jenkinsci.plugins.displayurlapi.user.PreferredProviderUserProperty.ProviderOption;

public class PreferredProviderUserPropertyDescriptor extends UserPropertyDescriptor {

public PreferredProviderUserPropertyDescriptor() {
super(PreferredProviderUserProperty.class);
}

@Override
public UserProperty newInstance(User user) {
return new PreferredProviderUserProperty(ProviderOption.DEFAULT_OPTION.getId());
}

@Override
public String getDisplayName() {
return Messages.display_url();
}
}
@@ -0,0 +1,2 @@
display.url = Notification URL
classic.name = Jenkins Classic
@@ -0,0 +1,20 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:block>
<f:entry name="providerId" field="providerId">
<select name="providerId">
<j:forEach var="provider" items="${instance.all}">
<j:choose>
<j:when test="${instance.isSelected(provider.getId())}">
<option value="${provider.getId()}" selected="true">${provider.getName()}</option>
</j:when>
<j:otherwise>
<option value="${provider.getId()}">${provider.getName()}</option>
</j:otherwise>
</j:choose>
</j:forEach>
</select>
</f:entry>
</f:block>
</j:jelly>
@@ -0,0 +1 @@
Allows the user to select their preferred user interface when clicking links to Jenkins from notifications (e.g. Email, Slack or Github)
@@ -6,7 +6,6 @@
import hudson.model.Run;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResult;
import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.junit.Test;
import org.jvnet.hudson.test.TestExtension;
@@ -83,28 +82,24 @@ protected DisplayURLProvider getRedirectedProvider() {

public static final String EXTRA_CONTENT_IN_URL = "another";

DisplayURLProvider getClassicProvider() {
return Iterables.find(DisplayURLProvider.all(), Predicates.instanceOf(ClassicDisplayURLProvider.class));
}

@Override
public String getRunURL(Run<?, ?> run) {
return getClassicProvider().getRunURL(run) + EXTRA_CONTENT_IN_URL;
return DisplayURLProvider.getDefault().getRunURL(run) + EXTRA_CONTENT_IN_URL;
}

@Override
public String getChangesURL(Run<?, ?> run) {
return getClassicProvider().getChangesURL(run) + EXTRA_CONTENT_IN_URL;
return DisplayURLProvider.getDefault().getChangesURL(run) + EXTRA_CONTENT_IN_URL;
}

@Override
public String getJobURL(Job<?, ?> project) {
return getClassicProvider().getJobURL(project) + EXTRA_CONTENT_IN_URL;
return DisplayURLProvider.getDefault().getJobURL(project) + EXTRA_CONTENT_IN_URL;
}

@Override
public String getTestUrl(hudson.tasks.test.TestResult result) {
return getClassicProvider().getTestUrl(result) + EXTRA_CONTENT_IN_URL;
return DisplayURLProvider.getDefault().getTestUrl(result) + EXTRA_CONTENT_IN_URL;
}
}
}

0 comments on commit 4f280c2

Please sign in to comment.