Skip to content

Commit

Permalink
Update code to support new Microsoft Teams webhook JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jul 10, 2024
1 parent 1cd41ab commit f99884f
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 34 deletions.
146 changes: 146 additions & 0 deletions alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,149 @@ func TestAlert_isThrottlingEnabled(t *testing.T) {
})
}
}

func TestNewMsTeam(t *testing.T) {
tests := []struct {
name string
err error
expandos *Expandos
want MsTeam
}{
{
name: "default",
err: errors.New("test error"),
expandos: &Expandos{
MsTeamsAlertCardSubject: "Test Alert",
MsTeamsCardSubject: "Test Card",
MsTeamsError: "Test Error",
},
want: MsTeam{
Type: "AdaptiveCard",
Version: "1.2",
Body: []BodyStruct{
{
Type: "TextBlock",
Text: "Test Alert",
Items: []ItemStruct{
{
Type: "TextBlock",
Text: "Test Card",
Weight: "Bolder",
Size: "Medium",
},
{
Type: "TextBlock",
Text: "Error: Test Error",
Weight: "Lighter",
Size: "Small",
},
},
},
},
Actions: []ActionStruct{
{
Type: "Action.OpenUrl",
Title: "View Details",
URL: os.Getenv("MS_TEAMS_WEBHOOK"),
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewMsTeam(tt.err, tt.expandos)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewMsTeam() = %v, want %v", got, tt.want)
}
})
}
}

func TestMsTeam_Send(t *testing.T) {
tests := []struct {
name string
card MsTeam
wantErr bool
}{
{
name: "send_success",
card: MsTeam{
Type: "AdaptiveCard",
Version: "1.2",
Body: []BodyStruct{
{
Type: "TextBlock",
Text: "Test Alert",
Items: []ItemStruct{
{
Type: "TextBlock",
Text: "Test Card",
Weight: "Bolder",
Size: "Medium",
},
{
Type: "TextBlock",
Text: "Error: Test Error",
Weight: "Lighter",
Size: "Small",
},
},
},
},
Actions: []ActionStruct{
{
Type: "Action.OpenUrl",
Title: "View Details",
URL: os.Getenv("MS_TEAMS_WEBHOOK"),
},
},
},
wantErr: false,
},
{
name: "send_failure",
card: MsTeam{
Type: "AdaptiveCard",
Version: "1.2",
Body: []BodyStruct{
{
Type: "TextBlock",
Text: "Test Alert",
Items: []ItemStruct{
{
Type: "TextBlock",
Text: "Test Card",
Weight: "Bolder",
Size: "Medium",
},
{
Type: "TextBlock",
Text: "Error: Test Error",
Weight: "Lighter",
Size: "Small",
},
},
},
},
Actions: []ActionStruct{
{
Type: "Action.OpenUrl",
Title: "View Details",
URL: "",
},
},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.card.Send(); (err != nil) != tt.wantErr {
t.Errorf("MsTeam.Send() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
81 changes: 47 additions & 34 deletions ms_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,34 @@ import (
"time"
)

// MsTeam is MessageCard for Team notification
// MsTeam is AdaptiveCard for Team notification
type MsTeam struct {
Type string `json:"@type"`
Context string `json:"@context"`
Summary string `json:"summary"`
ThemeColor string `json:"themeColor"`
Title string `json:"title"`
Sections []SectionStruct `json:"sections"`
Type string `json:"type"`
Version string `json:"version"`
Body []BodyStruct `json:"body"`
Actions []ActionStruct `json:"actions"`
}

// SectionStruct is sub-struct of MsTeam
type SectionStruct struct {
ActivityTitle string `json:"activityTitle"`
ActivitySubtitle string `json:"activitySubtitle"`
ActivityImage string `json:"activityImage"`
Facts []FactStruct `json:"facts"`
// BodyStruct is sub-struct of MsTeam
type BodyStruct struct {
Type string `json:"type"`
Text string `json:"text"`
Items []ItemStruct `json:"items"`
}

// FactStruct is sub-struct of SectionStruct
type FactStruct struct {
Name string `json:"name"`
Value string `json:"value"`
// ItemStruct is sub-struct of BodyStruct
type ItemStruct struct {
Type string `json:"type"`
Text string `json:"text"`
Weight string `json:"weight"`
Size string `json:"size"`
}

// ActionStruct is sub-struct of MsTeam
type ActionStruct struct {
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
}

// NewMsTeam is used to create MsTeam
Expand All @@ -55,28 +61,35 @@ func NewMsTeam(err error, expandos *Expandos) MsTeam {
}

notificationCard := MsTeam{
Type: "MessageCard",
Context: "http://schema.org/extensions",
Summary: summary,
ThemeColor: os.Getenv("ALERT_THEME_COLOR"),
Title: title,
Sections: []SectionStruct{
SectionStruct{
ActivityTitle: summary,
ActivitySubtitle: fmt.Sprintf("error has occured on %v", os.Getenv("APP_NAME")),
ActivityImage: "",
Facts: []FactStruct{
FactStruct{
Name: "Environment:",
Value: os.Getenv("APP_ENV"),
Type: "AdaptiveCard",
Version: "1.2",
Body: []BodyStruct{
BodyStruct{
Type: "TextBlock",
Text: title,
Items: []ItemStruct{
ItemStruct{
Type: "TextBlock",
Text: summary,
Weight: "Bolder",
Size: "Medium",
},
FactStruct{
Name: "ERROR",
Value: errMsg,
ItemStruct{
Type: "TextBlock",
Text: fmt.Sprintf("Error: %s", errMsg),
Weight: "Lighter",
Size: "Small",
},
},
},
},
Actions: []ActionStruct{
ActionStruct{
Type: "Action.OpenUrl",
Title: "View Details",
URL: os.Getenv("MS_TEAMS_WEBHOOK"),
},
},
}
return notificationCard
}
Expand Down

0 comments on commit f99884f

Please sign in to comment.