Skip to content
Permalink
Browse files
Merge branch 'JENKINS-13872' of git://github.com/cjo9900/parameterize…
…d-trigger-plugin into 2.17-RC
  • Loading branch information
cjo9900 committed Jan 29, 2013
2 parents fae74af + 89e0f19 commit e681c6924350457cd5db8c5e4c13aa1fda2a42ac
@@ -267,16 +267,20 @@ private static ParametersAction getDefaultParameters(AbstractProject<?,?> projec
dynamicBuildParameters.add(Collections.<AbstractBuildParameters>emptyList());
for (AbstractBuildParameterFactory configFactory : configFactories) {
List<List<AbstractBuildParameters>> newDynParameters = Lists.newArrayList();
for (AbstractBuildParameters config : configFactory.getParameters(build, listener)) {
for (List<AbstractBuildParameters> dynamicBuildParameter : dynamicBuildParameters) {
newDynParameters.add(
ImmutableList.<AbstractBuildParameters>builder()
.addAll(dynamicBuildParameter)
.add(config)
.build());
List<AbstractBuildParameters> factoryParameters = configFactory.getParameters(build, listener);
// if factory returns 0 parameters we need to skip assigning newDynParameters to dynamicBuildParameters as we would add invalid list
if(factoryParameters.size() > 0) {
for (AbstractBuildParameters config : factoryParameters) {
for (List<AbstractBuildParameters> dynamicBuildParameter : dynamicBuildParameters) {
newDynParameters.add(
ImmutableList.<AbstractBuildParameters>builder()
.addAll(dynamicBuildParameter)
.add(config)
.build());
}
}
dynamicBuildParameters = newDynParameters;
}
dynamicBuildParameters = newDynParameters;
}
return dynamicBuildParameters;
}
@@ -20,27 +20,67 @@

/**
* A BuildParameterFactory generating Predefined Parameters for a counter
*
* @author Chris Johnson
*/
public class CounterBuildParameterFactory extends AbstractBuildParameterFactory {

private final String from;
private final String to;
private final String step;
private final String paramExpr;
private final SteppingValidationEnum validationFail;

public enum SteppingValidationEnum {
FAIL("Fail the build step"), // previous behaviour (default)
SKIP("Don't trigger these projects"){
@Override
public void failCheck(TaskListener listener) throws AbstractBuildParameters.DontTriggerException {
listener.getLogger().println(Messages.CounterBuildParameterFactory_CountingWillNotTerminateSkipping());
throw new AbstractBuildParameters.DontTriggerException();
}},
NOPARMS("Skip these parameters"){
@Override
public void failCheck(TaskListener listener) throws AbstractBuildParameters.DontTriggerException {
listener.getLogger().println(Messages.CounterBuildParameterFactory_CountingWillNotTerminateIgnore());
}};

private String description;

public String getDescription() {
return description;
}
SteppingValidationEnum(String description) {
this.description = description;
}

public void failCheck(TaskListener listener) throws AbstractBuildParameters.DontTriggerException {
throw new RuntimeException(Messages.CounterBuildParameterFactory_CountingWillNotTerminate());
}
}

public CounterBuildParameterFactory(long from, long to, long step, String paramExpr) {
this(Long.toString(from), Long.toString(to), Long.toString(step), paramExpr);
}
public CounterBuildParameterFactory(long from, long to, long step, String paramExpr, SteppingValidationEnum validationFail) {
this(Long.toString(from), Long.toString(to), Long.toString(step), paramExpr, validationFail);
}

@DataBoundConstructor
public CounterBuildParameterFactory(String from, String to, String step, String paramExpr) {
// mimic old behaviour which failed job
this(from, to, step, paramExpr, SteppingValidationEnum.FAIL);
}

@DataBoundConstructor
public CounterBuildParameterFactory(String from, String to, String step, String paramExpr, SteppingValidationEnum validationFail) {
this.from = from;
this.to = to;
this.step = step;
this.paramExpr = paramExpr;
this.validationFail = validationFail;
}


@Override
public List<AbstractBuildParameters> getParameters(AbstractBuild<?, ?> build, TaskListener listener) throws IOException, InterruptedException, AbstractBuildParameters.DontTriggerException {
EnvVars envVars = build.getEnvironment(listener);
@@ -56,14 +96,13 @@ public CounterBuildParameterFactory(String from, String to, String step, String
params.add(getParameterForCount(fromNum));
} else {
if (stepNum == 0) {
throw new RuntimeException(Messages.CounterBuildParameterFactory_CountingWillNotTerminate());
}
if (upDown * stepNum < 0) {
throw new RuntimeException(Messages.CounterBuildParameterFactory_CountingWillNotTerminate());
}

for (Long i = fromNum; upDown * i <= upDown * toNum; i += stepNum) {
params.add(getParameterForCount(i));
validationFail.failCheck(listener);
} else if (upDown * stepNum < 0) {
validationFail.failCheck(listener);
} else {
for (Long i = fromNum; upDown * i <= upDown * toNum; i += stepNum) {
params.add(getParameterForCount(i));
}
}
}
return params;
@@ -127,6 +166,10 @@ public String getParamExpr() {
return paramExpr;
}

public SteppingValidationEnum getvalidationFail() {
return validationFail;
}

private static final VariableResolver<String> EMPTY_STRING_VARIABLE_RESOLVER = new VariableResolver<String>() {

@Override
@@ -35,53 +35,98 @@
/**
*
* @author shoughton
* @author Chris Johnson
*/
public class FileBuildParameterFactory extends AbstractBuildParameterFactory {

/**
* Enum containing the action that could occur when there are no files found in the workspace
*
*/
public enum NoFilesFoundEnum {
SKIP("Don't trigger these projects"){ // previous behaviour (default)
@Override
public void failCheck(TaskListener listener) throws AbstractBuildParameters.DontTriggerException {
listener.getLogger().println(Messages.FileBuildParameterFactory_NoFilesFoundSkipping());
throw new AbstractBuildParameters.DontTriggerException();
}},
NOPARMS("Skip these parameters"){
@Override
public void failCheck(TaskListener listener) throws AbstractBuildParameters.DontTriggerException {
listener.getLogger().println(Messages.FileBuildParameterFactory_NoFilesFoundIgnore());
}},
FAIL("Fail the build step"){
@Override
public void failCheck(TaskListener listener) throws AbstractBuildParameters.DontTriggerException {
listener.getLogger().println(Messages.FileBuildParameterFactory_NoFilesFoundTerminate());
throw new RuntimeException();
}};

private String description;

public String getDescription() {
return description;
}

NoFilesFoundEnum(String description) {
this.description = description;
}

public abstract void failCheck(TaskListener listener) throws AbstractBuildParameters.DontTriggerException;
}

private final String filePattern;
private final NoFilesFoundEnum noFilesFoundAction;

@DataBoundConstructor
public FileBuildParameterFactory(String filePattern) {
public FileBuildParameterFactory(String filePattern, NoFilesFoundEnum noFilesFoundAction) {
this.filePattern = filePattern;
this.noFilesFoundAction = noFilesFoundAction;
}

public FileBuildParameterFactory(String filePattern) {
this(filePattern, NoFilesFoundEnum.SKIP);
}

public String getFilePattern() {
return filePattern;
}

public NoFilesFoundEnum getNoFilesFoundAction() {
return noFilesFoundAction;
}

@Override
public List<AbstractBuildParameters> getParameters(AbstractBuild<?, ?> build, TaskListener listener) throws IOException, InterruptedException, AbstractBuildParameters.DontTriggerException {

List<AbstractBuildParameters> result = Lists.newArrayList();

try {
FilePath workspace = getWorkspace(build);
FilePath[] files = workspace.list(getFilePattern());
for(FilePath f: files) {
String parametersStr = f.readToString();
Logger.getLogger(FileBuildParameterFactory.class.getName()).log(Level.INFO, null, "Triggering build with " + f.getBaseName());
result.add(new PredefinedBuildParameters(parametersStr));
if(files.length == 0) {
noFilesFoundAction.failCheck(listener);
} else {
for(FilePath f: files) {
String parametersStr = f.readToString();
Logger.getLogger(FileBuildParameterFactory.class.getName()).log(Level.INFO, null, "Triggering build with " + f.getName());
result.add(new PredefinedBuildParameters(parametersStr));
}
}

} catch (Exception ex) {
} catch (IOException ex) {
Logger.getLogger(FileBuildParameterFactory.class.getName()).log(Level.SEVERE, null, ex);
}

return result;
}

private FilePath getWorkspace(AbstractBuild build) {
FilePath workspace = build.getWorkspace();
if (workspace == null) {
workspace = build.getProject().getSomeWorkspace();
}
return workspace;
}

//private PredefinedBuildParameters getParameterBlock() {
// String stringWithCount = Util.replaceMacro(paramExpr, ImmutableMap.of("CHANGELIST", changelist.toString()));
// return new PredefinedBuildParameters(stringWithCount);
//}

@Extension
public static class DescriptorImpl extends AbstractBuildParameterFactoryDescriptor {
@@ -9,7 +9,9 @@
<f:entry field="step" title="${%Step}">
<f:textbox default="1"/>
</f:entry>

<f:entry field="validationFail" title="${%Action to perform when stepping validation fails}">
<f:enum>${it.getDescription()}</f:enum>
</f:entry>
<f:entry field="paramExpr" title="${%Parameters}">
<f:textarea />
</f:entry>
@@ -0,0 +1,9 @@
<div>
This option determines the behaviour to take if the from,to and step values do not form a valid combination. One that gives a finite number of combinations.<br/>
The actions that can be taken are:
<ul>
<li><b>Fail the build step</b> This will fail the buildstep with an RuntimeException. (This is the default, and occured in older versions)</li>
<li><b>Don't trigger these projects</b> This will cause the projects not to be triggered at all for this configuration.</li>
<li><b>Skip these parameters</b> This will allow the projects to be triggered without these parameters.</li>
</ul>
</div>
@@ -3,5 +3,8 @@
<f:entry field="filePattern" title="${%File Pattern}">
<f:textarea />
</f:entry>
<f:entry field="noFilesFoundAction" title="${%Action to perform when no files found}">
<f:enum>${it.getDescription()}</f:enum>
</f:entry>

</j:jelly>
@@ -0,0 +1,9 @@
<div>
This option determines the behaviour to take if no files are found that match the pattern.<br/>
The actions that can be taken are:
<ul>
<li><b>Don't trigger these projects</b> This will cause the projects not to be triggered at all for this configuration. </li>
<li><b>Skip these parameters</b> This will allow the projects to be triggered without these parameters. (This is the default, and occured in older versions)</li>
<li><b>Fail the build step</b> This will fail the buildstep with an RuntimeException.</li>
</ul>
</div>
@@ -0,0 +1,6 @@
<div>
This ParameterFactory generates parameters based on the contents of the files found.

<p />
For each different file found a build of the project(s) is triggered.
</div>
@@ -1,3 +1,9 @@
CounterBuildParameterFactory.CounterBuildParameterFactory=Counter Parameter Factory
CounterBuildParameterFactory.CountingWillNotTerminate=Counting with step size 0 will not terminate!
FileBuildParameterFactory.FileBuildParameterFactory=File Parameter Factory
CounterBuildParameterFactory.CountingWillNotTerminate=To, from and step form a infinite set, counting will not terminate!
CounterBuildParameterFactory.CountingWillNotTerminateSkipping=To, from and step form a infinite set, counting will not terminate! Triggering Skipped.
CounterBuildParameterFactory.CountingWillNotTerminateIgnore=To, from and step form a infinite set, counting will not terminate! Parameters Ignored.

FileBuildParameterFactory.FileBuildParameterFactory=File Parameter Factory
FileBuildParameterFactory.NoFilesFoundSkipping=No Files Found! Triggering Skipped.
FileBuildParameterFactory.NoFilesFoundIgnore=No Files Found! Parameters Ignored.
FileBuildParameterFactory.NoFilesFoundTerminate=No Files Found!
@@ -5,6 +5,7 @@
import hudson.model.TaskListener;
import hudson.plugins.parameterizedtrigger.AbstractBuildParameters;
import hudson.plugins.parameterizedtrigger.CounterBuildParameterFactory;
import hudson.plugins.parameterizedtrigger.CounterBuildParameterFactory.SteppingValidationEnum;
import org.junit.Test;

import java.io.IOException;
@@ -13,6 +14,10 @@
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

/**
*
* @author Chris Johnson
*/
public class CounterBuildParameterFactoryUnitTest {

@Test
@@ -39,8 +44,10 @@ public void countingWithNoIncrementShouldNotWork() throws Exception {
}

@Test
public void countingWithNoIncrementOnlyOneElementShouldNotWork() throws Exception {
getParameters(1,1,0);
public void countingWithNoIncrementOnlyOneElement() throws Exception {
// step is ignored if from and to are equal
List<AbstractBuildParameters> parameters = getParameters(1,1,0);
assertEquals(1, parameters.size());
}

@Test
@@ -49,12 +56,37 @@ public void countingWhenFromToIsSameShouldWork() throws Exception {
assertEquals(1, parameters.size());
}

@Test(expected = RuntimeException.class)
public void countingWithNoIncrementShouldNotWork1() throws Exception {
getParameters(1,2,0,SteppingValidationEnum.FAIL);
}

@Test(expected = AbstractBuildParameters.DontTriggerException.class)
public void countingWithNoIncrementShouldNotWork2() throws Exception {
getParameters(1,2,0,SteppingValidationEnum.SKIP);

}

@Test
public void countingWithNoIncrementShouldNotWork3() throws Exception {
List<AbstractBuildParameters> parameters = getParameters(1,2,0,SteppingValidationEnum.NOPARMS);
assertEquals(0, parameters.size());
}

private List<AbstractBuildParameters> getParameters(long from, long to, long step) throws IOException, InterruptedException, AbstractBuildParameters.DontTriggerException {
return getParameters(from, to, step, SteppingValidationEnum.FAIL);
}

private List<AbstractBuildParameters> getParameters(long from, long to, long step, SteppingValidationEnum validationFailure) throws IOException, InterruptedException, AbstractBuildParameters.DontTriggerException {
AbstractBuild<?,?> build = mock(AbstractBuild.class);
EnvVars vars = new EnvVars();
TaskListener listener = mock(TaskListener.class);
when(build.getEnvironment(listener)).thenReturn(vars);
CounterBuildParameterFactory counterFactory = new CounterBuildParameterFactory(from,to,step,"");
when(listener.getLogger()).thenReturn(System.out);
CounterBuildParameterFactory counterFactory = new CounterBuildParameterFactory(from,to,step,"",validationFailure);

return counterFactory.getParameters(build, listener);
}


}

0 comments on commit e681c69

Please sign in to comment.