Skip to content

Commit ca821c6

Browse files
committed
feat: add dropdown for resource select strategies and fix empty resource strategy
- Add doFillResourceSelectStrategyItems with @RequirePOST and permission check - Validate resourceSelectStrategy in setter and throw exception for invalid values - Change config.jelly to use f:select instead of f:textbox Fixes security warning for missing permission check. Addresses review comment about silent failures for invalid strategy values.
1 parent 59635fe commit ca821c6

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

  • src/main

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import hudson.model.Item;
1010
import hudson.model.TaskListener;
1111
import hudson.util.FormValidation;
12+
import hudson.util.ListBoxModel;
1213
import java.io.Serializable;
1314
import java.util.ArrayList;
1415
import java.util.Arrays;
@@ -85,7 +86,20 @@ public void setInversePrecedence(boolean inversePrecedence) {
8586

8687
@DataBoundSetter
8788
public void setResourceSelectStrategy(String resourceSelectStrategy) {
88-
this.resourceSelectStrategy = resourceSelectStrategy;
89+
if (resourceSelectStrategy != null && !resourceSelectStrategy.isEmpty()) {
90+
// Validate the strategy is valid
91+
try {
92+
ResourceSelectStrategy.valueOf(resourceSelectStrategy.toUpperCase(Locale.ENGLISH));
93+
} catch (IllegalArgumentException e) {
94+
throw new IllegalArgumentException(Messages.error_invalidResourceSelectionStrategy(
95+
resourceSelectStrategy,
96+
Arrays.stream(ResourceSelectStrategy.values())
97+
.map(Enum::toString)
98+
.map(s -> s.toLowerCase(Locale.ENGLISH))
99+
.collect(Collectors.joining(", "))));
100+
}
101+
this.resourceSelectStrategy = resourceSelectStrategy;
102+
}
89103
}
90104

91105
@DataBoundSetter
@@ -153,6 +167,20 @@ public AutoCompletionCandidates doAutoCompleteResource(
153167
return RequiredResourcesProperty.DescriptorImpl.doAutoCompleteResourceNames(value, item);
154168
}
155169

170+
@RequirePOST
171+
public ListBoxModel doFillResourceSelectStrategyItems(@AncestorInPath Item item) {
172+
if (item != null) {
173+
item.checkPermission(Item.CONFIGURE);
174+
} else {
175+
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
176+
}
177+
ListBoxModel items = new ListBoxModel();
178+
for (ResourceSelectStrategy resSelStrategy : ResourceSelectStrategy.values()) {
179+
items.add(resSelStrategy.name());
180+
}
181+
return items;
182+
}
183+
156184
@RequirePOST
157185
public static FormValidation doCheckLabel(
158186
@QueryParameter String value, @QueryParameter String resource, @AncestorInPath Item item) {

src/main/resources/org/jenkins/plugins/lockableresources/LockStep/config.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<f:checkbox title="${%entry.skipIfLocked.title}"/>
2121
</f:entry>
2222
<f:entry title="${%entry.resourceSelectStrategy.title}" field="resourceSelectStrategy">
23-
<f:textbox/>
23+
<f:select/>
2424
</f:entry>
2525
<f:entry title="${%entry.priority.title}" field="priority">
2626
<f:number/>

0 commit comments

Comments
 (0)