Skip to content

Commit

Permalink
PR changes requested
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaupu committed Apr 6, 2021
1 parent afd7952 commit 54e6e84
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
29 changes: 14 additions & 15 deletions messagecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type MessageCardSectionImage struct {

// MessageCardSection represents a section to include in a message card.
type MessageCardSection struct {

// Title is the title property of a section. This property is displayed
// in a font that stands out, while not as prominent as the card's title.
// It is meant to introduce the section and summarize its content,
Expand Down Expand Up @@ -74,17 +73,6 @@ type MessageCardSection struct {
// for the update notification.
ActivityText string `json:"activityText,omitempty"`

// Markdown represents a toggle to enable or disable Markdown formatting.
// By default, all text fields in a card and its sections can be formatted
// using basic Markdown.
Markdown bool `json:"markdown,omitempty"`

// StartGroup is the section's startGroup property. This property marks
// the start of a logical group of information. Typically, sections with
// startGroup set to true will be visually separated from previous card
// elements.
StartGroup bool `json:"startGroup,omitempty"`

// HeroImage is a property that allows for setting an image as the
// centerpiece of a message card. This property can also be used to add a
// banner to the message card.
Expand All @@ -109,6 +97,17 @@ type MessageCardSection struct {
// https://stackoverflow.com/questions/18088294/how-to-not-marshal-an-empty-struct-into-json-with-go
// https://stackoverflow.com/questions/33447334/golang-json-marshal-how-to-omit-empty-nested-struct
Images []*MessageCardSectionImage `json:"images,omitempty"`

// Markdown represents a toggle to enable or disable Markdown formatting.
// By default, all text fields in a card and its sections can be formatted
// using basic Markdown.
Markdown bool `json:"markdown,omitempty"`

// StartGroup is the section's startGroup property. This property marks
// the start of a logical group of information. Typically, sections with
// startGroup set to true will be visually separated from previous card
// elements.
StartGroup bool `json:"startGroup,omitempty"`
}

// MessageCard represents a legacy actionable message card used via Office 365
Expand Down Expand Up @@ -143,11 +142,11 @@ type MessageCard struct {
// displayed in a non-obtrusive manner.
ThemeColor string `json:"themeColor,omitempty"`

// Sections is a collection of sections to include in the card.
Sections []*MessageCardSection `json:"sections,omitempty"`

// ValidateFunc is a validation function that validates a MessageCard
ValidateFunc func() error `json:"-"`

// Sections is a collection of sections to include in the card.
Sections []*MessageCardSection `json:"sections,omitempty"`
}

// AddSection adds one or many additional MessageCardSection values to a
Expand Down
20 changes: 15 additions & 5 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const (
WebhookURLOrgWebhookPrefix = "https://example.webhook.office.com"
)

// DisableWebhookURLValidation is a special keyword used to indicate to
// validation function(s) that webhook URL validation should be disabled.
//
// Deprecated: prefer using API.SkipWebhookURLValidationOnSend(bool) method instead
const DisableWebhookURLValidation string = "DISABLE_WEBHOOK_URL_VALIDATION"

// Regular Expression related constants that we can use to validate incoming
// webhook URLs provided by the user.
const (
Expand Down Expand Up @@ -90,8 +96,8 @@ type API interface {

type teamsClient struct {
httpClient *http.Client
skipWebhookURLValidation bool
webhookURLValidationPatterns []string
skipWebhookURLValidation bool
}

func init() {
Expand Down Expand Up @@ -332,7 +338,7 @@ func (c teamsClient) validateInput(webhookMessage MessageCard, webhookURL string
}

func (c teamsClient) ValidateWebhook(webhookURL string) error {
if c.skipWebhookURLValidation {
if c.skipWebhookURLValidation || webhookURL == DisableWebhookURLValidation {
return nil
}

Expand Down Expand Up @@ -365,7 +371,9 @@ func (c teamsClient) ValidateWebhook(webhookURL string) error {
// IsValidInput is a validation "wrapper" function. This function is intended
// to run current validation checks and offer easy extensibility for future
// validation requirements.
// Deprecated: still here for retro compatibility
//
// Deprecated: use API.ValidateWebhook() and MessageCard.Validate()
// methods instead.
func IsValidInput(webhookMessage MessageCard, webhookURL string) (bool, error) {
// validate url
if valid, err := IsValidWebhookURL(webhookURL); !valid {
Expand All @@ -382,7 +390,8 @@ func IsValidInput(webhookMessage MessageCard, webhookURL string) (bool, error) {

// IsValidWebhookURL performs validation checks on the webhook URL used to
// submit messages to Microsoft Teams.
// Deprecated: still here for retro compatibility
//
// Deprecated: use API.ValidateWebhook() method instead.
func IsValidWebhookURL(webhookURL string) (bool, error) {
c := teamsClient{}
err := c.ValidateWebhook(webhookURL)
Expand All @@ -391,7 +400,8 @@ func IsValidWebhookURL(webhookURL string) (bool, error) {

// IsValidMessageCard performs validation/checks for known issues with
// MessardCard values.
// Deprecated: still here for retro compatibility
//
// Deprecated: use MessageCard.Validate() instead.
func IsValidMessageCard(webhookMessage MessageCard) (bool, error) {
err := webhookMessage.Validate()
return err == nil, err
Expand Down
6 changes: 3 additions & 3 deletions send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func TestTeamsClientSend(t *testing.T) {
var tests = []struct {
reqURL string
reqMsg MessageCard
resStatus int // httpClient response status
resBody string // httpClient response body text
resError error // httpClient error
error error // method error
skipURLVal bool // whether webhook URL validation is applied (e.g., GH-68)
validationURLPatterns []string
skipURLVal bool // whether webhook URL validation is applied (e.g., GH-68)
resStatus int // httpClient response status
}{
// invalid webhookURL - url.Parse error
{
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestTeamsClientSend(t *testing.T) {
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: ExpectedWebhookURLResponseText,
resError: ErrWebhookURLUnexpected,
resError: nil,
error: ErrWebhookURLUnexpected,
skipURLVal: false,
validationURLPatterns: []string{DefaultWebhookURLValidationPattern, "arbitrary.domain.com"},
Expand Down

0 comments on commit 54e6e84

Please sign in to comment.