Skip to content

Commit

Permalink
[JENKINS-64119] Display URL Plugin does not use user prefered impleme…
Browse files Browse the repository at this point in the history
…ntations (#42)

* [JENKINS-64119] Display URL Plugin does not use user prefered implementations

Signed-off-by: olivier lamy <[email protected]>

* Update src/main/java/org/jenkinsci/plugins/displayurlapi/actions/AbstractDisplayAction.java

Co-authored-by: Carroll Chiou <[email protected]>

* use ACL to switch current user

Signed-off-by: olivier lamy <[email protected]>

Co-authored-by: Carroll Chiou <[email protected]>
  • Loading branch information
olamy and car-roll authored Nov 18, 2020
1 parent 8a5092a commit d259005
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 12 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<connection>scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git</connection>
<developerConnection>scm:git:[email protected]:jenkinsci/${project.artifactId}-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>
<tag>display-url-api-2.3.3</tag>
</scm>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.jenkinsci.plugins.displayurlapi;

import com.google.common.annotations.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.Util;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.User;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.displayurlapi.actions.AbstractDisplayAction;
import org.jenkinsci.plugins.displayurlapi.user.PreferredProviderUserProperty;

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

Expand All @@ -26,7 +29,8 @@ public abstract class DisplayURLProvider implements ExtensionPoint {
* @return DisplayURLProvider
*/
public static DisplayURLProvider get() {
return DisplayURLProviderImpl.INSTANCE;
DisplayURLProvider preferredProvider = getPreferredProvider();
return preferredProvider != null ? preferredProvider : DisplayURLProviderImpl.INSTANCE;
}

/**
Expand All @@ -52,7 +56,7 @@ public static DisplayURLProvider getDefault() {
*/
@NonNull
public String getRoot() {
String root = Jenkins.getInstance().getRootUrl();
String root = Jenkins.get().getRootUrl();
if (root == null) {
root = "http://unconfigured-jenkins-location/";
}
Expand Down Expand Up @@ -181,13 +185,24 @@ private static String findClass() {

@Nullable
public static DisplayURLProvider getPreferredProvider() {
PreferredProviderUserProperty prefProperty = getUserPreferredProviderProperty();

if (prefProperty != null && prefProperty.getConfiguredProvider() != null) {
return prefProperty.getConfiguredProvider();
}
String clazz = findClass();
if (isNotEmpty(clazz)) {
return ExtensionList.lookup(DisplayURLProvider.class).getDynamic(clazz);
}
return null;
}

@Nullable
public static PreferredProviderUserProperty getUserPreferredProviderProperty() {
User user = User.current();
return (user == null) ? null : user.getProperty(PreferredProviderUserProperty.class);
}

private static final String JENKINS_DISPLAYURL_PROVIDER_ENV = "JENKINS_DISPLAYURL_PROVIDER";
private static final String JENKINS_DISPLAYURL_PROVIDER_PROP = "jenkins.displayurl.provider";
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ DisplayURLProvider lookupProvider() {
}

@VisibleForTesting
@Deprecated
/**
* @deprecated use {@link DisplayURLProvider#getUserPReferredProviderProperty}
*/
protected PreferredProviderUserProperty getUserPreferredProviderProperty() {
User user = User.current();
return (user == null) ? null : user.getProperty(PreferredProviderUserProperty.class);
return DisplayURLProvider.getUserPreferredProviderProperty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.ExtensionList;
import hudson.PluginWrapper;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.Run;
import java.util.HashMap;
import java.util.Map;
import hudson.model.User;
import hudson.security.ACL;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.displayurlapi.user.PreferredProviderUserProperty;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.MockFolder;
import org.jvnet.hudson.test.TestExtension;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class DisplayURLProviderTest {
Expand Down Expand Up @@ -48,6 +54,162 @@ public void urls() throws Exception {
assertEquals(DisplayURLProvider.get().getJobURL(project), environment.get("JOB_DISPLAY_URL"));
}

@TestExtension
public static class TestSysPropDisplayURLProvider extends DisplayURLProvider
{

public static final String EXTRA_CONTENT_IN_URL = "syspropsdefinedprovider";

private DisplayURLProvider getRootProvider()
{
return ExtensionList.lookup( DisplayURLProvider.class ).getDynamic(
ClassicDisplayURLProvider.class.getName() );
}

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

@Override
public String getArtifactsURL( Run<?, ?> run )
{
return getRootProvider().getArtifactsURL( run ) + "/" + EXTRA_CONTENT_IN_URL;
}

@Override
public String getChangesURL( Run<?, ?> run )
{
return getRootProvider().getChangesURL( run ) + "/" + EXTRA_CONTENT_IN_URL;
}

@Override
public String getTestsURL( Run<?, ?> run )
{
return getRootProvider().getTestsURL( run ) + "/" + EXTRA_CONTENT_IN_URL;
}

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

@Test
public void urlsWithSysPropProvider() throws Exception {
System.setProperty("jenkins.displayurl.provider",
TestSysPropDisplayURLProvider.class.getName());
MockFolder folder = rule.createFolder("my folder");
FreeStyleProject project = (FreeStyleProject) folder
.createProject(rule.jenkins.getDescriptorByType(FreeStyleProject.DescriptorImpl.class), "my job",
false);
Run<?, ?> run = project.scheduleBuild2(0).get();

String root = DisplayURLProvider.get().getRoot();
assertEquals("http://localhost:" + rule.getLocalPort() + "/jenkins/", root);
assertEquals(root + "job/my%20folder/job/my%20job/1/" + TestSysPropDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getRunURL(run));
assertEquals(root + "job/my%20folder/job/my%20job/" + TestSysPropDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getJobURL(project));
assertEquals(root + "job/my%20folder/job/my%20job/1/artifact/" + TestSysPropDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getArtifactsURL(run));
assertEquals(root + "job/my%20folder/job/my%20job/changes/" + TestSysPropDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getChangesURL(run));
assertEquals(root + "job/my%20folder/job/my%20job/1/testReport/" + TestSysPropDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getTestsURL(run));

EnvVars environment = run.getEnvironment();
assertEquals(DisplayURLProvider.get().getRunURL(run), environment.get("RUN_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getArtifactsURL(run), environment.get("RUN_ARTIFACTS_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getChangesURL(run), environment.get("RUN_CHANGES_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getTestsURL(run), environment.get("RUN_TESTS_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getJobURL(project), environment.get("JOB_DISPLAY_URL"));
System.setProperty("jenkins.displayurl.provider", "");
}

@TestExtension
public static class TestUserDisplayURLProvider extends DisplayURLProvider
{

public static final String EXTRA_CONTENT_IN_URL = "userdefinedprovider";

private DisplayURLProvider getRootProvider()
{
return ExtensionList.lookup( DisplayURLProvider.class ).getDynamic(
ClassicDisplayURLProvider.class.getName() );
}

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

@Override
public String getArtifactsURL( Run<?, ?> run )
{
return getRootProvider().getArtifactsURL( run ) + "/" + EXTRA_CONTENT_IN_URL;
}

@Override
public String getChangesURL( Run<?, ?> run )
{
return getRootProvider().getChangesURL( run ) + "/" + EXTRA_CONTENT_IN_URL;
}

@Override
public String getTestsURL( Run<?, ?> run )
{
return getRootProvider().getTestsURL( run ) + "/" + EXTRA_CONTENT_IN_URL;
}

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

@Test
public void urlsWithUserDefinedProvider() throws Exception {
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
User foo = User.getById("foo", true);

PreferredProviderUserProperty userProperty =
new PreferredProviderUserProperty(TestUserDisplayURLProvider.class.getName());
foo.addProperty(userProperty);
ACL.as(foo);

MockFolder folder = rule.createFolder("my folder");
FreeStyleProject project = (FreeStyleProject) folder
.createProject(rule.jenkins.getDescriptorByType(FreeStyleProject.DescriptorImpl.class), "my job",
false);
Run<?, ?> run = project.scheduleBuild2(0).get();

String root = DisplayURLProvider.get().getRoot();
assertEquals("http://localhost:" + rule.getLocalPort() + "/jenkins/", root);
assertEquals(root + "job/my%20folder/job/my%20job/1/" + TestUserDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getRunURL(run));
assertEquals(root + "job/my%20folder/job/my%20job/" + TestUserDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getJobURL(project));
assertEquals(root + "job/my%20folder/job/my%20job/1/artifact/" + TestUserDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getArtifactsURL(run));
assertEquals(root + "job/my%20folder/job/my%20job/changes/" + TestUserDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getChangesURL(run));
assertEquals(root + "job/my%20folder/job/my%20job/1/testReport/" + TestUserDisplayURLProvider.EXTRA_CONTENT_IN_URL,
DisplayURLProvider.get().getTestsURL(run));

EnvVars environment = run.getEnvironment();
assertEquals(DisplayURLProvider.get().getRunURL(run), environment.get("RUN_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getArtifactsURL(run), environment.get("RUN_ARTIFACTS_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getChangesURL(run), environment.get("RUN_CHANGES_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getTestsURL(run), environment.get("RUN_TESTS_DISPLAY_URL"));
assertEquals(DisplayURLProvider.get().getJobURL(project), environment.get("JOB_DISPLAY_URL"));
}


@Test
public void decoration() throws Exception {
MockFolder folder = rule.createFolder("my folder");
Expand All @@ -71,9 +233,8 @@ public void decoration() throws Exception {
assertEquals(root + "job/my%20folder/job/my%20job/1/display/redirect?page=tests&utm_campaign=jenkins"
+ "&utm_source=Jenkins&utm_term=my+folder%2Fmy+job%231",
DisplayURLProvider.get().getTestsURL(run));
DisplayURLContext ctx = DisplayURLContext.open();
try {
ctx.plugin(Jenkins.getActiveInstance().getPluginManager().getPlugin("display-url-api"));
try (DisplayURLContext ctx = DisplayURLContext.open()) {
ctx.plugin(Jenkins.get().getPluginManager().getPlugin("display-url-api"));
assertEquals(
root
+ "job/my%20folder/job/my%20job/1/display/redirect?utm_campaign=display-url-api"
Expand All @@ -91,8 +252,6 @@ public void decoration() throws Exception {
assertEquals(root + "job/my%20folder/job/my%20job/1/display/redirect?page=tests&utm_campaign=display"
+ "-url-api&utm_source=Jenkins&utm_term=my+folder%2Fmy+job%231",
DisplayURLProvider.get().getTestsURL(run));
} finally {
ctx.close();
}

EnvVars environment = run.getEnvironment();
Expand Down

0 comments on commit d259005

Please sign in to comment.