From f35bd60807b7a47e9e5b71fe283c7e8068ac15b8 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Wed, 24 Apr 2019 01:08:44 +0200 Subject: [PATCH 1/3] Add yaml marshalling for variable and parameter --- yaml/env.go | 13 +++++++++++++ yaml/param.go | 21 +++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/yaml/env.go b/yaml/env.go index 5dbae74..9b8e002 100644 --- a/yaml/env.go +++ b/yaml/env.go @@ -42,3 +42,16 @@ func (v *Variable) UnmarshalYAML(unmarshal func(interface{}) error) error { v.Secret = d.Secret return err } + +// MarshalYAML implements yaml marshalling. +func (v *Variable) MarshalYAML() (interface{}, error) { + if v.Secret != "" { + m := map[string]interface{}{} + m["from_secret"] = v.Secret + return m, nil + } + if v.Value != "" { + return v.Value, nil + } + return nil, nil +} diff --git a/yaml/param.go b/yaml/param.go index 61fac2c..33cfbdb 100644 --- a/yaml/param.go +++ b/yaml/param.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -34,7 +34,7 @@ type ( func (p *Parameter) UnmarshalYAML(unmarshal func(interface{}) error) error { d := new(parameter) err := unmarshal(d) - if err == nil && d.Secret != ""{ + if err == nil && d.Secret != "" { p.Secret = d.Secret return nil } @@ -43,3 +43,16 @@ func (p *Parameter) UnmarshalYAML(unmarshal func(interface{}) error) error { p.Value = i return err } + +// MarshalYAML implements yaml marshalling. +func (p *Parameter) MarshalYAML() (interface{}, error) { + if p.Secret != "" { + m := map[string]interface{}{} + m["from_secret"] = p.Secret + return m, nil + } + if p.Value != "" { + return p.Value, nil + } + return nil, nil +} From 34c94916efecbeacdf731d6af6d28745185b92dd Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Wed, 24 Apr 2019 16:52:00 +0200 Subject: [PATCH 2/3] Execute gofmt for best practice --- yaml/build.go | 6 +++--- yaml/cron.go | 6 +++--- yaml/parse.go | 6 +++--- yaml/port.go | 6 +++--- yaml/push.go | 6 +++--- yaml/registry.go | 6 +++--- yaml/signature.go | 6 +++--- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/yaml/build.go b/yaml/build.go index 04f48ad..342f303 100644 --- a/yaml/build.go +++ b/yaml/build.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/yaml/cron.go b/yaml/cron.go index a0d3471..98712f0 100644 --- a/yaml/cron.go +++ b/yaml/cron.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/yaml/parse.go b/yaml/parse.go index b4ffd62..51905e6 100644 --- a/yaml/parse.go +++ b/yaml/parse.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/yaml/port.go b/yaml/port.go index bd8b1c8..205e167 100644 --- a/yaml/port.go +++ b/yaml/port.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/yaml/push.go b/yaml/push.go index 130394c..517ce22 100644 --- a/yaml/push.go +++ b/yaml/push.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/yaml/registry.go b/yaml/registry.go index 4e1902f..439335e 100644 --- a/yaml/registry.go +++ b/yaml/registry.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/yaml/signature.go b/yaml/signature.go index 5491fb1..b479341 100644 --- a/yaml/signature.go +++ b/yaml/signature.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. From 6c40ddf74e8de49c9bc1024fcbd74e4b0c362b99 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Wed, 24 Apr 2019 16:52:15 +0200 Subject: [PATCH 3/3] 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.