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/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. 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/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 +} 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.