Skip to content

Commit

Permalink
feat(pipeline_template): PipelineIdTag checks context for variables d…
Browse files Browse the repository at this point in the history
…efining application and name
  • Loading branch information
jonsie committed Oct 12, 2017
1 parent 2603640 commit ca5e34b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,38 @@ public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) {
});

Context context = interpreter.getContext();

String application = paramPairs.getOrDefault(APPLICATION, (String) context.get(APPLICATION)).replaceAll("^[\"\']|[\"\']$", "");
String name = paramPairs.get(NAME).replaceAll("^[\"\']|[\"\']$", "");
application = checkContext(application, context);

if (name == null || application == null) {
throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'pipelineId' is missing required fields: " + helper, tagNode.getLineNumber());
}
String name = paramPairs.get(NAME).replaceAll("^[\"\']|[\"\']$", "");
name = checkContext(name, context);

List<Map<String, Object>> pipelines = Optional.ofNullable(front50Service.getPipelines(application, false)).orElse(Collections.emptyList());
Map<String, Object> result = findPipeline(pipelines, application, name);
return (String) result.get("id");
}

Map<String, Object> result = pipelines
.stream()
.filter(p -> p.get(NAME).equals(name))
.findFirst()
.orElseThrow(
() -> TemplateRenderException.fromError(
new Error()
.withMessage(String.format("Failed to find pipeline ID with name '%s' in application '%s'", name, application)
))
);
private String checkContext(String param, Context context) {
Object var = context.get(param);

return (String) result.get("id");
if (var != null) {
return (String) var;
}

return param;
}

private Map<String, Object> findPipeline(List<Map<String, Object>> pipelines, String application, String pipelineName) {
return pipelines
.stream()
.filter(p -> p.get(NAME).equals(pipelineName))
.findFirst()
.orElseThrow(
() -> TemplateRenderException.fromError(
new Error()
.withMessage(String.format("Failed to find pipeline ID with name '%s' in application '%s'", pipelineName, application)
)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.netflix.spinnaker.orca.front50.Front50Service
import com.netflix.spinnaker.orca.pipelinetemplate.exceptions.TemplateRenderException
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.DefaultRenderContext
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.JinjaRenderer
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.RenderContext
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.Renderer
import spock.lang.Specification
import spock.lang.Subject
Expand Down Expand Up @@ -77,7 +78,26 @@ class PipelineIdTagSpec extends Specification {
'{% pipelineId name="Rob\'s great pipeline" %}' || '1685429e-beb1-4d35-963c-123456789012'
}

def 'should handle missing input params'() {
def 'should render pipeline id using variables defined in context'() {
given:
front50Service.getPipelines('myApp', false) >> [
[
name: 'Bake and Tag',
application: 'myApp',
id: '9595429f-afa0-4c34-852b-01a9a01967f9',
stages: []
]
]

RenderContext context = new DefaultRenderContext('myApp', null, [:])
context.variables.put("pipelineName", "Bake and Tag")
context.variables.put("applicationName", "myApp")

expect:
renderer.render('{% pipelineId application=applicationName name=pipelineName %}', context) == '9595429f-afa0-4c34-852b-01a9a01967f9'
}

def 'should handle missing input params'() {
given: 'a pipelineId tag with no app defined'
def applicationInContext = 'myApp'
def context = new DefaultRenderContext(applicationInContext,null, [:])
Expand Down

0 comments on commit ca5e34b

Please sign in to comment.