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

Tolerate HttpResponse thrown from Descriptor.newInstance #9495

Merged
merged 4 commits into from
Jul 28, 2024

Conversation

jglick
Copy link
Member

@jglick jglick commented Jul 25, 2024

Downstream of jenkinsci/stapler#571. Appears to solve the problem described in jenkinsci/workflow-cps-plugin#907 (comment): allows you to throw Descriptor.FormException from a @DataBoundConstructor and have it be rendered properly.

Testing done

diff --git plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition.java plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition.java
index eea31e59..2d04e223 100644
--- plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition.java
+++ plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition.java
@@ -27,6 +27,7 @@ package org.jenkinsci.plugins.workflow.cps;
 import edu.umd.cs.findbugs.annotations.NonNull;
 import hudson.Extension;
 import hudson.model.Action;
+import hudson.model.Descriptor;
 import hudson.model.Item;
 import hudson.model.Job;
 import hudson.model.Queue;
@@ -75,15 +76,18 @@ public class CpsFlowDefinition extends FlowDefinition {
      * @deprecated use {@link #CpsFlowDefinition(String, boolean)} instead
      */
     @Deprecated
-    public CpsFlowDefinition(String script) {
+    public CpsFlowDefinition(String script) throws Descriptor.FormException {
         this(script, false);
     }
 
     @DataBoundConstructor
-    public CpsFlowDefinition(String script, boolean sandbox) {
+    public CpsFlowDefinition(String script, boolean sandbox) throws Descriptor.FormException {
         StaplerRequest req = Stapler.getCurrentRequest();
         this.script = sandbox ? script : ScriptApproval.get().configuring(script, GroovyLanguage.get(),
                 ApprovalContext.create().withCurrentUser().withItemAsKey(req != null ? req.findAncestorObject(Item.class) : null), req == null);
+        if (!sandbox) {
+            throw new Descriptor.FormException("Sandbox is mandatory", "sandbox");
+        }
         this.sandbox = sandbox;
     }
 

displays a polite page with the message when you Save a form with the sandbox field unchecked, rather than an “angry Jenkins” / UUID as before. Also if you Apply then the error is shown nicely in the bottom bar.

Proposed changelog entries

  • Developer: HttpResponse exceptions such as Descriptor.FormException are now rendered properly from @DataBoundConstructors.

Proposed upgrade guidelines

N/A

Submitter checklist

Desired reviewers

@amuniz

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

Maintainer checklist

@amuniz
Copy link
Member

amuniz commented Jul 26, 2024

Verified that it works as expected in jenkinsci/workflow-cps-plugin#907

amuniz added a commit to amuniz/workflow-cps-plugin that referenced this pull request Jul 26, 2024
@jglick jglick marked this pull request as ready for review July 26, 2024 18:40
@MarkEWaite MarkEWaite added the developer Changes which impact plugin developers label Jul 27, 2024
Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

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

This PR is now ready for merge. We will merge it after approximately 24 hours if there is no negative feedback.

/label ready-for-merge

@comment-ops-bot comment-ops-bot bot added the ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback label Jul 27, 2024
@MarkEWaite MarkEWaite merged commit cbfe2d4 into jenkinsci:master Jul 28, 2024
16 checks passed
@jglick jglick deleted the HttpResponses.wrap branch July 29, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer Changes which impact plugin developers ready-for-merge The PR is ready to go, and it will be merged soon if there is no negative feedback
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants