Skip to content

Commit

Permalink
Merge pull request #12 from dasrick/some-linting
Browse files Browse the repository at this point in the history
polish some line, change setup of golangci-lint and fix tests
  • Loading branch information
dasrick authored Apr 8, 2020
2 parents 52b2f9f + 1b523a9 commit 823a70e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 4 additions & 20 deletions messagecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 != "":
Expand All @@ -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)
}
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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{
Expand Down
5 changes: 0 additions & 5 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -111,5 +107,4 @@ func IsValidMessageCard(webhookMessage MessageCard) (bool, error) {
}

return true, nil

}
19 changes: 10 additions & 9 deletions send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,63 +28,63 @@ func TestTeamsClientSend(t *testing.T) {
// invalid webhookURL - url.Parse error
{
reqURL: "ht\ttp://",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 0,
resError: nil,
error: &url.Error{},
},
// invalid webhookURL - missing prefix in webhook URL
{
reqURL: "",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 0,
resError: nil,
error: errors.New(""),
},
// invalid httpClient.Do call
{
reqURL: "https://outlook.office.com/webhook/xxx",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 200,
resError: errors.New("pling"),
error: &url.Error{},
},
// invalid httpClient.Do call
{
reqURL: "https://outlook.office365.com/webhook/xxx",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 200,
resError: errors.New("pling"),
error: &url.Error{},
},
// invalid response status code
{
reqURL: "https://outlook.office.com/webhook/xxx",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 400,
resError: nil,
error: errors.New(""),
},
// invalid response status code
{
reqURL: "https://outlook.office365.com/webhook/xxx",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 400,
resError: nil,
error: errors.New(""),
},
// valid
{
reqURL: "https://outlook.office.com/webhook/xxx",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 200,
resError: nil,
error: nil,
},
// valid
{
reqURL: "https://outlook.office365.com/webhook/xxx",
reqMsg: emptyMessage,
reqMsg: simpleMsgCard,
resStatus: 200,
resError: nil,
error: nil,
Expand Down

0 comments on commit 823a70e

Please sign in to comment.