@@ -72,6 +72,12 @@ public class LockableResource extends AbstractDescribableImpl<LockableResource>
7272 private Date reservedTimestamp = null ;
7373 private String note = "" ;
7474
75+ /**
76+ * The reason why this resource is currently locked. Set via the lock() step's reason parameter.
77+ * Cleared when the resource is unlocked.
78+ */
79+ private String lockReason = "" ;
80+
7581 /**
7682 * Track that a currently reserved resource was originally reserved for someone else, or locked
7783 * for some other job, and explicitly taken away - e.g. for SUT post-mortem while a test job runs.
@@ -255,6 +261,26 @@ public void setNote(@Nullable String note) {
255261 this .note = Util .fixNull (note );
256262 }
257263
264+ /**
265+ * Returns the reason why this resource is currently locked.
266+ *
267+ * @return The lock reason, or empty string if not set.
268+ */
269+ @ Exported
270+ public String getLockReason () {
271+ return this .lockReason ;
272+ }
273+
274+ /**
275+ * Sets the reason why this resource is being locked. This is typically set via the lock() step's
276+ * reason parameter and cleared when the resource is unlocked.
277+ *
278+ * @param lockReason The reason for locking, or null to clear.
279+ */
280+ public void setLockReason (@ Nullable String lockReason ) {
281+ this .lockReason = Util .fixNull (lockReason );
282+ }
283+
258284 @ DataBoundSetter
259285 public void setEphemeral (boolean ephemeral ) {
260286 this .ephemeral = ephemeral ;
@@ -422,6 +448,7 @@ private boolean evaluateScript(@NonNull SecureGroovyScript script, @CheckForNull
422448 binding .setVariable ("resourceDescription" , description );
423449 binding .setVariable ("resourceLabels" , this .getLabelsAsList ());
424450 binding .setVariable ("resourceNote" , note );
451+ binding .setVariable ("resourceLockReason" , lockReason );
425452 try {
426453 Object result = script .evaluate (Jenkins .get ().getPluginManager ().uberClassLoader , binding , null );
427454 if (LOGGER .isLoggable (Level .FINE )) {
@@ -650,20 +677,27 @@ public boolean isStolen() {
650677 }
651678
652679 public void reserve (String userName ) {
680+ reserve (userName , null );
681+ }
682+
683+ public void reserve (String userName , String reason ) {
653684 setReservedBy (userName );
654685 setReservedTimestamp (new Date ());
686+ setLockReason (reason );
655687 }
656688
657689 public void unReserve () {
658690 this .setReservedBy (null );
659691 this .setReservedTimestamp (null );
692+ this .setLockReason (null );
660693 this .stolen = false ;
661694 }
662695
663696 public void reset () {
664697 this .unReserve ();
665698 this .unqueue ();
666699 this .setBuild (null );
700+ this .setLockReason (null );
667701 invalidateCaches ();
668702 }
669703
@@ -678,6 +712,7 @@ public void copyUnconfigurableProperties(final LockableResource sourceResource)
678712 setReservedTimestamp (sourceResource .getReservedTimestamp ());
679713 setNote (sourceResource .getNote ());
680714 setReservedBy (sourceResource .getReservedBy ());
715+ setLockReason (sourceResource .getLockReason ());
681716 }
682717 }
683718
@@ -689,6 +724,7 @@ public void resetUnconfigurableProperties() {
689724 setReservedBy (null );
690725 setReservedTimestamp (null );
691726 setNote ("" );
727+ setLockReason ("" );
692728 }
693729
694730 /**
0 commit comments