Search in sources :

Example 6 with Step

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());
    }
}
Also used : DescribableParameter(org.jenkinsci.plugins.structs.describable.DescribableParameter) UninstantiatedDescribable(org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable) Describable(hudson.model.Describable) Step(org.jenkinsci.plugins.workflow.steps.Step) SimpleBuildStep(jenkins.tasks.SimpleBuildStep) Jenkins(jenkins.model.Jenkins) JSONObject(net.sf.json.JSONObject) BuildStepDescriptor(hudson.tasks.BuildStepDescriptor) Descriptor(hudson.model.Descriptor) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) BuildStepDescriptor(hudson.tasks.BuildStepDescriptor) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) JSONObject(net.sf.json.JSONObject) Restricted(org.kohsuke.accmod.Restricted)

Example 7 with Step

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);
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) StepDescriptor(org.jenkinsci.plugins.workflow.steps.StepDescriptor) Step(org.jenkinsci.plugins.workflow.steps.Step) GroovyShell(groovy.lang.GroovyShell) NoStaplerConstructorException(org.kohsuke.stapler.NoStaplerConstructorException) IOException(java.io.IOException)

Aggregations

Step (org.jenkinsci.plugins.workflow.steps.Step)7 List (java.util.List)3 Map (java.util.Map)3 UninstantiatedDescribable (org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable)3 StepDescriptor (org.jenkinsci.plugins.workflow.steps.StepDescriptor)3 EnvVars (hudson.EnvVars)2 BuildStepDescriptor (hudson.tasks.BuildStepDescriptor)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SimpleBuildStep (jenkins.tasks.SimpleBuildStep)2 JSONObject (net.sf.json.JSONObject)2 DescribableParameter (org.jenkinsci.plugins.structs.describable.DescribableParameter)2 NoStaplerConstructorException (org.kohsuke.stapler.NoStaplerConstructorException)2 Binding (groovy.lang.Binding)1 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)1 GroovyShell (groovy.lang.GroovyShell)1 ExtensionList (hudson.ExtensionList)1 Computer (hudson.model.Computer)1 Describable (hudson.model.Describable)1