diff --git a/pkg/event/kind/webhook/listener.go b/pkg/event/kind/webhook/listener.go index f098a2f9760..02f9f6ae1e0 100644 --- a/pkg/event/kind/webhook/listener.go +++ b/pkg/event/kind/webhook/listener.go @@ -16,6 +16,7 @@ import ( thttp "github.com/kubeshop/testkube/pkg/http" "github.com/kubeshop/testkube/pkg/log" "github.com/kubeshop/testkube/pkg/utils" + "github.com/kubeshop/testkube/pkg/utils/text" ) var _ common.Listener = (*WebhookListener)(nil) @@ -170,6 +171,7 @@ func (l *WebhookListener) processTemplate(field, body string, event testkube.Eve var tmpl *template.Template tmpl, err := utils.NewTemplate(field).Funcs(template.FuncMap{ + "tostr": text.ToStr, "executionstatustostring": testkube.ExecutionStatusString, "testsuiteexecutionstatustostring": testkube.TestSuiteExecutionStatusString, }).Parse(body) diff --git a/pkg/utils/text/tostr.go b/pkg/utils/text/tostr.go new file mode 100644 index 00000000000..99fd3626dcb --- /dev/null +++ b/pkg/utils/text/tostr.go @@ -0,0 +1,7 @@ +package text + +import "fmt" + +func ToStr(in any) string { + return fmt.Sprintf("%v", in) +} diff --git a/pkg/utils/text/tostr_test.go b/pkg/utils/text/tostr_test.go new file mode 100644 index 00000000000..f6cafec4a77 --- /dev/null +++ b/pkg/utils/text/tostr_test.go @@ -0,0 +1,44 @@ +package text + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type stringerTestImpl struct { + name string +} + +func (tt stringerTestImpl) String() string { + return tt.name +} + +func TestToStr(t *testing.T) { + + t.Run("converts stringer to string", func(t *testing.T) { + test := stringerTestImpl{name: "test"} + assert.Equal(t, "test", ToStr(test)) + }) + + t.Run("converts int to str", func(t *testing.T) { + test := 1 + assert.Equal(t, "1", ToStr(test)) + }) + + t.Run("converts float to str", func(t *testing.T) { + test := 1.1 + assert.Equal(t, "1.1", ToStr(test)) + }) + + t.Run("converts bool to str", func(t *testing.T) { + test := true + assert.Equal(t, "true", ToStr(test)) + }) + + t.Run("converts nil to str", func(t *testing.T) { + var test *string + assert.Equal(t, "", ToStr(test)) + }) + +}