Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
JENKINS-18967 add helper to support working with wrapped build steps
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/org/jenkinsci/plugins/conditionalbuildstep/ConditionalBuildStepHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.jenkinsci.plugins.conditionalbuildstep; | ||
|
||
import hudson.model.Project; | ||
import hudson.tasks.BuildStep; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.jenkinsci.plugins.conditionalbuildstep.singlestep.SingleConditionalBuilder; | ||
|
||
/** | ||
* Helper to work with {@link BuildStep}s wrapped by {@link ConditionalBuilder} or {@link SingleConditionalBuilder}. | ||
* | ||
* @author Dominik Bartholdi (imod) | ||
* | ||
*/ | ||
public class ConditionalBuildStepHelper { | ||
|
||
private ConditionalBuildStepHelper() { | ||
} | ||
|
||
/** | ||
* Gets the list of all buildsteps wrapped within any {@link ConditionalBuilder} or {@link SingleConditionalBuilder} from within the given project. | ||
* | ||
* @param p | ||
* the project to get all wrapped builders for | ||
* @param type | ||
* the type of builders to search for | ||
* @return a list of all buildsteps, never <code>null</code> | ||
*/ | ||
public static <T extends BuildStep> List<T> getContainedBuilders(Project<?, ?> p, Class<T> type) { | ||
|
||
List<T> r = new ArrayList<T>(); | ||
|
||
List<ConditionalBuilder> cbuilders = p.getBuildersList().getAll(ConditionalBuilder.class); | ||
for (ConditionalBuilder conditionalBuilder : cbuilders) { | ||
final List<BuildStep> cbs = conditionalBuilder.getConditionalbuilders(); | ||
if (cbs != null) { | ||
for (BuildStep buildStep : cbs) { | ||
if (type.isInstance(buildStep)) { | ||
r.add(type.cast(buildStep)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
List<SingleConditionalBuilder> scb = p.getBuildersList().getAll(SingleConditionalBuilder.class); | ||
for (SingleConditionalBuilder singleConditionalBuilder : scb) { | ||
BuildStep buildStep = singleConditionalBuilder.getBuildStep(); | ||
if (buildStep != null && type.isInstance(buildStep)) { | ||
r.add(type.cast(buildStep)); | ||
} | ||
} | ||
|
||
return r; | ||
} | ||
|
||
} |