From ea3ae52f1eb2a9e165ac680b7446f956ac3d9c0d Mon Sep 17 00:00:00 2001 From: Bugra Kocabay Date: Wed, 1 Nov 2023 03:01:54 +0300 Subject: [PATCH] Feat/modify discord title (#602) * feat(alerting): allow discord alert's title to be configurable * feat(alerting): modify documentation for discord title feature * feat(test): add tests for discord title modify feature --------- Co-authored-by: Bugra Kocabay Co-authored-by: TwiN --- README.md | 1 + alerting/provider/discord/discord.go | 9 ++++++++- alerting/provider/discord/discord_test.go | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 56774a62..231183d0 100644 --- a/README.md +++ b/README.md @@ -441,6 +441,7 @@ ignored. |:-------------------------------------------|:-------------------------------------------------------------------------------------------|:--------------| | `alerting.discord` | Configuration for alerts of type `discord` | `{}` | | `alerting.discord.webhook-url` | Discord Webhook URL | Required `""` | +| `alerting.discord.title` | Title of the notification | `":helmet_with_white_cross: Gatus"` | | `alerting.discord.default-alert` | Default alert configuration.
See [Setting a default alert](#setting-a-default-alert) | N/A | | `alerting.discord.overrides` | List of overrides that may be prioritized over the default configuration | `[]` | | `alerting.discord.overrides[].group` | Endpoint group for which the configuration will be overridden by this configuration | `""` | diff --git a/alerting/provider/discord/discord.go b/alerting/provider/discord/discord.go index e96be69e..21be934a 100644 --- a/alerting/provider/discord/discord.go +++ b/alerting/provider/discord/discord.go @@ -21,6 +21,9 @@ type AlertProvider struct { // Overrides is a list of Override that may be prioritized over the default configuration Overrides []Override `yaml:"overrides,omitempty"` + + // Title is the title of the message that will be sent + Title string `yaml:"title,omitempty"` } // Override is a case under which the default integration is overridden @@ -105,11 +108,15 @@ func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert * if alertDescription := alert.GetDescription(); len(alertDescription) > 0 { description = ":\n> " + alertDescription } + title := ":helmet_with_white_cross: Gatus" + if provider.Title != "" { + title = provider.Title + } body, _ := json.Marshal(Body{ Content: "", Embeds: []Embed{ { - Title: ":helmet_with_white_cross: Gatus", + Title: title, Description: message + description, Color: colorCode, Fields: []Field{ diff --git a/alerting/provider/discord/discord_test.go b/alerting/provider/discord/discord_test.go index 01cd8cd0..3d4374ab 100644 --- a/alerting/provider/discord/discord_test.go +++ b/alerting/provider/discord/discord_test.go @@ -63,6 +63,7 @@ func TestAlertProvider_Send(t *testing.T) { defer client.InjectHTTPClient(nil) firstDescription := "description-1" secondDescription := "description-2" + title := "provider-title" scenarios := []struct { Name string Provider AlertProvider @@ -111,6 +112,16 @@ func TestAlertProvider_Send(t *testing.T) { }), ExpectedError: true, }, + { + Name: "triggered-with-modified-title", + Provider: AlertProvider{Title: title}, + Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, + Resolved: false, + MockRoundTripper: test.MockRoundTripper(func(r *http.Request) *http.Response { + return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody} + }), + ExpectedError: false, + }, } for _, scenario := range scenarios { t.Run(scenario.Name, func(t *testing.T) { @@ -139,6 +150,7 @@ func TestAlertProvider_Send(t *testing.T) { func TestAlertProvider_buildRequestBody(t *testing.T) { firstDescription := "description-1" secondDescription := "description-2" + title := "provider-title" scenarios := []struct { Name string Provider AlertProvider @@ -160,6 +172,13 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Resolved: true, 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}]}]}", }, + { + Name: "triggered-with-modified-title", + Provider: AlertProvider{Title: title}, + Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, + Resolved: false, + ExpectedBody: "{\"content\":\"\",\"embeds\":[{\"title\":\"provider-title\",\"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}]}]}", + }, } for _, scenario := range scenarios { t.Run(scenario.Name, func(t *testing.T) {