Add WithConsoleUrl interface

This simplifies the logic of `Functions#getConsoleUrl` and makes it pluggable.
This commit is contained in:
Vincent Latombe committed 2024-08-22 15:33:03 +02:00
1 parent 0c31a3ac96
commit 39d30c8828
5 files changed
+84 -16

No files matched your search

+6 -8
View File
@@ -54,7 +54,6 @@ import hudson.model.PaneStatusProperties;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterDefinition.ParameterDescriptor;
import hudson.model.PasswordParameterDefinition;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.Slave;
import hudson.model.TimeZoneProperty;
@@ -153,6 +152,7 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jenkins.console.ConsoleUrlProvider;
import jenkins.console.WithConsoleUrl;
import jenkins.model.GlobalConfiguration;
import jenkins.model.GlobalConfigurationCategory;
import jenkins.model.Jenkins;
@@ -1912,14 +1912,12 @@ public class Functions {
* @return the absolute URL for accessing the build console for the executable, or null if there is no build associated with the executable
* @since 2.433
*/
public static @CheckForNull String getConsoleUrl(Queue.Executable executable) {
if (executable == null) {
return null;
} else if (executable instanceof Run) {
return ConsoleUrlProvider.getRedirectUrl((Run<?, ?>) executable);
public static @CheckForNull String getConsoleUrl(Object executable) {
if (executable instanceof WithConsoleUrl) {
String consoleUrl = ((WithConsoleUrl) executable).getConsoleUrl();
return consoleUrl != null ? Stapler.getCurrentRequest().getContextPath() + '/' + consoleUrl : null;
} else {
// Handles cases such as PlaceholderExecutable for Pipeline node steps.
return getConsoleUrl(executable.getParentExecutable());
return null;
}
}