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

feat(auto-cancel): add pipeline and yaml field for auto cancel #299

Merged
merged 3 commits into from
Oct 9, 2023
Merged
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
14 changes: 11 additions & 3 deletions pipeline/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ 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"`
}

// 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"`
DefaultBranch bool `yaml:"default_branch,omitempty" json:"default_branch,omitempty"`
}
42 changes: 38 additions & 4 deletions yaml/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ 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 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"`
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"`
}
)

Expand All @@ -27,10 +36,35 @@ func (m *Metadata) ToPipeline() *pipeline.Metadata {
clone = *m.Clone
}

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 {
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,
}
}

Expand Down
24 changes: 24 additions & 0 deletions yaml/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,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,
},
},
},
{
Expand All @@ -39,18 +49,32 @@ func TestYaml_Metadata_ToPipeline(t *testing.T) {
Template: false,
Clone: true,
Environment: []string{"steps", "services"},
AutoCancel: &pipeline.CancelOptions{
Pending: false,
Running: false,
DefaultBranch: false,
},
},
},
{
metadata: &Metadata{
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,
},
},
},
}
Expand Down