Skip to content

Commit

Permalink
Support extended-choice-parameter JSON parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
res0nance committed Dec 25, 2020
1 parent 53028c4 commit 7fd870a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
<artifactId>pipeline-model-definition</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>extended-choice-parameter</artifactId>
<version>0.82</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sf.json.JSONObject;

/**
* {@link Trigger} that runs a job periodically with support for parameters.
*
Expand Down Expand Up @@ -62,7 +64,15 @@ private List<ParameterValue> configurePropertyValues(Map<String, String> paramet
if (parameterValues.containsKey(paramDefinition.getName())) {
ParameterizedStaplerRequest request = new ParameterizedStaplerRequest(
parameterValues.get(paramDefinition.getName()));
ParameterValue value = paramDefinition.createValue(request);
ParameterValue value;
if (paramDefinition.getClass().getName().equals("com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition") &&
paramDefinition.getType().equals("PT_JSON")) {
JSONObject jO = new JSONObject();
jO.put("value", parameterValues.get(paramDefinition.getName()));
value = paramDefinition.createValue(request, jO);
} else {
value = paramDefinition.createValue(request);
}
if (value!= null) {
defValues.add(value);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.parameterizedscheduler;

import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
Expand Down Expand Up @@ -106,6 +107,30 @@ public void declarative() throws Exception {
assertThat((String) p.getLastCompletedBuild().getAction(ParametersAction.class).getParameter("foo").getValue(), is("bar"));
}

@Test
@Issue("JENKINS-49372")
public void extendedChoiceJson() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(new ExtendedChoiceParameterDefinition("foo", ExtendedChoiceParameterDefinition.PARAMETER_TYPE_JSON, "",
"", "", "def jsonSlurper = new groovy.json.JsonSlurper()\n" +
"def object = jsonSlurper.parseText('{\"schema\":{\"type\":\"object\",\"title\":\"Car\",\"properties\":{\"make\":{\"type\":\"string\",\"enum\":[\"Toyota\",\"BMW\",\"Honda\",\"Ford\",\"Chevy\",\"VW\"]},\"model\":{\"type\":\"string\"},\"year\":{\"type\":\"integer\",\"enum\":[1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014],\"default\":2008}}}}')\n" +
"return object", "", "", "", "",
"", "", "" , "", "", "",
"", "", "", "", "", "",
"", "", "", "", false, false,
0, "", "")));
assertThat(p.getLastCompletedBuild(), is(nullValue()));
Trigger<Job> t = new ParameterizedTimerTrigger("* * * * *%foo={\"make\":\"Toyota\",\"model\":\"test\",\"year\":2008}");
t.start(p, true);
p.addTrigger(t);
new Cron().doRun();
assertThat(p.isInQueue(), is(true));
r.waitUntilNoActivity();
// Build should complete successfully but will not have any value
assertThat(p.getLastCompletedBuild(), is(notNullValue()));
assertThat(p.getLastCompletedBuild().getAction(ParametersAction.class).getParameter("foo").getValue(), is("{\"make\":\"Toyota\",\"model\":\"test\",\"year\":2008}"));
}

@Test
@Issue("JENKINS-49372")
public void nullValueCreated() throws Exception {
Expand Down

0 comments on commit 7fd870a

Please sign in to comment.