fix(alerting): Fix Discord alert payload missing required field

This commit is contained in:
TwiN 2022-10-20 20:18:25 -04:00
parent d01a5d418b
commit fe4d9821f3
2 changed files with 14 additions and 15 deletions

View File

@ -83,7 +83,7 @@ type Field struct {
// buildRequestBody builds the request body for the provider // buildRequestBody builds the request body for the provider
func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) []byte { func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) []byte {
var message string var message, results string
var colorCode int var colorCode int
if resolved { 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) message = fmt.Sprintf("An alert for **%s** has been resolved after passing successfully %d time(s) in a row", endpoint.DisplayName(), alert.SuccessThreshold)
@ -92,34 +92,33 @@ 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) message = fmt.Sprintf("An alert for **%s** has been triggered due to having failed %d time(s) in a row", endpoint.DisplayName(), alert.FailureThreshold)
colorCode = 15158332 colorCode = 15158332
} }
fields := make([]Field, len(result.ConditionResults)) for _, conditionResult := range result.ConditionResults {
for i, conditionResult := range result.ConditionResults {
var prefix string var prefix string
if conditionResult.Success { if conditionResult.Success {
prefix = ":white_check_mark:" prefix = ":white_check_mark:"
} else { } else {
prefix = ":x:" prefix = ":x:"
} }
fields[i] = Field{ results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition)
Value: fmt.Sprintf("%s - `%s`", prefix, conditionResult.Condition),
Inline: false,
}
if i == 0 {
fields[i].Name = "Condition results"
}
} }
var description string var description string
if alertDescription := alert.GetDescription(); len(alertDescription) > 0 { if alertDescription := alert.GetDescription(); len(alertDescription) > 0 {
description = ":\n> " + alertDescription description = ":\n> " + alertDescription
} }
body, _ := json.Marshal(Body{ body, _ := json.Marshal(Body{
Content: message, Content: "",
Embeds: []Embed{ Embeds: []Embed{
{ {
Title: ":helmet_with_white_cross: Gatus", Title: ":helmet_with_white_cross: Gatus",
Description: message + description, Description: message + description,
Color: colorCode, Color: colorCode,
Fields: fields, Fields: []Field{
{
Name: "Condition results",
Value: results,
Inline: false,
},
},
}, },
}, },
}) })

View File

@ -151,14 +151,14 @@ func TestAlertProvider_buildRequestBody(t *testing.T) {
Provider: AlertProvider{}, Provider: AlertProvider{},
Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3},
Resolved: false, Resolved: false,
ExpectedBody: "{\"content\":\"An alert for **endpoint-name** has been triggered due to having failed 3 time(s) in a row\",\"embeds\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"description\":\"An alert for **endpoint-name** has been triggered due to having failed 3 time(s) in a row:\\n\\u003e description-1\",\"color\":15158332,\"fields\":[{\"name\":\"Condition results\",\"value\":\":x: - `[CONNECTED] == true`\",\"inline\":false},{\"name\":\"\",\"value\":\":x: - `[STATUS] == 200`\",\"inline\":false},{\"name\":\"\",\"value\":\":x: - `[BODY] != \\\"\\\"`\",\"inline\":false}]}]}", ExpectedBody: "{\"content\":\"\",\"embeds\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"description\":\"An alert for **endpoint-name** has been triggered due to having failed 3 time(s) in a row:\\n\\u003e description-1\",\"color\":15158332,\"fields\":[{\"name\":\"Condition results\",\"value\":\":x: - `[CONNECTED] == true`\\n:x: - `[STATUS] == 200`\\n:x: - `[BODY] != \\\"\\\"`\\n\",\"inline\":false}]}]}",
}, },
{ {
Name: "resolved", Name: "resolved",
Provider: AlertProvider{}, Provider: AlertProvider{},
Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3},
Resolved: true, Resolved: true,
ExpectedBody: "{\"content\":\"An alert for **endpoint-name** has been resolved after passing successfully 5 time(s) in a row\",\"embeds\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"description\":\"An alert for **endpoint-name** has been resolved after passing successfully 5 time(s) in a row:\\n\\u003e description-2\",\"color\":3066993,\"fields\":[{\"name\":\"Condition results\",\"value\":\":white_check_mark: - `[CONNECTED] == true`\",\"inline\":false},{\"name\":\"\",\"value\":\":white_check_mark: - `[STATUS] == 200`\",\"inline\":false},{\"name\":\"\",\"value\":\":white_check_mark: - `[BODY] != \\\"\\\"`\",\"inline\":false}]}]}", ExpectedBody: "{\"content\":\"\",\"embeds\":[{\"title\":\":helmet_with_white_cross: Gatus\",\"description\":\"An alert for **endpoint-name** has been resolved after passing successfully 5 time(s) in a row:\\n\\u003e description-2\",\"color\":3066993,\"fields\":[{\"name\":\"Condition results\",\"value\":\":white_check_mark: - `[CONNECTED] == true`\\n:white_check_mark: - `[STATUS] == 200`\\n:white_check_mark: - `[BODY] != \\\"\\\"`\\n\",\"inline\":false}]}]}",
}, },
} }
for _, scenario := range scenarios { for _, scenario := range scenarios {
@ -176,7 +176,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) {
scenario.Resolved, scenario.Resolved,
) )
if string(body) != scenario.ExpectedBody { 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{}) out := make(map[string]interface{})
if err := json.Unmarshal(body, &out); err != nil { if err := json.Unmarshal(body, &out); err != nil {