Skip to content
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
57e5162
Merge branch 'master' of github.com:go-vela/server
jbrockopp Feb 23, 2022
98b740d
chore: general refactor expand functions
jbrockopp Feb 23, 2022
461ca5a
chore: refactor template Render() functions
jbrockopp Feb 23, 2022
1e17b6a
enhance: support render inline templates
jbrockopp Feb 23, 2022
da5c94d
chore: fix api endpoints for compiler changes
jbrockopp Feb 23, 2022
776ddff
chore: add leftover todos
jbrockopp Feb 23, 2022
2eb7bd6
fix tests
JordanSussman Feb 24, 2022
4ebc042
Merge branch 'poc/stages/templates' of github.com:go-vela/server into…
jbrockopp Feb 25, 2022
8b5fe54
enhance: address leftover TODOs
jbrockopp Feb 25, 2022
5cd457c
chore: save work
jbrockopp Feb 25, 2022
805a6c3
fix pipeline type
JordanSussman Feb 25, 2022
bc62ada
Merge branch 'master' into poc/stages/templates
JordanSussman Feb 25, 2022
9c4e2fe
another fix
JordanSussman Feb 28, 2022
d1e9558
Merge branch 'poc/stages/templates' of github.com:go-vela/server into…
JordanSussman Feb 28, 2022
6a0e4b7
use branch from github instead of relative
JordanSussman Feb 28, 2022
e0a387d
feat: add a lite compile for pipeline endpoints
kneal Feb 28, 2022
87434f3
fix: add inline suppport to lite compile
kneal Feb 28, 2022
f3dac73
fix: update linter and add comments
kneal Feb 28, 2022
f667472
init tests for render_inline
JordanSussman Mar 6, 2022
25456e6
Merge branch 'master' into poc/stages/templates
JordanSussman Mar 6, 2022
f18ebe3
more updates
JordanSussman Mar 6, 2022
4e3fc56
Merge branch 'poc/stages/templates' of github.com:go-vela/server into…
JordanSussman Mar 6, 2022
8bb3b3c
more linter updates
JordanSussman Mar 6, 2022
469f91c
fix variables for templates
JordanSussman Mar 7, 2022
221597b
make linter happy
JordanSussman Mar 7, 2022
5049f4d
fix existing tests
JordanSussman Mar 7, 2022
43fb3a5
add more inline tests
JordanSussman Mar 7, 2022
00c0529
delete unused nolint
JordanSussman Mar 7, 2022
41af322
Merge branch 'poc/stages/templates' of github.com:go-vela/server into…
jbrockopp Mar 7, 2022
77da644
add test for pipeline type
JordanSussman Mar 7, 2022
2d02198
update goimports
JordanSussman Mar 7, 2022
82526a8
return build object
JordanSussman Mar 8, 2022
f450a0e
more code coverage
JordanSussman Mar 8, 2022
9bda378
Merge branch 'master' into poc/stages/templates
JordanSussman Mar 10, 2022
9a674fb
check if template is present
JordanSussman Mar 10, 2022
06b5871
fix linter
JordanSussman Mar 10, 2022
3e48d92
Merge branch 'poc/stages/templates' of github.com:go-vela/server into…
JordanSussman Mar 10, 2022
97b4f72
another linter update
JordanSussman Mar 10, 2022
40c2dd5
yet another update
JordanSussman Mar 10, 2022
08b1152
fix typo
JordanSussman Mar 10, 2022
795971b
yet more linter updates
JordanSussman Mar 10, 2022
8ce6fa0
fix test
JordanSussman Mar 11, 2022
4b65af5
make clean
JordanSussman Mar 11, 2022
af14284
return error if render_inline and step template mixed
JordanSussman Mar 11, 2022
44454a5
fix flaky test
JordanSussman Mar 11, 2022
d88662e
revert last_update code
JordanSussman Mar 11, 2022
4d9acce
fix test
JordanSussman Mar 11, 2022
4808ced
Merge branch 'master' into poc/stages/templates
JordanSussman Mar 11, 2022
fd7d467
fix test
JordanSussman Mar 11, 2022
81b8399
Merge branch 'poc/stages/templates' of github.com:go-vela/server into…
jbrockopp Mar 11, 2022
98204ca
chore: address linter feedback
jbrockopp Mar 11, 2022
15ca263
Merge branch 'master' into poc/stages/templates
wass3r Mar 15, 2022
28af1c7
default format to golang
JordanSussman Mar 15, 2022
770c636
make linter happy
JordanSussman Mar 15, 2022
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
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,14 @@ spec-version-update:
#
# Usage: `make spec`
.PHONY: spec
spec: spec-gen spec-version-update spec-validate
spec: spec-gen spec-version-update spec-validate

# The `lint` target is intended to lint the
# Go source code with golangci-lint.
#
# Usage: `make lint`
.PHONY: lint
lint:
@echo
@echo "### Linting Go Code"
@golangci-lint run ./...
110 changes: 39 additions & 71 deletions api/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,21 @@ func GetPipeline(ctx *gin.Context) {
"user": u.GetName(),
}).Infof("reading pipeline for repo %s", r.GetFullName())

pipeline, _, err := getUnprocessedPipeline(ctx)
config, comp, err := getUnprocessedPipeline(ctx)
if err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)

return
}

pipeline, err := comp.Parse(config, r.GetPipelineType(), map[string]interface{}{})
if err != nil {
retErr := fmt.Errorf("unable to validate pipeline configuration for %s: %w", repoName(ctx), err)
util.HandleError(ctx, http.StatusBadRequest, retErr)

return
}

writeOutput(ctx, pipeline)
}

Expand Down Expand Up @@ -160,13 +168,21 @@ func GetTemplates(ctx *gin.Context) {
"user": u.GetName(),
}).Infof("reading templates from pipeline for repo %s", r.GetFullName())

pipeline, _, err := getUnprocessedPipeline(ctx)
config, comp, err := getUnprocessedPipeline(ctx)
if err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)

return
}

pipeline, err := comp.Parse(config, r.GetPipelineType(), map[string]interface{}{})
if err != nil {
retErr := fmt.Errorf("unable to validate pipeline configuration for %s: %w", repoName(ctx), err)
util.HandleError(ctx, http.StatusBadRequest, retErr)

return
}

// create map of templates for response body
templates, err := getTemplateLinks(ctx, pipeline.Templates)
if err != nil {
Expand Down Expand Up @@ -225,6 +241,8 @@ func GetTemplates(ctx *gin.Context) {

// ExpandPipeline represents the API handler to capture and
// expand a pipeline configuration.
//
// nolint: dupl // ignore false positive of duplicate code
func ExpandPipeline(ctx *gin.Context) {
// capture middleware values
o := org.Retrieve(ctx)
Expand All @@ -240,15 +258,17 @@ func ExpandPipeline(ctx *gin.Context) {
"user": u.GetName(),
}).Infof("expanding templates from pipeline for repo %s", r.GetFullName())

pipeline, comp, err := getUnprocessedPipeline(ctx)
config, comp, err := getUnprocessedPipeline(ctx)
if err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)

return
}

if err := expandPipeline(ctx, pipeline, comp, false); err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)
pipeline, err := comp.CompileLite(config, true, false)
if err != nil {
retErr := fmt.Errorf("unable to validate pipeline configuration for %s: %w", repoName(ctx), err)
util.HandleError(ctx, http.StatusBadRequest, retErr)

return
}
Expand Down Expand Up @@ -315,24 +335,22 @@ func ValidatePipeline(ctx *gin.Context) {
"user": u.GetName(),
}).Infof("validating pipeline for repo %s", r.GetFullName())

pipeline, comp, err := getUnprocessedPipeline(ctx)
config, comp, err := getUnprocessedPipeline(ctx)
if err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)

return
}

template := false

// check optional template query parameter
if ok, _ := strconv.ParseBool(ctx.DefaultQuery("template", "true")); ok {
if err := expandPipeline(ctx, pipeline, comp, false); err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)

return
}
template = true
}

// validate the yaml configuration
if err = comp.Validate(pipeline); err != nil {
pipeline, err := comp.CompileLite(config, template, false)
if err != nil {
retErr := fmt.Errorf("unable to validate pipeline configuration for %s: %w", repoName(ctx), err)
util.HandleError(ctx, http.StatusBadRequest, retErr)

Expand Down Expand Up @@ -387,6 +405,7 @@ func ValidatePipeline(ctx *gin.Context) {

// CompilePipeline represents the API handler to capture,
// expand and compile a pipeline configuration.
// nolint: dupl // ignore false positive of duplicate code
func CompilePipeline(ctx *gin.Context) {
// capture middleware values
o := org.Retrieve(ctx)
Expand All @@ -402,21 +421,15 @@ func CompilePipeline(ctx *gin.Context) {
"user": u.GetName(),
}).Infof("compiling pipeline for repo %s", r.GetFullName())

pipeline, comp, err := getUnprocessedPipeline(ctx)
config, comp, err := getUnprocessedPipeline(ctx)
if err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)

return
}

if err := expandPipeline(ctx, pipeline, comp, true); err != nil {
util.HandleError(ctx, http.StatusBadRequest, err)

return
}

// validate the yaml configuration
if err = comp.Validate(pipeline); err != nil {
pipeline, err := comp.CompileLite(config, true, true)
if err != nil {
retErr := fmt.Errorf("unable to validate pipeline configuration for %s: %w", repoName(ctx), err)
util.HandleError(ctx, http.StatusBadRequest, retErr)

Expand All @@ -426,8 +439,9 @@ func CompilePipeline(ctx *gin.Context) {
writeOutput(ctx, pipeline)
}

// getUnprocessedPipeline retrieves the unprocessed pipeline from a given context.
func getUnprocessedPipeline(ctx *gin.Context) (*yaml.Build, compiler.Engine, error) {
// getUnprocessedPipeline retrieves the unprocessed pipeline from a given context
// and creates an instance of the compiler with metadata.
func getUnprocessedPipeline(ctx *gin.Context) ([]byte, compiler.Engine, error) {
// capture middleware values
meta := ctx.MustGet("metadata").(*types.Metadata)
repo := repo.Retrieve(ctx)
Expand All @@ -453,12 +467,7 @@ func getUnprocessedPipeline(ctx *gin.Context) (*yaml.Build, compiler.Engine, err
WithRepo(repo).
WithUser(user)

pipeline, err := comp.Parse(config)
if err != nil {
return nil, nil, fmt.Errorf("unable to parse pipeline configuration for %s: %w", repoName(ctx), err)
}

return pipeline, comp, nil
return config, comp, nil
}

// getTemplateLinks helper function that retrieves source provider links
Expand Down Expand Up @@ -529,44 +538,3 @@ func writeOutput(ctx *gin.Context, pipeline interface{}) {
ctx.YAML(http.StatusOK, pipeline)
}
}

// expandPipeline uses a given pipeline and compiler to expand stages and steps
// in the pipeline along with optionally substituting the environmental variables.
func expandPipeline(ctx *gin.Context, pipeline *yaml.Build, comp compiler.Engine, substituteEnv bool) error {
// create map of templates for easy lookup
templates := pipeline.Templates.Map()

var err error

if len(pipeline.Stages) > 0 {
// inject the templates into the stages
pipeline.Stages, pipeline.Secrets, pipeline.Services, pipeline.Environment, err = comp.ExpandStages(pipeline, templates)
if err != nil {
return fmt.Errorf("unable to expand stages in pipeline configuration for %s: %w", repoName(ctx), err)
}

if substituteEnv {
// inject the substituted environment variables into the stages
pipeline.Stages, err = comp.SubstituteStages(pipeline.Stages)
if err != nil {
return fmt.Errorf("unable to substitute stages in pipeline configuration for %s: %w", repoName(ctx), err)
}
}
} else {
// inject the templates into the steps
pipeline.Steps, pipeline.Secrets, pipeline.Services, pipeline.Environment, err = comp.ExpandSteps(pipeline, templates)
if err != nil {
return fmt.Errorf("unable to expand steps in pipeline configuration for %s: %w", repoName(ctx), err)
}

if substituteEnv {
// inject the substituted environment variables into the steps
pipeline.Steps, err = comp.SubstituteSteps(pipeline.Steps)
if err != nil {
return fmt.Errorf("unable to substitute steps in pipeline configuration for %s: %w", repoName(ctx), err)
}
}
}

return nil
}
13 changes: 8 additions & 5 deletions compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ type Engine interface {
// Parse internally to convert the object to a yaml configuration.
Compile(interface{}) (*pipeline.Build, error)

// CompileLite defines a function that produces an light executable
// representation of a pipeline from an object. This calls
// Parse internally to convert the object to a yaml configuration.
CompileLite(interface{}, bool, bool) (*yaml.Build, error)

// Duplicate defines a function that
// creates a clone of the Engine.
Duplicate() Engine

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

// ParseRaw defines a function that converts
// an object to a string.
Expand Down Expand Up @@ -66,12 +71,10 @@ type Engine interface {

// ExpandStages defines a function that injects the template
// for each templated step in every stage in a yaml configuration.

ExpandStages(*yaml.Build, map[string]*yaml.Template) (yaml.StageSlice, yaml.SecretSlice, yaml.ServiceSlice, raw.StringSliceMap, error)
ExpandStages(*yaml.Build, map[string]*yaml.Template) (*yaml.Build, error)
// ExpandSteps defines a function that injects the template
// for each templated step in a yaml configuration.

ExpandSteps(*yaml.Build, map[string]*yaml.Template) (yaml.StepSlice, yaml.SecretSlice, yaml.ServiceSlice, raw.StringSliceMap, error)
ExpandSteps(*yaml.Build, map[string]*yaml.Template) (*yaml.Build, error)

// Init Compiler Interface Functions

Expand Down
Loading