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

feat(pipeline_template): PipelineIdTag checks context for variables defining application and name #1688

Merged
merged 1 commit into from
Oct 12, 2017
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 @@ -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'() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reformat this so it's consistent with the rest of the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be better now.

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