Skip to content

Commit

Permalink
refactor(msteams): fix depreated parts in msteams service
Browse files Browse the repository at this point in the history
This change fixes all places in the msteams service where we were still
using deprecated parts of the provider library
https://github.com/atc0005/go-teams-notify. This change uses a
pre-release of go-teams-notify to verify full compatibility between
Notify and go-teams-notify, as discussed here:
atc0005/go-teams-notify#285

Regenerated the mock since the service interface changed. Fixed the
tests accordingly. Everything passes, we should be good to go.
  • Loading branch information
nikoksr committed Aug 7, 2024
1 parent 9b43af2 commit 5debe3b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.5
require (
github.com/RocketChat/Rocket.Chat.Go.SDK v0.0.0-20240116134246-a8cbe886bab0
github.com/antihax/optional v1.0.0 // indirect
github.com/atc0005/go-teams-notify/v2 v2.10.0
github.com/atc0005/go-teams-notify/v2 v2.12.0-alpha.1
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/credentials v1.17.27
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwc
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/appleboy/go-fcm v1.2.1 h1:NhpACabtRuAplYg6bTNfSr3LBwsSuutP55HsphzLU/g=
github.com/appleboy/go-fcm v1.2.1/go.mod h1:5FzMN+9J2sxnkoys9h3y48GQH8HnI637Q/ro/uP2Qsk=
github.com/atc0005/go-teams-notify/v2 v2.10.0 h1:eQvRIkyESQgBvlUdQ/iPol/lj3QcRyrdEQM3+c/nXhM=
github.com/atc0005/go-teams-notify/v2 v2.10.0/go.mod h1:SIeE1UfCcVRYMqP5b+r1ZteHyA/2UAjzWF5COnZ8q0w=
github.com/atc0005/go-teams-notify/v2 v2.12.0-alpha.1 h1:0km5FZ3RYE3nQn8nnptNJ3DjYIAm1k7+mtoZUByKjJc=
github.com/atc0005/go-teams-notify/v2 v2.12.0-alpha.1/go.mod h1:WSv9moolRsBcpZbwEf6gZxj7h0uJlJskJq5zkEWKO8Y=
github.com/aws/aws-sdk-go-v2 v1.30.3 h1:jUeBtG0Ih+ZIFH0F4UkmL9w3cSpaMv9tYYDbzILP8dY=
github.com/aws/aws-sdk-go-v2 v1.30.3/go.mod h1:nIQjQVp5sfpQcTc9mPSr1B0PaWK5ByX9MOoDadSN4lc=
github.com/aws/aws-sdk-go-v2/config v1.27.27 h1:HdqgGt1OAP0HkEDDShEl0oSYa9ZZBSOmKpdpsDMdO90=
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 5debe3b

Please sign in to comment.