Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pipeline_template): Allow conditional stages inside of partials #1631

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public GraphMutator(TemplateConfiguration configuration, Renderer renderer, Regi
visitors.add(new RenderTransform(configuration, renderer, registry, trigger));
visitors.add(new ConfigStageInjectionTransform(configuration));
visitors.add(new StageInheritanceControlTransform());
visitors.add(new ConditionalStanzaTransform(configuration, renderer, trigger));
visitors.add(new ConditionalStanzaTransform(configuration, trigger));
visitors.add(new TrimConditionalsTransform());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.TemplateConfiguration;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.DefaultRenderContext;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.RenderContext;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.Renderer;

import java.util.List;
import java.util.Map;
Expand All @@ -31,12 +30,10 @@ public class ConditionalStanzaTransform implements PipelineTemplateVisitor {

private TemplateConfiguration templateConfiguration;

private Renderer renderer;
private Map<String, Object> trigger;

public ConditionalStanzaTransform(TemplateConfiguration templateConfiguration, Renderer renderer, Map<String, Object> trigger) {
public ConditionalStanzaTransform(TemplateConfiguration templateConfiguration, Map<String, Object> trigger) {
this.templateConfiguration = templateConfiguration;
this.renderer = renderer;
this.trigger = trigger;
}

Expand All @@ -53,9 +50,9 @@ private <T extends Conditional> void trimConditionals(List<T> stages, PipelineTe
.forEach(stage -> {
RenderContext context = new DefaultRenderContext(templateConfiguration.getPipeline().getApplication(), template, trigger);
context.getVariables().putAll(templateConfiguration.getPipeline().getVariables());
// Conditionals have already been rendered
for (String conditional : stage.getWhen()) {
String rendered = renderer.render(conditional, context);
if (!Boolean.parseBoolean(rendered)) {
if (!Boolean.parseBoolean(conditional)) {
stage.setRemove();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,18 @@ private void renderStage(StageDefinition stage, RenderContext context, String lo

stage.setName(renderStageProperty(stage.getName(), context, getStagePropertyLocation(locationNamespace, stage.getId(), "name")));
stage.setComments(renderStageProperty(stage.getComments(), context, getStagePropertyLocation(locationNamespace, stage.getId(), "comments")));
stage.setWhen(
stage.getWhen()
.stream()
.map(w -> renderStageProperty(w, context, getStagePropertyLocation(locationNamespace, stage.getId(), "when")))
.collect(Collectors.toList())
);
}

private String renderStageProperty(String input, RenderContext context, String location) {
Object result;
try {
return (String) RenderUtil.deepRender(renderer, input, context);
result = RenderUtil.deepRender(renderer, input, context);
} catch (TemplateRenderException e) {
throw TemplateRenderException.fromError(
new Error()
Expand All @@ -140,6 +147,7 @@ private String renderStageProperty(String input, RenderContext context, String l
e
);
}
return (result == null) ? null : result.toString();
}

private static String getStagePropertyLocation(String namespace, String stageId, String propertyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.PipelineTempla
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.StageDefinition
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.TemplateConfiguration
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.TemplateConfiguration.PipelineDefinition
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.Renderer
import spock.lang.Specification
import spock.lang.Subject

Expand All @@ -29,13 +28,7 @@ class ConditionalStanzaTransformSpec extends Specification {
pipeline: new PipelineDefinition(variables: [:])
)

Renderer renderer = Mock() {
render(_, _) >> { conditional, context ->
return (conditional == 'true').toString()
}
}

@Subject ConditionalStanzaTransform subject = new ConditionalStanzaTransform(configuration, renderer, [:])
@Subject ConditionalStanzaTransform subject = new ConditionalStanzaTransform(configuration, [:])

def 'should remove falsy conditional stages'() {
given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class RenderTransformSpec extends Specification {
channel: '{{slackChannel}}',
when: ['awaiting']
]
],
when: [
"{{ application == 'blah' }}"
]
),
new StageDefinition(
Expand Down Expand Up @@ -153,6 +156,7 @@ class RenderTransformSpec extends Specification {
]
]
findStage(template, 'deploy').name == 'Deploy to Env'
findStage(template, 'manualjudgment').when == ['false']
}

def 'should render partials'() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
schema: "1"
pipeline:
application: orca
stages: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": "unknown",
"keepWaitingPipelines": false,
"limitConcurrent": true,
"application": "orca",
"name": "Unnamed Execution",
"stages": [
{
"refId": "foo.wait",
"requisiteStageRefIds": [],
"type": "wait",
"name": "wait",
"waitTime": 5,
"id": null,
"group": "myPartial: foo"
}
],
"notifications": [],
"parameterConfig": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
schema: "1"
id: nestedConditionalStage
metadata:
name: Nested partials conditional test
description: Asserts that stages inside of a partial can be conditionally excluded
stages:
- id: foo
type: partial.myPartial
config:
includeStage: false

partials:
- id: myPartial
usage: It has stages.
variables:
- name: includeStage
description: Whether the nested stage should be included
stages:
- id: wait
type: wait
config:
waitTime: 5
- id: wait2
type: wait
dependsOn:
- wait
config:
waitTime: 5
when:
- "{{ includeStage }}"