Skip to content

Commit

Permalink
Pipeline: Add 'abort' operation
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Jul 19, 2024
1 parent 3593365 commit 6045141
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 3 deletions.
40 changes: 40 additions & 0 deletions pipeline/abort_op.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"errors"
"fmt"
)

type AbortOp struct {
Message string `yaml:"message"`
}

func (ao *AbortOp) Do(_ ActionContext) error {
return errors.New(ao.String())
}

func (ao *AbortOp) String() string {
return fmt.Sprintf("Abort[message=%s]", ao.Message)
}

func (ao *AbortOp) CloneWith(ctx ActionContext) Action {
return &AbortOp{
Message: ctx.TemplateEngine().RenderLenient(ao.Message, ctx.Snapshot()),
}
}
63 changes: 63 additions & 0 deletions pipeline/abort_op_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2024 Richard Kosegi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pipeline

import (
"github.com/rkosegi/yaml-toolkit/dom"
"github.com/stretchr/testify/assert"
"testing"
)

func TestAbortOpDo(t *testing.T) {
eo := &AbortOp{
Message: "conditions not met",
}
assert.Error(t, eo.Do(mockEmptyActCtx()))
}

func TestAbortOpCloneWith(t *testing.T) {
eo := &AbortOp{
Message: "Unsupported format: {{ .Format }}",
}
d := b.Container()
d.AddValue("Format", dom.LeafNode("toml"))
eo = eo.CloneWith(mockActCtx(d)).(*AbortOp)
assert.Equal(t, "Unsupported format: toml", eo.Message)
}

func TestAbortPipeline(t *testing.T) {
var (
err error
)
p := ActionSpec{
ActionMeta: ActionMeta{
When: strPointer("{{ eq .ENV \"prod\"}}"),
},
Operations: OpSpec{
Abort: &AbortOp{
Message: "Pipeline should not run in production",
},
},
}
d := b.Container()
d.AddValue("ENV", dom.LeafNode("prod"))
err = newTestExec(d).Execute(p)
assert.Error(t, err)
d.AddValue("ENV", dom.LeafNode("dev"))
err = newTestExec(d).Execute(p)
assert.NoError(t, err)
}
43 changes: 40 additions & 3 deletions pipeline/opspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package pipeline

import "fmt"
import (
"fmt"
"strings"
)

func (as OpSpec) toList() []Action {
actions := make([]Action, 0)
Expand All @@ -41,6 +44,9 @@ func (as OpSpec) toList() []Action {
if as.ForEach != nil {
actions = append(actions, as.ForEach)
}
if as.Abort != nil {
actions = append(actions, as.Abort)
}
return actions
}

Expand Down Expand Up @@ -77,10 +83,41 @@ func (as OpSpec) CloneWith(ctx ActionContext) Action {
if as.Env != nil {
r.Env = as.Env.CloneWith(ctx).(*EnvOp)
}
if as.Abort != nil {
r.Abort = as.Abort.CloneWith(ctx).(*AbortOp)
}
return r
}

func (as OpSpec) String() string {
return fmt.Sprintf("OpSpec[Env=%s,Export=%v,ForEach=%v,Import=%v,Patch=%v,Set=%v,Template=%v]",
as.Env, as.Export, as.ForEach, as.Import, as.Patch, as.Set, as.Template)
var sb strings.Builder
parts := make([]string, 0)
sb.WriteString("OpSpec[")
if as.Abort != nil {
parts = append(parts, fmt.Sprintf("Abort=%v", as.Abort.String()))
}
if as.Env != nil {
parts = append(parts, fmt.Sprintf("Env=%v", as.Env.String()))
}
if as.Export != nil {
parts = append(parts, fmt.Sprintf("Export=%v", as.Export.String()))
}
if as.ForEach != nil {
parts = append(parts, fmt.Sprintf("ForEach=%v", as.ForEach.String()))
}
if as.Import != nil {
parts = append(parts, fmt.Sprintf("Import=%v", as.Import.String()))
}
if as.Patch != nil {
parts = append(parts, fmt.Sprintf("Patch=%v", as.Patch.String()))
}
if as.Set != nil {
parts = append(parts, fmt.Sprintf("Set=%v", as.Set.String()))
}
if as.Template != nil {
parts = append(parts, fmt.Sprintf("Template=%v", as.Template.String()))
}
sb.WriteString(strings.Join(parts, ","))
sb.WriteString("]")
return sb.String()
}
3 changes: 3 additions & 0 deletions pipeline/opspec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func TestOpSpecCloneWith(t *testing.T) {
Path: "{{ .Path }}",
Format: OutputFormatYaml,
},
Abort: &AbortOp{
Message: "abort",
},
}

a := o.CloneWith(mockActCtx(b.FromMap(map[string]interface{}{
Expand Down
3 changes: 3 additions & 0 deletions pipeline/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ type OpSpec struct {
Export *ExportOp `yaml:"export,omitempty"`
// ForEach execute same operation in a loop for every configured item
ForEach *ForEachOp `yaml:"forEach,omitempty"`

// Abort is able to signal error, so that pipeline can abort execution
Abort *AbortOp `yaml:"abort,omitempty"`
}

type ActionSpec struct {
Expand Down

0 comments on commit 6045141

Please sign in to comment.