From f80df9a187d9d2b0b0345c888e0bb465c8f46daa Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 22 Aug 2022 17:18:08 +0200 Subject: [PATCH] [v9.1.x] Alerting: Fix width of Adaptive Cards in Teams notifications (#54045) Co-authored-by: George Robinson --- .../ngalert/notifier/channels/teams.go | 24 +++++++++++++++---- .../ngalert/notifier/channels/teams_test.go | 12 ++++++++++ .../alerting/api_notification_channel_test.go | 5 +++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/services/ngalert/notifier/channels/teams.go b/pkg/services/ngalert/notifier/channels/teams.go index 7a40fb60857..51535bcbcb4 100644 --- a/pkg/services/ngalert/notifier/channels/teams.go +++ b/pkg/services/ngalert/notifier/channels/teams.go @@ -69,10 +69,10 @@ type AdaptiveCardsAttachment struct { // AdapativeCard repesents an Adaptive Card. // https://adaptivecards.io/explorer/AdaptiveCard.html type AdaptiveCard struct { - Body []AdaptiveCardItem `json:"body"` - Schema string `json:"$schema"` - Type string `json:"type"` - Version string `json:"version"` + Body []AdaptiveCardItem + Schema string + Type string + Version string } // NewAdaptiveCard returns a prepared Adaptive Card. @@ -85,6 +85,22 @@ func NewAdaptiveCard() AdaptiveCard { } } +func (c *AdaptiveCard) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Body []AdaptiveCardItem `json:"body"` + Schema string `json:"$schema"` + Type string `json:"type"` + Version string `json:"version"` + MsTeams map[string]interface{} `json:"msTeams,omitempty"` + }{ + Body: c.Body, + Schema: c.Schema, + Type: c.Type, + Version: c.Version, + MsTeams: map[string]interface{}{"width": "Full"}, + }) +} + // AppendItem appends an item, such as text or an image, to the Adaptive Card. func (c *AdaptiveCard) AppendItem(i AdaptiveCardItem) { c.Body = append(c.Body, i) diff --git a/pkg/services/ngalert/notifier/channels/teams_test.go b/pkg/services/ngalert/notifier/channels/teams_test.go index 0eb3b75d0f0..7673e162bbe 100644 --- a/pkg/services/ngalert/notifier/channels/teams_test.go +++ b/pkg/services/ngalert/notifier/channels/teams_test.go @@ -70,6 +70,9 @@ func TestTeamsNotifier(t *testing.T) { }}, "type": "AdaptiveCard", "version": "1.4", + "msTeams": map[string]interface{}{ + "width": "Full", + }, }, "contentType": "application/vnd.microsoft.card.adaptive", }}, @@ -123,6 +126,9 @@ func TestTeamsNotifier(t *testing.T) { }}, "type": "AdaptiveCard", "version": "1.4", + "msTeams": map[string]interface{}{ + "width": "Full", + }, }, "contentType": "application/vnd.microsoft.card.adaptive", }}, @@ -176,6 +182,9 @@ func TestTeamsNotifier(t *testing.T) { }}, "type": "AdaptiveCard", "version": "1.4", + "msTeams": map[string]interface{}{ + "width": "Full", + }, }, "contentType": "application/vnd.microsoft.card.adaptive", }}, @@ -228,6 +237,9 @@ func TestTeamsNotifier(t *testing.T) { }}, "type": "AdaptiveCard", "version": "1.4", + "msTeams": map[string]interface{}{ + "width": "Full", + }, }, "contentType": "application/vnd.microsoft.card.adaptive", }}, diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index f65469d6dfc..c6103637b9a 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -2224,7 +2224,10 @@ var expNonEmailNotifications = map[string][]string{ } ], "type": "AdaptiveCard", - "version": "1.4" + "version": "1.4", + "msTeams": { + "width": "Full" + } }, "contentType": "application/vnd.microsoft.card.adaptive" }