From d76c533140938b0065af6c1310f7bc5c53917ad0 Mon Sep 17 00:00:00 2001 From: Massaki Date: Mon, 12 Jun 2023 10:46:36 -0300 Subject: [PATCH 1/8] Adding reason field --- .../lockableresources/LockableResource.java | 15 ++++++++++++++- .../LockableResourcesManager.java | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java b/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java index acb0aefe4..f126c28c0 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java @@ -62,6 +62,7 @@ public class LockableResource extends AbstractDescribableImpl private final String name; private String description = ""; + private String reason = ""; /** @deprecated use labelsAsList instead due performance. */ @Deprecated private transient String labels = null; @@ -115,7 +116,7 @@ public class LockableResource extends AbstractDescribableImpl /** @deprecated Use single-argument constructor instead (since 1.8) */ @Deprecated @ExcludeFromJacocoGeneratedReport - public LockableResource(String name, String description, String labels, String reservedBy, String note) { + public LockableResource(String name, String description, String labels, String reservedBy, String note, String reason) { // todo throw exception, when the name is empty // todo check if the name contains only valid characters (no spaces, new lines ...) this.name = name; @@ -123,6 +124,7 @@ public LockableResource(String name, String description, String labels, String r this.setLabels(labels); this.setReservedBy(reservedBy); this.setNote(note); + this.setReason(reason); } @DataBoundConstructor @@ -180,6 +182,16 @@ public void setDescription(String description) { this.description = Util.fixNull(description); } + @DataBoundSetter + public void setReason(String reason) { + this.reason = Util.fixNull(reason); + } + + @Exported + public String getReason() { + return this.reason; + } + @Exported public String getNote() { return this.note; @@ -308,6 +320,7 @@ public boolean scriptMatches( binding.setVariable("resourceDescription", description); binding.setVariable("resourceLabels", this.getLabelsAsList()); binding.setVariable("resourceNote", note); + binding.setVariable("resourceReason", reason); try { Object result = script.evaluate(Jenkins.get().getPluginManager().uberClassLoader, binding, null); diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java b/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java index 298a5c1b6..6c42e7081 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java @@ -103,6 +103,7 @@ public synchronized void setDeclaredResources(List declaredRes locked.setDescription(r.getDescription()); locked.setLabels(r.getLabels()); locked.setEphemeral(false); + locked.setReason(r.getReason()); locked.setNote(r.getNote()); mergedResources.add(locked); continue; @@ -115,6 +116,7 @@ public synchronized void setDeclaredResources(List declaredRes r.setDescription(""); r.setLabels(""); r.setNote(""); + r.setReason(""); r.setEphemeral(true); mergedResources.add(r); } From c7a55ef538b18ad34590887f61bfa9ae62942418 Mon Sep 17 00:00:00 2001 From: Massaki Date: Mon, 12 Jun 2023 14:51:16 -0300 Subject: [PATCH 2/8] extending .jelly resource files --- .../jenkins/plugins/lockableresources/LockStep/config.jelly | 3 +++ .../plugins/lockableresources/LockableResource/config.jelly | 3 +++ .../LockableResourcesRootAction/tableResources/table.jelly | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly index c481710b9..01161aa60 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly @@ -13,6 +13,9 @@ + + + diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly index df4a099a7..2d66f8bde 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly @@ -17,6 +17,9 @@ + + + diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly index 66910bf62..cf6f0e0a4 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly @@ -50,6 +50,8 @@ THE SOFTWARE. + + @@ -62,6 +64,7 @@ THE SOFTWARE. ${%resources.table.column.resource} ${%resources.table.column.status} ${%resources.table.column.timestamp} + ${%resources.table.column.reason} ${%resources.table.column.labels} ${%resources.table.column.properties} ${%resources.table.column.action} From 35e313ef6fe90ecd165bdb705a199576614b3f8f Mon Sep 17 00:00:00 2001 From: Massaki Date: Mon, 12 Jun 2023 16:25:35 -0300 Subject: [PATCH 3/8] setting reason form to work with lockable-resources.js --- .../reasonForm.jelly | 21 ++++++++++++++++ src/main/webapp/js/lockable-resources.js | 24 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/reasonForm.jelly diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/reasonForm.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/reasonForm.jelly new file mode 100644 index 000000000..d78883148 --- /dev/null +++ b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/reasonForm.jelly @@ -0,0 +1,21 @@ + + + + + + + +
+
+ + + +
+
+ +
+
+
+
+
diff --git a/src/main/webapp/js/lockable-resources.js b/src/main/webapp/js/lockable-resources.js index 4f9c863cb..f4f88fe5d 100644 --- a/src/main/webapp/js/lockable-resources.js +++ b/src/main/webapp/js/lockable-resources.js @@ -42,3 +42,27 @@ function replaceNote(element, resourceName) { }); return false; } + +function replaceReason(element, resourceName) { + var d = document.getElementById("reason-" + resourceName); + d.innerHTML = "
loading...
"; + fetch("reasonForm", { + method: "post", + headers: crumb.wrap({ + "Content-Type": "application/x-www-form-urlencoded", + }), + body: new URLSearchParams({ + resource: resourceName, + }), + }).then((rsp) => { + rsp.text().then((responseText) => { + d.innerHTML = responseText; + evalInnerHtmlScripts(responseText, function () { + Behaviour.applySubtree(d); + d.getElementsByTagName("TEXTAREA")[0].focus(); + }); + layoutUpdateCallback.call(); + }); + }); + return false; +} From f89dba9d999cf7cb17c0e4dfba992bbf972caee8 Mon Sep 17 00:00:00 2001 From: Massaki Date: Mon, 12 Jun 2023 16:25:51 -0300 Subject: [PATCH 4/8] extending LockStep to add setReason --- .../org/jenkins/plugins/lockableresources/LockStep.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java b/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java index 9025df2d9..a9408e481 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java @@ -36,6 +36,8 @@ public class LockStep extends Step implements Serializable { @CheckForNull public String label = null; + @CheckForNull public String reason = null; + public int quantity = 0; /** name of environment variable to store locked resources in */ @@ -63,6 +65,11 @@ public LockStep(@Nullable String resource) { public void setInversePrecedence(boolean inversePrecedence) { this.inversePrecedence = inversePrecedence; } + + @DataBoundSetter + public void setReason(String reason) { + this.reason = reason; + } @DataBoundSetter public void setResourceSelectStrategy(String resourceSelectStrategy) { From beda44fad921701f7bddfbe9658788b3f1f34933 Mon Sep 17 00:00:00 2001 From: Massaki Date: Tue, 13 Jun 2023 11:45:13 -0300 Subject: [PATCH 5/8] changing .properties files + fixing reason in .jelly files --- .../jenkins/plugins/lockableresources/LockStep/config.jelly | 2 +- .../plugins/lockableresources/LockStep/config.properties | 1 + .../plugins/lockableresources/LockableResource/config.jelly | 4 ++-- .../lockableresources/LockableResource/config.properties | 1 + .../tableResources/table.properties | 1 + 5 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly index 01161aa60..243585f1d 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly @@ -13,7 +13,7 @@ - + diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.properties b/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.properties index 05ec09e76..7fc8366ce 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.properties +++ b/src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.properties @@ -24,6 +24,7 @@ entry.resource.title=Resource entry.label.title=Label entry.quantity.title=Quantity entry.variable.title=Result variable +entry.reason.title=Reason entry.inversePrecedence.checkbox.title=Inverse precedence entry.inversePrecedence.skipIfLocked.title=Skip queue entry.resourceSelectStrategy.title=Strategy for resource selection diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly index 2d66f8bde..676a87c11 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.jelly @@ -18,8 +18,8 @@ - - + + diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.properties b/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.properties index 29239d255..2895437a2 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.properties +++ b/src/main/resources/org/jenkins/plugins/lockableresources/LockableResource/config.properties @@ -23,6 +23,7 @@ entry.name.title=Name entry.description.title=Description entry.labels.title=Labels +entry.reason.title=Reason entry.reservedBy.title=Reserved by entry.properties.title=Properties entry.properties.add=Add Property diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.properties b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.properties index 1de386550..785338e67 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.properties +++ b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.properties @@ -25,6 +25,7 @@ resources.table.column.index=Index resources.table.column.resource=Resource resources.table.column.status=Status resources.table.column.labels=Labels +resources.table.column.reason=Reason resources.table.column.properties=Properties resources.table.column.timestamp=Timestamp resources.table.column.action=Action From b7dbca5095700381c5e7ceb58160c10f68cb3fdd Mon Sep 17 00:00:00 2001 From: Massaki Date: Tue, 13 Jun 2023 11:45:33 -0300 Subject: [PATCH 6/8] adding reason to LockStepResource --- .../plugins/lockableresources/LockStepResource.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java b/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java index 76a31cf8d..276b1179c 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java @@ -28,13 +28,17 @@ public class LockStepResource extends AbstractDescribableImpl @CheckForNull public String label = null; + + @CheckForNull + public String reason = null; public int quantity = 0; - LockStepResource(@Nullable String resource, @Nullable String label, int quantity) { + LockStepResource(@Nullable String resource, @Nullable String label, int quantity, String reason) { this.resource = resource; this.label = label; this.quantity = quantity; + this.reason = reason; } @DataBoundConstructor @@ -51,6 +55,13 @@ public void setLabel(String label) { } } + @DataBoundSetter + public void setReason(String reason) { + if (reason != null && !reason.isEmpty()) { + this.reason = reason; + } + } + @DataBoundSetter public void setQuantity(int quantity) { this.quantity = quantity; From c685ca3cbe16318466fa1c75c572028e415cdc0a Mon Sep 17 00:00:00 2001 From: Massaki Date: Tue, 13 Jun 2023 14:30:31 -0300 Subject: [PATCH 7/8] adding new to table.jelly --- .../tableResources/table.jelly | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly index cf6f0e0a4..0ed2189d6 100644 --- a/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly +++ b/src/main/resources/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction/tableResources/table.jelly @@ -38,7 +38,7 @@ THE SOFTWARE. id="lockable-resources" data-remember-search-text="true" isLoaded="true" - data-columns-definition="[null, null, null, null, null, null, null]" + data-columns-definition="[null, null, null, null, null, null, null, null]" data-table-configuration="{}" > @@ -209,7 +209,30 @@ THE SOFTWARE. + + + +
+
+ ${resource.reason} +
+
+
+ + + +
+
+ ${resource.reason} +
+
+
+ From d2e73b8b7402a95656d4a690b3957727829b27d3 Mon Sep 17 00:00:00 2001 From: Massaki Date: Wed, 14 Jun 2023 11:17:01 -0300 Subject: [PATCH 8/8] updating the tests with the reason field --- .../org/jenkins/plugins/lockableresources/LockStep.java | 4 ++-- .../jenkins/plugins/lockableresources/LockStepResource.java | 6 +++--- .../plugins/lockableresources/DeclarativePipelineTest.java | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java b/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java index a9408e481..9b86c7819 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockStep.java @@ -179,7 +179,7 @@ public String toString() { .map(res -> "{" + res.toString() + "}") .collect(Collectors.joining(",")); } else if (resource != null || label != null) { - return LockStepResource.toString(resource, label, quantity); + return LockStepResource.toString(resource, label, quantity, reason); } else { return "nothing"; } @@ -193,7 +193,7 @@ public void validate() { public List getResources() { List resources = new ArrayList<>(); if (resource != null || label != null) { - resources.add(new LockStepResource(resource, label, quantity)); + resources.add(new LockStepResource(resource, label, quantity, reason)); } if (extra != null) { diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java b/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java index 276b1179c..835c29ac0 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockStepResource.java @@ -34,7 +34,7 @@ public class LockStepResource extends AbstractDescribableImpl public int quantity = 0; - LockStepResource(@Nullable String resource, @Nullable String label, int quantity, String reason) { + LockStepResource(@Nullable String resource, @Nullable String label, int quantity, @Nullable String reason) { this.resource = resource; this.label = label; this.quantity = quantity; @@ -69,10 +69,10 @@ public void setQuantity(int quantity) { @Override public String toString() { - return toString(resource, label, quantity); + return toString(resource, label, quantity, reason); } - public static String toString(String resource, String label, int quantity) { + public static String toString(String resource, String label, int quantity, String reason) { // a label takes always priority if (label != null) { if (quantity > 0) { diff --git a/src/test/java/org/jenkins/plugins/lockableresources/DeclarativePipelineTest.java b/src/test/java/org/jenkins/plugins/lockableresources/DeclarativePipelineTest.java index e6c313883..1f2d6199f 100644 --- a/src/test/java/org/jenkins/plugins/lockableresources/DeclarativePipelineTest.java +++ b/src/test/java/org/jenkins/plugins/lockableresources/DeclarativePipelineTest.java @@ -74,7 +74,7 @@ public void stepScriptLockByLabel() throws Exception { " stage('test') {", " steps {", " script {", - " lock(label: 'label1', resource : null, variable: 'LABEL_LOCKED', quantity: 1) {", + " lock(label: 'label1', resource : null, variable: 'LABEL_LOCKED', quantity: 1, reason: 'test') {", " echo \"Lock acquired: ${LABEL_LOCKED}\"", " }", " }", @@ -98,7 +98,7 @@ public void stepLockByLabel() throws Exception { " stages {", " stage('test') {", " steps {", - " lock(label: 'label1', resource : null, variable: 'LABEL_LOCKED', quantity: 1) {", + " lock(label: 'label1', resource : null, variable: 'LABEL_LOCKED', quantity: 1, reason: 'test') {", " echo \"Lock acquired: ${LABEL_LOCKED}\"", " }", " }",