Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-41516] Add script console listener #6539

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e745089
Set location for listener
meiswjn Apr 26, 2022
060fe1e
Add ScriptListener
meiswjn May 4, 2022
89ac091
Remove debugging statements
meiswjn May 4, 2022
2aed668
Merge branch 'master' into feature/add-script-console-listener
meiswjn May 4, 2022
8d41ad1
Add listener for CLI
meiswjn May 9, 2022
5160902
Merge branch 'master' into feature/add-script-console-listener
meiswjn May 9, 2022
6b86d43
Merge branch 'jenkinsci:master' into feature/add-script-console-listener
meiswjn Jun 10, 2022
dc7728f
Add new listener for other sources
meiswjn Jun 23, 2022
c533407
Simplify listener
meiswjn Jun 23, 2022
8e9e618
Merge branch 'master' into feature/add-script-console-listener
meiswjn Jun 24, 2022
02ffdd1
Fix whitespace and comments
meiswjn Jun 24, 2022
0ceef7c
Merge branch 'feature/add-script-console-listener' of https://github.…
meiswjn Jun 24, 2022
d30108d
Merge branch 'master' into feature/add-script-console-listener
daniel-beck Jul 18, 2022
a2ede02
Merge branch 'master' into feature/add-script-console-listener
daniel-beck Jul 31, 2022
70b1334
Update core/src/main/java/hudson/cli/GroovyCommand.java
meiswjn Aug 11, 2022
eadd842
Add precise origins; Correctly log CLI input
meiswjn Aug 11, 2022
cc4ce09
Merge branch 'jenkinsci:master' into feature/add-script-console-listener
meiswjn Aug 11, 2022
a4a541c
Merge branch 'feature/add-script-console-listener' of https://github.…
meiswjn Aug 11, 2022
301dea2
Add LoggingGroovySh as suggested by @daniel-beck
meiswjn Aug 11, 2022
f1e5b03
Add ScriptListener Tests
meiswjn Aug 12, 2022
fcf5506
Merge branch 'master' into feature/add-script-console-listener
meiswjn Aug 25, 2022
df5b49c
Merge branch 'master' into feature/add-script-console-listener
basil Aug 25, 2022
567e03b
Add basic default listener that just logs
daniel-beck Aug 31, 2022
0526568
Merge branch 'master' into feature/add-script-console-listener
meiswjn Oct 7, 2022
19e7c04
Merge branch 'master' into feature/add-script-console-listener
meiswjn Nov 7, 2022
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
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: 7 additions & 0 deletions 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 @@ -4694,6 +4695,7 @@ public void run() {
* Run arbitrary Groovy script.
*/
public void doScript(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {

_doScript(req, rsp, req.getView(this, "_script.jelly"), FilePath.localChannel, getACL());
}

Expand Down Expand Up @@ -4722,6 +4724,11 @@ public static void _doScript(StaplerRequest req, StaplerResponse rsp, RequestDis
}

try {
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
56 changes: 56 additions & 0 deletions core/src/main/java/jenkins/model/ScriptListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package jenkins.model;

import hudson.ExtensionPoint;
import jenkins.util.Listeners;
import org.kohsuke.stapler.StaplerRequest;

/**
* 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 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 script The script to be executed.
* @param runner Descriptive name of the runner executing the script.
*/
void onScriptFromConsole(String script, String runner);

/**
* 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 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.
*/
static void fireScriptFromCLIEvent(String script) {
Listeners.notify(ScriptListener.class, true, listener -> listener.onScriptFromCLI(script));
}
}