Add Path/Query Escape functions

Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
Luca Bigliardi 2020-11-10 11:01:49 +01:00
parent c07a4e5288
commit 60b1c5eb64
2 changed files with 23 additions and 0 deletions

View File

@ -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)

View File

@ -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)
}