Skip to content

Commit 651617d

Browse files
authored
Merge pull request jenkinsci#113 from haithir/allow-groovy-specify-number-resources
groovy and number of resources fixed
2 parents 06d9c3a + 9af9a51 commit 651617d

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/main/java/org/jenkins/plugins/lockableresources/LockableResourcesManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public synchronized List<LockableResource> tryQueue(LockableResourcesStruct requ
216216
return null;
217217
}
218218

219+
boolean candidatesByScript=false;
219220
List<LockableResource> candidates = new ArrayList<LockableResource>();
220221
final SecureGroovyScript systemGroovyScript = requiredResources.getResourceMatchScript();
221222
if (requiredResources.label != null && requiredResources.label.isEmpty() && systemGroovyScript == null) {
@@ -224,6 +225,7 @@ public synchronized List<LockableResource> tryQueue(LockableResourcesStruct requ
224225
candidates = getResourcesWithLabel(requiredResources.label, params);
225226
} else {
226227
candidates = getResourcesMatchingScript(systemGroovyScript, params);
228+
candidatesByScript = true;
227229
}
228230

229231
for (LockableResource rs : candidates) {
@@ -234,7 +236,17 @@ public synchronized List<LockableResource> tryQueue(LockableResourcesStruct requ
234236
}
235237

236238
// if did not get wanted amount or did not get all
237-
int required_amount = number == 0 ? candidates.size() : number;
239+
final int required_amount;
240+
if (candidatesByScript && candidates.size() == 0) {
241+
/**
242+
* If the groovy script does not return any candidates, it means nothing is needed, even
243+
* if a higher amount is specified. A valid use case is a Matrix job, when not all
244+
* configurations need resources.
245+
*/
246+
required_amount = 0;
247+
} else {
248+
required_amount = number == 0 ? candidates.size() : number;
249+
}
238250

239251
if (selected.size() != required_amount) {
240252
log.log(Level.FINEST, "{0} found {1} resource(s) to queue." +

src/main/java/org/jenkins/plugins/lockableresources/RequiredResourcesProperty.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,14 @@ public FormValidation doCheckLabelName(
206206

207207
public FormValidation doCheckResourceNumber(@QueryParameter String value,
208208
@QueryParameter String resourceNames,
209-
@QueryParameter String labelName) {
209+
@QueryParameter String labelName,
210+
@QueryParameter String resourceMatchScript)
211+
{
210212

211213
String number = Util.fixEmptyAndTrim(value);
212214
String names = Util.fixEmptyAndTrim(resourceNames);
213215
String label = Util.fixEmptyAndTrim(labelName);
216+
String script = Util.fixEmptyAndTrim(resourceMatchScript);
214217

215218
if (number == null || number.equals("") || number.trim().equals("0")) {
216219
return FormValidation.ok();
@@ -226,9 +229,9 @@ public FormValidation doCheckResourceNumber(@QueryParameter String value,
226229
int numResources = 0;
227230
if (names != null) {
228231
numResources = names.split("\\s+").length;
229-
} else if (label != null) {
230-
numResources = Integer.MAX_VALUE;
231-
}
232+
} else if (label != null || script != null) {
233+
numResources = Integer.MAX_VALUE;
234+
}
232235

233236
if (numResources < numAsInt) {
234237
return FormValidation.error(String.format(

src/main/java/org/jenkins/plugins/lockableresources/queue/LockableResourcesCandidatesStruct.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ public LockableResourcesCandidatesStruct(List<LockableResource> candidates, int
1414
this.candidates = candidates;
1515
this.requiredAmount = requiredAmount;
1616
}
17+
18+
@Override
19+
public String toString()
20+
{
21+
return "LockableResourcesCandidatesStruct [candidates=" + candidates + ", requiredAmount=" + requiredAmount
22+
+ ", selected=" + selected + "]";
23+
}
24+
1725
}

0 commit comments

Comments
 (0)