Skip to content

Commit 9d3888c

Browse files
Artmorsejglick
andauthored
[JENKINS-69917] - Snippetizer/index.jelly javascript un-inlined. (#599)
* [JENKINS-69917] - Snippetizer/index.jelly javascript un-inlined. * [JENKINS-69917] - Attribute used to pass url from jelly to JS. * [JENKINS-69917] - Apply suggestions from code review Co-authored-by: Jesse Glick <[email protected]> * [JENKINS-69917] - handle-prototype fixed. * [JENKINS-69917] - handle-prototype use fetch. --------- Co-authored-by: Jesse Glick <[email protected]>
1 parent 4e2b31c commit 9d3888c

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function handlePrototype(url, crumb) {
2+
buildFormTree(document.forms.config);
3+
// TODO JSON.stringify fails in some circumstances: https://gist.github.com/jglick/70ec4b15c1f628fdf2e9 due to Array.prototype.toJSON
4+
// TODO simplify when Prototype.js is removed
5+
const json = Object.toJSON ? Object.toJSON(JSON.parse(document.forms.config.elements.json.value).prototype) : JSON.stringify(JSON.parse(document.forms.config.elements.json.value).prototype);
6+
if (!json) {
7+
return; // just a separator
8+
}
9+
10+
const headers = new Headers();
11+
headers.append("Content-Type", "application/x-www-form-urlencoded");
12+
headers.append("Jenkins-Crumb", crumb);
13+
14+
fetch(url, {
15+
method: "POST",
16+
headers: headers,
17+
body: "json=" + encodeURIComponent(json),
18+
19+
})
20+
.then(response => {
21+
if (response.ok) {
22+
response.text().then((responseText) => {
23+
document.getElementById('prototypeText').value = responseText;
24+
});
25+
}
26+
})
27+
.catch(error => {
28+
console.error('Fetch error:', error);
29+
});
30+
}
31+
32+
33+
document.addEventListener('DOMContentLoaded', () => {
34+
35+
const generatePipelineScript = document.getElementById("generatePipelineScript");
36+
const url = generatePipelineScript.getAttribute("data-url");
37+
const crumb = generatePipelineScript.getAttribute("data-crumb");
38+
generatePipelineScript.onclick = (_) => {
39+
handlePrototype(url, crumb);
40+
return false;
41+
};
42+
43+
});

plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/Snippetizer/index.jelly

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,13 @@ THE SOFTWARE.
6767
<f:dropdownListBlock title="${%— Advanced/Deprecated —}"/>
6868
<local:listSteps quasiDescriptors="${it.getQuasiDescriptors(true)}"/>
6969
</f:dropdownList>
70-
<j:set var="id" value="${h.generateId()}"/>
7170
<f:block>
72-
<input type="button" value="${%Generate Pipeline Script}" onclick="handlePrototype_${id}(); return false" class="submit-button primary"/>
73-
<f:textarea id="prototypeText_${id}" readonly="true" style="margin-top: 10px" />
74-
<script>
75-
function handlePrototype_${id}() {
76-
buildFormTree(document.forms.config);
77-
// TODO JSON.stringify fails in some circumstances: https://gist.github.com/jglick/70ec4b15c1f628fdf2e9 due to Array.prototype.toJSON
78-
// TODO simplify when Prototype.js is removed
79-
var json = Object.toJSON ? Object.toJSON(JSON.parse(document.forms.config.elements.json.value).prototype) : JSON.stringify(JSON.parse(document.forms.config.elements.json.value).prototype);
80-
if (!json) {
81-
return; // just a separator
82-
}
83-
fetch('${rootURL}/${it.GENERATE_URL}', {
84-
method: 'post',
85-
headers: crumb.wrap({
86-
"Content-Type": "application/x-www-form-urlencoded",
87-
}),
88-
body: new URLSearchParams({
89-
json: json,
90-
}),
91-
}).then((rsp) => {
92-
if (rsp.ok) {
93-
rsp.text().then((responseText) => {
94-
document.getElementById('prototypeText_${id}').value = responseText;
95-
});
96-
}
97-
});
98-
}
99-
</script>
71+
<input type="button" id="generatePipelineScript" value="${%Generate Pipeline Script}"
72+
class="submit-button primary"
73+
data-url="${rootURL}/${it.GENERATE_URL}"
74+
data-crumb="${h.getCrumb(request)}"/>
75+
<f:textarea id="prototypeText" readonly="true" style="margin-top: 10px"/>
76+
<st:adjunct includes="org.jenkinsci.plugins.workflow.cps.Snippetizer.handle-prototype"/>
10077
</f:block>
10178
<f:section title="${%Global Variables}"/>
10279
<f:block>

0 commit comments

Comments
 (0)