Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-26093] Merging #69.
Conflicts:
	CHANGES.md

Originally-Committed-As: 59d5d337e70fc15a69cdc308ee052d79ab7ffbd1
  • Loading branch information
jglick committed Mar 4, 2015
2 parents 5abe16b + 914a000 commit ae3e688
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Expand Up @@ -26,6 +26,7 @@

import hudson.Extension;
import hudson.Functions;
import hudson.model.Descriptor;
import hudson.model.RootAction;
import java.io.IOException;
import java.util.Collection;
Expand Down Expand Up @@ -196,9 +197,10 @@ public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String
throw new IllegalStateException("Jenkins is not running");
}
Class<?> c = j.getPluginManager().uberClassLoader.loadClass(jsonO.getString("stapler-class"));
Descriptor<?> descriptor = j.getDescriptor(c.asSubclass(Step.class));
Object o;
try {
o = req.bindJSON(c, jsonO);
o = descriptor.newInstance(req, jsonO);
} catch (RuntimeException x) { // e.g. IllegalArgumentException
return HttpResponses.plainText(Functions.printThrowable(x));
}
Expand Down
Expand Up @@ -30,6 +30,9 @@
import groovy.lang.GroovyObjectSupport;
import groovy.lang.GroovyShell;
import hudson.model.BooleanParameterValue;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.tasks.ArtifactArchiver;
import java.net.URL;
Expand All @@ -54,6 +57,7 @@
import org.jvnet.hudson.test.Email;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockFolder;

public class SnippetizerTest {

Expand Down Expand Up @@ -101,11 +105,7 @@ public class SnippetizerTest {
BuildTriggerStep step = new BuildTriggerStep("downstream");
assertRoundTrip(step, "build 'downstream'");
step.setParameters(Arrays.asList(new StringParameterValue("branch", "default"), new BooleanParameterValue("correct", true)));
/* TODO figure out how to add support for ParameterValue without those having Descriptor’s yet
currently instantiate works but uninstantiate does not offer a FQN
(which does not matter in this case since BuildTriggerStep/config.jelly does not offer to bind parameters anyway)
assertRoundTrip(step, "build job: 'downstream', parameters: [[$class: 'hudson.model.StringParameterValue', name: 'branch', value: 'default'], [$class: 'hudson.model.BooleanParameterValue', name: 'correct', value: true]]");
*/
assertRoundTrip(step, "build job: 'downstream', parameters: [[$class: 'StringParameterValue', name: 'branch', value: 'default'], [$class: 'BooleanParameterValue', name: 'correct', value: true]]");
}

@Issue("JENKINS-25779")
Expand Down Expand Up @@ -140,7 +140,7 @@ private static void assertRoundTrip(Step step, String expected) throws Exception
JenkinsRule.WebClient wc = r.createWebClient();
WebRequestSettings wrs = new WebRequestSettings(new URL(r.getURL(), Snippetizer.GENERATE_URL), HttpMethod.POST);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new NameValuePair("json", "{'stapler-class':'org.jenkinsci.plugins.workflow.steps.EchoStep', 'message':'hello world'}"));
params.add(new NameValuePair("json", "{'stapler-class':'" + EchoStep.class.getName() + "', 'message':'hello world'}"));
// WebClient.addCrumb *replaces* rather than *adds*:
params.add(new NameValuePair(r.jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField(), r.jenkins.getCrumbIssuer().getCrumb(null)));
wrs.setRequestParameters(params);
Expand All @@ -149,4 +149,24 @@ private static void assertRoundTrip(Step step, String expected) throws Exception
assertEquals("echo 'hello world'", response.getContentAsString().trim());
}

@Issue("JENKINS-26093")
@Test public void generateSnippetForBuildTrigger() throws Exception {
MockFolder d1 = r.createFolder("d1");
FreeStyleProject ds = d1.createProject(FreeStyleProject.class, "ds");
MockFolder d2 = r.createFolder("d2");
// Really this would be a WorkflowJob, but we cannot depend on that here, and it should not matter since we are just looking for Job:
FreeStyleProject us = d2.createProject(FreeStyleProject.class, "us");
ds.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("key", "")));
JenkinsRule.WebClient wc = r.createWebClient();
WebRequestSettings wrs = new WebRequestSettings(new URL(r.getURL(), Snippetizer.GENERATE_URL), HttpMethod.POST);
wrs.setAdditionalHeader("Referer", us.getAbsoluteUrl() + "configure");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new NameValuePair("json", "{'stapler-class':'" + BuildTriggerStep.class.getName() + "', 'job':'../d1/ds', 'parameter':[{'name':'key', 'value':'stuff'}]}"));
params.add(new NameValuePair(r.jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField(), r.jenkins.getCrumbIssuer().getCrumb(null)));
wrs.setRequestParameters(params);
WebResponse response = wc.getPage(wrs).getWebResponse();
assertEquals("text/plain", response.getContentType());
assertEquals("build job: '../d1/ds', parameters: [[$class: 'StringParameterValue', name: 'key', value: 'stuff']]", response.getContentAsString().trim());
}

}

0 comments on commit ae3e688

Please sign in to comment.