Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-73949] Extract inline event handlers from LockableResourcesRootAction/tableResources/table.jelly #720

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ THE SOFTWARE.
</div>
<div class="col-auto jenkins-!-margin-right-2">
<l:hasPermission permission="${it.RESERVE}">
<a class="jenkins-table__link" id="note-link" href="editNote" onclick="return replaceNote(this, '${resource.name}');">
<a class="jenkins-table__link lockable-resources-replace-note" data-resource-name="${resource.name}"
id="note-link" href="editNote">
<l:icon class="symbol-edit-note icon-sm" />
${%btn.editNote}
</a>
Expand Down Expand Up @@ -251,17 +252,17 @@ THE SOFTWARE.
<j:when test="${resource.locked}">
<l:hasPermission permission="${it.UNLOCK}">
<button
onClick="resource_action(this, 'unlock');"
class="jenkins-button j.jenkins-!-warning-color"
data-action="unlock"
class="jenkins-button j.jenkins-!-warning-color lockable-resources-action-button"
tooltip="${%btn.unlock.detail}"
>
${%btn.unlock}
</button>
</l:hasPermission>
<l:hasPermission permission="${it.STEAL}">
<button
onClick="resource_action(this, 'steal');"
class="jenkins-button jenkins-!-destructive-color"
data-action="steal"
class="jenkins-button jenkins-!-destructive-color lockable-resources-action-button"
tooltip="${%btn.steal.detail}"
>
${%btn.steal}
Expand All @@ -272,8 +273,8 @@ THE SOFTWARE.
<l:hasPermission permission="${it.RESERVE}">
<j:if test="${resource.isReservedByCurrentUser() or h.hasPermission(app.ADMINISTER)}">
<button
onClick="resource_action(this, 'unreserve');"
class="jenkins-button jenkins-!-success-color"
data-action="unreserve"
class="jenkins-button jenkins-!-success-color lockable-resources-action-button"
tooltip="${%btn.unReserve.detail}"
>
${%btn.unReserve}
Expand All @@ -283,8 +284,8 @@ THE SOFTWARE.
<l:hasPermission permission="${it.STEAL}">
<j:if test="${!resource.isReservedByCurrentUser()}">
<button
onClick="resource_action(this, 'reassign');"
class="jenkins-button jenkins-button--primary"
data-action="reassign"
class="jenkins-button jenkins-button--primary lockable-resources-action-button"
tooltip="${%btn.reassign.detail}"
>
${%btn.reassign}
Expand All @@ -295,8 +296,8 @@ THE SOFTWARE.
<j:when test="${resource.queued}">
<l:hasPermission permission="${it.UNLOCK}">
<button
onClick="resource_action(this, 'reset');"
class="jenkins-button jenkins-!-destructive-color"
data-action="reset"
class="jenkins-button jenkins-!-destructive-color lockable-resources-action-button"
tooltip="${%btn.reset.detail}"
>
${%btn.reset}
Expand All @@ -306,8 +307,8 @@ THE SOFTWARE.
<j:otherwise>
<l:hasPermission permission="${it.RESERVE}">
<button
onClick="resource_action(this, 'reserve');"
class="jenkins-button jenkins-button--primary"
data-action="reserve"
class="jenkins-button jenkins-button--primary lockable-resources-action-button"
tooltip="${%btn.reserve.detail}"
>
${%btn.reserve}
Expand Down
18 changes: 16 additions & 2 deletions src/main/webapp/js/lockable-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function resource_action(button, action) {
});
}

function replaceNote(element, resourceName) {
function replaceNote(resourceName) {
var d = document.getElementById("note-" + resourceName);
d.innerHTML = "<div class='spinner-right' style='flex-grow: 1;'>loading...</div>";
fetch("noteForm", {
Expand All @@ -80,7 +80,6 @@ function replaceNote(element, resourceName) {
layoutUpdateCallback.call();
});
});
return false;
}

function format(d) {
Expand Down Expand Up @@ -146,6 +145,21 @@ jQuery(document).ready(function () {
tr.addClass('shown');
}
});

document.querySelectorAll(".lockable-resources-action-button").forEach(function (button) {
button.addEventListener("click", function (event) {
const target = event.target;
const action = target.dataset.action;
resource_action(target, action);
});
});

document.querySelectorAll(".lockable-resources-replace-note").forEach(function (anchor) {
anchor.addEventListener("click", function (event) {
event.preventDefault();
replaceNote(event.target.dataset.resourceName);
});
});
});


Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private void checkXssWithResourceName(String resourceName) throws Exception {

HtmlElement reserveButton = null;
for (HtmlElement b : allButtons) {
String onClick = b.getAttribute("onClick");
if (onClick != null && onClick.contains("reserve")) {
String action = b.getAttribute("data-action");
if (action != null && action.contains("reserve")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this test makes enough sense to keep it around at this point. The code for which it was originally written has been changed to the point that it's barely recognizable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A valid point; on the other hand, it does provide some automated coverage of the frontend, which is arguably valuable in and of itself…

reserveButton = b;
}
}
Expand Down