Skip to content

Commit

Permalink
Added a template function 'replace' to BlockKitTemplate parser, which…
Browse files Browse the repository at this point in the history
… lets user replace substrings in the template (#161)

Co-authored-by: Yaniv Brener <ybrener@beyond12.org>
  • Loading branch information
ezeYaniv and ezeYaniv authored Jun 5, 2023
1 parent f695cd1 commit ec3dbaa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 3 additions & 0 deletions slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\"" "'"}}`
8 changes: 7 additions & 1 deletion slack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"fmt"
"text/template"
"strings"

"github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers"
log "github.com/golang/glog"
Expand Down Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions slack/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()
Expand Down Expand Up @@ -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",
}},
},
Expand Down

0 comments on commit ec3dbaa

Please sign in to comment.