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 12 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
6 changes: 5 additions & 1 deletion core/src/main/java/hudson/cli/GroovyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.Extension;
import hudson.model.User;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
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 +72,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.fireScriptEvent(script, "CLI", User.current());
meiswjn marked this conversation as resolved.
Show resolved Hide resolved
groovy.run(script, "RemoteClass", remaining.toArray(new String[0]));
return 0;
}

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/hudson/cli/GroovyshCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import groovy.lang.Binding;
import groovy.lang.Closure;
import hudson.Extension;
import hudson.model.User;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -39,6 +40,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 +81,7 @@ protected int run() {
}

Groovysh shell = createShell(stdin, stdout, stderr);
ScriptListener.fireScriptEvent(commandLine.toString(), "CLI", User.current());
meiswjn marked this conversation as resolved.
Show resolved Hide resolved
return shell.run(commandLine.toString());
}

Expand Down
9 changes: 9 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 @@ -4723,6 +4724,14 @@ public static void _doScript(StaplerRequest req, StaplerResponse rsp, RequestDis
}

try {
String runner = "Script Console ";

if (!(channel instanceof LocalChannel)) {
runner += ((Channel) channel).getName();
} else {
runner += "Controller";
}
ScriptListener.fireScriptEvent(text, runner, User.current());
req.setAttribute("output",
RemotingDiagnostics.executeGroovy(text, channel));
} catch (InterruptedException e) {
Expand Down
38 changes: 38 additions & 0 deletions core/src/main/java/jenkins/model/ScriptListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package jenkins.model;

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

/**
* A listener to track Groovy scripts.
*
* @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 (privileged) groovy script is executed.
*
* @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 origin Descriptive identifier of the origin where the script is executed (Controller, Agent ID, Run ID).
* @param u If available, the user that executed the script. Can be null.
*/
void onScript(String script, String origin, User u);

/**
* Fires the {@link #onScript(String, String, User)} 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 origin Descriptive identifier of the origin where the script is executed (Controller, Agent ID, Run ID).
* @param u If available, the user that executed the script.
*/
static void fireScriptEvent(String script, String origin, User u) {
Listeners.notify(ScriptListener.class, true, listener -> listener.onScript(script, origin, u));
}
}