Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.jenkinsci.plugins.scriptler.config.Parameter;
import org.jenkinsci.plugins.scriptler.config.Script;
import org.jenkinsci.plugins.scriptler.config.ScriptlerConfiguration;
import org.jenkinsci.plugins.scriptler.util.ControllerGroovyScript;
import org.jenkinsci.plugins.scriptler.util.GroovyScript;
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
import org.jenkinsci.plugins.scriptler.util.UIHelper;
Expand Down Expand Up @@ -263,8 +262,8 @@
final Object output;
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));
output = FilePath.localChannel.call(
new GroovyScript(script.getScriptText(), expandedParams, true, listener, launcher, build));

Check warning on line 266 in src/main/java/org/jenkinsci/plugins/scriptler/builder/ScriptlerBuilder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 265-266 are not covered by tests
} else {
VirtualChannel channel = launcher.getChannel();
if (channel == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import hudson.remoting.ChannelClosedException;
import hudson.remoting.VirtualChannel;
import java.io.IOException;
import java.util.List;
import jenkins.model.Jenkins;
import java.util.Collections;
import org.jenkinsci.plugins.scriptler.Messages;
import org.jenkinsci.plugins.scriptler.config.Script;
import org.jenkinsci.plugins.scriptler.util.ControllerGroovyScript;
import org.jenkinsci.plugins.scriptler.util.GroovyScript;
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
Expand Down Expand Up @@ -41,23 +39,21 @@
throw new MacroEvaluationException(Messages.tokenmacro_AdminScriptOnly(scriptId));
}

String scriptText = script.getScriptText();
VirtualChannel channel;
GroovyScript groovyScript;
if (script.onlyBuiltIn || Jenkins.get().equals(context.getBuiltOn())) {
if (script.onlyBuiltIn) {

Check warning on line 43 in src/main/java/org/jenkinsci/plugins/scriptler/tokenmacro/ScriptlerTokenMacro.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 43 is only partially covered, one branch is missing
channel = FilePath.localChannel;
groovyScript = new ControllerGroovyScript(scriptText, List.of(), true, listener, null, context);
} else {
FilePath remoteFilePath = context.getWorkspace();
if (remoteFilePath == null) {
// the remote node has apparently disconnected, so we can't run our script
throw new ChannelClosedException((Channel) null, null);
}
channel = remoteFilePath.getChannel();
groovyScript = new GroovyScript(scriptText, List.of(), true, listener);
}

Object output = channel.call(groovyScript);
Object output = channel.call(
new GroovyScript(script.getScriptText(), Collections.emptyList(), true, listener, null, context));

return output != null ? output.toString() : "";
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import java.io.PrintStream;
import java.io.Serial;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import jenkins.security.Roles;
import org.apache.commons.collections.map.LRUMap;
import org.jenkinsci.plugins.scriptler.Messages;
import org.jenkinsci.plugins.scriptler.config.Parameter;
import org.jenkinsci.remoting.Role;
import org.jenkinsci.remoting.RoleChecker;

/**
* Inspired by hudson.util.RemotingDiagnostics.Script, but adding parameters.
Expand All @@ -27,6 +33,9 @@

private final boolean failWithException;
private final TaskListener listener;
private final transient AbstractBuild<?, ?> build;
private final transient Launcher launcher;
private transient ClassLoader cl;

@SuppressWarnings("unchecked")
private static final Map<String, ConcurrentLinkedQueue<Script>> cache = Collections.synchronizedMap(new LRUMap(10));
Expand All @@ -41,30 +50,57 @@
}

/**
* Constructor
* This constructor can only be used when the script is executed on the built-in node, because launcher and build
* can not be transferred to an agent and therefore the execution will fail
* @param script the script to be executed
* @param parameters the parameters to be passed to the script
* @param failWithException should the job fail with an exception
* @param listener access to logging via listener
* @param launcher the launcher
* @param build the current build
*/
public GroovyScript(
String script,
@NonNull Collection<Parameter> parameters,
boolean failWithException,
TaskListener listener) {
TaskListener listener,
Launcher launcher,
AbstractBuild<?, ?> build) {
this.script = script;
this.parameters = new ArrayList<>(parameters);
this.failWithException = failWithException;
this.listener = listener;
this.cl = getClassLoader();
this.build = build;
this.launcher = launcher;
}

/**
* Constructor
* @param script the script to be executed
* @param parameters the parameters to be passed to the script
* @param failWithException should the job fail with an exception
* @param listener access to logging via listener
*/
public GroovyScript(
String script,
@NonNull Collection<Parameter> parameters,
boolean failWithException,
TaskListener listener) {
this(script, parameters, failWithException, listener, null, null);
}

public ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
return Jenkins.get().getPluginManager().uberClassLoader;
}

public Object call() {
// if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader.
if (cl == null) {

Check warning on line 99 in src/main/java/org/jenkinsci/plugins/scriptler/util/GroovyScript.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 99 is only partially covered, one branch is missing
cl = Thread.currentThread().getContextClassLoader();

Check warning on line 100 in src/main/java/org/jenkinsci/plugins/scriptler/util/GroovyScript.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 100 is not covered by tests
}
PrintStream logger = listener.getLogger();
GroovyShell shell = new GroovyShell(getClassLoader());
GroovyShell shell = new GroovyShell(cl);

for (Parameter param : parameters) {
final String paramName = param.getName();
Expand All @@ -77,7 +113,9 @@

// set default variables
shell.setVariable("out", logger);
setShellVariables(shell);
shell.setVariable("listener", listener);
if (build != null) shell.setVariable("build", build);
if (launcher != null) shell.setVariable("launcher", launcher);

Check warning on line 118 in src/main/java/org/jenkinsci/plugins/scriptler/util/GroovyScript.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 118 is only partially covered, one branch is missing

ConcurrentLinkedQueue<Script> scriptPool = cache.get(script);
if (scriptPool == null) {
Expand Down Expand Up @@ -115,8 +153,13 @@
}
}

protected void setShellVariables(@NonNull GroovyShell shell) {
shell.setVariable("listener", listener);
@Override
public void checkRoles(RoleChecker roleChecker) throws SecurityException {
if (launcher != null && build != null) {
roleChecker.check(this, Roles.MASTER);
} else {
roleChecker.check(this, Role.UNKNOWN);

Check warning on line 161 in src/main/java/org/jenkinsci/plugins/scriptler/util/GroovyScript.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 158-161 are not covered by tests
}
}

private static final class ScriptlerExecutionException extends RuntimeException {
Expand Down
Loading