From f1711b5c0b93877e1687a1768af68fd0a0f34ed6 Mon Sep 17 00:00:00 2001 From: TwiN Date: Wed, 9 Nov 2022 18:31:01 -0500 Subject: [PATCH] fix(alerting): Omit empty KeyValue parameters when marshalling to JSON --- alerting/provider/googlechat/googlechat.go | 12 ++++++------ alerting/provider/googlechat/googlechat_test.go | 6 ++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/alerting/provider/googlechat/googlechat.go b/alerting/provider/googlechat/googlechat.go index faa6173d..42b95fab 100644 --- a/alerting/provider/googlechat/googlechat.go +++ b/alerting/provider/googlechat/googlechat.go @@ -89,11 +89,11 @@ type Widgets struct { } type KeyValue struct { - TopLabel string `json:"topLabel"` - Content string `json:"content"` - ContentMultiline string `json:"contentMultiline"` - BottomLabel string `json:"bottomLabel"` - Icon string `json:"icon"` + TopLabel string `json:"topLabel,omitempty"` + Content string `json:"content,omitempty"` + ContentMultiline string `json:"contentMultiline,omitempty"` + BottomLabel string `json:"bottomLabel,omitempty"` + Icon string `json:"icon,omitempty"` } type Buttons struct { @@ -164,7 +164,7 @@ func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert * Buttons: []Buttons{ { TextButton: TextButton{ - Text: "Open", + Text: "URL", OnClick: OnClick{OpenLink: OpenLink{URL: endpoint.URL}}, }, }, diff --git a/alerting/provider/googlechat/googlechat_test.go b/alerting/provider/googlechat/googlechat_test.go index ed7de1a2..62fa515a 100644 --- a/alerting/provider/googlechat/googlechat_test.go +++ b/alerting/provider/googlechat/googlechat_test.go @@ -151,14 +151,14 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Provider: AlertProvider{}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, - ExpectedBody: `{"cards":[{"sections":[{"widgets":[{"keyValue":{"topLabel":"endpoint-name","content":"\u003cfont color='#DD0000'\u003eAn alert has been triggered due to having failed 3 time(s) in a row\u003c/font\u003e","contentMultiline":"true","bottomLabel":":: description-1","icon":"BOOKMARK"}},{"keyValue":{"topLabel":"Condition results","content":"❌ [CONNECTED] == true\u003cbr\u003e❌ [STATUS] == 200\u003cbr\u003e","contentMultiline":"true","bottomLabel":"","icon":"DESCRIPTION"}},{"keyValue":{"topLabel":"","content":"","contentMultiline":"","bottomLabel":"","icon":""},"buttons":[{"textButton":{"text":"Open","onClick":{"openLink":{"url":"https://example.org"}}}}]}]}]}]}`, + ExpectedBody: `{"cards":[{"sections":[{"widgets":[{"keyValue":{"topLabel":"endpoint-name","content":"\u003cfont color='#DD0000'\u003eAn alert has been triggered due to having failed 3 time(s) in a row\u003c/font\u003e","contentMultiline":"true","bottomLabel":":: description-1","icon":"BOOKMARK"}},{"keyValue":{"topLabel":"Condition results","content":"❌ [CONNECTED] == true\u003cbr\u003e❌ [STATUS] == 200\u003cbr\u003e","contentMultiline":"true","icon":"DESCRIPTION"}},{"keyValue":{},"buttons":[{"textButton":{"text":"URL","onClick":{"openLink":{"url":"https://example.org"}}}}]}]}]}]}`, }, { Name: "resolved", Provider: AlertProvider{}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: true, - ExpectedBody: `{"cards":[{"sections":[{"widgets":[{"keyValue":{"topLabel":"endpoint-name","content":"\u003cfont color='#36A64F'\u003eAn alert has been resolved after passing successfully 5 time(s) in a row\u003c/font\u003e","contentMultiline":"true","bottomLabel":":: description-2","icon":"BOOKMARK"}},{"keyValue":{"topLabel":"Condition results","content":"✅ [CONNECTED] == true\u003cbr\u003e✅ [STATUS] == 200\u003cbr\u003e","contentMultiline":"true","bottomLabel":"","icon":"DESCRIPTION"}},{"keyValue":{"topLabel":"","content":"","contentMultiline":"","bottomLabel":"","icon":""},"buttons":[{"textButton":{"text":"Open","onClick":{"openLink":{"url":"https://example.org"}}}}]}]}]}]}`, + ExpectedBody: `{"cards":[{"sections":[{"widgets":[{"keyValue":{"topLabel":"endpoint-name","content":"\u003cfont color='#36A64F'\u003eAn alert has been resolved after passing successfully 5 time(s) in a row\u003c/font\u003e","contentMultiline":"true","bottomLabel":":: description-2","icon":"BOOKMARK"}},{"keyValue":{"topLabel":"Condition results","content":"✅ [CONNECTED] == true\u003cbr\u003e✅ [STATUS] == 200\u003cbr\u003e","contentMultiline":"true","icon":"DESCRIPTION"}},{"keyValue":{},"buttons":[{"textButton":{"text":"URL","onClick":{"openLink":{"url":"https://example.org"}}}}]}]}]}]}`, }, } for _, scenario := range scenarios { @@ -174,8 +174,6 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { }, scenario.Resolved, ) - //b, _ := json.Marshal(body) - //e, _ := json.Marshal(scenario.ExpectedBody) if string(body) != scenario.ExpectedBody { t.Errorf("expected:\n%s\ngot:\n%s", scenario.ExpectedBody, body) }