From ade3d059838db498284375d7e90a411187d6e6fc Mon Sep 17 00:00:00 2001 From: TwiN Date: Thu, 20 Oct 2022 16:50:21 -0400 Subject: [PATCH] fix(alerting): Use required Field.Title in Slack provider even if it's not enforced Just to prevent future issues --- alerting/provider/slack/slack.go | 29 +++++++++++++-------------- alerting/provider/slack/slack_test.go | 10 ++++----- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/alerting/provider/slack/slack.go b/alerting/provider/slack/slack.go index f3487bc2..25b31e6a 100644 --- a/alerting/provider/slack/slack.go +++ b/alerting/provider/slack/slack.go @@ -82,7 +82,7 @@ type Field struct { // buildRequestBody builds the request body for the provider func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) []byte { - var message, color string + var message, color, results string if resolved { message = fmt.Sprintf("An alert for *%s* has been resolved after passing successfully %d time(s) in a row", endpoint.DisplayName(), alert.SuccessThreshold) color = "#36A64F" @@ -90,21 +90,14 @@ func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert * message = fmt.Sprintf("An alert for *%s* has been triggered due to having failed %d time(s) in a row", endpoint.DisplayName(), alert.FailureThreshold) color = "#DD0000" } - fields := make([]Field, len(result.ConditionResults)) - for i, conditionResult := range result.ConditionResults { + for _, conditionResult := range result.ConditionResults { var prefix string if conditionResult.Success { prefix = ":white_check_mark:" } else { prefix = ":x:" } - fields[i] = Field{ - Value: fmt.Sprintf("%s - `%s`", prefix, conditionResult.Condition), - Short: false, - } - if i == 0 { - fields[i].Title = "Condition results" - } + results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition) } var description string if alertDescription := alert.GetDescription(); len(alertDescription) > 0 { @@ -114,11 +107,17 @@ func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert * Text: "", Attachments: []Attachment{ { - Title: ":helmet_with_white_cross: Gatus", - Text: message + description, - Short: false, - Color: color, - Fields: fields, + Title: ":helmet_with_white_cross: Gatus", + Text: message + description, + Short: false, + Color: color, + Fields: []Field{ + { + Title: "Condition results", + Value: results, + Short: false, + }, + }, }, }, }) diff --git a/alerting/provider/slack/slack_test.go b/alerting/provider/slack/slack_test.go index 64f96f30..aa1dc763 100644 --- a/alerting/provider/slack/slack_test.go +++ b/alerting/provider/slack/slack_test.go @@ -153,7 +153,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Endpoint: core.Endpoint{Name: "name"}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, - ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *name* has been triggered due to having failed 3 time(s) in a row:\\n\\u003e description-1\",\"short\":false,\"color\":\"#DD0000\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":x: - `[CONNECTED] == true`\",\"short\":false},{\"title\":\"\",\"value\":\":x: - `[STATUS] == 200`\",\"short\":false}]}]}", + ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *name* has been triggered due to having failed 3 time(s) in a row:\\n\\u003e description-1\",\"short\":false,\"color\":\"#DD0000\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":x: - `[CONNECTED] == true`\\n:x: - `[STATUS] == 200`\\n\",\"short\":false}]}]}", }, { Name: "triggered-with-group", @@ -161,7 +161,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Endpoint: core.Endpoint{Name: "name", Group: "group"}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, - ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *group/name* has been triggered due to having failed 3 time(s) in a row:\\n\\u003e description-1\",\"short\":false,\"color\":\"#DD0000\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":x: - `[CONNECTED] == true`\",\"short\":false},{\"title\":\"\",\"value\":\":x: - `[STATUS] == 200`\",\"short\":false}]}]}", + ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *group/name* has been triggered due to having failed 3 time(s) in a row:\\n\\u003e description-1\",\"short\":false,\"color\":\"#DD0000\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":x: - `[CONNECTED] == true`\\n:x: - `[STATUS] == 200`\\n\",\"short\":false}]}]}", }, { Name: "resolved", @@ -169,7 +169,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Endpoint: core.Endpoint{Name: "name"}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: true, - ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *name* has been resolved after passing successfully 5 time(s) in a row:\\n\\u003e description-2\",\"short\":false,\"color\":\"#36A64F\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":white_check_mark: - `[CONNECTED] == true`\",\"short\":false},{\"title\":\"\",\"value\":\":white_check_mark: - `[STATUS] == 200`\",\"short\":false}]}]}", + ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *name* has been resolved after passing successfully 5 time(s) in a row:\\n\\u003e description-2\",\"short\":false,\"color\":\"#36A64F\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":white_check_mark: - `[CONNECTED] == true`\\n:white_check_mark: - `[STATUS] == 200`\\n\",\"short\":false}]}]}", }, { Name: "resolved-with-group", @@ -177,7 +177,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Endpoint: core.Endpoint{Name: "name", Group: "group"}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: true, - ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *group/name* has been resolved after passing successfully 5 time(s) in a row:\\n\\u003e description-2\",\"short\":false,\"color\":\"#36A64F\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":white_check_mark: - `[CONNECTED] == true`\",\"short\":false},{\"title\":\"\",\"value\":\":white_check_mark: - `[STATUS] == 200`\",\"short\":false}]}]}", + ExpectedBody: "{\"text\":\"\",\"attachments\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"text\":\"An alert for *group/name* has been resolved after passing successfully 5 time(s) in a row:\\n\\u003e description-2\",\"short\":false,\"color\":\"#36A64F\",\"fields\":[{\"title\":\"Condition results\",\"value\":\":white_check_mark: - `[CONNECTED] == true`\\n:white_check_mark: - `[STATUS] == 200`\\n\",\"short\":false}]}]}", }, } for _, scenario := range scenarios { @@ -194,7 +194,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { scenario.Resolved, ) if string(body) != scenario.ExpectedBody { - t.Errorf("expected %s, got %s", scenario.ExpectedBody, body) + t.Errorf("expected:\n%s\ngot:\n%s", scenario.ExpectedBody, body) } out := make(map[string]interface{}) if err := json.Unmarshal(body, &out); err != nil {