2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
package matrix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2023-04-29 12:49:41 +00:00
|
|
|
"codeberg.org/6543/xyaml"
|
2024-01-14 17:22:06 +00:00
|
|
|
|
2024-04-15 08:04:21 +00:00
|
|
|
errorTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors/types"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
limitTags = 10
|
|
|
|
limitAxis = 25
|
|
|
|
)
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// Matrix represents the pipeline matrix.
|
2017-03-05 07:56:08 +00:00
|
|
|
type Matrix map[string][]string
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// Axis represents a single permutation of entries from the pipeline matrix.
|
2017-03-05 07:56:08 +00:00
|
|
|
type Axis map[string]string
|
|
|
|
|
|
|
|
// String returns a string representation of an Axis as a comma-separated list
|
|
|
|
// of environment variables.
|
|
|
|
func (a Axis) String() string {
|
|
|
|
var envs []string
|
|
|
|
for k, v := range a {
|
|
|
|
envs = append(envs, k+"="+v)
|
|
|
|
}
|
|
|
|
return strings.Join(envs, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the Yaml matrix definition.
|
|
|
|
func Parse(data []byte) ([]Axis, error) {
|
|
|
|
axis, err := parseList(data)
|
|
|
|
if err == nil && len(axis) != 0 {
|
|
|
|
return axis, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix, err := parse(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matrix) == 0 {
|
2019-06-05 08:11:01 +00:00
|
|
|
return []Axis{}, nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return calc(matrix), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseString parses the Yaml string matrix definition.
|
|
|
|
func ParseString(data string) ([]Axis, error) {
|
|
|
|
return Parse([]byte(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func calc(matrix Matrix) []Axis {
|
|
|
|
// calculate number of permutations and extract the list of tags
|
|
|
|
// (ie go_version, redis_version, etc)
|
|
|
|
var perm int
|
|
|
|
var tags []string
|
|
|
|
for k, v := range matrix {
|
|
|
|
perm *= len(v)
|
|
|
|
if perm == 0 {
|
|
|
|
perm = len(v)
|
|
|
|
}
|
|
|
|
tags = append(tags, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
// structure to hold the transformed result set
|
2021-09-24 14:29:26 +00:00
|
|
|
var axisList []Axis
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-10-08 16:35:56 +00:00
|
|
|
// for each axis calculate the unique set of values that should be used.
|
2017-03-05 07:56:08 +00:00
|
|
|
for p := 0; p < perm; p++ {
|
|
|
|
axis := map[string]string{}
|
2024-05-24 20:35:04 +00:00
|
|
|
decrease := perm
|
2017-03-05 07:56:08 +00:00
|
|
|
for i, tag := range tags {
|
|
|
|
elems := matrix[tag]
|
2024-05-24 20:35:04 +00:00
|
|
|
decrease /= len(elems)
|
|
|
|
elem := p / decrease % len(elems)
|
2017-03-05 07:56:08 +00:00
|
|
|
axis[tag] = elems[elem]
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// enforce a maximum number of tags in the pipeline matrix.
|
2017-03-05 07:56:08 +00:00
|
|
|
if i > limitTags {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append to the list of axis.
|
|
|
|
axisList = append(axisList, axis)
|
|
|
|
|
|
|
|
// enforce a maximum number of axis that should be calculated.
|
|
|
|
if p > limitAxis {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return axisList
|
|
|
|
}
|
|
|
|
|
|
|
|
func parse(raw []byte) (Matrix, error) {
|
|
|
|
data := struct {
|
|
|
|
Matrix map[string][]string
|
|
|
|
}{}
|
2023-04-29 12:49:41 +00:00
|
|
|
if err := xyaml.Unmarshal(raw, &data); err != nil {
|
2024-04-15 08:04:21 +00:00
|
|
|
return nil, &errorTypes.PipelineError{Message: err.Error(), Type: errorTypes.PipelineErrorTypeCompiler}
|
2022-08-15 12:37:46 +00:00
|
|
|
}
|
|
|
|
return data.Matrix, nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseList(raw []byte) ([]Axis, error) {
|
|
|
|
data := struct {
|
|
|
|
Matrix struct {
|
|
|
|
Include []Axis
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
|
2023-04-29 12:49:41 +00:00
|
|
|
if err := xyaml.Unmarshal(raw, &data); err != nil {
|
2024-04-15 08:04:21 +00:00
|
|
|
return nil, &errorTypes.PipelineError{Message: err.Error(), Type: errorTypes.PipelineErrorTypeCompiler}
|
2022-08-15 12:37:46 +00:00
|
|
|
}
|
|
|
|
return data.Matrix.Include, nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|