use of org.jenkinsci.plugins.workflow.steps.Step in project workflow-cps-plugin by jenkinsci.
the class Snippetizer method doGenerateSnippet.
// accessed via REST API
@Restricted(DoNotUse.class)
public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String json) throws Exception {
// TODO is there not an easier way to do this? Maybe Descriptor.newInstancesFromHeteroList on a one-element JSONArray?
JSONObject jsonO = JSONObject.fromObject(json);
Jenkins j = Jenkins.getActiveInstance();
Class<?> c = j.getPluginManager().uberClassLoader.loadClass(jsonO.getString("stapler-class"));
Descriptor descriptor = j.getDescriptor(c.asSubclass(Describable.class));
if (descriptor == null) {
return HttpResponses.plainText("<could not find " + c.getName() + ">");
}
Object o;
try {
o = descriptor.newInstance(req, jsonO);
} catch (RuntimeException x) {
// e.g. IllegalArgumentException
return HttpResponses.plainText(Functions.printThrowable(x));
}
try {
Step step = null;
if (o instanceof Step) {
step = (Step) o;
} else {
// Look for a metastep which could take this as its delegate.
for (StepDescriptor d : StepDescriptor.allMeta()) {
if (d.getMetaStepArgumentType().isInstance(o)) {
DescribableModel<?> m = DescribableModel.of(d.clazz);
DescribableParameter soleRequiredParameter = m.getSoleRequiredParameter();
if (soleRequiredParameter != null) {
step = d.newInstance(Collections.singletonMap(soleRequiredParameter.getName(), o));
break;
}
}
}
}
if (step == null) {
return HttpResponses.plainText("Cannot find a step corresponding to " + o.getClass().getName());
}
String groovy = step2Groovy(step);
if (descriptor instanceof StepDescriptor && ((StepDescriptor) descriptor).isAdvanced()) {
String warning = Messages.Snippetizer_this_step_should_not_normally_be_used_in();
groovy = "// " + warning + "\n" + groovy;
}
return HttpResponses.plainText(groovy);
} catch (UnsupportedOperationException x) {
Logger.getLogger(CpsFlowExecution.class.getName()).log(Level.WARNING, "failed to render " + json, x);
return HttpResponses.plainText(x.getMessage());
}
}
use of org.jenkinsci.plugins.workflow.steps.Step in project workflow-cps-plugin by jenkinsci.
the class SnippetizerTester method assertRoundTrip.
/**
* Given a fully configured {@link Step}, make sure the output from the snippetizer matches the expected value.
*
* <p>
* As an additional measure, this method also executes the generated snippet and makes sure
* it yields identical {@link Step} object.
*
* @param step
* A fully configured step object
* @param expected
* Expected snippet to be generated.
*/
public void assertRoundTrip(Step step, String expected) throws Exception {
assertEquals(expected, Snippetizer.step2Groovy(step));
CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(DelegatingScript.class.getName());
GroovyShell shell = new GroovyShell(r.jenkins.getPluginManager().uberClassLoader, new Binding(), cc);
DelegatingScript s = (DelegatingScript) shell.parse(expected);
s.o = new DSL(new DummyOwner()) {
// for testing, instead of executing the step just return an instantiated Step
@Override
protected Object invokeStep(StepDescriptor d, Object args) {
try {
return d.newInstance(parseArgs(args, d).namedArgs);
} catch (Exception e) {
throw new AssertionError(e);
}
}
};
Step actual = (Step) s.run();
r.assertEqualDataBoundBeans(step, actual);
}
Aggregations