From 173c9896de110236cbba0d1041ba04e41dc2fed3 Mon Sep 17 00:00:00 2001 From: ecrupper Date: Thu, 29 Jun 2023 09:13:18 -0500 Subject: [PATCH 1/2] init commit --- pipeline/metadata.go | 13 ++++++++++--- yaml/metadata.go | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/pipeline/metadata.go b/pipeline/metadata.go index 4617ebaa..27678486 100644 --- a/pipeline/metadata.go +++ b/pipeline/metadata.go @@ -8,7 +8,14 @@ package pipeline // // swagger:model PipelineMetadata type Metadata struct { - Template bool `json:"template,omitempty" yaml:"template,omitempty"` - Clone bool `json:"clone,omitempty" yaml:"clone,omitempty"` - Environment []string `json:"environment,omitempty" yaml:"environment,omitempty"` + Template bool `json:"template,omitempty" yaml:"template,omitempty"` + Clone bool `json:"clone,omitempty" yaml:"clone,omitempty"` + Environment []string `json:"environment,omitempty" yaml:"environment,omitempty"` + AutoCancel *CancelOptions `json:"auto_cancel,omitempty" yaml:"auto_cancel,omitempty"` +} + +type CancelOptions struct { + Running bool `yaml:"running,omitempty" json:"running,omitempty"` + Pending bool `yaml:"pending,omitempty" json:"pending,omitempty"` + DefaultBranch bool `yaml:"default_branch,omitempty" json:"default_branch,omitempty"` } diff --git a/yaml/metadata.go b/yaml/metadata.go index c927f0a7..10d4cf09 100644 --- a/yaml/metadata.go +++ b/yaml/metadata.go @@ -12,10 +12,17 @@ type ( // Metadata is the yaml representation of // the metadata block for a pipeline. Metadata struct { - Template bool `yaml:"template,omitempty" json:"template,omitempty" jsonschema:"description=Enables compiling the pipeline as a template.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-template-tag"` - RenderInline bool `yaml:"render_inline,omitempty" json:"render_inline,omitempty" jsonschema:"description=Enables inline compiling for the pipeline templates.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-render-inline-tag"` - Clone *bool `yaml:"clone,omitempty" json:"clone,omitempty" jsonschema:"default=true,description=Enables injecting the default clone process.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-clone-tag"` - Environment []string `yaml:"environment,omitempty" json:"environment,omitempty" jsonschema:"description=Controls which containers processes can have global env injected.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-environment-tag"` + Template bool `yaml:"template,omitempty" json:"template,omitempty" jsonschema:"description=Enables compiling the pipeline as a template.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-template-tag"` + RenderInline bool `yaml:"render_inline,omitempty" json:"render_inline,omitempty" jsonschema:"description=Enables inline compiling for the pipeline templates.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-render-inline-tag"` + Clone *bool `yaml:"clone,omitempty" json:"clone,omitempty" jsonschema:"default=true,description=Enables injecting the default clone process.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-clone-tag"` + Environment []string `yaml:"environment,omitempty" json:"environment,omitempty" jsonschema:"description=Controls which containers processes can have global env injected.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-environment-tag"` + AutoCancel *CancelOptions `yaml:"auto_cancel,omitempty" json:"auto_cancel,omitempty" jsonschema:"description=Enables auto canceling of queued or running pipelines that become stale due to new push.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-auto-cancel-tag"` + } + + CancelOptions struct { + Running *bool `yaml:"running,omitempty" json:"running,omitempty" jsonschema:"description=Enables auto canceling of running pipelines that become stale due to new push.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-auto-cancel-tag"` + Pending *bool `yaml:"pending,omitempty" json:"pending,omitempty" jsonschema:"description=Enables auto canceling of queued pipelines that become stale due to new push.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-auto-cancel-tag"` + DefaultBranch *bool `yaml:"default_branch,omitempty" json:"default_branch,omitempty" jsonschema:"description=Enables auto canceling of queued or running pipelines that become stale due to new push to default branch.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-auto-cancel-tag"` } ) @@ -29,10 +36,32 @@ func (m *Metadata) ToPipeline() *pipeline.Metadata { clone = *m.Clone } + autoCancel := new(pipeline.CancelOptions) + if m.AutoCancel == nil { + autoCancel.Pending = false + autoCancel.Running = false + autoCancel.DefaultBranch = false + } else { + if m.AutoCancel.Pending != nil { + autoCancel.Pending = *m.AutoCancel.Pending + } else { + autoCancel.Pending = true + } + + if m.AutoCancel.Running != nil { + autoCancel.Running = *m.AutoCancel.Running + } + + if m.AutoCancel.DefaultBranch != nil { + autoCancel.DefaultBranch = *m.AutoCancel.DefaultBranch + } + } + return &pipeline.Metadata{ Template: m.Template, Clone: clone, Environment: m.Environment, + AutoCancel: autoCancel, } } From 45549c2e84771a1364360c355db97e6c9ae52ca4 Mon Sep 17 00:00:00 2001 From: ecrupper Date: Thu, 29 Jun 2023 15:50:16 -0500 Subject: [PATCH 2/2] types testing --- pipeline/metadata.go | 1 + yaml/metadata.go | 5 +++++ yaml/metadata_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/pipeline/metadata.go b/pipeline/metadata.go index 27678486..cee1d4c1 100644 --- a/pipeline/metadata.go +++ b/pipeline/metadata.go @@ -14,6 +14,7 @@ type Metadata struct { AutoCancel *CancelOptions `json:"auto_cancel,omitempty" yaml:"auto_cancel,omitempty"` } +// CancelOptions is the pipeline representation of the auto_cancel block for a pipeline. type CancelOptions struct { Running bool `yaml:"running,omitempty" json:"running,omitempty"` Pending bool `yaml:"pending,omitempty" json:"pending,omitempty"` diff --git a/yaml/metadata.go b/yaml/metadata.go index 10d4cf09..08fa7a0a 100644 --- a/yaml/metadata.go +++ b/yaml/metadata.go @@ -19,6 +19,8 @@ type ( AutoCancel *CancelOptions `yaml:"auto_cancel,omitempty" json:"auto_cancel,omitempty" jsonschema:"description=Enables auto canceling of queued or running pipelines that become stale due to new push.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-auto-cancel-tag"` } + // CancelOptions is the yaml representation of + // the auto_cancel block for a pipeline. CancelOptions struct { Running *bool `yaml:"running,omitempty" json:"running,omitempty" jsonschema:"description=Enables auto canceling of running pipelines that become stale due to new push.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-auto-cancel-tag"` Pending *bool `yaml:"pending,omitempty" json:"pending,omitempty" jsonschema:"description=Enables auto canceling of queued pipelines that become stale due to new push.\nReference: https://go-vela.github.io/docs/reference/yaml/metadata/#the-auto-cancel-tag"` @@ -37,11 +39,14 @@ func (m *Metadata) ToPipeline() *pipeline.Metadata { } autoCancel := new(pipeline.CancelOptions) + + // default to false for all fields if block isn't found if m.AutoCancel == nil { autoCancel.Pending = false autoCancel.Running = false autoCancel.DefaultBranch = false } else { + // if block is found but pending field isn't, default to true if m.AutoCancel.Pending != nil { autoCancel.Pending = *m.AutoCancel.Pending } else { diff --git a/yaml/metadata_test.go b/yaml/metadata_test.go index 9219f6cb..901155bb 100644 --- a/yaml/metadata_test.go +++ b/yaml/metadata_test.go @@ -24,11 +24,21 @@ func TestYaml_Metadata_ToPipeline(t *testing.T) { Template: false, Clone: &fBool, Environment: []string{"steps", "services", "secrets"}, + AutoCancel: &CancelOptions{ + Pending: &tBool, + Running: &tBool, + DefaultBranch: &fBool, + }, }, want: &pipeline.Metadata{ Template: false, Clone: false, Environment: []string{"steps", "services", "secrets"}, + AutoCancel: &pipeline.CancelOptions{ + Pending: true, + Running: true, + DefaultBranch: false, + }, }, }, { @@ -41,6 +51,11 @@ func TestYaml_Metadata_ToPipeline(t *testing.T) { Template: false, Clone: true, Environment: []string{"steps", "services"}, + AutoCancel: &pipeline.CancelOptions{ + Pending: false, + Running: false, + DefaultBranch: false, + }, }, }, { @@ -48,11 +63,20 @@ func TestYaml_Metadata_ToPipeline(t *testing.T) { Template: false, Clone: nil, Environment: []string{"steps"}, + AutoCancel: &CancelOptions{ + Running: &tBool, + DefaultBranch: &tBool, + }, }, want: &pipeline.Metadata{ Template: false, Clone: true, Environment: []string{"steps"}, + AutoCancel: &pipeline.CancelOptions{ + Pending: true, + Running: true, + DefaultBranch: true, + }, }, }, }