Skip to content

Commit 2268a69

Browse files
author
Aditya
committed
JENKINS-76241: Defensive wrapper around buildFormTree to handle null elements
1 parent 4fb211e commit 2268a69

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

war/src/main/webapp/scripts/hudson-behavior.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,7 +2712,7 @@ var layoutUpdateCallback = {
27122712

27132713
/**
27142714
* Fix for JENKINS-76241: Prevent POST race condition
2715-
* Apply Behaviour rules synchronously after all rules are registered
2715+
* Make form submission more defensive against timing issues
27162716
*/
27172717
(function () {
27182718
"use strict";
@@ -2722,24 +2722,53 @@ var layoutUpdateCallback = {
27222722
}
27232723
window._jenkinsPostHandlerFixApplied = true;
27242724

2725-
// Apply Behaviour rules as soon as they're all registered
2726-
function applyBehavioursImmediately() {
2725+
// Store the original buildFormTree function
2726+
var originalBuildFormTree = window.buildFormTree;
2727+
2728+
// Create a defensive wrapper around buildFormTree
2729+
window.buildFormTree = function (form) {
2730+
try {
2731+
return originalBuildFormTree(form);
2732+
} catch (e) {
2733+
// If buildFormTree fails, log it but don't prevent form submission
2734+
console.warn("buildFormTree error (non-fatal):", e);
2735+
2736+
// Try to at least set the json field to empty object instead of "init"
2737+
var jsonField = null;
2738+
for (var i = 0; i < form.elements.length; i++) {
2739+
if (form.elements[i].name === "json") {
2740+
jsonField = form.elements[i];
2741+
break;
2742+
}
2743+
}
2744+
2745+
if (jsonField && jsonField.value === "init") {
2746+
// Set to empty object so server doesn't get "init"
2747+
jsonField.value = "{}";
2748+
}
2749+
2750+
// Return true to allow form submission to continue
2751+
return true;
2752+
}
2753+
};
2754+
2755+
// Apply Behaviour rules early
2756+
function applyBehavioursEarly() {
27272757
if (typeof Behaviour !== "undefined" && Behaviour.apply) {
27282758
try {
27292759
Behaviour.apply();
27302760
} catch (e) {
2731-
// Silently ignore errors - Behaviour.apply() will be called again later
2732-
console.log("Early Behaviour.apply():", e);
2761+
console.log("Behaviour.apply() error:", e);
27332762
}
27342763
}
27352764
}
27362765

2737-
// Execute immediately - this runs RIGHT NOW, not waiting for any event
2738-
applyBehavioursImmediately();
2766+
// Apply immediately
2767+
applyBehavioursEarly();
27392768

2740-
// Also apply on DOMContentLoaded as a safety net
2769+
// Also apply on DOMContentLoaded
27412770
if (document.readyState === "loading") {
2742-
document.addEventListener("DOMContentLoaded", applyBehavioursImmediately);
2771+
document.addEventListener("DOMContentLoaded", applyBehavioursEarly);
27432772
}
27442773

27452774
// Watch for dynamically added content
@@ -2761,7 +2790,7 @@ var layoutUpdateCallback = {
27612790
});
27622791

27632792
if (needsReapply) {
2764-
applyBehavioursImmediately();
2793+
applyBehavioursEarly();
27652794
}
27662795
});
27672796

0 commit comments

Comments
 (0)