Skip to content

Commit 76639db

Browse files
committed
fix: propagation of attributes mode and fail_fast from originalYamlString
1 parent 6eeaf1c commit 76639db

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

client/pipeline.go

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type Spec struct {
8181
Steps *Steps `json:"steps,omitempty"`
8282
Stages *Stages `json:"stages,omitempty"`
8383
Mode string `json:"mode,omitempty"`
84+
FailFast bool `json:"fail_fast,omitempty"`
8485
RuntimeEnvironment RuntimeEnvironment `json:"runtimeEnvironment,omitempty"`
8586
TerminationPolicy []map[string]interface{} `json:"terminationPolicy,omitempty"`
8687
}

codefresh/resource_pipeline.go

+26-15
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,7 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
575575
Context: d.Get("spec.0.spec_template.0.context").(string),
576576
}
577577
} else {
578-
stages, steps := extractStagesAndSteps(originalYamlString)
579-
pipeline.Spec.Steps = &cfClient.Steps{
580-
Steps: steps,
581-
}
582-
pipeline.Spec.Stages = &cfClient.Stages{
583-
Stages: stages,
584-
}
578+
extractSpecAttributesFromOriginalYamlString(originalYamlString, pipeline)
585579
}
586580

587581
if _, ok := d.GetOk("spec.0.runtime_environment"); ok {
@@ -659,24 +653,30 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
659653
return pipeline
660654
}
661655

662-
// extractStagesAndSteps extracts the steps and stages from the original yaml string to enable propagation in the `Spec` attribute of the pipeline
656+
// extractSpecAttributesFromOriginalYamlString extracts the steps and stages from the original yaml string to enable propagation in the `Spec` attribute of the pipeline
663657
// We cannot leverage on the standard marshal/unmarshal because the steps attribute needs to maintain the order of elements
664658
// while by default the standard function doesn't do it because in JSON maps are unordered
665-
func extractStagesAndSteps(originalYamlString string) (stages, steps string) {
659+
func extractSpecAttributesFromOriginalYamlString(originalYamlString string, pipeline *cfClient.Pipeline) {
666660
// Use mapSlice to preserve order of items from the YAML string
667661
m := yaml.MapSlice{}
668662
err := yaml.Unmarshal([]byte(originalYamlString), &m)
669663
if err != nil {
670664
log.Fatal("Unable to unmarshall original_yaml_string")
671665
}
672666

673-
stages = "[]"
667+
stages := "[]"
674668
// Dynamically build JSON object for steps using String builder
675669
stepsBuilder := strings.Builder{}
676670
stepsBuilder.WriteString("{")
671+
// Dynamically build JSON object for steps using String builder
672+
hooksBuilder := strings.Builder{}
673+
hooksBuilder.WriteString("{")
674+
677675
// Parse elements of the YAML string to extract Steps and Stages if defined
678676
for _, item := range m {
679-
if item.Key == "steps" {
677+
key := item.Key.(string)
678+
switch key {
679+
case "steps":
680680
switch x := item.Value.(type) {
681681
default:
682682
log.Fatalf("unsupported value type: %T", item.Value)
@@ -694,17 +694,28 @@ func extractStagesAndSteps(originalYamlString string) (stages, steps string) {
694694
}
695695
}
696696
}
697-
}
698-
if item.Key == "stages" {
697+
case "stages":
699698
// For Stages we don't have ordering issue because it's a list
700699
y, _ := yaml.Marshal(item.Value)
701700
j2, _ := ghodss.YAMLToJSON(y)
702701
stages = string(j2)
702+
case "mode":
703+
pipeline.Spec.Mode = item.Value.(string)
704+
case "fail_fast":
705+
pipeline.Spec.FailFast = item.Value.(bool)
706+
default:
707+
log.Printf("Unsupported entry %s", key)
703708
}
704709
}
705710
stepsBuilder.WriteString("}")
706-
steps = stepsBuilder.String()
707-
return
711+
steps := stepsBuilder.String()
712+
pipeline.Spec.Steps = &cfClient.Steps{
713+
Steps: steps,
714+
}
715+
pipeline.Spec.Stages = &cfClient.Stages{
716+
Stages: stages,
717+
}
718+
708719
}
709720

710721
func getSupportedTerminationPolicyAttributes(policy string) map[string]interface{} {

codefresh/resource_pipeline_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestAccCodefreshPipeline_RuntimeEnvironment(t *testing.T) {
165165
func TestAccCodefreshPipeline_OriginalYamlString(t *testing.T) {
166166
name := pipelineNamePrefix + acctest.RandString(10)
167167
resourceName := "codefresh_pipeline.test"
168-
originalYamlString := "version: \"1.0\"\nsteps:\n test:\n image: alpine:latest\n commands:\n - echo \"ACC tests\""
168+
originalYamlString := "version: \"1.0\"\nfail_fast: false\nmode: parallel\nsteps:\n test:\n image: alpine:latest\n commands:\n - echo \"ACC tests\""
169169

170170
resource.ParallelTest(t, resource.TestCase{
171171
PreCheck: func() { testAccPreCheck(t) },

codefresh/resource_step_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func mapResourceToStepTypesVersions(d *schema.ResourceData) *cfClient.StepTypesV
382382
return &stepTypesVersions
383383
}
384384

385-
// extractStagesAndSteps extracts the steps and stages from the original yaml string to enable propagation in the `Spec` attribute of the pipeline
385+
// extractSteps extracts the steps and stages from the original yaml string to enable propagation in the `Spec` attribute of the pipeline
386386
// We cannot leverage on the standard marshal/unmarshal because the steps attribute needs to maintain the order of elements
387387
// while by default the standard function doesn't do it because in JSON maps are unordered
388388
func extractSteps(stepTypesYaml string) (steps *orderedmap.OrderedMap) {

0 commit comments

Comments
 (0)