Skip to content

Commit

Permalink
fix(pipelinetemplate): Add template variables to module tag (#3493)
Browse files Browse the repository at this point in the history
* fix(pipelinetemplate): Add template variables to module tag

According to documentation, the module tag should be able to use variables both from the template and variables scoped strictly to the module, however only the latter was the case. Fixed by adding the template variables to the render context.

* Added test
  • Loading branch information
jervi committed Mar 20, 2020
1 parent 82fce07 commit db7849f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,19 @@ public static RenderContext createDefaultRenderContext(
PipelineTemplate template, TemplateConfiguration configuration, Map<String, Object> trigger) {
RenderContext context =
new DefaultRenderContext(configuration.getPipeline().getApplication(), template, trigger);
addTemplateVariablesToContext(template, context);
if (configuration.getPipeline().getVariables() != null) {
context.getVariables().putAll(configuration.getPipeline().getVariables());
}
return context;
}

public static void addTemplateVariablesToContext(
PipelineTemplate template, RenderContext context) {
if (template != null && template.getVariables() != null) {
template.getVariables().stream()
.filter(v -> (v.isNullable() && v.getDefaultValue() == null) || v.hasDefaultValue())
.forEach(v -> context.getVariables().put(v.getName(), v.getDefaultValue()));
}
if (configuration.getPipeline().getVariables() != null) {
context.getVariables().putAll(configuration.getPipeline().getVariables());
}
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Splitter;
import com.hubspot.jinjava.interpret.*;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.InterpretException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateStateException;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.interpret.UnknownTokenException;
import com.hubspot.jinjava.lib.tag.Tag;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.util.HelperStringTokenizer;
Expand All @@ -31,7 +36,11 @@
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.RenderUtil;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.Renderer;
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors.Error;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -87,6 +96,7 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
template,
(Map<String, Object>) context.get("trigger"));
moduleContext.setLocation("module:" + moduleId);
RenderUtil.addTemplateVariablesToContext(template, moduleContext);

// Assign parameters into the context
Map<String, String> paramPairs = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,37 @@ class ModuleTagSpec extends Specification {
then:
result == '1'
}

def 'can use variables defined in the pipelineTemplate'() {
given:
PipelineTemplate pipelineTemplate = new PipelineTemplate(
variables: [
new PipelineTemplate.Variable(
name: 'myConfig',
type: 'object',
defaultValue: [
myApp: [
key1: 'value1',
key2: 'value2'
]
]
)
],
modules: [
new TemplateModule(
id: 'myModule',
variables: [
[name: 'app'] as NamedHashMap,
],
definition: "{{ myConfig[app].key2 }}")
]
)
RenderContext context = new DefaultRenderContext('myApp', pipelineTemplate, [:])

when:
def result = renderer.render("{% module myModule app=myApp %}", context)

then:
result == 'value2'
}
}

0 comments on commit db7849f

Please sign in to comment.