Skip to content

Commit

Permalink
Add summary to msteams notification (#3616)
Browse files Browse the repository at this point in the history
* Add summary to msteams notification

Signed-off-by: Roman Kubu <Roman.Kubu@airbank.cz>
---------

Signed-off-by: Roman Kubu <Roman.Kubu@airbank.cz>
Signed-off-by: romankubu <112698283+romankubu@users.noreply.github.com>
Co-authored-by: Jack <jack4zhang@gmail.com>
  • Loading branch information
romankubu and zhan9san authored Nov 24, 2023
1 parent aaf9490 commit 4f0bdf4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions asset/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ var (
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
Title: `{{ template "msteams.default.title" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
}
)

Expand Down Expand Up @@ -788,8 +789,9 @@ type MSTeamsConfig struct {
HTTPConfig *commoncfg.HTTPClientConfig `yaml:"http_config,omitempty" json:"http_config,omitempty"`
WebhookURL *SecretURL `yaml:"webhook_url,omitempty" json:"webhook_url,omitempty"`

Title string `yaml:"title,omitempty" json:"title,omitempty"`
Text string `yaml:"text,omitempty" json:"text,omitempty"`
Title string `yaml:"title,omitempty" json:"title,omitempty"`
Summary string `yaml:"summary,omitempty" json:"summary,omitempty"`
Text string `yaml:"text,omitempty" json:"text,omitempty"`
}

func (c *MSTeamsConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ Microsoft Teams notifications are sent via the [Incoming Webhooks](https://learn
# Message title template.
[ title: <tmpl_string> | default = '{{ template "msteams.default.title" . }}' ]
# Message summary template.
[ summary: <tmpl_string> | default = '{{ template "msteams.default.summary" . }}' ]
# Message body template.
[ text: <tmpl_string> | default = '{{ template "msteams.default.text" . }}' ]
Expand Down
6 changes: 6 additions & 0 deletions notify/msteams/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type teamsMessage struct {
Context string `json:"@context"`
Type string `json:"type"`
Title string `json:"title"`
Summary string `json:"summary"`
Text string `json:"text"`
ThemeColor string `json:"themeColor"`
}
Expand Down Expand Up @@ -98,6 +99,10 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
if err != nil {
return false, err
}
summary := tmpl(n.conf.Summary)
if err != nil {
return false, err
}

alerts := types.Alerts(as...)
color := colorGrey
Expand All @@ -112,6 +117,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
Context: "http://schema.org/extensions",
Type: "MessageCard",
Title: title,
Summary: summary,
Text: text,
ThemeColor: color,
}
Expand Down
18 changes: 14 additions & 4 deletions notify/msteams/msteams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ func TestMSTeamsTemplating(t *testing.T) {
{
title: "full-blown message",
cfg: &config.MSTeamsConfig{
Title: `{{ template "msteams.default.title" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
},
retry: false,
},
Expand All @@ -89,11 +90,20 @@ func TestMSTeamsTemplating(t *testing.T) {
},
errMsg: "template: :1: unclosed action",
},
{
title: "summary with templating errors",
cfg: &config.MSTeamsConfig{
Title: `{{ template "msteams.default.title" . }}`,
Summary: "{{ ",
},
errMsg: "template: :1: unclosed action",
},
{
title: "message with templating errors",
cfg: &config.MSTeamsConfig{
Title: `{{ template "msteams.default.title" . }}`,
Text: "{{ ",
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: "{{ ",
},
errMsg: "template: :1: unclosed action",
},
Expand Down
1 change: 1 addition & 0 deletions template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Alerts Resolved:
{{ end }}
{{ end }}

{{ define "msteams.default.summary" }}{{ template "__subject" . }}{{ end }}
{{ define "msteams.default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "msteams.default.text" }}
{{ if gt (len .Alerts.Firing) 0 }}
Expand Down

0 comments on commit 4f0bdf4

Please sign in to comment.