Skip to content

Commit

Permalink
[FIXED JENKINS-7162] Add missing parameters as defaults when called f…
Browse files Browse the repository at this point in the history
…rom CLI

Add all the job defined Parameters to the ParameterAction using Defaults
if not passed via the command line.
  • Loading branch information
c3johnso committed Aug 14, 2012
1 parent e0986b5 commit 7e0d7c0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 15 additions & 6 deletions core/src/main/java/hudson/cli/BuildCommand.java
Expand Up @@ -91,7 +91,7 @@ protected int run() throws Exception {
if (pdp==null)
throw new AbortException(job.getFullDisplayName()+" is not parameterized but the -p option was specified");

List<ParameterValue> values = new ArrayList<ParameterValue>();
List<ParameterValue> values = new ArrayList<ParameterValue>();

for (Entry<String, String> e : parameters.entrySet()) {
String name = e.getKey();
Expand All @@ -101,7 +101,16 @@ protected int run() throws Exception {
name, EditDistance.findNearest(name, pdp.getParameterDefinitionNames())));
values.add(pd.createValue(this,e.getValue()));
}


// handle missing parameters by adding as default values ISSUE JENKINS-7162
for(ParameterDefinition pd : pdp.getParameterDefinitions()) {
if (parameters.containsKey(pd.getName()))
continue;

// not passed in use default
values.add(pd.getDefaultParameterValue());
}

a = new ParametersAction(values);
}

Expand Down Expand Up @@ -145,17 +154,17 @@ protected void printUsageSummary(PrintStream stderr) {
}

public static class CLICause extends UserIdCause {

private String startedBy;

public CLICause(){
startedBy = "unknown";
}

public CLICause(String startedBy){
this.startedBy = startedBy;
}

@Override
public String getShortDescription() {
return Messages.BuildCommand_CLICause_ShortDescription(startedBy);
Expand Down
15 changes: 15 additions & 0 deletions test/src/test/groovy/hudson/cli/BuildCommandTest.groovy
Expand Up @@ -98,6 +98,21 @@ public class BuildCommandTest extends HudsonTestCase {
}
}

void testDefaultParameters() {
def p = createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty([new StringParameterDefinition("key","default"), new StringParameterDefinition("key2","default2") ]));

def cli = new CLI(getURL())
try {
cli.execute(["build","-s","-p","key=foobar",p.name])
def b = assertBuildStatusSuccess(p.getBuildByNumber(1))
assertEquals("foobar",b.getAction(ParametersAction.class).getParameter("key").value)
assertEquals("default2",b.getAction(ParametersAction.class).getParameter("key2").value)
} finally {
cli.close();
}
}

void testConsoleOutput() {
def p = createFreeStyleProject()
def cli = new CLI(getURL())
Expand Down

0 comments on commit 7e0d7c0

Please sign in to comment.