Skip to content

Commit

Permalink
Merge pull request #178 from GoogleCloudPlatform/allowrepl
Browse files Browse the repository at this point in the history
Allow replace function in template validation
  • Loading branch information
monicacinom authored Jul 11, 2023
2 parents 17dd0b4 + 96c9c3d commit 603e6b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 9 additions & 4 deletions lib/notifiers/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
11 changes: 6 additions & 5 deletions lib/notifiers/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand Down

0 comments on commit 603e6b4

Please sign in to comment.