-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[JENKINS-41516] Add script console listener #7056
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
Merged
NotMyFault
merged 47 commits into
jenkinsci:master
from
daniel-beck:meiswjn-feature-add-script-console-listener
Oct 5, 2023
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
e745089
Set location for listener
meiswjn 060fe1e
Add ScriptListener
meiswjn 89ac091
Remove debugging statements
meiswjn 2aed668
Merge branch 'master' into feature/add-script-console-listener
meiswjn 8d41ad1
Add listener for CLI
meiswjn 5160902
Merge branch 'master' into feature/add-script-console-listener
meiswjn 6b86d43
Merge branch 'jenkinsci:master' into feature/add-script-console-listener
meiswjn dc7728f
Add new listener for other sources
meiswjn c533407
Simplify listener
meiswjn 8e9e618
Merge branch 'master' into feature/add-script-console-listener
meiswjn 02ffdd1
Fix whitespace and comments
meiswjn 0ceef7c
Merge branch 'feature/add-script-console-listener' of https://github.…
meiswjn d30108d
Merge branch 'master' into feature/add-script-console-listener
daniel-beck a2ede02
Merge branch 'master' into feature/add-script-console-listener
daniel-beck 70b1334
Update core/src/main/java/hudson/cli/GroovyCommand.java
meiswjn eadd842
Add precise origins; Correctly log CLI input
meiswjn cc4ce09
Merge branch 'jenkinsci:master' into feature/add-script-console-listener
meiswjn a4a541c
Merge branch 'feature/add-script-console-listener' of https://github.…
meiswjn 301dea2
Add LoggingGroovySh as suggested by @daniel-beck
meiswjn f1e5b03
Add ScriptListener Tests
meiswjn fcf5506
Merge branch 'master' into feature/add-script-console-listener
meiswjn df5b49c
Merge branch 'master' into feature/add-script-console-listener
basil 567e03b
Add basic default listener that just logs
daniel-beck 9ae14b4
WIP Script Listener
daniel-beck 6b43b1e
Cleaner ScriptListener API
daniel-beck 91bc8a9
Add license
daniel-beck f79a9eb
Cleaner Binding stringification
daniel-beck db09ba6
Note TODO
daniel-beck d6c64a3
Add Object feature parameters
daniel-beck c9d641f
Thanks Checkstyle
daniel-beck 1c7c9b3
Thanks Spotbugs
daniel-beck 7b2ad6d
Simplify test, make it pass again
daniel-beck 3a60178
Add since TODO
daniel-beck 86c4118
No mocking, use CLICommandInvoker for CLI commands
daniel-beck 2ebfaae
Minor cleanup
daniel-beck 9d0863f
Merge remote-tracking branch 'jenkinsci/master' into meiswjn-feature-…
daniel-beck ccfe494
Merge branch 'master' into meiswjn-feature-add-script-console-listener
daniel-beck b7329eb
Merge branch 'master' into meiswjn-feature-add-script-console-listener
daniel-beck 40e698b
Merge branch 'master' into meiswjn-feature-add-script-console-listener
daniel-beck 35d4041
Fix imports
daniel-beck 7a0b747
So far, #onScriptDefinition would be unused, so remove it
daniel-beck c666dac
Checkstyle
daniel-beck f1844c8
Add test for DefaultScriptListener
daniel-beck 3db1dad
Nicer message for the (hopefully rare) case of anon script execution
daniel-beck 3d8db19
More thoroughly test logging
daniel-beck 2f5fe9f
Remove obsolete TODO
daniel-beck d8f4544
Merge branch 'master' into meiswjn-feature-add-script-console-listener
daniel-beck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
core/src/main/java/jenkins/util/DefaultScriptListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2022 CloudBees, Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package jenkins.util; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import groovy.lang.Binding; | ||
| import hudson.Extension; | ||
| import hudson.model.User; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import org.codehaus.groovy.runtime.InvokerHelper; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
|
||
| /** | ||
| * Basic default implementation of {@link jenkins.util.ScriptListener} that just logs. | ||
| * | ||
| * @since TODO | ||
| */ | ||
| @Extension | ||
| @Restricted(NoExternalUse.class) | ||
| public class DefaultScriptListener implements ScriptListener { | ||
| public static final Logger LOGGER = Logger.getLogger(DefaultScriptListener.class.getName()); | ||
|
|
||
| @Override | ||
| public void onScriptExecution(String script, Binding binding, @NonNull Object feature, Object context, @NonNull String correlationId, User user) { | ||
| String userFragment = user == null ? " (no user)" : " by user: '" + user + "'"; | ||
| LOGGER.log(Level.FINE, LOGGER.isLoggable(Level.FINEST) ? new Exception() : null, | ||
| () -> "Execution of script: '" + script + "' with binding: '" + stringifyBinding(binding) + "' in feature: '" + feature + "' and context: '" + context + "' with correlation: '" + correlationId + "'" + userFragment); | ||
| } | ||
|
|
||
| @Override | ||
| public void onScriptOutput(String output, @NonNull Object feature, Object context, @NonNull String correlationId, User user) { | ||
| String userFragment = user == null ? " (no user)" : " for user: '" + user + "'"; | ||
| LOGGER.log(Level.FINER, LOGGER.isLoggable(Level.FINEST) ? new Exception() : null, | ||
| () -> "Script output: '" + output + "' in feature: '" + feature + "' and context: '" + context + "' with correlation: '" + correlationId + "'" + userFragment); | ||
| } | ||
|
|
||
| private static String stringifyBinding(Binding binding) { | ||
| if (binding == null) { | ||
| return null; | ||
| } | ||
| return InvokerHelper.toString(binding.getVariables()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be omitted because it is not part of the API anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it to quickly see when a feature was added. While I could get there via
git blame, given the negligible cost, this is reasonable to have IMO.