diff --git a/go.mod b/go.mod index 821fc67f..292d7502 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b955db2d..37b8cd9b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/service/msteams/mock_teams_client.go b/service/msteams/mock_teams_client.go index 3d63f044..a2011530 100644 --- a/service/msteams/mock_teams_client.go +++ b/service/msteams/mock_teams_client.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.43.2. DO NOT EDIT. +// Code generated by mockery v2.44.1. DO NOT EDIT. package msteams @@ -22,17 +22,17 @@ func (_m *mockteamsClient) EXPECT() *mockteamsClient_Expecter { return &mockteamsClient_Expecter{mock: &_m.Mock} } -// SendWithContext provides a mock function with given fields: ctx, webhookURL, webhookMessage -func (_m *mockteamsClient) SendWithContext(ctx context.Context, webhookURL string, webhookMessage goteamsnotify.MessageCard) error { - ret := _m.Called(ctx, webhookURL, webhookMessage) +// SendWithContext provides a mock function with given fields: ctx, webhookURL, message +func (_m *mockteamsClient) SendWithContext(ctx context.Context, webhookURL string, message goteamsnotify.TeamsMessage) error { + ret := _m.Called(ctx, webhookURL, message) if len(ret) == 0 { panic("no return value specified for SendWithContext") } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, goteamsnotify.MessageCard) error); ok { - r0 = rf(ctx, webhookURL, webhookMessage) + if rf, ok := ret.Get(0).(func(context.Context, string, goteamsnotify.TeamsMessage) error); ok { + r0 = rf(ctx, webhookURL, message) } else { r0 = ret.Error(0) } @@ -48,14 +48,14 @@ type mockteamsClient_SendWithContext_Call struct { // SendWithContext is a helper method to define mock.On call // - ctx context.Context // - webhookURL string -// - webhookMessage goteamsnotify.MessageCard -func (_e *mockteamsClient_Expecter) SendWithContext(ctx interface{}, webhookURL interface{}, webhookMessage interface{}) *mockteamsClient_SendWithContext_Call { - return &mockteamsClient_SendWithContext_Call{Call: _e.mock.On("SendWithContext", ctx, webhookURL, webhookMessage)} +// - message goteamsnotify.TeamsMessage +func (_e *mockteamsClient_Expecter) SendWithContext(ctx interface{}, webhookURL interface{}, message interface{}) *mockteamsClient_SendWithContext_Call { + return &mockteamsClient_SendWithContext_Call{Call: _e.mock.On("SendWithContext", ctx, webhookURL, message)} } -func (_c *mockteamsClient_SendWithContext_Call) Run(run func(ctx context.Context, webhookURL string, webhookMessage goteamsnotify.MessageCard)) *mockteamsClient_SendWithContext_Call { +func (_c *mockteamsClient_SendWithContext_Call) Run(run func(ctx context.Context, webhookURL string, message goteamsnotify.TeamsMessage)) *mockteamsClient_SendWithContext_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(string), args[2].(goteamsnotify.MessageCard)) + run(args[0].(context.Context), args[1].(string), args[2].(goteamsnotify.TeamsMessage)) }) return _c } @@ -65,25 +65,25 @@ func (_c *mockteamsClient_SendWithContext_Call) Return(_a0 error) *mockteamsClie return _c } -func (_c *mockteamsClient_SendWithContext_Call) RunAndReturn(run func(context.Context, string, goteamsnotify.MessageCard) error) *mockteamsClient_SendWithContext_Call { +func (_c *mockteamsClient_SendWithContext_Call) RunAndReturn(run func(context.Context, string, goteamsnotify.TeamsMessage) error) *mockteamsClient_SendWithContext_Call { _c.Call.Return(run) return _c } // SkipWebhookURLValidationOnSend provides a mock function with given fields: skip -func (_m *mockteamsClient) SkipWebhookURLValidationOnSend(skip bool) goteamsnotify.API { +func (_m *mockteamsClient) SkipWebhookURLValidationOnSend(skip bool) *goteamsnotify.TeamsClient { ret := _m.Called(skip) if len(ret) == 0 { panic("no return value specified for SkipWebhookURLValidationOnSend") } - var r0 goteamsnotify.API - if rf, ok := ret.Get(0).(func(bool) goteamsnotify.API); ok { + var r0 *goteamsnotify.TeamsClient + if rf, ok := ret.Get(0).(func(bool) *goteamsnotify.TeamsClient); ok { r0 = rf(skip) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(goteamsnotify.API) + r0 = ret.Get(0).(*goteamsnotify.TeamsClient) } } @@ -108,12 +108,12 @@ func (_c *mockteamsClient_SkipWebhookURLValidationOnSend_Call) Run(run func(skip return _c } -func (_c *mockteamsClient_SkipWebhookURLValidationOnSend_Call) Return(_a0 goteamsnotify.API) *mockteamsClient_SkipWebhookURLValidationOnSend_Call { +func (_c *mockteamsClient_SkipWebhookURLValidationOnSend_Call) Return(_a0 *goteamsnotify.TeamsClient) *mockteamsClient_SkipWebhookURLValidationOnSend_Call { _c.Call.Return(_a0) return _c } -func (_c *mockteamsClient_SkipWebhookURLValidationOnSend_Call) RunAndReturn(run func(bool) goteamsnotify.API) *mockteamsClient_SkipWebhookURLValidationOnSend_Call { +func (_c *mockteamsClient_SkipWebhookURLValidationOnSend_Call) RunAndReturn(run func(bool) *goteamsnotify.TeamsClient) *mockteamsClient_SkipWebhookURLValidationOnSend_Call { _c.Call.Return(run) return _c } diff --git a/service/msteams/ms_teams.go b/service/msteams/ms_teams.go index cd9f32b6..02c0a85b 100644 --- a/service/msteams/ms_teams.go +++ b/service/msteams/ms_teams.go @@ -1,4 +1,3 @@ -//nolint:staticcheck // Will fix deprecated dependencies soon. package msteams import ( @@ -6,20 +5,23 @@ import ( "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. @@ -27,7 +29,7 @@ type MSTeams struct { // // -> https://github.com/atc0005/go-teams-notify#example-basic func New() *MSTeams { - client := teams.NewClient() + client := teams.NewTeamsClient() m := &MSTeams{ client: client, @@ -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) { @@ -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) } } } diff --git a/service/msteams/ms_teams_test.go b/service/msteams/ms_teams_test.go index 0ee110d3..deb0eaa0 100644 --- a/service/msteams/ms_teams_test.go +++ b/service/msteams/ms_teams_test.go @@ -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: "", @@ -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: "", @@ -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", }, }