From 6c40ddf74e8de49c9bc1024fcbd74e4b0c362b99 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Wed, 24 Apr 2019 16:52:15 +0200 Subject: [PATCH] Check matrix conditions correctly We are using matrix conditions heavily, and without that fix they are ignored and could lead to undefined variables. With this fix only matching matrix conditions are getting converted from the legacy format. --- yaml/cond.go | 19 ++++++----- yaml/converter/legacy/internal/config.go | 35 ++++++++++++++++++++ yaml/converter/legacy/internal/constraint.go | 1 + 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/yaml/cond.go b/yaml/cond.go index 713a16d..a26a021 100644 --- a/yaml/cond.go +++ b/yaml/cond.go @@ -18,15 +18,16 @@ import filepath "github.com/bmatcuk/doublestar" // Conditions defines a group of conditions. type Conditions struct { - Cron Condition `json:"cron,omitempty"` - Ref Condition `json:"ref,omitempty"` - Repo Condition `json:"repo,omitempty"` - Instance Condition `json:"instance,omitempty"` - Target Condition `json:"target,omitempty"` - Event Condition `json:"event,omitempty"` - Branch Condition `json:"branch,omitempty"` - Status Condition `json:"status,omitempty"` - Paths Condition `json:"paths,omitempty"` + Cron Condition `json:"cron,omitempty"` + Ref Condition `json:"ref,omitempty"` + Repo Condition `json:"repo,omitempty"` + Instance Condition `json:"instance,omitempty"` + Target Condition `json:"target,omitempty"` + Event Condition `json:"event,omitempty"` + Branch Condition `json:"branch,omitempty"` + Status Condition `json:"status,omitempty"` + Paths Condition `json:"paths,omitempty"` + Matrix map[string]string `json:"matrix,omitempty"` } // Condition defines a runtime condition. diff --git a/yaml/converter/legacy/internal/config.go b/yaml/converter/legacy/internal/config.go index 14a9070..5714632 100644 --- a/yaml/converter/legacy/internal/config.go +++ b/yaml/converter/legacy/internal/config.go @@ -91,6 +91,40 @@ func Convert(d []byte) ([]byte, error) { current := pipeline current.Name = fmt.Sprintf("matrix-%d", index+1) + services := make([]*droneyaml.Container, 0) + for _, service := range current.Services { + if len(service.When.Matrix) == 0 { + services = append(services, service) + continue + } + + for whenKey, whenValue := range service.When.Matrix { + for envKey, envValue := range environ { + if whenKey == envKey && whenValue == envValue { + services = append(services, service) + } + } + } + } + current.Services = services + + steps := make([]*droneyaml.Container, 0) + for _, step := range current.Steps { + if len(step.When.Matrix) == 0 { + steps = append(steps, step) + continue + } + + for whenKey, whenValue := range step.When.Matrix { + for envKey, envValue := range environ { + if whenKey == envKey && whenValue == envValue { + steps = append(steps, step) + } + } + } + } + current.Steps = steps + marshaled, err := yaml.Marshal(¤t) if err != nil { @@ -180,6 +214,7 @@ func toConditions(from Constraints) droneyaml.Conditions { Include: from.Status.Include, Exclude: from.Status.Exclude, }, + Matrix: from.Matrix, } } diff --git a/yaml/converter/legacy/internal/constraint.go b/yaml/converter/legacy/internal/constraint.go index 8131a17..518db2a 100644 --- a/yaml/converter/legacy/internal/constraint.go +++ b/yaml/converter/legacy/internal/constraint.go @@ -14,6 +14,7 @@ type ( Event Constraint Branch Constraint Status Constraint + Matrix map[string]string } // Constraint defines a runtime constraint.