Alerting: Fix notifications for Microsoft Teams (#53810)

This commit fixes notifications for Microsoft Teams where notifications
would just contain "Card" rather than the title.
This commit is contained in:
George Robinson
2022-08-17 16:46:46 +01:00
committed by GitHub
parent 57d87389e0
commit 6d69d7acb8
3 changed files with 10 additions and 3 deletions

View File

@ -43,6 +43,7 @@ const (
// AdaptiveCardsMessage represents a message for adaptive cards.
type AdaptiveCardsMessage struct {
Attachments []AdaptiveCardsAttachment `json:"attachments"`
Summary string `json:"summary,omitempty"` // Summary is the text shown in notifications
Type string `json:"type"`
}
@ -317,6 +318,9 @@ func (tn *TeamsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
},
})
msg := NewAdaptiveCardsMessage(card)
msg.Summary = tmpl(tn.Title)
// This check for tmplErr must happen before templating the URL
if tmplErr != nil {
tn.log.Warn("failed to template Teams message", "err", tmplErr.Error())
@ -329,7 +333,7 @@ func (tn *TeamsNotifier) Notify(ctx context.Context, as ...*types.Alert) (bool,
u = tn.URL
}
b, err := json.Marshal(NewAdaptiveCardsMessage(card))
b, err := json.Marshal(msg)
if err != nil {
return false, fmt.Errorf("failed to marshal JSON: %w", err)
}

View File

@ -73,7 +73,8 @@ func TestTeamsNotifier(t *testing.T) {
},
"contentType": "application/vnd.microsoft.card.adaptive",
}},
"type": "message",
"summary": "[FIRING:1] (val1)",
"type": "message",
},
expMsgError: nil,
}, {
@ -125,7 +126,8 @@ func TestTeamsNotifier(t *testing.T) {
},
"contentType": "application/vnd.microsoft.card.adaptive",
}},
"type": "message",
"summary": "alert1",
"type": "message",
},
expMsgError: nil,
}, {

View File

@ -2229,6 +2229,7 @@ var expNonEmailNotifications = map[string][]string{
"contentType": "application/vnd.microsoft.card.adaptive"
}
],
"summary": "[FIRING:1] TeamsAlert (default)",
"type": "message"
}`,
},