From d017d30898a4c2d9caf6a30cfc7b0d5bd8307594 Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Mon, 11 Nov 2024 15:09:21 +0000 Subject: [PATCH] [JENKINS-37241] Support for query parameters in autocomplete --- .../main/java/hudson/model/Descriptor.java | 6 +++++ eslint.config.cjs | 2 ++ .../js/components/dropdowns/autocomplete.js | 27 ++++++++++++++++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/hudson/model/Descriptor.java b/core/src/main/java/hudson/model/Descriptor.java index 33c981d657f5..c55b7982695e 100644 --- a/core/src/main/java/hudson/model/Descriptor.java +++ b/core/src/main/java/hudson/model/Descriptor.java @@ -474,6 +474,12 @@ public void calcAutoCompleteSettings(String field, Map attribute if (method == null) return; // no auto-completion + // build query parameter line by figuring out what should be submitted + List depends = buildFillDependencies(method, new ArrayList<>()); + if (!depends.isEmpty()) { + attributes.put("fillDependsOn", String.join(" ", depends)); + } + attributes.put("autoCompleteUrl", String.format("%s/%s/autoComplete%s", getCurrentDescriptorByNameUrl(), getDescriptorUrl(), capitalizedFieldName)); } diff --git a/eslint.config.cjs b/eslint.config.cjs index 0362bf627a7a..dcbd22d73060 100644 --- a/eslint.config.cjs +++ b/eslint.config.cjs @@ -61,6 +61,7 @@ module.exports = [ object: "readonly", objectToUrlFormEncoded: "readonly", onSetupWizardInitialized: "readonly", + qs: "readonly", refillOnChange: "readonly", refreshPart: "readonly", registerSortableDragDrop: "readonly", @@ -72,6 +73,7 @@ module.exports = [ shortenName: "readonly", Sortable: "readonly", toQueryString: "readonly", + TryEach: "readonly", ts_refresh: "readonly", updateOptionalBlock: "readonly", Utilities: "readonly", diff --git a/src/main/js/components/dropdowns/autocomplete.js b/src/main/js/components/dropdowns/autocomplete.js index b6d982c44911..068313f84b36 100644 --- a/src/main/js/components/dropdowns/autocomplete.js +++ b/src/main/js/components/dropdowns/autocomplete.js @@ -59,9 +59,30 @@ function init() { } return; } - const url = - e.getAttribute("autoCompleteUrl") + "?value=" + encodeURIComponent(word); - fetch(url) + + const url = e.getAttribute("autoCompleteUrl"); + + const depends = e.getAttribute("fillDependsOn"); + const q = qs(e).addThis(); + if (depends && depends.length > 0) { + depends.split(" ").forEach( + TryEach(function (n) { + q.nearBy(n); + }), + ); + } + + const queryString = q.toString(); + const idx = queryString.indexOf("?"); + const parameters = queryString.substring(idx + 1); + + fetch(url, { + method: "post", + headers: crumb.wrap({ + "Content-Type": "application/x-www-form-urlencoded", + }), + body: parameters, + }) .then((rsp) => (rsp.ok ? rsp.json() : {})) .then((response) => createAndShowDropdown(e, response.suggestions || [])); }