Skip to content
This repository was archived by the owner on Apr 7, 2024. It is now read-only.

Commit 3fb1e6b

Browse files
authored
Merge pull request #27 from WinnerSoftLab/ITT-1480/feature/fix_links
Update url encoding functions to fix links
2 parents a164029 + 41d79ba commit 3fb1e6b

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

notify/slackV2/blocks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (n *Notifier) formatMessage(data *template.Data) slack.Blocks {
7979
filters = append(filters, fmt.Sprintf("%s=\"%s\"", v.Name, v.Value))
8080
}
8181
args.Add("filter", fmt.Sprintf("{%s}", strings.Join(filters, ",")))
82-
urlParsed.RawQuery = args.Encode()
82+
urlParsed.RawQuery = EncodeUrlArgs(args)
8383
url = urlParsed.String()
8484
url = strings.Replace(url, "%23", "#", 1)
8585
}

notify/slackV2/grafana.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func genGrafanaRenderUrl(grafanaUrl string, grafanaTZ string, org string, dash s
4242
q.Set("width", imageWidth)
4343
q.Set("height", imageHeight)
4444
q.Set("tz", grafanaTZ)
45-
u.RawQuery = q.Encode()
45+
u.RawQuery = EncodeUrlArgs(q)
4646
return u.String(), nil
4747

4848
}
@@ -64,7 +64,7 @@ func genGrafanaUrl(grafanaUrl string, org string, dash string, panel string) (st
6464
if panel != "" {
6565
q.Set("viewPanel", panel)
6666
}
67-
u.RawQuery = q.Encode()
67+
u.RawQuery = EncodeUrlArgs(q)
6868
return u.String(), nil
6969
}
7070

@@ -83,7 +83,7 @@ func urlMerger(publicUrl string, privateUrl string) (string, error) {
8383
}
8484
q := u.Query()
8585
q.Set("pub_secret", key)
86-
u.RawQuery = q.Encode()
86+
u.RawQuery = EncodeUrlArgs(q)
8787

8888
return u.String(), nil
8989
}
@@ -192,17 +192,23 @@ func (n *Notifier) formatGrafanaMessage(data *template.Data) slack.Blocks {
192192
for _, v := range data.CommonLabels.SortedPairs() {
193193
filters = append(filters, fmt.Sprintf("%s=\"%s\"", v.Name, v.Value))
194194
}
195+
195196
args.Add("filter", fmt.Sprintf("{%s}", strings.Join(filters, ",")))
196-
urlParsed.RawQuery = args.Encode()
197+
urlParsed.RawQuery = EncodeUrlArgs(args)
197198
url = urlParsed.String()
198199
url = strings.Replace(url, "%23", "#", 1)
199200
}
200201

201202
alertEditUrl := ""
202203
for _, alert := range data.Alerts {
203204
if alert.GeneratorURL != "" {
204-
alertEditUrl = alert.GeneratorURL + "?orgId=" + orgId
205-
break
205+
if urlParsed, err := url2.Parse(alert.GeneratorURL); err == nil {
206+
args := urlParsed.Query()
207+
args.Add("orgId", orgId)
208+
urlParsed.RawQuery = EncodeUrlArgs(args)
209+
alertEditUrl = urlParsed.String()
210+
break
211+
}
206212
}
207213
}
208214

notify/slackV2/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package slackV2
22

33
import (
44
"github.com/prometheus/alertmanager/template"
5+
url2 "net/url"
6+
"strings"
57
"unicode/utf8"
68
)
79

@@ -105,3 +107,9 @@ func cut(text string, limit int) string {
105107
}
106108
return text
107109
}
110+
111+
func EncodeUrlArgs(values url2.Values) string {
112+
result := values.Encode()
113+
result = strings.Replace(result, "+", "%20", -1)
114+
return result
115+
}

0 commit comments

Comments
 (0)