From 60b1c5eb6482f9e34825a2f718d4bfed54db0c6f Mon Sep 17 00:00:00 2001 From: Luca Bigliardi Date: Tue, 10 Nov 2020 11:01:49 +0100 Subject: [PATCH] Add Path/Query Escape functions Signed-off-by: Luca Bigliardi --- format.go | 4 ++++ format_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/format.go b/format.go index 32355fd..f5558e2 100644 --- a/format.go +++ b/format.go @@ -18,6 +18,7 @@ import ( "bytes" "encoding/json" "log" + "net/url" "strings" "text/template" @@ -34,6 +35,9 @@ func NewFormatter(config *Config) (*Formatter, error) { "ToUpper": strings.ToUpper, "ToLower": strings.ToLower, "Join": strings.Join, + + "QueryEscape": url.QueryEscape, + "PathEscape": url.PathEscape, } tmpl, err := template.New("msg").Funcs(funcMap).Parse(config.MsgTemplate) diff --git a/format_test.go b/format_test.go index 46eb655..0b834e2 100644 --- a/format_test.go +++ b/format_test.go @@ -90,3 +90,22 @@ func TestStringsFunctions(t *testing.T) { CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs) } + +func TestUrlFunctions(t *testing.T) { + testingConfig := Config{ + MsgTemplate: "{{ .Annotations.SUMMARY | PathEscape }}", + } + + expectedAlertMsgs := []AlertMsg{ + AlertMsg{ + Channel: "#somechannel", + Alert: "service%20%2Fprometheus%20air%20down%20on%20instance1", + }, + AlertMsg{ + Channel: "#somechannel", + Alert: "service%20%2Fprometheus%20air%20down%20on%20instance2", + }, + } + + CreateFormatterAndCheckOutput(t, &testingConfig, expectedAlertMsgs) +}