Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JENKINS-40207 - Uninstalling alternative display url providers should…
… keep issued URLs the same (#4)
- Loading branch information
James William Dumay
committed
Dec 9, 2016
1 parent
638719d
commit ffadcca
Showing
11 changed files
with
486 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/org/jenkinsci/plugins/displayurlapi/ClassicDisplayURLProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.jenkinsci.plugins.displayurlapi; | ||
|
||
import hudson.Extension; | ||
import hudson.Util; | ||
import hudson.model.Job; | ||
import hudson.model.Run; | ||
import hudson.tasks.junit.TestResult; | ||
import hudson.tasks.test.AbstractTestResultAction; | ||
import hudson.tasks.test.TestObject; | ||
|
||
/** | ||
* Display URL Provider for the Classical Jenkins UI | ||
*/ | ||
@Extension | ||
public class ClassicDisplayURLProvider extends DisplayURLProvider { | ||
@Override | ||
public String getRunURL(Run<?, ?> run) { | ||
return getRoot() + Util.encode(run.getUrl()); | ||
} | ||
|
||
@Override | ||
public String getChangesURL(Run<?, ?> run) { | ||
return getJobURL(run.getParent()) + "changes"; | ||
} | ||
|
||
@Override | ||
public String getJobURL(Job<?, ?> job) { | ||
return getRoot() + Util.encode(job.getUrl()); | ||
} | ||
|
||
@Override | ||
public String getTestUrl(hudson.tasks.test.TestResult result) { | ||
String buildUrl = getRunURL(result.getRun()); | ||
AbstractTestResultAction action = result.getTestResultAction(); | ||
|
||
TestObject parent = result.getParent(); | ||
TestResult testResultRoot = null; | ||
while(parent != null) { | ||
if (parent instanceof TestResult) { | ||
testResultRoot = (TestResult) parent; | ||
break; | ||
} | ||
parent = parent.getParent(); | ||
} | ||
|
||
String testUrl = action.getUrlName() | ||
+ (testResultRoot != null ? testResultRoot.getUrl() : "") | ||
+ result.getUrl(); | ||
|
||
String[] pathComponents = testUrl.split("/"); | ||
StringBuilder buf = new StringBuilder(); | ||
for (String c : pathComponents) { | ||
buf.append(Util.rawEncode(c)).append('/'); | ||
} | ||
// remove last / | ||
buf.deleteCharAt(buf.length() - 1); | ||
|
||
return buildUrl + buf.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/jenkinsci/plugins/displayurlapi/actions/AbstractDisplayAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.jenkinsci.plugins.displayurlapi.actions; | ||
|
||
import com.google.common.base.Predicates; | ||
import com.google.common.collect.Iterables; | ||
import hudson.model.Action; | ||
import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider; | ||
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
public abstract class AbstractDisplayAction implements Action { | ||
|
||
public static final String URL_NAME = "display"; | ||
|
||
@Override | ||
public String getIconFileName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getUrlName() { | ||
return URL_NAME; | ||
} | ||
|
||
public final Object doRedirect(StaplerRequest req, StaplerResponse rsp) throws IOException { | ||
DisplayURLProvider provider = lookupProvider(); | ||
rsp.sendRedirect(HttpServletResponse.SC_MOVED_TEMPORARILY, getRedirectURL(provider)); | ||
return null; | ||
} | ||
|
||
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/org/jenkinsci/plugins/displayurlapi/actions/JobDisplayAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.jenkinsci.plugins.displayurlapi.actions; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import hudson.Extension; | ||
import hudson.model.Action; | ||
import hudson.model.Job; | ||
import jenkins.model.TransientActionFactory; | ||
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Collection; | ||
|
||
public class JobDisplayAction extends AbstractDisplayAction { | ||
|
||
private final Job job; | ||
|
||
JobDisplayAction(Job job) { | ||
this.job = job; | ||
} | ||
|
||
protected String getRedirectURL(DisplayURLProvider provider) { | ||
return provider.getJobURL(job); | ||
} | ||
|
||
@Extension | ||
public static class TransientActionFactoryImpl extends TransientActionFactory { | ||
@Override | ||
public Class type() { | ||
return Job.class; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public Collection<? extends Action> createFor(@Nonnull Object target) { | ||
return ImmutableList.of(new JobDisplayAction((Job) target)); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/org/jenkinsci/plugins/displayurlapi/actions/RunDisplayAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.jenkinsci.plugins.displayurlapi.actions; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import hudson.Extension; | ||
import hudson.model.Action; | ||
import hudson.model.Run; | ||
import hudson.tasks.test.AbstractTestResultAction; | ||
import hudson.tasks.test.TestResult; | ||
import jenkins.model.TransientActionFactory; | ||
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; | ||
import org.kohsuke.stapler.Stapler; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Collection; | ||
|
||
public class RunDisplayAction extends AbstractDisplayAction { | ||
private final Run run; | ||
|
||
RunDisplayAction(Run run) { | ||
this.run = run; | ||
} | ||
|
||
@Override | ||
protected String getRedirectURL(DisplayURLProvider provider) { | ||
StaplerRequest req = Stapler.getCurrentRequest(); | ||
String page = req.getParameter("page"); | ||
String url; | ||
if ("changes".equals(page)) { | ||
url = provider.getChangesURL(run); | ||
} else if ("test".equals(page)) { | ||
String id = req.getParameter("id"); | ||
if (id == null) { | ||
throw new IllegalArgumentException("id parameter not specified"); | ||
} | ||
AbstractTestResultAction action = run.getAction(AbstractTestResultAction.class); | ||
if (action == null) { | ||
throw new IllegalStateException("No AbstractTestResultAction on this run"); | ||
} | ||
TestResult result = action.findCorrespondingResult(id); | ||
url = provider.getTestUrl(result); | ||
} else { | ||
url = provider.getRunURL(run); | ||
} | ||
return url; | ||
} | ||
|
||
@Extension | ||
public static class TransientActionFactoryImpl extends TransientActionFactory { | ||
@Override | ||
public Class type() { | ||
return Run.class; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public Collection<? extends Action> createFor(@Nonnull Object target) { | ||
return ImmutableList.of(new RunDisplayAction((Run) target)); | ||
} | ||
} | ||
} |
Oops, something went wrong.