Skip to content

Commit

Permalink
Add listener for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
NajmWasTaken committed May 9, 2022
1 parent 2aed668 commit 07d6de1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
5 changes: 4 additions & 1 deletion core/src/main/java/hudson/cli/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import jenkins.model.Jenkins;
import jenkins.model.ScriptListener;
import org.apache.commons.io.IOUtils;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
Expand Down Expand Up @@ -70,7 +71,9 @@ protected int run() throws Exception {
binding.setProperty("stderr", stderr);

GroovyShell groovy = new GroovyShell(Jenkins.get().getPluginManager().uberClassLoader, binding);
groovy.run(loadScript(), "RemoteClass", remaining.toArray(new String[0]));
String script = loadScript();
ScriptListener.fireScriptFromCLIEvent(script);
groovy.run(script, "RemoteClass", remaining.toArray(new String[0]));
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/hudson/cli/GroovyshCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.ArrayList;
import java.util.List;
import jenkins.model.Jenkins;
import jenkins.model.ScriptListener;
import jline.TerminalFactory;
import jline.UnsupportedTerminal;
import org.codehaus.groovy.tools.shell.Groovysh;
Expand Down Expand Up @@ -79,6 +80,7 @@ protected int run() {
}

Groovysh shell = createShell(stdin, stdout, stderr);
ScriptListener.fireScriptFromCLIEvent(commandLine.toString());
return shell.run(commandLine.toString());
}

Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/jenkins/model/Jenkins.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
import hudson.model.listeners.SCMListener;
import hudson.model.listeners.SaveableListener;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
import hudson.remoting.LocalChannel;
import hudson.remoting.VirtualChannel;
import hudson.scm.RepositoryBrowser;
Expand Down Expand Up @@ -4723,7 +4724,11 @@ public static void _doScript(StaplerRequest req, StaplerResponse rsp, RequestDis
}

try {
ScriptListener.fireScriptEvent(req);
String runner = "Controller";
if (!(channel instanceof LocalChannel)) {
runner = ((Channel) channel).getName();
}
ScriptListener.fireScriptConsoleEvent(text, runner);
req.setAttribute("output",
RemotingDiagnostics.executeGroovy(text, channel));
} catch (InterruptedException e) {
Expand Down
44 changes: 33 additions & 11 deletions core/src/main/java/jenkins/model/ScriptListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,55 @@

import hudson.ExtensionPoint;
import jenkins.util.Listeners;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;

/**
* A listener to track usage of the script console.
* A listener to track Groovy scripts from the CLI and console.
*
* @see Jenkins#_doScript(StaplerRequest, org.kohsuke.stapler.StaplerResponse, javax.servlet.RequestDispatcher, hudson.remoting.VirtualChannel, hudson.security.ACL)
* @see hudson.cli.GroovyCommand#run()
* @see hudson.cli.GroovyshCommand#run()
*/
public interface ScriptListener extends ExtensionPoint {

/**
* Called when script is executed in Script console.
* Called when a groovy script is executed in Script console.
*
* @see Jenkins#_doScript(StaplerRequest, org.kohsuke.stapler.StaplerResponse, javax.servlet.RequestDispatcher, hudson.remoting.VirtualChannel, hudson.security.ACL)
* @param req The StaplerRequest (POST) that contains the script to be executed.
* @param script The script to be executed.
* @param runner Descriptive name of the runner executing the script.
*/
void onScript(StaplerRequest req);
void onScriptFromConsole(String script, String runner);

/**
* Fires the {@link #onScript(StaplerRequest)} event to track the usage of the script console.
* Called when a groovy script is executed from the CLI.
*
* @see hudson.cli.GroovyCommand#run()
* @see hudson.cli.GroovyshCommand#run()
* @param script The script to be executed.
*/
void onScriptFromCLI(String script);


/**
* Fires the {@link #onScriptFromConsole(String, String)} event to track the usage of the script console.
*
* @see Jenkins#_doScript(StaplerRequest, org.kohsuke.stapler.StaplerResponse, javax.servlet.RequestDispatcher, hudson.remoting.VirtualChannel, hudson.security.ACL)
* @param req The StaplerRequest (POST) that contians the script to be executed.
* @param script The script to be executed.
* @param runner Descriptive name of the runner executing the script.
*/
static void fireScriptConsoleEvent(String script, String runner) {
Listeners.notify(ScriptListener.class, true, listener -> listener.onScriptFromConsole(script, runner));
}

/**
* Fires the {@link #onScriptFromCLI(String)} event to track the usage of the script console.
*
* @see hudson.cli.GroovyCommand#run()
* @see hudson.cli.GroovyshCommand#run()
* @param script The script to be executed.
*/
@Restricted(NoExternalUse.class)
static void fireScriptEvent(StaplerRequest req) {
Listeners.notify(ScriptListener.class, true, listener -> listener.onScript(req));
static void fireScriptFromCLIEvent(String script) {
Listeners.notify(ScriptListener.class, true, listener -> listener.onScriptFromCLI(script));
}
}

0 comments on commit 07d6de1

Please sign in to comment.