diff --git a/alerting/alert/alert.go b/alerting/alert/alert.go index e1a220e4..f991debd 100644 --- a/alerting/alert/alert.go +++ b/alerting/alert/alert.go @@ -79,6 +79,7 @@ func (alert Alert) GetDescription() string { // IsEnabled returns whether an alert is enabled or not func (alert Alert) IsEnabled() bool { if alert.Enabled == nil { + // TODO: Default to true in v5.0.0 (unless default-alert.enabled is set to false) return false } return *alert.Enabled diff --git a/alerting/provider/googlechat/googlechat.go b/alerting/provider/googlechat/googlechat.go index 5d9a1764..06b5f7ee 100644 --- a/alerting/provider/googlechat/googlechat.go +++ b/alerting/provider/googlechat/googlechat.go @@ -37,7 +37,6 @@ func (provider *AlertProvider) IsValid() bool { if provider.ClientConfig == nil { provider.ClientConfig = client.GetDefaultConfig() } - registeredGroups := make(map[string]bool) if provider.Overrides != nil { for _, override := range provider.Overrides { @@ -47,7 +46,6 @@ func (provider *AlertProvider) IsValid() bool { registeredGroups[override.Group] = true } } - return len(provider.WebhookURL) > 0 } @@ -137,7 +135,7 @@ func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert * if alertDescription := alert.GetDescription(); len(alertDescription) > 0 { description = ":: " + alertDescription } - body, _ := json.Marshal(Body{ + payload := Body{ Cards: []Cards{ { Sections: []Sections{ @@ -160,22 +158,28 @@ func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert * Icon: "DESCRIPTION", }, }, - { - Buttons: []Buttons{ - { - TextButton: TextButton{ - Text: "URL", - OnClick: OnClick{OpenLink: OpenLink{URL: endpoint.URL}}, - }, - }, - }, - }, }, }, }, }, }, - }) + } + if endpoint.Type() == core.EndpointTypeHTTP { + // We only include a button targeting the URL if the endpoint is an HTTP endpoint + // If the URL isn't prefixed with https://, Google Chat will just display a blank message aynways. + // See https://github.com/TwiN/gatus/issues/362 + payload.Cards[0].Sections[0].Widgets = append(payload.Cards[0].Sections[0].Widgets, Widgets{ + Buttons: []Buttons{ + { + TextButton: TextButton{ + Text: "URL", + OnClick: OnClick{OpenLink: OpenLink{URL: endpoint.URL}}, + }, + }, + }, + }) + } + body, _ := json.Marshal(payload) return body } diff --git a/alerting/provider/googlechat/googlechat_test.go b/alerting/provider/googlechat/googlechat_test.go index 702c22a5..4ccf7b2d 100644 --- a/alerting/provider/googlechat/googlechat_test.go +++ b/alerting/provider/googlechat/googlechat_test.go @@ -141,6 +141,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { secondDescription := "description-2" scenarios := []struct { Name string + Endpoint core.Endpoint Provider AlertProvider Alert alert.Alert Resolved bool @@ -148,6 +149,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { }{ { Name: "triggered", + Endpoint: core.Endpoint{Name: "endpoint-name", URL: "https://example.org"}, Provider: AlertProvider{}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, @@ -155,16 +157,33 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { }, { Name: "resolved", + Endpoint: core.Endpoint{Name: "endpoint-name", URL: "https://example.org"}, 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","icon":"DESCRIPTION"}},{"buttons":[{"textButton":{"text":"URL","onClick":{"openLink":{"url":"https://example.org"}}}}]}]}]}]}`, }, + { + Name: "icmp-should-not-include-url", // See https://github.com/TwiN/gatus/issues/362 + Endpoint: core.Endpoint{Name: "endpoint-name", URL: "icmp://example.org"}, + 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","icon":"DESCRIPTION"}}]}]}]}`, + }, + { + Name: "tcp-should-not-include-url", // See https://github.com/TwiN/gatus/issues/362 + Endpoint: core.Endpoint{Name: "endpoint-name", URL: "tcp://example.org"}, + 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","icon":"DESCRIPTION"}}]}]}]}`, + }, } for _, scenario := range scenarios { t.Run(scenario.Name, func(t *testing.T) { body := scenario.Provider.buildRequestBody( - &core.Endpoint{Name: "endpoint-name", URL: "https://example.org"}, + &scenario.Endpoint, &scenario.Alert, &core.Result{ ConditionResults: []*core.ConditionResult{