Skip to content
Permalink
Browse files

[FIXED JENKINS-50682] Actually pass descriptor to when condition forms

Also add display name for a few when conditions, move all of
ChangeRequestConditional's fields into advanced, since they're
optional and all, and smack things around a bit internally to not
display when directive parameters when they're not required and
they've got an empty string value.
  • Loading branch information...
abayer committed Apr 9, 2018
1 parent 6d3b9dc commit eac90be72124d7ef32d957af898a5eef913f478b
@@ -29,13 +29,16 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditional;
import org.jenkinsci.plugins.pipeline.modeldefinition.when.DeclarativeStageConditionalDescriptor;
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.structs.describable.DescribableParameter;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.cps.Snippetizer;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WhenDirective extends AbstractDirective<WhenDirective> {
private DeclarativeStageConditional conditional;
@@ -81,12 +84,29 @@ public String getDisplayName() {
public String toGroovy(@Nonnull WhenDirective directive) {
if (directive.conditional != null) {
UninstantiatedDescribable ud = UninstantiatedDescribable.from(directive.conditional);
DescribableModel model = ud.getModel();
DescribableModel<? extends DeclarativeStageConditional> model = ud.getModel();
if (model != null) {
Map<String,Object> args = new HashMap<>();
// Hack to null out non-required fields for UI prettiness.
for (String argKey : ud.getArguments().keySet()) {
DescribableParameter p = model.getParameter(argKey);
if (p == null ||
p.isRequired() ||
!(String.class.equals(p.getErasedType())) ||
!ud.getArguments().get(argKey).equals("")) {
args.put(argKey, ud.getArguments().get(argKey));
}
}

StringBuilder result = new StringBuilder();
result.append("when {\n");

result.append(conditionalToGroovy(directive.conditional));
try {
result.append(conditionalToGroovy(model.instantiate(args)));
} catch (Exception e) {
result.append("// ERROR TRANSLATING CONDITIONAL: ").append(e).append("\n");
}

if (directive.isBeforeAgent()) {
result.append("beforeAgent true\n");
}
@@ -46,6 +46,11 @@ public BuildingTagConditional() {
@Extension
@Symbol("buildingTag")
public static class DescriptorImpl extends DeclarativeStageConditionalDescriptor<TagConditional> {
@Override
public String getDisplayName() {
return "Execute this stage if the build is running against an SCM tag";
}

@Override
public Expression transformToRuntimeAST(@CheckForNull ModelASTWhenContent original) {
return ASTParserUtils.transformWhenContentToRuntimeAST(original);
@@ -249,6 +249,11 @@ private void check(String pattern, Comparator comparator, EnvironmentNames varNa
@Extension
@Symbol("changeRequest")
public static class DescriptorImpl extends DeclarativeStageConditionalDescriptor<ChangeRequestConditional> {
@Override
public String getDisplayName() {
return "Execute the stage if the build is on a change request";
}

@Override
public Expression transformToRuntimeAST(@CheckForNull ModelASTWhenContent original) {
return ASTParserUtils.transformWhenContentToRuntimeAST(original);
@@ -90,6 +90,11 @@ public boolean tagMatches(String actualTag) {
@Extension
@Symbol("tag")
public static class DescriptorImpl extends DeclarativeStageConditionalDescriptor<TagConditional> {
@Override
public String getDisplayName() {
return "Execute this stage if the build is running against an SCM tag matching the given pattern";
}

@Override
public Expression transformToRuntimeAST(@CheckForNull ModelASTWhenContent original) {
return ASTParserUtils.transformWhenContentToRuntimeAST(original);
@@ -34,6 +34,7 @@
selected="false" staplerClass="${d.clazz.name}" lazy="d,it">
<l:ajax>
<j:set var="instance" value="${null}" />
<j:set var="descriptor" value="${d}" />
<st:include from="${d}" page="${d.configPage}" optional="true" />
</l:ajax>
</f:dropdownListBlock>
@@ -25,34 +25,36 @@
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="comparator" title="Comparator">
<f:select/>
</f:entry>
<f:entry field="id" title="Id">
<f:textbox/>
</f:entry>
<f:entry field="target" title="Target">
<f:textbox/>
</f:entry>
<f:entry field="branch" title="Branch">
<f:textbox/>
</f:entry>
<f:entry field="fork" title="Fork">
<f:textbox/>
</f:entry>
<f:entry field="url" title="Url">
<f:textbox/>
</f:entry>
<f:entry field="title" title="Title">
<f:textbox/>
</f:entry>
<f:entry field="author" title="Author">
<f:textbox/>
</f:entry>
<f:entry field="authorDisplayName" title="Author Display name">
<f:textbox/>
</f:entry>
<f:entry field="authorEmail" title="Author E-Mail">
<f:textbox/>
</f:entry>
<f:advanced>
<f:entry field="comparator" title="Comparator">
<f:select />
</f:entry>
<f:entry field="id" title="Id">
<f:textbox/>
</f:entry>
<f:entry field="target" title="Target">
<f:textbox/>
</f:entry>
<f:entry field="branch" title="Branch">
<f:textbox/>
</f:entry>
<f:entry field="fork" title="Fork">
<f:textbox/>
</f:entry>
<f:entry field="url" title="Url">
<f:textbox/>
</f:entry>
<f:entry field="title" title="Title">
<f:textbox/>
</f:entry>
<f:entry field="author" title="Author">
<f:textbox/>
</f:entry>
<f:entry field="authorDisplayName" title="Author Display name">
<f:textbox/>
</f:entry>
<f:entry field="authorEmail" title="Author E-Mail">
<f:textbox/>
</f:entry>
</f:advanced>
</j:jelly>

0 comments on commit eac90be

Please sign in to comment.
You can’t perform that action at this time.