Skip to content

Commit

Permalink
enhance(yaml): allow for users to parse pipelines using old library
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Nov 5, 2024
1 parent 9a4003b commit 8ba7055
Show file tree
Hide file tree
Showing 210 changed files with 6,061 additions and 84 deletions.
2 changes: 1 addition & 1 deletion api/pipeline/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/compiler/registry/github"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/internal"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/pipeline"
Expand Down
2 changes: 1 addition & 1 deletion api/types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strconv"
"strings"

"github.com/buildkite/yaml"
json "github.com/ghodss/yaml"
"gopkg.in/yaml.v3"
)

// ToString is a helper function to convert
Expand Down
2 changes: 1 addition & 1 deletion compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/go-vela/server/api/types/settings"
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/internal"
)

Expand Down
2 changes: 1 addition & 1 deletion compiler/native/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package native

import (
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/constants"
)

Expand Down
2 changes: 1 addition & 1 deletion compiler/native/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/urfave/cli/v2"

"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
)

const defaultCloneImage = "target/vela-git-slim:latest"
Expand Down
4 changes: 2 additions & 2 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"strings"
"time"

yml "github.com/buildkite/yaml"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-retryablehttp"
yml "gopkg.in/yaml.v3"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/constants"
)

Expand Down
205 changes: 201 additions & 4 deletions compiler/native/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"testing"
"time"

yml "github.com/buildkite/yaml"
"github.com/gin-gonic/gin"
"github.com/google/go-cmp/cmp"
"github.com/google/go-github/v65/github"
"github.com/urfave/cli/v2"
yml "gopkg.in/yaml.v3"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/constants"
"github.com/go-vela/server/internal"
)
Expand Down Expand Up @@ -1981,6 +1981,203 @@ func TestNative_Compile_StepsandStages(t *testing.T) {
}
}

func TestNative_Compile_LegacyMergeAnchor(t *testing.T) {
// setup types
set := flag.NewFlagSet("test", 0)
set.String("clone-image", defaultCloneImage, "doc")
c := cli.NewContext(nil, set, nil)
name := "foo"
author := "author"
event := "push"
number := 1

m := &internal.Metadata{
Database: &internal.Database{
Driver: "foo",
Host: "foo",
},
Queue: &internal.Queue{
Channel: "foo",
Driver: "foo",
Host: "foo",
},
Source: &internal.Source{
Driver: "foo",
Host: "foo",
},
Vela: &internal.Vela{
Address: "foo",
WebAddress: "foo",
},
}

compiler, err := FromCLIContext(c)
if err != nil {
t.Errorf("Creating compiler returned err: %v", err)
}

compiler.repo = &api.Repo{Name: &author}
compiler.build = &api.Build{Author: &name, Number: &number, Event: &event}
compiler.WithMetadata(m)

testEnv := environment(&api.Build{Author: &name, Number: &number, Event: &event}, m, &api.Repo{Name: &author}, nil)

serviceEnv := environment(&api.Build{Author: &name, Number: &number, Event: &event}, m, &api.Repo{Name: &author}, nil)
serviceEnv["REGION"] = "dev"

alphaEnv := environment(&api.Build{Author: &name, Number: &number, Event: &event}, m, &api.Repo{Name: &author}, nil)
alphaEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"echo alpha"})
alphaEnv["HOME"] = "/root"
alphaEnv["SHELL"] = "/bin/sh"

betaEnv := environment(&api.Build{Author: &name, Number: &number, Event: &event}, m, &api.Repo{Name: &author}, nil)
betaEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"echo beta"})
betaEnv["HOME"] = "/root"
betaEnv["SHELL"] = "/bin/sh"

gammaEnv := environment(&api.Build{Author: &name, Number: &number, Event: &event}, m, &api.Repo{Name: &author}, nil)
gammaEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"echo gamma"})
gammaEnv["HOME"] = "/root"
gammaEnv["SHELL"] = "/bin/sh"
gammaEnv["REGION"] = "dev"

want := &pipeline.Build{
Version: "legacy",
ID: "_author_1",
Metadata: pipeline.Metadata{
Clone: true,
Template: false,
Environment: []string{"steps", "services", "secrets"},
AutoCancel: &pipeline.CancelOptions{
Running: false,
Pending: false,
DefaultBranch: false,
},
},
Worker: pipeline.Worker{
Flavor: "",
Platform: "",
},
Services: pipeline.ContainerSlice{
&pipeline.Container{
ID: "service__author_1_service-a",
Detach: true,
Directory: "",
Environment: serviceEnv,
Image: "postgres",
Name: "service-a",
Number: 1,
Pull: "not_present",
Ports: []string{"5432:5432"},
},
},
Steps: pipeline.ContainerSlice{
&pipeline.Container{
ID: "step__author_1_init",
Directory: "/vela/src/foo//author",
Environment: testEnv,
Image: "#init",
Name: "init",
Number: 1,
Pull: "not_present",
},
&pipeline.Container{
ID: "step__author_1_clone",
Directory: "/vela/src/foo//author",
Environment: testEnv,
Image: defaultCloneImage,
Name: "clone",
Number: 2,
Pull: "not_present",
},
&pipeline.Container{
ID: "step__author_1_alpha",
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Directory: "/vela/src/foo//author",
Entrypoint: []string{"/bin/sh", "-c"},
Environment: alphaEnv,
Image: "alpine:latest",
Name: "alpha",
Number: 3,
Pull: "not_present",
Ruleset: pipeline.Ruleset{
If: pipeline.Rules{
Event: []string{"push"},
},
Matcher: "filepath",
Operator: "and",
},
},
&pipeline.Container{
ID: "step__author_1_beta",
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Directory: "/vela/src/foo//author",
Entrypoint: []string{"/bin/sh", "-c"},
Environment: betaEnv,
Image: "alpine:latest",
Name: "beta",
Number: 4,
Pull: "not_present",
Ruleset: pipeline.Ruleset{
If: pipeline.Rules{
Event: []string{"push"},
},
Matcher: "filepath",
Operator: "and",
},
},
&pipeline.Container{
ID: "step__author_1_gamma",
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Directory: "/vela/src/foo//author",
Entrypoint: []string{"/bin/sh", "-c"},
Environment: gammaEnv,
Image: "alpine:latest",
Name: "gamma",
Number: 5,
Pull: "not_present",
Ruleset: pipeline.Ruleset{
If: pipeline.Rules{
Event: []string{"push"},
},
Matcher: "filepath",
Operator: "and",
},
},
},
}

// run test on legacy version
yaml, err := os.ReadFile("testdata/steps_merge_anchor.yml")
if err != nil {
t.Errorf("Reading yaml file return err: %v", err)
}

got, _, err := compiler.Compile(context.Background(), yaml)
if err != nil {
t.Errorf("Compile returned err: %v", err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Compile() mismatch (-want +got):\n%s", diff)
}

// run test on current version (should fail)
yaml, err = os.ReadFile("../types/yaml/buildkite/testdata/merge_anchor.yml") // has `version: "1"` instead of `version: "legacy"`
if err != nil {
t.Errorf("Reading yaml file return err: %v", err)
}

got, _, err = compiler.Compile(context.Background(), yaml)
if err == nil {
t.Errorf("Compile should have returned err")
}

if got != nil {
t.Errorf("Compile is %v, want %v", got, nil)
}
}

// convertResponse converts the build to the ModifyResponse.
func convertResponse(build *yaml.Build) (*ModifyResponse, error) {
data, err := yml.Marshal(build)
Expand Down Expand Up @@ -2056,7 +2253,7 @@ func Test_client_modifyConfig(t *testing.T) {
Name: "docker",
Pull: "always",
Parameters: map[string]interface{}{
"init_options": map[interface{}]interface{}{
"init_options": map[string]interface{}{
"get_plugins": "true",
},
},
Expand Down Expand Up @@ -2089,7 +2286,7 @@ func Test_client_modifyConfig(t *testing.T) {
Name: "docker",
Pull: "always",
Parameters: map[string]interface{}{
"init_options": map[interface{}]interface{}{
"init_options": map[string]interface{}{
"get_plugins": "true",
},
},
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/constants"
"github.com/go-vela/server/internal"
)
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/internal"
)

Expand Down
2 changes: 1 addition & 1 deletion compiler/native/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/go-vela/server/compiler/template/starlark"
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/constants"
)

Expand Down
6 changes: 3 additions & 3 deletions compiler/native/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
)

func TestNative_ExpandStages(t *testing.T) {
Expand Down Expand Up @@ -589,7 +589,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) {
"auth_method": "token",
"username": "octocat",
"items": []interface{}{
map[interface{}]interface{}{"path": "docker", "source": "secret/docker"},
map[string]interface{}{"path": "docker", "source": "secret/docker"},
},
},
},
Expand All @@ -610,7 +610,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) {
"auth_method": "token",
"username": "octocat",
"items": []interface{}{
map[interface{}]interface{}{"path": "docker", "source": "secret/docker"},
map[string]interface{}{"path": "docker", "source": "secret/docker"},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion compiler/native/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package native

import (
"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/constants"
)

Expand Down
2 changes: 1 addition & 1 deletion compiler/native/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/urfave/cli/v2"

"github.com/go-vela/server/compiler/types/yaml"
"github.com/go-vela/server/compiler/types/yaml/yaml"
)

func TestNative_InitStage(t *testing.T) {
Expand Down
Loading

0 comments on commit 8ba7055

Please sign in to comment.