Skip to content

Commit

Permalink
Backward compatibility fix for resource-labels (#437)
Browse files Browse the repository at this point in the history
Backward compatibility fix for resource-labels
  • Loading branch information
mPokornyETM authored Jan 5, 2023
1 parent 9488cda commit c9153f1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LockableResource> resources = LockableResourcesManager.get().getResources();
LOG.log(Level.FINE, "lockable-resource-plugin compatibility migration task run for " + resources.size() + " resources");
for (LockableResource resource : resources) {
List<StepContext> queuedContexts = resource.getQueuedContexts();
if (!queuedContexts.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,7 +58,10 @@ public class LockableResource extends AbstractDescribableImpl<LockableResource>

private final String name;
private String description = "";
private List<String> labels = new ArrayList<>();
/** @deprecated use labelsAsList instead due performance.
*/
@Deprecated private transient String labels = null;
private List<String> labelsAsList = new ArrayList<>();
private String reservedBy = null;
private Date reservedTimestamp = null;
private String note = "";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -193,7 +223,7 @@ public void setLabels(String labels) {
*/
@Exported
public List<String> getLabelsAsList() {
return this.labels;
return this.labelsAsList;
}

/**
Expand Down Expand Up @@ -279,6 +309,7 @@ public boolean isReserved() {
}

@Restricted(NoExternalUse.class)
@CheckForNull
public static String getUserName() {
User current = User.current();
if (current != null) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -79,6 +81,7 @@ public Api getApi() {
return new Api(this);
}

@CheckForNull
public String getUserName() {
return LockableResource.getUserName();
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c9153f1

Please sign in to comment.