From ec3dbaa847230f9c4c6aae2ef19102afdc1c571a Mon Sep 17 00:00:00 2001 From: Yaniv Brener Date: Mon, 5 Jun 2023 10:27:23 -0700 Subject: [PATCH] Added a template function 'replace' to BlockKitTemplate parser, which lets user replace substrings in the template (#161) Co-authored-by: Yaniv Brener --- slack/README.md | 3 +++ slack/main.go | 8 +++++++- slack/main_test.go | 13 +++++++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/slack/README.md b/slack/README.md index 7886ca03..eb836e86 100644 --- a/slack/README.md +++ b/slack/README.md @@ -27,3 +27,6 @@ gcloud run deploy service-name \ --no-allow-unauthenticated \ --update-env-vars=CONFIG_PATH=config-path,PROJECT_ID=project-id ``` +## Slack BlockKit Template Functions +- The `replace` function allows replacement of substrings in any {{template variables}} in the .json Slack template. (For example, the variable `.Build.FailureInfo.Detail` contains double quotes, which breaks the BlockKitTemplate parsing.) + - Usage: `{{replace .Build.FailureInfo.Detail "\"" "'"}}` \ No newline at end of file diff --git a/slack/main.go b/slack/main.go index de82c6f5..76e16aad 100644 --- a/slack/main.go +++ b/slack/main.go @@ -19,6 +19,7 @@ import ( "context" "fmt" "text/template" + "strings" "github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers" log "github.com/golang/glog" @@ -64,7 +65,12 @@ func (s *slackNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, blockK return fmt.Errorf("failed to get token secret: %w", err) } s.webhookURL = wu - tmpl, err := template.New("blockkit_template").Parse(blockKitTemplate) + tmpl, err := template.New("blockkit_template").Funcs(template.FuncMap{ + "replace": func(s, old, new string) string { + return strings.ReplaceAll(s, old, new) + }, + }).Parse(blockKitTemplate) + s.tmpl = tmpl s.br = br diff --git a/slack/main_test.go b/slack/main_test.go index 227ef30e..dcbb4a79 100644 --- a/slack/main_test.go +++ b/slack/main_test.go @@ -3,6 +3,7 @@ package main import ( "testing" "text/template" + "strings" "github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers" "github.com/google/go-cmp/cmp" @@ -37,13 +38,17 @@ func TestWriteMessage(t *testing.T) { "text": "Logs" }, "value": "click_me_123", - "url": "{{.Build.LogUrl}}", + "url": "{{replace .Build.LogUrl "\"" "'"}}", "action_id": "button-action" } } ]` - tmpl, err := template.New("blockkit_template").Parse(blockKitTemplate) + tmpl, err := template.New("blockkit_template").Funcs(template.FuncMap{ + "replace": func(s, old, new string) string { + return strings.ReplaceAll(s, old, new) + }, + }).Parse(blockKitTemplate) if err != nil { t.Fatalf("failed to parse template: %v", err) } @@ -52,7 +57,7 @@ func TestWriteMessage(t *testing.T) { ProjectId: "my-project-id", Id: "some-build-id", Status: cbpb.Build_SUCCESS, - LogUrl: "https://some.example.com/log/url?foo=bar", + LogUrl: "https://some.example.com/log/url?foo=bar\"", }}} got, err := n.writeMessage() @@ -85,7 +90,7 @@ func TestWriteMessage(t *testing.T) { Type: "button", Text: &slack.TextBlockObject{Type: "plain_text", Text: "Logs"}, ActionID: "button-action", - URL: "https://some.example.com/log/url?foo=bar", + URL: "https://some.example.com/log/url?foo=bar'", Value: "click_me_123", }}, },