diff --git a/src/main/java/org/jenkinsci/plugins/scriptler/NodeNames.java b/src/main/java/org/jenkinsci/plugins/scriptler/NodeNames.java new file mode 100644 index 00000000..18e74d20 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/scriptler/NodeNames.java @@ -0,0 +1,36 @@ +package org.jenkinsci.plugins.scriptler; + +import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public final class NodeNames { + public static final String BUILT_IN = "(built-in)"; + public static final String ALL = "(all)"; + public static final String ALL_AGENTS = "(all agents)"; + private static final Map DEPRECATED_ALIASES; + + static { + Map> deprecatedNames = + Map.of(BUILT_IN, List.of("(master)", "(controller)"), ALL_AGENTS, List.of("(all slaves)")); + + Map aliases = new HashMap<>(); + deprecatedNames.forEach( + (newName, oldNames) -> oldNames.forEach(oldName -> aliases.put(normalizeName(oldName), newName))); + + DEPRECATED_ALIASES = Map.copyOf(aliases); + } + + @NonNull + private static String normalizeName(@NonNull String name) { + return name.toLowerCase(); + } + + @NonNull + public static String normalizeNodeName(@NonNull String nodeName) { + return DEPRECATED_ALIASES.getOrDefault(normalizeName(nodeName), nodeName); + } + + private NodeNames() {} +} diff --git a/src/main/java/org/jenkinsci/plugins/scriptler/ScriptlerManagement.java b/src/main/java/org/jenkinsci/plugins/scriptler/ScriptlerManagement.java index 0e596f1e..02e92e7e 100644 --- a/src/main/java/org/jenkinsci/plugins/scriptler/ScriptlerManagement.java +++ b/src/main/java/org/jenkinsci/plugins/scriptler/ScriptlerManagement.java @@ -76,11 +76,6 @@ public class ScriptlerManagement extends ManagementLink implements RootAction { private static final String NOT_APPROVED_YET = "notApprovedYet"; private static final String CAN_BYPASS_APPROVAL = "canByPassScriptApproval"; private static final String SCRIPT = "script"; - private static final String MASTER = "(master)"; - private static final String CONTROLLER = "(controller)"; - private static final String ALL = "(all)"; - private static final String ALL_SLAVES = "(all slaves)"; - private static final String ALL_AGENTS = "(all agents)"; private static final MarkupFormatter INSTANCE = RawHtmlMarkupFormatter.INSTANCE; @@ -244,8 +239,8 @@ public HttpResponse doDownloadScript( * script code * @param nonAdministerUsing * allow usage in Scriptler build step - * @param onlyController - * this script is only allowed to run on the controller + * @param onlyBuiltIn + * this script is only allowed to run on the built-in node * @param originCatalogName * (optional) the name of the catalog the script is loaded/added from * @param originId @@ -261,7 +256,7 @@ public HttpResponse doScriptAdd( @QueryParameter("comment") String comment, @QueryParameter(SCRIPT) String script, @QueryParameter("nonAdministerUsing") boolean nonAdministerUsing, - @QueryParameter("onlyController") boolean onlyController, + @QueryParameter("onlyBuiltIn") boolean onlyBuiltIn, String originCatalogName, String originId) throws IOException, ServletException { @@ -271,7 +266,7 @@ public HttpResponse doScriptAdd( List parameters = UIHelper.extractParameters(req.getSubmittedForm()); saveScriptAndForward( - id, name, comment, script, nonAdministerUsing, onlyController, originCatalogName, originId, parameters); + id, name, comment, script, nonAdministerUsing, onlyBuiltIn, originCatalogName, originId, parameters); return new HttpRedirect(INDEX); } @@ -286,7 +281,7 @@ private String saveScriptAndForward( String comment, String script, boolean nonAdministerUsing, - boolean onlyController, + boolean onlyBuiltIn, String originCatalogName, String originId, @NonNull List parameters) @@ -330,7 +325,7 @@ private String saveScriptAndForward( parameters); } else { // save (overwrite) the meta information - newScript = new Script(finalFileName, displayName, comment, nonAdministerUsing, parameters, onlyController); + newScript = new Script(finalFileName, displayName, comment, nonAdministerUsing, parameters, onlyBuiltIn); } ScriptlerConfiguration cfg = getConfiguration(); cfg.addOrReplace(newScript); @@ -520,7 +515,7 @@ public void doRunScript(StaplerRequest2 req, StaplerResponse2 rsp, @QueryParamet req.setAttribute(SCRIPT, script); // set default selection - req.setAttribute("currentNode", CONTROLLER); + req.setAttribute("currentNode", NodeNames.BUILT_IN); req.getView(this, "runScript.jelly").forward(req, rsp); } @@ -603,7 +598,7 @@ public void doTriggerScript( * @param script * the script code (groovy) * @param node - * the node, to execute the code on, defaults to {@value #CONTROLLER} + * the node, to execute the code on, defaults to {@value NodeNames#BUILT_IN} * @param contentType * the contentType to use in the response, defaults to text/plain */ @@ -653,7 +648,7 @@ public void doRun( rsp.setContentType(contentType == null ? "text/plain" : contentType); - final List computers = resolveComputerNames(node == null ? CONTROLLER : node); + final List computers = resolveComputerNames(node == null ? NodeNames.BUILT_IN : node); if (computers.size() > 1) { rsp.getOutputStream().print(ScriptHelper.runScript(computers, script, paramArray)); } else { @@ -678,17 +673,14 @@ private Collection prepareParameters(StaplerRequest2 req, Script temp return params.values(); } - private List resolveComputerNames(String nameAlias) { + private List resolveComputerNames(String rawNameAlias) { + final String nameAlias = NodeNames.normalizeNodeName(rawNameAlias); final List computers; - if (nameAlias.equalsIgnoreCase(ALL) - || nameAlias.equalsIgnoreCase(ALL_AGENTS) - || nameAlias.equalsIgnoreCase(ALL_SLAVES)) { + if (nameAlias.equalsIgnoreCase(NodeNames.ALL) || nameAlias.equalsIgnoreCase(NodeNames.ALL_AGENTS)) { computers = getComputerNames(); - if (nameAlias.equalsIgnoreCase(ALL)) { - computers.add(CONTROLLER); + if (nameAlias.equalsIgnoreCase(NodeNames.ALL)) { + computers.add(NodeNames.BUILT_IN); } - } else if (nameAlias.equalsIgnoreCase(MASTER)) { - computers = List.of(CONTROLLER); } else { computers = List.of(nameAlias); } @@ -765,18 +757,18 @@ public List getSlaveAlias(Script script) { * @return list with all computer names */ public List getComputerAliases(Script script) { - if (script.onlyController) { - return List.of(CONTROLLER); + if (script.onlyBuiltIn) { + return List.of(NodeNames.BUILT_IN); } final List computerNames = getComputerNames(); - // add 'magic' name for the controller, so all nodes can be handled the same way - computerNames.addAll(0, List.of(CONTROLLER, ALL, ALL_AGENTS)); + // add 'magic' name for the built-in node, so all nodes can be handled the same way + computerNames.addAll(0, List.of(NodeNames.BUILT_IN, NodeNames.ALL, NodeNames.ALL_AGENTS)); return computerNames; } private List getComputerNames() { return Arrays.stream(Jenkins.get().getComputers()) - // remove the controller's computer as it has an empty name + // remove the built-in's computer as it has an empty name .filter(Predicate.not(Jenkins.MasterComputer.class::isInstance)) .map(Computer::getName) .collect(Collectors.toCollection(ArrayList::new)); diff --git a/src/main/java/org/jenkinsci/plugins/scriptler/builder/ScriptlerBuilder.java b/src/main/java/org/jenkinsci/plugins/scriptler/builder/ScriptlerBuilder.java index 8c989531..da020838 100644 --- a/src/main/java/org/jenkinsci/plugins/scriptler/builder/ScriptlerBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/scriptler/builder/ScriptlerBuilder.java @@ -255,8 +255,8 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen parameter.getName(), TokenMacro.expandAll(build, listener, parameter.getValue()))); } final Object output; - if (script.onlyController) { - // When run on controller, make build, launcher, listener available to script + if (script.onlyBuiltIn) { + // When run on the built-in node, make build, launcher, listener available to script output = FilePath.localChannel.call(new ControllerGroovyScript( script.getScriptText(), expandedParams, true, listener, launcher, build)); } else { diff --git a/src/main/java/org/jenkinsci/plugins/scriptler/config/Script.java b/src/main/java/org/jenkinsci/plugins/scriptler/config/Script.java index f847cf35..069fd0fd 100644 --- a/src/main/java/org/jenkinsci/plugins/scriptler/config/Script.java +++ b/src/main/java/org/jenkinsci/plugins/scriptler/config/Script.java @@ -54,11 +54,17 @@ public class Script implements Comparable