Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-63509

JavaScript inside "Formatted HTML" stop work, when Groovy script uses a value of another parameter

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • active-choices-plugin
    • None
    • Windows 10 64 bit
      java.version 1.8.0_162
      Tomcat 9.0.34
      Jenkins 2.254
      Active Choices 2.4
      Firefox 79.0

      Here is two parameters:

      Param1 - simple single select:

      return ['value1', 'value2']
      

      Param2 - formatted HTML:

      def tst = "new"
      html = '''
      <html>
      <body>
      <input type="checkbox" onClick="Result(this)" /> Select all<br/>
      <input type="checkbox" name="cbx" value="One" onClick="Result()"/> One<br/>
      <input type="checkbox" name="cbx" value="Two" onClick="Result()"/> Two<br/>
      <input type="checkbox" name="cbx" value="Three" onClick="Result()"/> Three<br/>
      <script>
      function Result(source) {
      	var checkboxes = document.getElementsByName("cbx");
      	var result = document.getElementById("result");
      	result.value = "";
      	for(var i=0, n=checkboxes.length;i<n;i++) {
      		if (source) checkboxes[i].checked = source.checked;
      		if (checkboxes[i].checked) result.value = result.value + checkboxes[i].value + ",";
      	}  
      	result.value = result.value + "''' + tst + '''";
      }
      </script>
      <input type="text" id = "result" name="value" value="''' + tst + '''">
      </body>
      </html>
      '''
      return html
      

      When Param2 does not use Param1 value, function Result works great:


      But when I try to use value of Param1, JavaScript does not work:

      def tst = Param1
      

       

          [JENKINS-63509] JavaScript inside "Formatted HTML" stop work, when Groovy script uses a value of another parameter

          Vladimir Buravtsov added a comment - - edited

          From Console:

          Function Result is missing in build script:

           <tr><td class="setting-leftspace"> </td><td class="setting-name">Param2</td><td class="setting-main"><div name="parameter" id="choice-parameter-172075458256007" class=""><input name="name" type="hidden" value="Param2"><div id="formattedHtml_choice-parameter-172075458481046">[error]</div><input type="text" name="value" value="" style="visibility: hidden; position: absolute;"></div></td><td class="setting-no-help"></td></tr><tr class="validation-error-area"><td colspan="2"></td><td></td><td></td></tr><script type="text/javascript">
           

          Vladimir Buravtsov added a comment - - edited From Console: Function Result is missing in build script: <tr><td class= "setting-leftspace" > </td><td class= "setting-name" >Param2</td><td class= "setting-main" ><div name= "parameter" id= "choice-parameter-172075458256007" class= ""><input name=" name " type=" hidden " value=" Param2 "><div id=" formattedHtml_choice-parameter-172075458481046 ">[error]</div><input type=" text " name=" value " value=" " style=" visibility: hidden; position: absolute; "></div></td><td class=" setting-no-help "></td></tr><tr class=" validation-error-area "><td colspan=" 2 "></td><td></td><td></td></tr><script type=" text/javascript">

          I think this is happening when you put JavaScript in a parameter that gets updated due to a change form another Active Choice parameter. Try to put your JavaScript function in a separate AC that does not reference any parameters. 

          Ioannis Moutsatsos added a comment - I think this is happening when you put JavaScript in a parameter that gets updated due to a change form another Active Choice parameter. Try to put your JavaScript function in a separate AC that does not reference any parameters. 

          Cedric added a comment - - edited

          Hello Dears,

          I am facing exactly the same issue. May I kindly ask you if there is any update ?

          I do not know if it may help, but if I encapsulate the 
          def tst = Param1
          inside a try catch sequence like this :

           

          try {
          {
          def tst = Param1
          } catch (Exception e) {
          html_to_render = """
             <label id="label"> "${e.message}"</label>
          """    
          return   html_to_render 
          }
          

           

          It generates the following html 

          <label id="label">"No such property: Param1 for class: Script1"</label>

          My log is also full of exceptions caught by "No such property" as follow:

          ENTRY
          Sep 04, 2024 3:55:50 PM FINEST org.biouno.unochoice.model.GroovyScriptFallback to default script...
          groovy.lang.MissingPropertyException: No such property: SCRATCH for class: Script1
          	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66)
          	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:471)
          	at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:377)

          But I do not understand yet why, for non-"Formated-HTML" type, it does not seem to hurt, because the UI reacts as expected.

          ioannis, Indeed if the JavaScript function is in a separate AC that does not reference any parameters, it is properly generated, but it is not an option, as the target here is to generate a JavaScript Code based on this parameter. In my case I need to react based on the Node that is selected graphically.

           

          My config is the following:

          Jenkins 2.426.3
          Active Choices 2.8.1

          Thanks a lot.

          Cedric

          Cedric added a comment - - edited Hello Dears, I am facing exactly the same issue. May I kindly ask you if there is any update ? I do not know if it may help, but if I encapsulate the  def tst = Param1 inside a try catch sequence like this :   try { { def tst = Param1 } catch (Exception e) { html_to_render = """    <label id= "label" > "${e.message}" </label> """     return   html_to_render  }   It generates the following html  <label id= "label" > "No such property: Param1 for class: Script1" </label> My log is also full of exceptions caught by "No such property" as follow: ENTRY Sep 04, 2024 3:55:50 PM FINEST org.biouno.unochoice.model.GroovyScriptFallback to default script... groovy.lang.MissingPropertyException: No such property: SCRATCH for class: Script1 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:471) at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:377) But I do not understand yet why, for non-"Formated-HTML" type, it does not seem to hurt, because the UI reacts as expected. ioannis , Indeed if the JavaScript function is in a separate AC that does not reference any parameters, it is properly generated, but it is not an option, as the target here is to generate a JavaScript Code based on this parameter. In my case I need to react based on the Node that is selected graphically.   My config is the following: Jenkins 2.426.3 Active Choices 2.8.1 Thanks a lot. Cedric

          Hi ced2c , I'll have a look at this one after I revert a change (from some versions ago) that is affecting how reactive parameters work. I am not sure if that bug is affecting this issue too, but to be sure I'm focusing on fixing that one first, so that parameters are updated in the correct order, and then will have a look if this one is still happening. Thanks for the details in your comment!

          Bruno P. Kinoshita added a comment - Hi ced2c , I'll have a look at this one after I revert a change (from some versions ago) that is affecting how reactive parameters work. I am not sure if that bug is affecting this issue too, but to be sure I'm focusing on fixing that one first, so that parameters are updated in the correct order, and then will have a look if this one is still happening. Thanks for the details in your comment!

          Cedric added a comment -

          Hi Bruno

          Thanks a lot for your quick feedback, and your amazing work for this plugin. I guess you are referring to the work done around JENKINS-71909.

          I think JENKINS-69045 can be considered as a duplicate of this one. despite its description is limited.

          Regards

          Cedric

          Cedric added a comment - Hi Bruno Thanks a lot for your quick feedback, and your amazing work for this plugin. I guess you are referring to the work done around JENKINS-71909 . I think JENKINS-69045 can be considered as a duplicate of this one. despite its description is limited. Regards Cedric

          Hi,

          I confirm the issue is still happening, even with the fix for reactivity. I can also confirm that the variable is added to the Groovy context, and sometimes it works. If you refresh the page, wait the scripts to be executed, and then select Param1 value again, then you should see the right value in the Param2. But then selecting things, you might end up in an inconsistent status again.

          There seems to be a JavaScript issue somewhere. This needs more testing, so no fix for now, unfortunately.

          Bruno P. Kinoshita added a comment - Hi, I confirm the issue is still happening, even with the fix for reactivity. I can also confirm that the variable is added to the Groovy context, and sometimes it works. If you refresh the page, wait the scripts to be executed, and then select Param1 value again, then you should see the right value in the Param2. But then selecting things, you might end up in an inconsistent status again. There seems to be a JavaScript issue somewhere. This needs more testing, so no fix for now, unfortunately.

          Hi Cedric. Have you tried to pass an identifying parameter into your on click function? I would have one reactive AC reference parameter that generates the check boxes and uses the Param1 as needed. The onclick should pass the parameters and IDs of interest for making the results function work. So, instead of onclick="results()" I would have something like onclick"results('one')" for the value='one' checkbox and so on. This way you can separate the AC JS function to another parameter not referencing any parameters. That's usually how I have it setup. I hope it makes sense and it helps kinow

          Ioannis Moutsatsos added a comment - Hi Cedric. Have you tried to pass an identifying parameter into your on click function? I would have one reactive AC reference parameter that generates the check boxes and uses the Param1 as needed. The onclick should pass the parameters and IDs of interest for making the results function work. So, instead of onclick="results()" I would have something like onclick"results('one')" for the value='one' checkbox and so on. This way you can separate the AC JS function to another parameter not referencing any parameters. That's usually how I have it setup. I hope it makes sense and it helps kinow

            kinow Bruno P. Kinoshita
            ngagex Vladimir Buravtsov
            Votes:
            3 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: