Skip to content

Commit

Permalink
refactor(msteams): fix depreated parts in msteams service (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr authored Nov 2, 2024
1 parent cf3a7c5 commit 9e1bf26
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ export GOPROXY = https://proxy.golang.org,direct

# Install all the build and lint dependencies
tools:
go mod tidy
@go install mvdan.cc/gofumpt@latest
@go install github.com/daixiang0/gci@latest
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
@go install github.com/vektra/mockery/v2@latest
go install github.com/segmentio/golines@latest
@go install github.com/vektra/mockery/v2@v2.44.1
@go install github.com/segmentio/golines@latest
.PHONY: tools

###############################################################################
Expand Down
36 changes: 18 additions & 18 deletions service/msteams/mock_teams_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 18 additions & 11 deletions service/msteams/ms_teams.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
//nolint:staticcheck // Will fix deprecated dependencies soon.
package msteams

import (
"context"
"fmt"

teams "github.com/atc0005/go-teams-notify/v2"
"github.com/atc0005/go-teams-notify/v2/adaptivecard"
)

type teamsClient interface {
SendWithContext(ctx context.Context, webhookURL string, webhookMessage teams.MessageCard) error
SkipWebhookURLValidationOnSend(skip bool) teams.API
SendWithContext(ctx context.Context, webhookURL string, message teams.TeamsMessage) error
SkipWebhookURLValidationOnSend(skip bool) *teams.TeamsClient
}

// Compile-time check to ensure that teams.Client implements the teamsClient interface.
var _ teamsClient = teams.NewClient()
var _ teamsClient = teams.NewTeamsClient()

// MSTeams struct holds necessary data to communicate with the MSTeams API.
type MSTeams struct {
client teamsClient
webHooks []string

wrapText bool
}

// New returns a new instance of a MSTeams notification service.
// For more information about telegram api token:
//
// -> https://github.com/atc0005/go-teams-notify#example-basic
func New() *MSTeams {
client := teams.NewClient()
client := teams.NewTeamsClient()

m := &MSTeams{
client: client,
Expand All @@ -46,6 +48,11 @@ func (m *MSTeams) DisableWebhookValidation() {
m.client.SkipWebhookURLValidationOnSend(true)
}

// WithWrapText sets the wrapText field to the provided value. This is disabled by default.
func (m *MSTeams) WithWrapText(wrapText bool) {
m.wrapText = wrapText
}

// AddReceivers takes MSTeams channel web-hooks and adds them to the internal web-hook list. The Send method will send
// a given message to all those chats.
func (m *MSTeams) AddReceivers(webHooks ...string) {
Expand All @@ -58,18 +65,18 @@ func (m *MSTeams) AddReceivers(webHooks ...string) {
//
// -> https://github.com/atc0005/go-teams-notify#example-basic
func (m MSTeams) Send(ctx context.Context, subject, message string) error {
msgCard := teams.NewMessageCard()
msgCard.Title = subject
msgCard.Text = message
msg, err := adaptivecard.NewSimpleMessage(message, subject, m.wrapText)
if err != nil {
return fmt.Errorf("create message: %w", err)
}

for _, webHook := range m.webHooks {
select {
case <-ctx.Done():
return ctx.Err()
default:
err := m.client.SendWithContext(ctx, webHook, msgCard)
if err != nil {
return fmt.Errorf("send messag to channel %q: %w", webHook, err)
if err = m.client.SendWithContext(ctx, webHook, msg); err != nil {
return fmt.Errorf("send message to channel %q: %w", webHook, err)
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions service/msteams/ms_teams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestMSTeams_Send(t *testing.T) {
subject: "Test Subject",
message: "Test Message",
mockSetup: func(m *mockteamsClient) {
m.On("SendWithContext", mock.Anything, "https://webhook1.example.com", mock.AnythingOfType("MessageCard")).
m.On("SendWithContext", mock.Anything,
"https://webhook1.example.com", mock.AnythingOfType("*adaptivecard.Message")).
Return(nil)
},
expectedError: "",
Expand All @@ -37,9 +38,11 @@ func TestMSTeams_Send(t *testing.T) {
subject: "Test Subject",
message: "Test Message",
mockSetup: func(m *mockteamsClient) {
m.On("SendWithContext", mock.Anything, "https://webhook1.example.com", mock.AnythingOfType("MessageCard")).
m.On("SendWithContext", mock.Anything,
"https://webhook1.example.com", mock.AnythingOfType("*adaptivecard.Message")).
Return(nil)
m.On("SendWithContext", mock.Anything, "https://webhook2.example.com", mock.AnythingOfType("MessageCard")).
m.On("SendWithContext", mock.Anything,
"https://webhook2.example.com", mock.AnythingOfType("*adaptivecard.Message")).
Return(nil)
},
expectedError: "",
Expand All @@ -50,10 +53,11 @@ func TestMSTeams_Send(t *testing.T) {
subject: "Test Subject",
message: "Test Message",
mockSetup: func(m *mockteamsClient) {
m.On("SendWithContext", mock.Anything, "https://webhook1.example.com", mock.AnythingOfType("MessageCard")).
m.On("SendWithContext", mock.Anything,
"https://webhook1.example.com", mock.AnythingOfType("*adaptivecard.Message")).
Return(errors.New("Teams error"))
},
expectedError: "send messag to channel \"https://webhook1.example.com\": Teams error",
expectedError: "send message to channel \"https://webhook1.example.com\": Teams error",
},
}

Expand Down

0 comments on commit 9e1bf26

Please sign in to comment.