From 7fd870aed54b967d99c073e66a2b06671deb821a Mon Sep 17 00:00:00 2001 From: res0nance Date: Thu, 24 Dec 2020 21:18:42 +0800 Subject: [PATCH] Support extended-choice-parameter JSON parameter --- pom.xml | 6 +++++ .../ParameterizedTimerTrigger.java | 12 ++++++++- .../ParameterizedSchedulerTest.java | 25 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index de68997..1a88181 100644 --- a/pom.xml +++ b/pom.xml @@ -84,6 +84,12 @@ pipeline-model-definition test + + org.jenkins-ci.plugins + extended-choice-parameter + 0.82 + test + org.mockito mockito-core diff --git a/src/main/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedTimerTrigger.java b/src/main/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedTimerTrigger.java index a7257e9..b7fbda2 100644 --- a/src/main/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedTimerTrigger.java +++ b/src/main/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedTimerTrigger.java @@ -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. * @@ -62,7 +64,15 @@ private List configurePropertyValues(Map 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 { diff --git a/src/test/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedSchedulerTest.java b/src/test/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedSchedulerTest.java index 48245e1..ee9acb9 100644 --- a/src/test/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedSchedulerTest.java +++ b/src/test/java/org/jenkinsci/plugins/parameterizedscheduler/ParameterizedSchedulerTest.java @@ -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; @@ -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 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 {