diff --git a/Makefile b/Makefile index 2618bbd5..1b120c91 100644 --- a/Makefile +++ b/Makefile @@ -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 ############################################################################### 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", }, }