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

enhance(yaml)!: allow for users to parse pipelines using old library #1220

Closed
wants to merge 5 commits 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: 2 additions & 2 deletions 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 Expand Up @@ -101,7 +101,7 @@ func GetTemplates(c *gin.Context) {
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)

// parse the pipeline configuration
pipeline, _, err := compiler.Parse(p.GetData(), p.GetType(), new(yaml.Template))
pipeline, _, _, err := compiler.Parse(p.GetData(), p.GetType(), new(yaml.Template))
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to parse pipeline %s: %w", entry, err))

Expand Down
57 changes: 43 additions & 14 deletions api/types/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ import (
//
// swagger:model Pipeline
type Pipeline struct {
ID *int64 `json:"id,omitempty"`
Repo *Repo `json:"repo,omitempty"`
Commit *string `json:"commit,omitempty"`
Flavor *string `json:"flavor,omitempty"`
Platform *string `json:"platform,omitempty"`
Ref *string `json:"ref,omitempty"`
Type *string `json:"type,omitempty"`
Version *string `json:"version,omitempty"`
ExternalSecrets *bool `json:"external_secrets,omitempty"`
InternalSecrets *bool `json:"internal_secrets,omitempty"`
Services *bool `json:"services,omitempty"`
Stages *bool `json:"stages,omitempty"`
Steps *bool `json:"steps,omitempty"`
Templates *bool `json:"templates,omitempty"`
ID *int64 `json:"id,omitempty"`
Repo *Repo `json:"repo,omitempty"`
Commit *string `json:"commit,omitempty"`
Flavor *string `json:"flavor,omitempty"`
Platform *string `json:"platform,omitempty"`
Ref *string `json:"ref,omitempty"`
Type *string `json:"type,omitempty"`
Version *string `json:"version,omitempty"`
ExternalSecrets *bool `json:"external_secrets,omitempty"`
InternalSecrets *bool `json:"internal_secrets,omitempty"`
Services *bool `json:"services,omitempty"`
Stages *bool `json:"stages,omitempty"`
Steps *bool `json:"steps,omitempty"`
Templates *bool `json:"templates,omitempty"`
Warnings *[]string `json:"warnings,omitempty"`
// swagger:strfmt base64
Data *[]byte `json:"data,omitempty"`
}
Expand Down Expand Up @@ -210,6 +211,19 @@ func (p *Pipeline) GetTemplates() bool {
return *p.Templates
}

// GetWarnings returns the Warnings field.
//
// When the provided Pipeline type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (p *Pipeline) GetWarnings() []string {
// return zero value if Pipeline type or Warnings field is nil
if p == nil || p.Warnings == nil {
return []string{}
}

return *p.Warnings
}

// GetData returns the Data field.
//
// When the provided Pipeline type is nil, or the field within
Expand Down Expand Up @@ -405,6 +419,19 @@ func (p *Pipeline) SetTemplates(v bool) {
p.Templates = &v
}

// SetWarnings sets the Warnings field.
//
// When the provided Pipeline type is nil, it
// will set nothing and immediately return.
func (p *Pipeline) SetWarnings(v []string) {
// return if Pipeline type is nil
if p == nil {
return
}

p.Warnings = &v
}

// SetData sets the Data field.
//
// When the provided Pipeline type is nil, it
Expand Down Expand Up @@ -436,6 +463,7 @@ func (p *Pipeline) String() string {
Templates: %t,
Type: %s,
Version: %s,
Warnings: %v,
}`,
p.GetCommit(),
p.GetData(),
Expand All @@ -452,5 +480,6 @@ func (p *Pipeline) String() string {
p.GetTemplates(),
p.GetType(),
p.GetVersion(),
p.GetWarnings(),
)
}
12 changes: 12 additions & 0 deletions api/types/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func TestAPI_Pipeline_Getters(t *testing.T) {
t.Errorf("GetTemplates is %v, want %v", test.pipeline.GetTemplates(), test.want.GetTemplates())
}

if !reflect.DeepEqual(test.pipeline.GetWarnings(), test.want.GetWarnings()) {
t.Errorf("GetWarnings is %v, want %v", test.pipeline.GetWarnings(), test.want.GetWarnings())
}

if !reflect.DeepEqual(test.pipeline.GetData(), test.want.GetData()) {
t.Errorf("GetData is %v, want %v", test.pipeline.GetData(), test.want.GetData())
}
Expand Down Expand Up @@ -123,6 +127,7 @@ func TestAPI_Pipeline_Setters(t *testing.T) {
test.pipeline.SetStages(test.want.GetStages())
test.pipeline.SetSteps(test.want.GetSteps())
test.pipeline.SetTemplates(test.want.GetTemplates())
test.pipeline.SetWarnings(test.want.GetWarnings())
test.pipeline.SetData(test.want.GetData())

if test.pipeline.GetID() != test.want.GetID() {
Expand Down Expand Up @@ -181,6 +186,10 @@ func TestAPI_Pipeline_Setters(t *testing.T) {
t.Errorf("SetTemplates is %v, want %v", test.pipeline.GetTemplates(), test.want.GetTemplates())
}

if !reflect.DeepEqual(test.pipeline.GetWarnings(), test.want.GetWarnings()) {
t.Errorf("SetWarnings is %v, want %v", test.pipeline.GetWarnings(), test.want.GetWarnings())
}

if !reflect.DeepEqual(test.pipeline.GetData(), test.want.GetData()) {
t.Errorf("SetData is %v, want %v", test.pipeline.GetData(), test.want.GetData())
}
Expand All @@ -207,6 +216,7 @@ func TestAPI_Pipeline_String(t *testing.T) {
Templates: %t,
Type: %s,
Version: %s,
Warnings: %v,
}`,
p.GetCommit(),
p.GetData(),
Expand All @@ -223,6 +233,7 @@ func TestAPI_Pipeline_String(t *testing.T) {
p.GetTemplates(),
p.GetType(),
p.GetVersion(),
p.GetWarnings(),
)

// run test
Expand Down Expand Up @@ -252,6 +263,7 @@ func testPipeline() *Pipeline {
p.SetStages(false)
p.SetSteps(true)
p.SetTemplates(false)
p.SetWarnings([]string{"42:this is a warning"})
p.SetData(testPipelineData())

return p
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
4 changes: 2 additions & 2 deletions 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 All @@ -34,7 +34,7 @@ type Engine interface {

// Parse defines a function that converts
// an object to a yaml configuration.
Parse(interface{}, string, *yaml.Template) (*yaml.Build, []byte, error)
Parse(interface{}, string, *yaml.Template) (*yaml.Build, []byte, []string, error)

// ParseRaw defines a function that converts
// an object to a string.
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
12 changes: 7 additions & 5 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 All @@ -39,7 +39,7 @@ type ModifyResponse struct {

// Compile produces an executable pipeline from a yaml configuration.
func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *api.Pipeline, error) {
p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
p, data, warnings, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
if err != nil {
return nil, nil, err
}
Expand All @@ -48,6 +48,7 @@ func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *
_pipeline := p.ToPipelineAPI()
_pipeline.SetData(data)
_pipeline.SetType(c.repo.GetPipelineType())
_pipeline.SetWarnings(warnings)

// create map of templates for easy lookup
templates := mapFromTemplates(p.Templates)
Expand Down Expand Up @@ -104,7 +105,7 @@ func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *

// CompileLite produces a partial of an executable pipeline from a yaml configuration.
func (c *client) CompileLite(ctx context.Context, v interface{}, ruleData *pipeline.RuleData, substitute bool) (*yaml.Build, *api.Pipeline, error) {
p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
p, data, warnings, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
if err != nil {
return nil, nil, err
}
Expand All @@ -113,6 +114,7 @@ func (c *client) CompileLite(ctx context.Context, v interface{}, ruleData *pipel
_pipeline := p.ToPipelineAPI()
_pipeline.SetData(data)
_pipeline.SetType(c.repo.GetPipelineType())
_pipeline.SetWarnings(warnings)

if p.Metadata.RenderInline {
newPipeline, err := c.compileInline(ctx, p, c.GetTemplateDepth())
Expand Down Expand Up @@ -248,7 +250,7 @@ func (c *client) compileInline(ctx context.Context, p *yaml.Build, depth int) (*
// inject template name into variables
template.Variables["VELA_TEMPLATE_NAME"] = template.Name

parsed, _, err := c.Parse(bytes, format, template)
parsed, _, _, err := c.Parse(bytes, format, template)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading