Skip to content

Commit 5346234

Browse files
authored
Make code.jelly compatible with CSP in Jenkins 2.539+ (#458)
Co-authored-by: Daniel Beck <daniel-beck@users.noreply.github.com>
1 parent 31d2a11 commit 5346234

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

src/main/resources/io/jenkins/plugins/designlibrary/Dialogs/index.jelly

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?jelly escape-by-default='true'?>
2-
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:s="/lib/samples" xmlns:f="/lib/form">
2+
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:s="/lib/samples" xmlns:f="/lib/form" xmlns:st="jelly:stapler">
33
<s:layout>
44
<s:section title="Alerts">
55
<s:group>
@@ -15,7 +15,7 @@
1515
</div>
1616
</dialog>
1717
</s:preview>
18-
<s:code language="java" file="alert.js" code="showAlert()" executable="true"/>
18+
<s:code language="java" file="alert.js" callback="showAlert"/>
1919
</s:group>
2020
<p class="jdl-paragraph">${%alerts}</p>
2121
</s:section>
@@ -35,7 +35,7 @@
3535
</div>
3636
</dialog>
3737
</s:preview>
38-
<s:code language="java" file="prompt.js" code="showPrompt()" executable="true"/>
38+
<s:code language="java" file="prompt.js" callback="showPrompt"/>
3939
</s:group>
4040
<p class="jdl-paragraph">${%prompts}</p>
4141
</s:section>
@@ -52,7 +52,7 @@
5252
</div>
5353
</dialog>
5454
</s:preview>
55-
<s:code language="java" file="confirm.js" code="showConfirm()" executable="true"/>
55+
<s:code language="java" file="confirm.js" code="showConfirm()" callback="showConfirm"/>
5656
</s:group>
5757
<p class="jdl-paragraph">${%confirmations}</p>
5858
</s:section>
@@ -87,7 +87,7 @@
8787
</s:preview>
8888
<s:code-panes>
8989
<s:code-pane title="JavaScript">
90-
<s:code language="java" file="modal.js" code="showModal()" executable="true"/>
90+
<s:code language="java" file="modal.js" code="showModal()" callback="showModal"/>
9191
</s:code-pane>
9292
<s:code-pane title="Jelly">
9393
<s:code file="modal.jelly"/>
@@ -123,7 +123,7 @@
123123
</s:preview>
124124
<s:code-panes>
125125
<s:code-pane title="JavaScript">
126-
<s:code language="java" file="form.js" code="showForm()" executable="true"/>
126+
<s:code language="java" file="form.js" code="showForm()" callback="showForm"/>
127127
</s:code-pane>
128128
<s:code-pane title="Jelly">
129129
<s:code file="form.jelly"/>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function showDefault() {
2+
notificationBar.show("Default");
3+
}
4+
5+
function showSuccess() {
6+
notificationBar.show("Success", notificationBar.SUCCESS);
7+
}
8+
9+
function showWarning() {
10+
notificationBar.show("Warning", notificationBar.WARNING);
11+
}
12+
13+
function showError() {
14+
notificationBar.show("Error", notificationBar.ERROR);
15+
}
16+
17+
function hideNotification() {
18+
notificationBar.hide();
19+
}

src/main/resources/io/jenkins/plugins/designlibrary/Notifications/index.jelly

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?jelly escape-by-default='true'?>
2-
<j:jelly xmlns:j="jelly:core" xmlns:s="/lib/samples">
2+
<j:jelly xmlns:j="jelly:core" xmlns:s="/lib/samples" xmlns:st="jelly:stapler">
33
<s:layout>
44
<s:section title="Default">
55
<s:group>
@@ -17,6 +17,7 @@
1717
</s:preview>
1818
<s:code language="java"
1919
code="notificationBar.show('Default')"
20+
callback="showDefault"
2021
executable="true"/>
2122
</s:group>
2223
</s:section>
@@ -37,6 +38,7 @@
3738
</s:preview>
3839
<s:code language="java"
3940
code="notificationBar.show('Success', notificationBar.SUCCESS)"
41+
callback="showSuccess"
4042
executable="true"/>
4143
</s:group>
4244
</s:section>
@@ -57,6 +59,7 @@
5759
</s:preview>
5860
<s:code language="java"
5961
code="notificationBar.show('Warning', notificationBar.WARNING)"
62+
callback="showWarning"
6063
executable="true"/>
6164
</s:group>
6265
</s:section>
@@ -77,14 +80,17 @@
7780
</s:preview>
7881
<s:code language="java"
7982
code="notificationBar.show('Error', notificationBar.ERROR)"
83+
callback="showError"
8084
executable="true"/>
8185
</s:group>
8286
</s:section>
8387

8488
<s:section title="Closing notifications" description="${%usage.2}">
8589
<s:code language="java"
8690
code="notificationBar.hide()"
91+
callback="hideNotification"
8792
executable="true"/>
8893
</s:section>
8994
</s:layout>
95+
<st:adjunct includes="io.jenkins.plugins.designlibrary.Notifications.adjunct" />
9096
</j:jelly>

src/main/resources/io/jenkins/plugins/designlibrary/sample.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
document.addEventListener("DOMContentLoaded", () => {
22
const url = document.querySelector("head").dataset.rooturl;
33

4+
document.querySelectorAll(".callback-button").forEach((element) => {
5+
let callback = element.dataset.callback;
6+
element.onclick = () => {
7+
if (
8+
callback &&
9+
window[callback] &&
10+
typeof window[callback] === "function"
11+
) {
12+
window[callback]();
13+
} else {
14+
console.warn(`Callback function ${callback} is not defined`);
15+
}
16+
};
17+
});
18+
419
document.querySelectorAll(".sample-remote").forEach((element) => {
520
const uiComponentName = element.dataset.componentName;
621
const fileName = element.dataset.sample;

src/main/resources/lib/samples/code.jelly

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,36 @@
33
<st:documentation>
44
Displays a copyable code snippet.
55

6-
<st:attribute name="code" />
7-
<st:attribute name="file" />
6+
<st:attribute name="code">
7+
Code snippet to be displayed if and only if "file" is not set.
8+
</st:attribute>
9+
<st:attribute name="callback">
10+
JavaScript function to be called when the "Execute code" button is clicked.
11+
If "code" attribute is set, these two attributes should be in sync ("code" cannot be inline JS anymore due to CSP).
12+
</st:attribute>
13+
<st:attribute name="file">
14+
File containing the code snippet to be displayed.
15+
If the "callback" attribute is set, the file will be referenced in a script tag, so that it can be invoked.
16+
</st:attribute>
817
<st:attribute name="language" use="optional">
918
Defaults to 'xml'.
1019
</st:attribute>
11-
<st:attribute name="executable" type="boolean" />
1220
<st:attribute name="previewable" type="boolean" />
1321
</st:documentation>
1422

1523
<div class="jdl-component-code">
1624
<pre class="jdl-component-code__code">
17-
<code class="language-${language != null ? language : 'xml'} ${file != null ? 'sample-remote' : ''}" data-component-name="${it.class.simpleName}" data-sample="${file}" data-executable="${executable}">
25+
<code class="language-${language != null ? language : 'xml'} ${file != null ? 'sample-remote' : ''}" data-component-name="${it.class.simpleName}" data-sample="${file}" data-executable="${callback != null}">
1826
${code}
1927
</code>
2028
</pre>
2129
<div class="jdl-component-code__controls">
2230
<div>
2331
<d:invokeBody />
2432

25-
<j:if test="${executable != null}">
33+
<j:if test="${callback != null}">
2634
<!-- This only works if the 'code' attribute is set -->
27-
<button class="jenkins-button jenkins-button--tertiary jenkins-!-build-color" onclick="${code}" tooltip="${%Execute code}">
35+
<button class="jenkins-button jenkins-button--tertiary jenkins-!-build-color callback-button" data-callback="${callback}" tooltip="${%Execute code}">
2836
<l:icon src="symbol-play" />
2937
</button>
3038
</j:if>

0 commit comments

Comments
 (0)