Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add version to workflow configs #1834

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pipeline/frontend/yaml/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ package yaml

import "errors"

var (
ErrUnsuportedVersion = errors.New("unsuported yaml version detected")
)

// PipelineParseError is an error that occurs when the pipeline parsing fails.
type PipelineParseError struct {
Err error
Expand Down
30 changes: 28 additions & 2 deletions pipeline/frontend/yaml/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@ import (
"fmt"

"codeberg.org/6543/xyaml"
"gopkg.in/yaml.v3"

"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/constraint"
"github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types"
)

// ParseBytes parses the configuration from bytes b.
func ParseBytes(b []byte) (*types.Workflow, error) {
yamlVersion, err := checkVersion(b)
if err != nil {
&PipelineParseError{Err: err}
}

out := new(types.Workflow)
err := xyaml.Unmarshal(b, out)
if err != nil {
return nil, err
return nil, &PipelineParseError{Err: err}
}

// make sure detected version is set
out.Version = yamlVersion

// support deprecated branch filter
if out.BranchesDontUseIt != nil {
if out.When.Constraints == nil {
out.When.Constraints = []constraint.Constraint{{Branch: *out.BranchesDontUseIt}}
} else if len(out.When.Constraints) == 1 && out.When.Constraints[0].Branch.IsEmpty() {
out.When.Constraints[0].Branch = *out.BranchesDontUseIt
} else {
return nil, fmt.Errorf("could not apply deprecated branches filter into global when filter")
return nil, &PipelineParseError{Err: fmt.Errorf("could not apply deprecated branches filter into global when filter")}
}
out.BranchesDontUseIt = nil
}
Expand All @@ -38,3 +48,19 @@ func ParseString(s string) (*types.Workflow, error) {
[]byte(s),
)
}

func checkVersion(b []byte) (string, error) {
verStr := struct {
Version string `yaml:"version"`
}{}
_ = yaml.Unmarshal(b, &verStr)
// TODO: should we require a version number -> in therms of UX we should not, in terms of strong typisation we should
if verStr == "" {
verStr = Version
}

if verStr != Version {
return "", ErrUnsuportedVersion
}
return verStr, nil
}
1 change: 1 addition & 0 deletions pipeline/frontend/yaml/types/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type (
DependsOn []string `yaml:"depends_on,omitempty"`
RunsOn []string `yaml:"runs_on,omitempty"`
SkipClone bool `yaml:"skip_clone"`
Version string `yaml:"version"`
// Undocumented
Cache base.StringOrSlice `yaml:"cache,omitempty"`
Networks WorkflowNetworks `yaml:"networks,omitempty"`
Expand Down
18 changes: 18 additions & 0 deletions pipeline/frontend/yaml/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.

package yaml

// Version of this package and it's subpackages
const Version = "1"