Skip to content

Commit

Permalink
migrate connector to power platform for msteams
Browse files Browse the repository at this point in the history
Signed-off-by: Jack <[email protected]>
  • Loading branch information
zhan9san committed Jul 11, 2024
1 parent eb8369e commit 0ecc664
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 46 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: 4 additions & 6 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ var (
NotifierConfig: NotifierConfig{
VSendResolved: true,
},
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
Title: `{{ template "msteams.default.title" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
}
)

Expand Down Expand Up @@ -803,9 +802,8 @@ type MSTeamsConfig struct {
WebhookURL *SecretURL `yaml:"webhook_url,omitempty" json:"webhook_url,omitempty"`
WebhookURLFile string `yaml:"webhook_url_file,omitempty" json:"webhook_url_file,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"`
Title string `yaml:"title,omitempty" json:"title,omitempty"`
Text string `yaml:"text,omitempty" json:"text,omitempty"`
}

func (c *MSTeamsConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
Expand Down
3 changes: 0 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -934,9 +934,6 @@ 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
76 changes: 56 additions & 20 deletions notify/msteams/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ import (
)

const (
colorRed = "8C1A1A"
colorGreen = "2DC72D"
colorGrey = "808080"
colorRed = "Attention"
colorGreen = "Good"
colorGrey = "Warning"
)

type Notifier struct {
Expand All @@ -50,14 +50,33 @@ type Notifier struct {
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
}

// Message card reference can be found at https://learn.microsoft.com/en-us/outlook/actionable-messages/message-card-reference.
// https://learn.microsoft.com/en-us/connectors/teams/?tabs=text1#adaptivecarditemschema
type Content struct {
Schema string `json:"$schema"`
Type string `json:"type"`
Version string `json:"version"`
Body []Body `json:"body"`
}

type Body struct {
Type string `json:"type"`
Text string `json:"text"`
Weight string `json:"weigth,omitempty"`
Size string `json:"size,omitempty"`
Wrap bool `json:"wrap,omitempty"`
Style string `json:"style,omitempty"`
Color string `json:"color,omitempty"`
}

type Attachment struct {
ContentType string `json:"contentType"`
ContentURL *string `json:"contentUrl"` // Use a pointer to handle null values
Content Content `json:"content"`
}

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"`
Type string `json:"type"`
Attachments []Attachment `json:"attachments"`
}

// New returns a new notifier that uses the Microsoft Teams Webhook API.
Expand Down Expand Up @@ -102,10 +121,6 @@ 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 @@ -128,12 +143,33 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
}

t := teamsMessage{
Context: "http://schema.org/extensions",
Type: "MessageCard",
Title: title,
Summary: summary,
Text: text,
ThemeColor: color,
Type: "message",
Attachments: []Attachment{
{
ContentType: "application/vnd.microsoft.card.adaptive",
ContentURL: nil,
Content: Content{
Schema: "http://adaptivecards.io/schemas/adaptive-card.json",
Type: "AdaptiveCard",
Version: "1.2",
Body: []Body{
{
Type: "TextBlock",
Text: title,
Weight: "Bolder",
Size: "Medium",
Wrap: true,
Style: "heading",
Color: color,
},
{
Type: "TextBlock",
Text: text,
},
},
},
},
},
}

var payload bytes.Buffer
Expand Down
18 changes: 4 additions & 14 deletions notify/msteams/msteams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ func TestMSTeamsTemplating(t *testing.T) {
{
title: "full-blown message",
cfg: &config.MSTeamsConfig{
Title: `{{ template "msteams.default.title" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
Title: `{{ template "msteams.default.title" . }}`,
Text: `{{ template "msteams.default.text" . }}`,
},
retry: false,
},
Expand All @@ -91,20 +90,11 @@ 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" . }}`,
Summary: `{{ template "msteams.default.summary" . }}`,
Text: "{{ ",
Title: `{{ template "msteams.default.title" . }}`,
Text: "{{ ",
},
errMsg: "template: :1: unclosed action",
},
Expand Down
1 change: 0 additions & 1 deletion template/default.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ 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 0ecc664

Please sign in to comment.