Skip to content

Commit

Permalink
Merge pull request #318 from bvwells/notifier-fields
Browse files Browse the repository at this point in the history
Fix slack/teams notification fields
  • Loading branch information
stefanprodan authored Oct 3, 2019
2 parents 6706ca5 + fc68635 commit e3801cb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/notifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *Slack) Post(workload string, namespace string, message string, fields [
color = "danger"
}

sfields := make([]SlackField, len(fields))
sfields := make([]SlackField, 0, len(fields))
for _, f := range fields {
sfields = append(sfields, SlackField{f.Name, f.Value, false})
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/notifier/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
)

func TestSlack_Post(t *testing.T) {
fields := []Field{
{Name: "name1", Value: "value1"},
{Name: "name2", Value: "value2"},
}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -20,6 +25,10 @@ func TestSlack_Post(t *testing.T) {
if payload.Attachments[0].AuthorName != "podinfo.test" {
t.Fatal("wrong author name")
}

if len(payload.Attachments[0].Fields) != len(fields) {
t.Fatal("wrong facts")
}
}))
defer ts.Close()

Expand All @@ -28,7 +37,7 @@ func TestSlack_Post(t *testing.T) {
t.Fatal(err)
}

err = slack.Post("podinfo", "test", "test", nil, true)
err = slack.Post("podinfo", "test", "test", fields, true)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/notifier/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewMSTeams(hookURL string) (*MSTeams, error) {

// Post MS Teams message
func (s *MSTeams) Post(workload string, namespace string, message string, fields []Field, warn bool) error {
facts := make([]MSTeamsField, len(fields))
facts := make([]MSTeamsField, 0, len(fields))
for _, f := range fields {
facts = append(facts, MSTeamsField{f.Name, f.Value})
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/notifier/teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
)

func TestTeams_Post(t *testing.T) {

fields := []Field{
{Name: "name1", Value: "value1"},
{Name: "name2", Value: "value2"},
}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand All @@ -20,6 +26,9 @@ func TestTeams_Post(t *testing.T) {
if payload.Sections[0].ActivitySubtitle != "podinfo.test" {
t.Fatal("wrong activity subtitle")
}
if len(payload.Sections[0].Facts) != len(fields) {
t.Fatal("wrong facts")
}
}))
defer ts.Close()

Expand All @@ -28,7 +37,7 @@ func TestTeams_Post(t *testing.T) {
t.Fatal(err)
}

err = teams.Post("podinfo", "test", "test", nil, true)
err = teams.Post("podinfo", "test", "test", fields, true)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit e3801cb

Please sign in to comment.