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-75378] Adding a CLI command listener #10382

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

apuig
Copy link
Contributor

@apuig apuig commented Mar 6, 2025

See JENKINS-75378.

The default implementation mimics the current logs in CLICommand.

Testing done

Executed commands using cli.jar using different transports

Proposed changelog entries

  • Add a new ExtensionPoint to receive events from CLI executions
  • Move logs from CLICommand to a new default listener

Proposed changelog category

/label rfe

Proposed upgrade guidelines

N/A

Submitter checklist

  • The Jira issue, if it exists, is well-described.
  • The changelog entries and upgrade guidelines are appropriate for the audience affected by the change (users or developers, depending on the change) and are in the imperative mood (see examples). Fill in the Proposed upgrade guidelines section only if there are breaking changes or changes that may require extra steps from users during upgrade.
  • There is automated testing or an explanation as to why this change has no tests.
  • New public classes, fields, and methods are annotated with @Restricted or have @since TODO Javadocs, as appropriate.
  • New deprecations are annotated with @Deprecated(since = "TODO") or @Deprecated(forRemoval = true, since = "TODO"), if applicable.
  • New or substantially changed JavaScript is not defined inline and does not call eval to ease future introduction of Content Security Policy (CSP) directives (see documentation).
  • For dependency updates, there are links to external changelogs and, if possible, full differentials.
  • For new APIs and extension points, there is a link to at least one consumer.

Desired reviewers

@mention

Before the changes are marked as ready-for-merge:

Maintainer checklist

  • There are at least two (2) approvals for the pull request and no outstanding requests for change.
  • Conversations in the pull request are over, or it is explicit that a reviewer is not blocking the change.
  • Changelog entries in the pull request title and/or Proposed changelog entries are accurate, human-readable, and in the imperative mood.
  • Proper changelog labels are set so that the changelog can be generated automatically.
  • If the change needs additional upgrade steps from users, the upgrade-guide-needed label is set and there is a Proposed upgrade guidelines section in the pull request title (see example).
  • If it would make sense to backport the change to LTS, a Jira issue must exist, be a Bug or Improvement, and be labeled as lts-candidate to be considered (see query).

@comment-ops-bot comment-ops-bot bot added the rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted label Mar 6, 2025
@daniel-beck daniel-beck self-requested a review March 6, 2025 20:02
@Wadeck Wadeck changed the title cli command listener [JENKINS-75378] Adding a CLI command listener Mar 7, 2025
@apuig apuig marked this pull request as draft March 7, 2025 08:36
Copy link
Member

@aneveux aneveux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good to me, thanks a lot! ❤️

@apuig apuig marked this pull request as ready for review March 11, 2025 07:46
} else if (e instanceof AccessDeniedException) {
exitCode = 6;
printError(e.getMessage());
} else if (e instanceof BadCredentialsException) {
Copy link
Contributor Author

@apuig apuig Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like BadCredentialsException is not longer possible inside CLICommand#main, note {set/get}TransportAuth2:

  • for HTTP or WebSocket the BadCredential is thrown/handled in the filters. When using command line -auth user:pass basic auth : BasicHeaderAuthenticator / AbstractUserAuth.

  • for SSH auth errors (not a BadCredentials) is also handled before executing the command (PublicKeyAuthenticatorImpl / UserAuthNamedFactory).

b1803a9 I can't find CliAuthenticationTest, may not apply anymore.

EDIT: this branch is only possible if a CLICommand implementation throws a BadCredentialsException,

@@ -239,6 +238,9 @@ public int main(List<String> args, Locale locale, InputStream stdin, PrintStream
this.locale = locale;
CmdLineParser p = getCmdLineParser();

final String correlationId = UUID.randomUUID().toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The introduction of correlationId common to all events could be highlighted in the PR description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point correlationId is only used in BadCredentialsException handling. (where the original randomUUID comes form)

@jeromepochat
Copy link
Contributor

Looks good, thanks @apuig !

Copy link
Contributor

@Wadeck Wadeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good concept, thanks for the proposal Albert 👍
Some minor comments here and there.

Not manually tested.

@kmartens27
Copy link

Hi @apuig, would this need to be added to the jenkins.io documentation, and if so is there any additional content/context beyond what is described in this and the JIRA tickets?

* Invoked after successful execution.
*
* @param context Information about the command being executed
* @param exitCode `run` returned exit code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero, presumably?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementations of CLICommand#run can return other values to indicate custom errors.

I try to avoid onSuccess because of this (but the javadoc comment still mention success :S), the idea is we call this if the command returned instead of throwing an exception.
I agree onCompleted vs onError its not clear enough, but can't find a better option (onException?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated javadoc and using onException

* Invoked on execution failure.
*
* @param context Information about the command being executed
* @param exitCode `run` returned exit code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unnecessary to pass this parameter to the listener. If anyone really cared it could be inferred by the same logic as in CLICommand but generally a listener is going to want to report any error at the Java level, not by using a Unix-like exit code value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ATM exit code is used to respect the current log levels. Notably unhanded exceptions and BadCredentials.
We can indeed check the same kind of Exceptions as in CLICommand#handleException, but exit codes handled by jenkins are properly documented on CLICommand#main, perhaps more maintainable ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit code removed in the signature with the exception

clarify interface about onComplete not always success

remove exitCode from onException

remove fire methods

args in CliContext

rename CliListenerTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rfe For changelog: Minor enhancement. use `major-rfe` for changes to be highlighted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants