Skip to content

Commit

Permalink
feat(pipeline_template): Partials-generated stages should be given ge…
Browse files Browse the repository at this point in the history
…nerated group name (#1574)
  • Loading branch information
robzienert authored Aug 29, 2017
1 parent 34c449b commit 5ef9a58
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public Map<String, Object> generate(PipelineTemplate template, TemplateConfigura
stage.put("type", s.getType());
stage.put("name", s.getName());
stage.put("requisiteStageRefIds", s.getRequisiteStageRefIds());
if (s.getPartialDefinitionContext() != null) {
stage.put("group", String.format("%s: %s",
s.getPartialDefinitionContext().getPartialDefinition().getName(),
s.getPartialDefinitionContext().getMarkerStage().getName()
));
}
stage.putAll(s.getConfig());
return stage;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.PartialDefinition;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.PipelineTemplate;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.StageDefinition;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.StageDefinition.PartialDefinitionContext;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.TemplateConfiguration;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.RenderContext;
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.render.RenderUtil;
Expand Down Expand Up @@ -180,6 +181,7 @@ private void renderPartials(List<PartialDefinition> partials, List<StageDefiniti
throw new TemplateRenderException("StageDefinition clone unsupported", e);
}

renderedStage.setPartialDefinitionContext(new PartialDefinitionContext(partial, stage));
renderedStage.setId(String.format("%s.%s", stage.getId(), renderedStage.getId()));
renderedStage.setDependsOn(
renderedStage.getDependsOn().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class PartialDefinition implements Identifiable {

private String id;
private String name;
private String usage;
private List<NamedHashMap> variables = new ArrayList<>();
private List<StageDefinition> stages = new ArrayList<>();
Expand All @@ -38,6 +39,14 @@ public void setId(String id) {
this.id = id;
}

public String getName() {
return name == null ? id : name;
}

public void setName(String name) {
this.name = name;
}

public String getUsage() {
return usage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -37,9 +39,13 @@ public class StageDefinition implements Identifiable, Conditional, Cloneable {
private String comments;
private List<String> when = new ArrayList<>();
private InheritanceControl inheritanceControl;
private Set<String> requisiteStageRefIds = new LinkedHashSet<>();

@JsonIgnore
private Boolean removed = false;

private Set<String> requisiteStageRefIds = new LinkedHashSet<>();
@JsonIgnore
private PartialDefinitionContext partialDefinitionContext;

public static class InjectionRule implements Cloneable {

Expand Down Expand Up @@ -164,6 +170,25 @@ public Object clone() throws CloneNotSupportedException {
}
}

public static class PartialDefinitionContext {

private final PartialDefinition partialDefinition;
private final StageDefinition markerStage;

public PartialDefinitionContext(PartialDefinition partialDefinition, StageDefinition markerStage) {
this.partialDefinition = partialDefinition;
this.markerStage = markerStage;
}

public PartialDefinition getPartialDefinition() {
return partialDefinition;
}

public StageDefinition getMarkerStage() {
return markerStage;
}
}

@Override
public String getId() {
return id;
Expand Down Expand Up @@ -263,10 +288,12 @@ public void setRequisiteStageRefIds(Set<String> requisiteStageRefIds) {
this.requisiteStageRefIds = requisiteStageRefIds;
}

@JsonIgnore
public boolean isPartialType() {
return type != null && type.startsWith("partial.");
}

@JsonIgnore
public String getPartialId() {
if (type == null) {
return null;
Expand All @@ -275,6 +302,14 @@ public String getPartialId() {
return bits[bits.length - 1];
}

public PartialDefinitionContext getPartialDefinitionContext() {
return partialDefinitionContext;
}

public void setPartialDefinitionContext(PartialDefinitionContext partialDefinitionContext) {
this.partialDefinitionContext = partialDefinitionContext;
}

@Override
public Object clone() throws CloneNotSupportedException {
StageDefinition stage = (StageDefinition) super.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.netflix.spinnaker.orca.front50.Front50Service
import com.netflix.spinnaker.orca.pipelinetemplate.loader.FileTemplateSchemeLoader
import com.netflix.spinnaker.orca.pipelinetemplate.loader.TemplateLoader
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.PipelineTemplate
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.StageDefinition
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.Renderer
Expand Down Expand Up @@ -255,6 +256,62 @@ class PipelineTemplatePipelinePreprocessorSpec extends Specification {
["{{ trigger.parameters.list | split(',') }}", "string2"] || [["us-west-2", "us-east-1"], "string2"]
}

def "should include group for partials-generated stages"() {
def pipeline = [
type: 'templatedPipeline',
config: [
schema: '1',
pipeline: [
application: 'myapp'
]
],
template: [
schema: '1',
id: 'myTemplate',
configuration: [:],
partials: [
[
id: 'mypartial',
name: 'my group of stages',
stages: [
new StageDefinition(
id: 'wait',
type: 'wait',
requisiteStageRefIds: [] as Set,
config: [
waitTime: 5
]
),
new StageDefinition(
id: 'wait2',
type: 'wait',
requisiteStageRefIds: [] as Set,
config: [
waitTime: 5
]
)
]
]
],
stages: [
[
id: 'waiting',
name: 'wowow waiting',
type: 'partial.mypartial',
config: [:]
]
]
],
plan: true
]

when:
def result = subject.process(pipeline)

then:
result.stages*.group == ['my group of stages: wowow waiting', 'my group of stages: wowow waiting']
}

Map<String, Object> createTemplateRequest(String templatePath, Map<String, Object> variables = [:], List<Map<String, Object>> stages = [], boolean plan = false) {
return [
type: 'templatedPipeline',
Expand Down

0 comments on commit 5ef9a58

Please sign in to comment.