diff --git a/lib/notifiers/notifiers.go b/lib/notifiers/notifiers.go index 537d9bf9..03323345 100644 --- a/lib/notifiers/notifiers.go +++ b/lib/notifiers/notifiers.go @@ -36,12 +36,12 @@ import ( smpb "cloud.google.com/go/secretmanager/apiv1/secretmanagerpb" "cloud.google.com/go/storage" log "github.com/golang/glog" - "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/protoadapt" - "google.golang.org/protobuf/encoding/prototext" "github.com/google/cel-go/cel" "github.com/google/cel-go/checker/decls" "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/encoding/prototext" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/protoadapt" "gopkg.in/yaml.v2" ) @@ -453,7 +453,12 @@ func validateConfig(cfg *Config) error { } func validateTemplate(s string) error { - _, err := template.New("").Parse(s) + _, err := template.New("").Funcs(template.FuncMap{ + "replace": func(s, old, new string) string { + return strings.ReplaceAll(s, old, new) + }, + }).Parse(s) + return err } diff --git a/lib/notifiers/notifiers_test.go b/lib/notifiers/notifiers_test.go index aea81e85..e8a18de7 100644 --- a/lib/notifiers/notifiers_test.go +++ b/lib/notifiers/notifiers_test.go @@ -32,10 +32,10 @@ import ( "google.golang.org/protobuf/protoadapt" cbpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb" - "google.golang.org/protobuf/proto" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/testing/protocmp" "google.golang.org/protobuf/types/known/timestamppb" ) @@ -833,9 +833,10 @@ func TestReceiverWithIgnoredBadMessage(t *testing.T) { func TestParseTemplate(t *testing.T) { ctx := context.Background() + validTemplate := `{{.Build.Status}} {{ replace "bye world" "bye" "hello"}}` validFakeFactory := &fakeGCSReaderFactory{ data: map[string]string{ - "gs://path/to/my/template.yaml": "{{.Build.Status}}", + "gs://path/to/my/template.yaml": validTemplate, }, } for _, tc := range []struct { @@ -850,14 +851,14 @@ func TestParseTemplate(t *testing.T) { Type: "golang", URI: "gs://path/to/my/template.yaml", }, - want: "{{.Build.Status}}", + want: validTemplate, }, { name: "valid content", tmpl: &Template{ Type: "golang", - Content: "{{.Build.Status}}", + Content: validTemplate, }, - want: "{{.Build.Status}}", + want: validTemplate, }, { name: "invalid URI", tmpl: &Template{