1
0
mirror of https://github.com/prometheus/alertmanager.git synced 2026-02-05 15:45:34 +01:00

Add templating functions for working with URLs (#4625)

* Add safeUrl to mark URL strings as safe

Signed-off-by: Ruben Simons <r.simons@fz-juelich.de>

* Add urlUnescape to allow for reversing of escaped URL strings

Signed-off-by: Ruben Simons <r.simons@fz-juelich.de>

---------

Signed-off-by: Ruben Simons <r.simons@fz-juelich.de>
Co-authored-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
StopMotionCuber
2025-11-05 18:04:58 +01:00
committed by GitHub
parent cfc4731dd0
commit f969a4c8f6
3 changed files with 23 additions and 0 deletions

View File

@@ -93,6 +93,8 @@ templating.
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
| safeUrl | text string | [html/template.URL](https://golang.org/pkg/html/template/#URL), Marks string as URL not requiring auto-escaping. |
| urlUnescape | text string | [url.QueryUnescape](https://pkg.go.dev/net/url#QueryUnescape), unescapes a URL with % encoding |
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
| date | string, time.Time | Returns the text representation of the time in the specified format. For documentation on formats refer to [pkg.go.dev/time](https://pkg.go.dev/time#pkg-constants). |
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |

View File

@@ -188,6 +188,10 @@ var DefaultFuncs = FuncMap{
"safeHtml": func(text string) tmplhtml.HTML {
return tmplhtml.HTML(text)
},
"safeUrl": func(text string) tmplhtml.URL {
return tmplhtml.URL(text)
},
"urlUnescape": url.QueryUnescape,
"reReplaceAll": func(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)

View File

@@ -367,11 +367,28 @@ func TestTemplateExpansion(t *testing.T) {
html: true,
exp: "<b>",
},
{
title: "URL template with escaping",
in: `<a href="/search?{{ "q=test%20foo" }}"></a>`,
html: true,
exp: `<a href="/search?q%3dtest%2520foo"></a>`,
},
{
title: "URL template using safeUrl",
in: `<a href="/search?{{ "q=test%20foo" | safeUrl }}"></a>`,
html: true,
exp: `<a href="/search?q=test%20foo"></a>`,
},
{
title: "Template using reReplaceAll",
in: `{{ reReplaceAll "ab" "AB" "abcdabcda"}}`,
exp: "ABcdABcda",
},
{
title: "Template using urlUnescape",
in: `{{ "search?q=test%20foo" | urlUnescape }}`,
exp: "search?q=test foo",
},
{
title: "Template using stringSlice",
in: `{{ with .GroupLabels }}{{ with .Remove (stringSlice "key1" "key3") }}{{ .SortedPairs.Values }}{{ end }}{{ end }}`,