diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fa30b84e9..703a45b87 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,13 +9,13 @@ You could also go the [GitPod](https://gitpod.io/#https://github.com/jenkinsci/l If you have the proper environment, typing: ```sh - mvn verify +mvn verify ``` should create a plugin as `target/*.hpi`, which you can install in your Jenkins instance. Running ```sh - mvn hpi:run -Djenkins.version=2.361.1 +mvn hpi:run ``` allows you to spin up a test Jenkins instance on [localhost] to test your diff --git a/src/main/java/org/jenkins/plugins/lockableresources/BackwardCompatibility.java b/src/main/java/org/jenkins/plugins/lockableresources/BackwardCompatibility.java index 3654f810d..0a6abe87f 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/BackwardCompatibility.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/BackwardCompatibility.java @@ -33,8 +33,8 @@ private BackwardCompatibility() {} @Initializer(after = InitMilestone.JOB_LOADED) public static void compatibilityMigration() { - LOG.log(Level.FINE, "lockable-resource-plugin compatibility migration task run"); List resources = LockableResourcesManager.get().getResources(); + LOG.log(Level.FINE, "lockable-resource-plugin compatibility migration task run for " + resources.size() + " resources"); for (LockableResource resource : resources) { List queuedContexts = resource.getQueuedContexts(); if (!queuedContexts.isEmpty()) { diff --git a/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java b/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java index cde8dbe27..5ab0a40ee 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/LockableResource.java @@ -37,6 +37,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import jenkins.model.Jenkins; +import org.apache.commons.lang3.StringUtils; import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript; import org.jenkinsci.plugins.workflow.steps.StepContext; import org.kohsuke.accmod.Restricted; @@ -57,7 +58,10 @@ public class LockableResource extends AbstractDescribableImpl private final String name; private String description = ""; - private List labels = new ArrayList<>(); + /** @deprecated use labelsAsList instead due performance. + */ + @Deprecated private transient String labels = null; + private List labelsAsList = new ArrayList<>(); private String reservedBy = null; private Date reservedTimestamp = null; private String note = ""; @@ -125,9 +129,20 @@ protected Object readResolve() { if (queuedContexts == null) { // this field was added after the initial version if this class queuedContexts = new ArrayList<>(); } + this.repairLabels(); return this; } + private void repairLabels() { + if (this.labels == null) { + return; + } + + LOGGER.fine("Repair labels for resource " + this); + this.setLabels(this.labels); + this.labels = null; + } + /** @deprecated Replaced with LockableResourcesManager.queuedContexts (since 1.11) */ @Deprecated @ExcludeFromJacocoGeneratedReport @@ -170,20 +185,35 @@ public boolean isEphemeral() { return ephemeral; } + /** Use getLabelsAsList instead + * todo This function is marked as deprecated but it is still used in tests ans + * jelly (config) files. + */ + @Deprecated @Exported public String getLabels() { - return String.join(" ", this.labels); + if (this.labelsAsList == null) { + return ""; + } + return String.join(" ", this.labelsAsList); } + /** @deprecated no equivalent at the time. + * todo It shall be created new one function selLabelsAsList() and use that one. + * But it must be checked and changed all config.jelly files and + * this might takes more time as expected. + * That the reason why a deprecated function/property is still data-bound-setter + */ + // @Deprecated can not be used, because of JCaC @DataBoundSetter public void setLabels(String labels) { // todo use label parser from Jenkins.Label to allow the same syntax - this.labels = new ArrayList<>(); + this.labelsAsList = new ArrayList<>(); for(String label : labels.split("\\s+")) { if (label == null || label.isEmpty()) { continue; } - this.labels.add(label); + this.labelsAsList.add(label); } } @@ -193,7 +223,7 @@ public void setLabels(String labels) { */ @Exported public List getLabelsAsList() { - return this.labels; + return this.labelsAsList; } /** @@ -279,6 +309,7 @@ public boolean isReserved() { } @Restricted(NoExternalUse.class) + @CheckForNull public static String getUserName() { User current = User.current(); if (current != null) { @@ -294,7 +325,7 @@ public static String getUserName() { */ @Restricted(NoExternalUse.class) // called by jelly public boolean isReservedByCurrentUser() { - return (this.reservedBy != null && getUserName().equals(this.reservedBy)); + return (this.reservedBy != null && StringUtils.equals(getUserName(), this.reservedBy)); } @Exported diff --git a/src/main/java/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction.java b/src/main/java/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction.java index b5e872cb3..48f472c34 100644 --- a/src/main/java/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction.java +++ b/src/main/java/org/jenkins/plugins/lockableresources/actions/LockableResourcesRootAction.java @@ -32,6 +32,8 @@ import org.kohsuke.stapler.export.ExportedBean; import org.kohsuke.stapler.interceptor.RequirePOST; +import edu.umd.cs.findbugs.annotations.CheckForNull; + @Extension @ExportedBean public class LockableResourcesRootAction implements RootAction { @@ -79,6 +81,7 @@ public Api getApi() { return new Api(this); } + @CheckForNull public String getUserName() { return LockableResource.getUserName(); } @@ -111,9 +114,9 @@ public int getFreeResourceAmount(String label) { return LockableResourcesManager.get().getFreeResourceAmount(label); } - /** + /** * Get percentage (0-100) usage of resources assigned to given *label* - * + * * Used by {@code actions/LockableResourcesRootAction/index.jelly} * @since 2.19 * @param label Label to search. @@ -146,7 +149,7 @@ public int getNumberOfAllLabels() { /** * Get amount of resources assigned to given *label* - * + * * Used by {@code actions/LockableResourcesRootAction/index.jelly} * @param label Label to search. * @return Amount of assigned resources.