Skip to content

Commit

Permalink
refactor: Do not rely on prototype.js
Browse files Browse the repository at this point in the history
Jenkins has been using a very old version of Prototype.js.
Starting with 2.406 of Jenkins, prototype is being gradually removed.

This change removes direct calls to prototype in the git-parameter plugin.

Refs: JENKINS-71298
  • Loading branch information
rahulsom committed Jun 28, 2023
1 parent 149e028 commit f6b2f3a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,99 @@ function gitParameterUpdateSelect(listBox, url, divId, config) {
config = config || {};
config = object(config);
var originalOnSuccess = config.onSuccess;
var l = $(listBox);
// TODO Inline this variable, maybe?
var l = listBox;
// TODO - Remove '.firstChild?.nextSibling' once this plugin targets https://github.com/jenkinsci/jenkins/pull/6460 or higher
var status = findFollowingTR(listBox, "validation-error-area").firstChild?.nextSibling || findFollowingTR(listBox, "validation-error-area");
if (status.firstChild && status.firstChild.getAttribute('data-select-ajax-error')) {
status.innerHTML = "";
}
config.onSuccess = function (rsp) {
l.removeClassName("select-ajax-pending");
l.classList.remove("select-ajax-pending");
var currentSelection = l.value;

// clear the contents
while (l.length > 0) l.options[0] = null;

var selectionSet = false; // is the selection forced by the server?
var possibleIndex = null; // if there's a new option that matches the current value, remember its index
var opts = eval('(' + rsp.responseText + ')').values;
for (var i = 0; i < opts.length; i++) {
l.options[i] = new Option(opts[i].name, opts[i].value);
if (opts[i].selected) {
l.selectedIndex = i;
selectionSet = true;
rsp.json().then(json => {
var opts = json.values;
for (var i = 0; i < opts.length; i++) {
l.options[i] = new Option(opts[i].name, opts[i].value);
if (opts[i].selected) {
l.selectedIndex = i;
selectionSet = true;
}
if (opts[i].value === currentSelection)
possibleIndex = i;
}
if (opts[i].value === currentSelection)
possibleIndex = i;
}

// if no value is explicitly selected by the server, try to select the same value
if (!selectionSet && possibleIndex != null)
l.selectedIndex = possibleIndex;
// if no value is explicitly selected by the server, try to select the same value
if (!selectionSet && possibleIndex != null)
l.selectedIndex = possibleIndex;

if (originalOnSuccess !== undefined)
originalOnSuccess(rsp);
if (originalOnSuccess !== undefined)
originalOnSuccess(rsp);

var errors = eval('(' + rsp.responseText + ')').errors
let error_div = $("git_parameter_errors_" + divId);
if (errors.length !== 0) {
error_div.show();
error_div.addClassName("error");
let $error_ul = $("git_parameter_errors_ul_" + divId);
var lis = "";
for (var j = 0; j < errors.length; j++) {
lis += "<li>" + escapeHTML(errors[j]) + "</li>"
var errors = json.errors
let error_div = document.getElementById("git_parameter_errors_" + divId);
if (errors.length !== 0) {
error_div.style.display = "";
error_div.classList.add("error");
let $error_ul = document.getElementById("git_parameter_errors_ul_" + divId);
var lis = "";
for (var j = 0; j < errors.length; j++) {
lis += "<li>" + escapeHTML(errors[j]) + "</li>"
}
$error_ul.update(lis);
} else {
error_div.style.display = "none";
}
$error_ul.update(lis);
}
else {
error_div.hide()
}

});
};
config.onFailure = function (rsp) {
l.removeClassName("select-ajax-pending");
status.innerHTML = rsp.responseText;
if (status.firstChild) {
status.firstChild.setAttribute('data-select-ajax-error', 'true')
}
Behaviour.applySubtree(status);
// deleting values can result in the data loss, so let's not do that unless instructed
var header = rsp.getResponseHeader('X-Jenkins-Select-Error');
if (header && "clear" === header.toLowerCase()) {
// clear the contents
while (l.length > 0) l.options[0] = null;
}
rsp.text().then(text => {
l.classList.remove("select-ajax-pending");
status.innerHTML = text;
if (status.firstChild) {
status.firstChild.setAttribute('data-select-ajax-error', 'true')
}
Behaviour.applySubtree(status);
// deleting values can result in the data loss, so let's not do that unless instructed
var header = rsp.headers.get('X-Jenkins-Select-Error');
if (header && "clear" === header.toLowerCase()) {
// clear the contents
while (l.length > 0) l.options[0] = null;
}
});

};

l.addClassName("select-ajax-pending");
new Ajax.Request(url, config);
l.classList.add("select-ajax-pending");

fetch(url, {
method: 'POST',
body: stringify(config.parameters),
headers: crumb.wrap({})
}).then((response) => {
if (response.ok) {
config.onSuccess(response);
} else {
config.onFailure(response);
}
});
}

function stringify(obj) {
// TODO simplify when Prototype.js is removed
if (Object.toJSON) {
// Prototype.js
return Object.toJSON(obj);
} else {
// Standard
return JSON.stringify(obj);
}
}

Behaviour.specify("SELECT.gitParameterSelect", 'gitParameterSelect', 1000, function (e) {
Expand Down Expand Up @@ -120,4 +145,4 @@ Behaviour.specify("SELECT.gitParameterSelect", 'gitParameterSelect', 1000, funct

function escapeHTML(str) {
return str.replace(/&/g, "&#38;").replace(/"/g, "&#34;").replace(/'/g, "&#39;").replace(/</g, "&#60;");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ var GitParameter = GitParameter || (function($) {
var regex = new RegExp(search,"gi");

var filteredOptions = Array();
$.each(originalOptions, function(i) {
var option = originalOptions[i];
originalOptions.forEach(option => {
if(option.text.match(regex) !== null) {
filteredOptions.push(option)
}
Expand Down

0 comments on commit f6b2f3a

Please sign in to comment.