diff --git a/pom.xml b/pom.xml index e196ce0d..9968959c 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,7 @@ 2.2.7 2.18 3.1 + 2.62 @@ -88,7 +89,14 @@ org.jenkins-ci.plugins.workflow workflow-cps - 2.62 + ${workflow-cps-plugin.version} + test + + + org.jenkins-ci.plugins.workflow + workflow-cps + ${workflow-cps-plugin.version} + tests test diff --git a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStep.java b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStep.java index 4be9495b..39fef579 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStep.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStep.java @@ -86,6 +86,9 @@ public DescriptorImpl() { super(BuildTriggerStepExecution.class); } + // Note: This is necessary because the JSON format of the parameters produced by config.jelly when + // using the snippet generator does not match what would be neccessary for databinding to work automatically. + // For non-snippet generator use, this is unnecessary. @Override public Step newInstance(StaplerRequest req, JSONObject formData) throws FormException { BuildTriggerStep step = (BuildTriggerStep) super.newInstance(req, formData); // Cf. ParametersDefinitionProperty._doBuild: diff --git a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStepTest.java b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStepTest.java index b86d3fd7..26253f4b 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStepTest.java @@ -6,6 +6,7 @@ import hudson.model.AbstractBuild; import hudson.model.Action; import hudson.model.BooleanParameterDefinition; +import hudson.model.BooleanParameterValue; import hudson.model.BuildListener; import hudson.model.Cause; import hudson.model.Computer; @@ -21,6 +22,7 @@ import hudson.model.Queue; import hudson.model.Result; import hudson.model.StringParameterDefinition; +import hudson.model.StringParameterValue; import hudson.model.TaskListener; import hudson.model.User; import hudson.model.queue.QueueTaskFuture; @@ -45,6 +47,7 @@ import static org.hamcrest.Matchers.nullValue; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution; +import org.jenkinsci.plugins.workflow.cps.SnippetizerTester; import org.jenkinsci.plugins.workflow.flow.FlowExecution; import org.jenkinsci.plugins.workflow.graph.FlowNode; import org.jenkinsci.plugins.workflow.job.WorkflowJob; @@ -549,4 +552,47 @@ public void invalidChoiceParameterValue() throws Exception { j.assertLogContains("Value for choice parameter 'letter' is 'c', but valid choices are [a, b]", j.assertBuildStatus(Result.FAILURE, us.scheduleBuild2(0))); } + + @Test public void snippetizerRoundTrip() throws Exception { + SnippetizerTester st = new SnippetizerTester(j); + BuildTriggerStep step = new BuildTriggerStep("downstream"); + st.assertRoundTrip(step, "build 'downstream'"); + step.setParameters(Arrays.asList(new StringParameterValue("branch", "default"), new BooleanParameterValue("correct", true))); + // Note: This does not actually test the format of the JSON produced by the snippet generator for parameters, see generateSnippet* for tests of that behavior. + st.assertRoundTrip(step, "build job: 'downstream', parameters: [string(name: 'branch', value: 'default'), booleanParam(name: 'correct', value: true)]"); + } + + @Issue("JENKINS-26093") + @Test public void generateSnippetForBuildTrigger() throws Exception { + SnippetizerTester st = new SnippetizerTester(j); + MockFolder d1 = j.createFolder("d1"); + FreeStyleProject ds = d1.createProject(FreeStyleProject.class, "ds"); + MockFolder d2 = j.createFolder("d2"); + WorkflowJob us = d2.createProject(WorkflowJob.class, "us"); + ds.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("key", ""), new BooleanParameterDefinition("flag", false, ""))); + String snippet = "build job: '../d1/ds', parameters: [string(name: 'key', value: 'stuff'), booleanParam(name: 'flag', value: true)]"; + st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'../d1/ds', 'parameter': [{'name':'key', 'value':'stuff'}, {'name':'flag', 'value':true}]}", snippet, us.getAbsoluteUrl() + "configure"); + } + + @Issue("JENKINS-29739") + @Test public void generateSnippetForBuildTriggerSingle() throws Exception { + SnippetizerTester st = new SnippetizerTester(j); + FreeStyleProject ds = j.jenkins.createProject(FreeStyleProject.class, "ds1"); + FreeStyleProject us = j.jenkins.createProject(FreeStyleProject.class, "us1"); + ds.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("key", ""))); + String snippet = "build job: 'ds1', parameters: [string(name: 'key', value: 'stuff')]"; + st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'ds1', 'parameter': {'name':'key', 'value':'stuff'}}", snippet, us.getAbsoluteUrl() + "configure"); + } + + @Test public void generateSnippetForBuildTriggerNone() throws Exception { + SnippetizerTester st = new SnippetizerTester(j); + FreeStyleProject ds = j.jenkins.createProject(FreeStyleProject.class, "ds0"); + FreeStyleProject us = j.jenkins.createProject(FreeStyleProject.class, "us0"); + st.assertGenerateSnippet("{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'ds0'}", "build 'ds0'", us.getAbsoluteUrl() + "configure"); + } + + @Test + public void buildStepDocs() throws Exception { + SnippetizerTester.assertDocGeneration(BuildTriggerStep.class); + } }