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

[electricflow] Extract inline script block and event handlers in org/jenkinsci/plugins/electricflow/ElectricFlowDeployApplication/config.jelly

XMLWordPrintable

      Problems

      == Inline Script Block
      Line: 85
      ----
      <script>
              fillDeployApplicationParameters();
      
              function deleteDeployApplicationParameterRows() {
                  var paramRows = document.querySelectorAll("._ef_da_row");
                  [].forEach.call(paramRows, function (row) {
                      row.parentNode.removeChild(row);
                  });
              }
      
              function fillDeployApplicationParameters() {
                  deleteDeployApplicationParameterRows();
      
                  if (!document.getElementById('ef_da_parameters')) {
                      return;
                  }
      
                  var option = document.getElementById('ef_da_parameters').options[0];
                  var param = '{}';
                  if (option) {
                      param = option.value;
                  }
                  var json = JSON.parse(param);
      
                  var table = document.querySelector("[descriptorid='electricFlowDeployApplication'] table");
                  if (table == null) {
                      table = getElementByXpath("//select[@checkdependson='validationTrigger']/ancestor::table[@name]");
                  }
      
                  if (json) {
      
                      if (json.runProcess) {
                          var parameters;
                          try {
                              parameters = JSON.parse(json.runProcess.parameter);
                          }
                          catch (error) {
                              parameters = json.runProcess.parameter;
                          }
      
      
                          if (table == null) {
                              if (parameters.length) {
                                  var el = document.createElement("div");
                                  el.innerHTML = "&lt;br&gt;&lt;b&gt;Deploy Parameters&lt;/b&gt;";
                                  el.className = "_ef_da_row";
                                  var div = document.getElementById("ef_da_environmentName");
                                  div = cbcd_insertAfter(div, el);
                                  parameters.forEach(function (elem) {
                                      var el_param = document.createElement("div");
                                      el_param.className = "_ef_da_parameters _ef_da_row";
                                      div = cbcd_insertAfter(div, el_param);
                                      addParameterRowForDeployApplication(el_param, elem.actualParameterName, elem.value, false, false);
                                  });
                              }
                          } else {
                              var row1 = table.insertRow(table.rows.length - 2);
                              row1.className = "_ef_da_row";
                              if (parameters.length) {
                                  row1.appendChild(createTitleForDeployApplication("Deploy Parameters"));
                                  parameters.forEach(function (elem) {
                                      var row = table.insertRow(table.rows.length - 2);
                                      row.className = "_ef_da_parameters _ef_da_row";
                                      addParameterRowForDeployApplication(row, elem.actualParameterName, elem.value, false, true);
                                  });
                              }
                          }
                      }
                  }
              }
      
              function cbcd_insertAfter(referenceNode, newNode) {
                  return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
              }
      
              function updateDeployJson() {
                  var applicationName = document.getElementById("ef_da_applicationName").value;
                  var applicationProcessName = document.getElementById("ef_da_applicationProcessName").value;
                  var json = JSON.parse('{"runProcess":{"applicationName":"' + applicationName + '", "applicationProcessName":"' + applicationProcessName + '", "parameter":[]}}');
      
                  var parameters = document.querySelectorAll("._ef_da_parameters");
      
                  [].forEach.call(parameters, function (elem) {
                      json.runProcess.parameter.push({
                          'actualParameterName': elem.querySelector(".setting-name").textContent,
                          'value': elem.querySelector(".setting-input").value
                      })
                  });
      
      
                  document.getElementById('ef_da_parameters').options[0].value = JSON.stringify(json);
                  document.getElementById('ef_da_parameters').options[0].textContent = JSON.stringify(json);
              }
      
              function createTitleForDeployApplication(title) {
                  var td = document.createElement('td');
                  td.colSpan = 4;
                  var div = document.createElement('div');
                  div.className = "section-header";
                  div.textContent = title;
                  td.appendChild(div);
                  return td;
              }
      
              function addParameterRowForDeployApplication(row, label, value, isCheckbox, isTable) {
                  var td1 = document.createElement(isTable ? 'td' : 'div');
                  td1.className = 'setting-leftspace';
                  td1.textContent = '';
                  row.appendChild(td1);
      
                  var td2 = document.createElement(isTable ? 'td' : 'div');
                  td2.className = 'setting-name';
                  td2.textContent = label;
                  row.appendChild(td2);
      
                  var td3 = document.createElement(isTable ? 'td' : 'div');
                  td3.className = 'setting-main';
                  var input1 = document.createElement('input');
                  input1.name = 'parameterName';
                  if (isCheckbox) {
                      input1.type = "checkbox";
                      input1.checked = value;
                      input1.style = "width: auto;";
                  }
                  input1.value = value;
                  input1.onchange = updateDeployJson;
                  input1.className = 'setting-input';
                  td3.appendChild(input1);
                  row.appendChild(td3);
      
                  var td4 = document.createElement(isTable ? 'td' : 'div');
                  td4.className = 'setting-no-help';
                  row.appendChild(td4);
              }
      
              function triggerExtraValidationForDeployApplication() {
                  document.getElementById("ef_da_validationTrigger").click();
              }
      
              function getElementByXpath(path) {
                  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
              }
          </script>
      ----
      
      == Inline Event Handler
      Line: 13
      ----
      <f:select onchange="deleteDeployApplicationParameterRows()"/>
      ----
      
      == Inline Event Handler
      Line: 18
      ----
      <f:select onchange="deleteDeployApplicationParameterRows()"/>
      ----
      
      == Inline Event Handler
      Line: 21
      ----
      <f:select id="ef_da_applicationName" onchange="deleteDeployApplicationParameterRows()"/>
      ----
      
      == Inline Event Handler
      Line: 24
      ----
      <f:select id="ef_da_applicationProcessName" onchange="fillDeployApplicationParameters()"/>
      ----
      
      == Inline Event Handler
      Line: 33
      ----
      <f:select id="ef_da_parameters" onchange="fillDeployApplicationParameters()" style="display:none;"/>
      ----
      
      == Inline Event Handler
      Line: 74
      ----
      <button type="button" onclick="triggerExtraValidationForDeployApplication();">
      ----
      

      Solutions

      https://www.jenkins.io/doc/developer/security/csp/#inline-javascript-blocks
      https://www.jenkins.io/doc/developer/security/csp/#inline-event-handlers

            shlomo_dahan Shlomo
            basil Basil Crow
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: