diff --git a/.golangci.yaml b/.golangci.yaml index 45a3b9b..6b57680 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -28,7 +28,7 @@ linters-settings: gocyclo: # minimal code complexity to report, 30 by default (but we recommend 10-20) - min-complexity: 10 + min-complexity: 15 golint: # minimal confidence for issues, default is 0.8 diff --git a/messagecard.go b/messagecard.go index 84c8c46..19d6d29 100644 --- a/messagecard.go +++ b/messagecard.go @@ -143,12 +143,10 @@ type MessageCard struct { // MessageCard. Validation is performed to reject invalid values with an error // message. func (mc *MessageCard) AddSection(section ...*MessageCardSection) error { - for _, s := range section { - // bail if a completely nil section provided if s == nil { - return fmt.Errorf("AddSection: nil MessageCardSection received") + return fmt.Errorf("func AddSection: nil MessageCardSection received") } // Perform validation of all MessageCardSection fields in an effort to @@ -160,14 +158,13 @@ func (mc *MessageCard) AddSection(section ...*MessageCardSection) error { // field in the output JSON. // See also https://github.com/golang/go/issues/11939 switch { - // If any of these cases trigger, skip over the `default` case // statement and add the section. case s.Images != nil: case s.Facts != nil: case s.HeroImage != nil: - case s.StartGroup != false: - case s.Markdown != false: + case s.StartGroup: + case s.Markdown: case s.ActivityText != "": case s.ActivitySubtitle != "": case s.ActivityTitle != "": @@ -180,19 +177,15 @@ func (mc *MessageCard) AddSection(section ...*MessageCardSection) error { } mc.Sections = append(mc.Sections, s) - } return nil - } // AddFact adds one or many additional MessageCardSectionFact values to a // MessageCardSection func (mcs *MessageCardSection) AddFact(fact ...MessageCardSectionFact) error { - for _, f := range fact { - if f.Name == "" { return fmt.Errorf("empty Name field received for new fact: %+v", f) } @@ -203,13 +196,11 @@ func (mcs *MessageCardSection) AddFact(fact ...MessageCardSectionFact) error { } return nil - } // AddFactFromKeyValue accepts a key and slice of values and converts them to // MessageCardSectionFact values func (mcs *MessageCardSection) AddFactFromKeyValue(key string, values ...string) error { - // validate arguments if key == "" { @@ -238,7 +229,6 @@ func (mcs *MessageCardSection) AddFactFromKeyValue(key string, values ...string) // AddImage adds an image to a MessageCard section. These images are used to // provide a photo gallery inside a MessageCard section. func (mcs *MessageCardSection) AddImage(sectionImage ...MessageCardSectionImage) error { - for _, img := range sectionImage { if img.Image == "" { return fmt.Errorf("cannot add empty image URL") @@ -248,8 +238,7 @@ func (mcs *MessageCardSection) AddImage(sectionImage ...MessageCardSectionImage) return fmt.Errorf("cannot add empty image title") } - mcs.Images = append(mcs.Images, &img) - + mcs.Images = append(mcs.Images, &img) // nolint:scopelint } return nil @@ -259,7 +248,6 @@ func (mcs *MessageCardSection) AddImage(sectionImage ...MessageCardSectionImage) // arguments. This image is used as the centerpiece or banner of a message // card. func (mcs *MessageCardSection) AddHeroImageStr(imageURL string, imageTitle string) error { - if imageURL == "" { return fmt.Errorf("cannot add empty hero image URL") } @@ -281,14 +269,12 @@ func (mcs *MessageCardSection) AddHeroImageStr(imageURL string, imageTitle strin // our validation checks didn't find any problems return nil - } // AddHeroImage adds a Hero Image to a MessageCard section using a // MessageCardSectionImage argument. This image is used as the centerpiece or // banner of a message card. func (mcs *MessageCardSection) AddHeroImage(heroImage MessageCardSectionImage) error { - if heroImage.Image == "" { return fmt.Errorf("cannot add empty hero image URL") } @@ -301,13 +287,11 @@ func (mcs *MessageCardSection) AddHeroImage(heroImage MessageCardSectionImage) e // our validation checks didn't find any problems return nil - } // NewMessageCard creates a new message card with fields required by the // legacy message card format already predefined func NewMessageCard() MessageCard { - // define expected values to meet Office 365 Connector card requirements // https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference#card-fields msgCard := MessageCard{ diff --git a/send.go b/send.go index f7d25e1..8864669 100644 --- a/send.go +++ b/send.go @@ -32,7 +32,6 @@ func NewClient() API { // Send - will post a notification to MS Teams webhook URL func (c teamsClient) Send(webhookURL string, webhookMessage MessageCard) error { - // Validate input data if valid, err := IsValidInput(webhookMessage, webhookURL); !valid { return err @@ -65,7 +64,6 @@ func (c teamsClient) Send(webhookURL string, webhookMessage MessageCard) error { // to run current validation checks and offer easy extensibility for future // validation requirements. func IsValidInput(webhookMessage MessageCard, webhookURL string) (bool, error) { - // validate url if valid, err := IsValidWebhookURL(webhookURL); !valid { return false, err @@ -77,7 +75,6 @@ func IsValidInput(webhookMessage MessageCard, webhookURL string) (bool, error) { } return true, nil - } // IsValidWebhookURL performs validation checks on the webhook URL used to @@ -102,7 +99,6 @@ func IsValidWebhookURL(webhookURL string) (bool, error) { // IsValidMessageCard performs validation/checks for known issues with // MessardCard values. func IsValidMessageCard(webhookMessage MessageCard) (bool, error) { - if (webhookMessage.Text == "") && (webhookMessage.Summary == "") { // This scenario results in: // 400 Bad Request @@ -111,5 +107,4 @@ func IsValidMessageCard(webhookMessage MessageCard) (bool, error) { } return true, nil - } diff --git a/send_test.go b/send_test.go index d0426c8..0cbab0b 100644 --- a/send_test.go +++ b/send_test.go @@ -16,7 +16,8 @@ func TestNewClient(t *testing.T) { func TestTeamsClientSend(t *testing.T) { // THX@Hassansin ... http://hassansin.github.io/Unit-Testing-http-client-in-Go - emptyMessage := NewMessageCard() + simpleMsgCard := NewMessageCard() + simpleMsgCard.Text = "Hello World" var tests = []struct { reqURL string reqMsg MessageCard @@ -27,7 +28,7 @@ func TestTeamsClientSend(t *testing.T) { // invalid webhookURL - url.Parse error { reqURL: "ht\ttp://", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 0, resError: nil, error: &url.Error{}, @@ -35,7 +36,7 @@ func TestTeamsClientSend(t *testing.T) { // invalid webhookURL - missing prefix in webhook URL { reqURL: "", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 0, resError: nil, error: errors.New(""), @@ -43,7 +44,7 @@ func TestTeamsClientSend(t *testing.T) { // invalid httpClient.Do call { reqURL: "https://outlook.office.com/webhook/xxx", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 200, resError: errors.New("pling"), error: &url.Error{}, @@ -51,7 +52,7 @@ func TestTeamsClientSend(t *testing.T) { // invalid httpClient.Do call { reqURL: "https://outlook.office365.com/webhook/xxx", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 200, resError: errors.New("pling"), error: &url.Error{}, @@ -59,7 +60,7 @@ func TestTeamsClientSend(t *testing.T) { // invalid response status code { reqURL: "https://outlook.office.com/webhook/xxx", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 400, resError: nil, error: errors.New(""), @@ -67,7 +68,7 @@ func TestTeamsClientSend(t *testing.T) { // invalid response status code { reqURL: "https://outlook.office365.com/webhook/xxx", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 400, resError: nil, error: errors.New(""), @@ -75,7 +76,7 @@ func TestTeamsClientSend(t *testing.T) { // valid { reqURL: "https://outlook.office.com/webhook/xxx", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 200, resError: nil, error: nil, @@ -83,7 +84,7 @@ func TestTeamsClientSend(t *testing.T) { // valid { reqURL: "https://outlook.office365.com/webhook/xxx", - reqMsg: emptyMessage, + reqMsg: simpleMsgCard, resStatus: 200, resError: nil, error: nil,