Skip to content
Permalink
Browse files

Merge pull request #10 from williambernardet/master

[FIXED JENKINS-19140] Fix the handling of (default) parameters for project triggering.
  • Loading branch information...
kutzi committed Aug 10, 2013
2 parents 29e24e0 + 3e9a0ed commit 6b139e304c7fd214c8d3d0caa3799a5361c28ca2
@@ -8,16 +8,20 @@
import hudson.model.BooleanParameterValue;
import hudson.model.Cause;
import hudson.model.Item;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Queue;
import hudson.model.StringParameterValue;
import hudson.plugins.im.IMCause;
import hudson.plugins.im.Sender;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@@ -111,28 +115,39 @@ public String getReply(Bot bot, Sender sender, String args[]) {
}
}

// parse possible parameters:
// parse possible parameters from the command
Map<String, ParameterValue> parsedParameters = new Hashtable<String, ParameterValue>();
for (int i=parametersStartIndex; i < args.length; i++) {
String[] split = args[i].split("=");
// TODO: guess ParameterValue type from ParametersDefintion of job?
if (split.length == 2) {
String[] split = args[i].split("=");
if (split.length == 2) {
String value = split[1];
if ("true".equals(value) || "false".equals(value)) {
parameters.add(new BooleanParameterValue(split[0], Boolean.parseBoolean(split[1])));
parsedParameters.put(split[0], new BooleanParameterValue(split[0], Boolean.parseBoolean(split[1])));
} else {
parameters.add(new StringParameterValue(split[0], split[1]));
parsedParameters.put(split[0], new StringParameterValue(split[0], split[1]));
}
} else {
msg += "Unparseable parameter: " + args[i];
}
} else {
msg += "Unparseable parameter: " + args[i];
}
}
if (project.isParameterized()) {
// Then resolve the value based on the project definition
ParametersDefinitionProperty propDefs = project.getProperty(ParametersDefinitionProperty.class);
for (ParameterDefinition pd : propDefs.getParameterDefinitions()) {
if (pd.getName() != null && parsedParameters.containsKey(pd.getName())) {
parameters.add(parsedParameters.get(pd.getName()));
} else {
ParameterValue pv = pd.getDefaultParameterValue();
if (pv != null) {
parameters.add(pv);
}
}
}
} else if (!parsedParameters.isEmpty() && !project.isParameterized()) {
msg += "Ignoring parameters as project is not parametrized!\n";
}
}


if (!parameters.isEmpty() && !project.isParameterized()) {
msg += "Ignoring parameters as project is not parametrized!\n";
}


if (scheduleBuild(bot, project, delay, sender, parameters)) {
if (delay == 0) {
return msg + sender.getNickname() + ": job " + jobName + " build scheduled now";
@@ -8,6 +8,8 @@
import hudson.model.Cause;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.plugins.im.Sender;

@@ -82,13 +84,16 @@ public void testParameters() {
cmd.setJobProvider(jobProvider);

AbstractProject<?, ?> project = mockProject(jobProvider);
when(project.isParameterized()).thenReturn(Boolean.TRUE);
when(project.getProperty(ParametersDefinitionProperty.class)).thenReturn(new ParametersDefinitionProperty(new StringParameterDefinition("key", "default value", "")));

Sender sender = new Sender("sender");
cmd.getReply(bot, sender, new String[]{ "build", "project", "key=value" });
cmd.getReply(bot, sender, new String[]{ "build", "project", "key=value", "unexisting_key=value" });

ArgumentCaptor<ParametersAction> captor = ArgumentCaptor.forClass(ParametersAction.class);
verify(project).hasPermission(Item.BUILD);
verify(project).scheduleBuild(Mockito.anyInt(), (Cause) Mockito.any(),
verify(project).isParameterized();
verify(project).scheduleBuild(anyInt(), (Cause) any(),
captor.capture());

Assert.assertEquals(1, captor.getValue().getParameters().size());
@@ -97,6 +102,9 @@ public void testParameters() {


project = mockProject(jobProvider);
when(project.isParameterized()).thenReturn(Boolean.TRUE);
when(project.getProperty(ParametersDefinitionProperty.class)).thenReturn(new ParametersDefinitionProperty(new StringParameterDefinition("key", "default value", ""),
new StringParameterDefinition("key2", "false", "")));
cmd.getReply(bot, sender, new String[]{ "build", "project", "3s", "key=value", "key2=true" });
captor = ArgumentCaptor.forClass(ParametersAction.class);
verify(project).hasPermission(Item.BUILD);

0 comments on commit 6b139e3

Please sign in to comment.
You can’t perform that action at this time.