Skip to content

Commit

Permalink
Merge pull request #1 from abayer/JENKINS-38585-step-editor
Browse files Browse the repository at this point in the history
Move to using DescribableModel and friends.
  • Loading branch information
kzantow committed Nov 16, 2016
2 parents 24eab65 + 4d3fd7b commit 1f369b7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Expand Up @@ -43,23 +43,23 @@
<dependency>
<groupId>io.jenkins.blueocean</groupId>
<artifactId>blueocean</artifactId>
<version>1.0.0-b12-SNAPSHOT</version>
<version>1.0.0-b12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.blueocean</groupId>
<artifactId>blueocean-rest</artifactId>
<version>1.0.0-b12-SNAPSHOT</version>
<version>1.0.0-b12</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.1</version>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.8</version>
<version>2.23</version>
</dependency>
</dependencies>

Expand Down
Expand Up @@ -46,7 +46,13 @@ public interface PipelineStepMetadata {
*/
@Exported
public String getSnippetizerUrl();


/**
* Whether this step has one and only one parameter and it is required.
*/
@Exported
public boolean getHasSingleRequiredParameter();

/**
* Properties the steps supports
*/
Expand Down
@@ -1,15 +1,12 @@
package io.blueocean.rest.pipeline.editor;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.structs.describable.DescribableParameter;
import org.jenkinsci.plugins.workflow.cps.Snippetizer;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.export.Exported;
Expand Down Expand Up @@ -49,6 +46,7 @@ public static class BasicPipelineStepMetadata implements PipelineStepMetadata {
private List<Class<?>> requiredContext = new ArrayList<Class<?>>();
private List<Class<?>> providedContext = new ArrayList<Class<?>>();
private boolean isWrapper = false;
private boolean hasSingleRequiredParameter = false;
private String snippetizerUrl;
private List<PipelineStepPropertyMetadata> props = new ArrayList<PipelineStepPropertyMetadata>();

Expand Down Expand Up @@ -114,15 +112,25 @@ public String getType() {
return type.getName();
}

@Exported
@Override
public boolean getHasSingleRequiredParameter() {
return hasSingleRequiredParameter;
}

@Exported
@Override
public PipelineStepPropertyMetadata[] getProperties() {
return props.toArray(new PipelineStepPropertyMetadata[props.size()]);
}

public DescribableModel getDescribableModel() {
return new DescribableModel<>(type);
}
}

/**
* Basic exported model for {@link PipelineStepPropertyDescriptor)
* Basic exported model for {@link PipelineStepPropertyMetadata)
*/
@ExportedBean
public static class BasicPipelineStepPropertyMetadata implements PipelineStepPropertyMetadata{
Expand Down Expand Up @@ -178,49 +186,31 @@ public PipelineStepMetadata[] getPipelineStepMetadata() {
return pd.toArray(new PipelineStepMetadata[pd.size()]);
}

@SuppressWarnings("deprecation")
private PipelineStepMetadata getStepMetadata(StepDescriptor d, String snippetizerUrl) {
BasicPipelineStepMetadata step = new BasicPipelineStepMetadata(d.getFunctionName(), d.clazz, d.getDisplayName());

DescribableModel<?> model = step.getDescribableModel();

step.snippetizerUrl = snippetizerUrl + "?$class=" + d.clazz.getName(); // this isn't really accurate

step.isWrapper = d.takesImplicitBlockArgument();
step.requiredContext.addAll(d.getRequiredContext());
step.providedContext.addAll(d.getProvidedContext());
step.descriptorUrl = d.getDescriptorFullUrl();
step.hasSingleRequiredParameter = model.hasSingleRequiredParameter();

for (Method m : d.clazz.getDeclaredMethods()) {
if (m.isAnnotationPresent(DataBoundSetter.class)) {
String paramName = StringUtils.uncapitalize(m.getName().substring(3));
Class<?> paramType = m.getParameterTypes()[0];
BasicPipelineStepPropertyMetadata param = new BasicPipelineStepPropertyMetadata();
param.name = paramName;
param.type = paramType;
Descriptor<?> pd = Descriptor.find(paramType.getName());
if (pd != null) {
param.descriptorUrl = pd.getDescriptorFullUrl();
}
step.props.add(param);
}
}
for (DescribableParameter descParam : model.getParameters()) {
BasicPipelineStepPropertyMetadata param = new BasicPipelineStepPropertyMetadata();

param.type = descParam.getErasedType();
param.name = descParam.getName();
param.isRequired = descParam.isRequired();

Descriptor<?> pd = Descriptor.findByDescribableClassName(ExtensionList.lookup(Descriptor.class),
param.type.getName());

for (Constructor<?> c : d.clazz.getDeclaredConstructors()) {
if (c.isAnnotationPresent(DataBoundConstructor.class)) {
Class<?>[] paramTypes = c.getParameterTypes();
String[] paramNames = nameFinder.getParameterNames(c);
if(paramNames != null) {
for (int i = 0; i < paramNames.length; i++) {
String paramName = paramNames[i];
Class<?> paramType = paramTypes[i];
BasicPipelineStepPropertyMetadata param = new BasicPipelineStepPropertyMetadata();
param.name = paramName;
param.type = paramType;
Descriptor<?> pd = Descriptor.find(paramType.getName());
if (pd != null) {
param.descriptorUrl = pd.getDescriptorFullUrl();
}
step.props.add(param);
}
}
if (pd != null) {
param.descriptorUrl = pd.getDescriptorFullUrl();
}
}

Expand Down

0 comments on commit 1f369b7

Please sign in to comment.