From 61ce920972914d188948b64ac03666730d29afa7 Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Thu, 1 Sep 2022 17:26:31 -0700 Subject: [PATCH] Cleanup parameters to functions to appease linters (#730) ## Description Group like parameters to graph functions so SonarCloud is happier ## Type of change Please check the type of change your PR introduces: - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Test - [x] :hamster: Trivial/Minor ## Issue(s) on the path to: * #456 ## Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [x] :green_heart: E2E --- .../connector/exchange/iterators_test.go | 14 +++- .../connector/exchange/service_iterators.go | 77 +++++++------------ .../connector/exchange/service_query.go | 22 ++---- src/internal/connector/graph/service.go | 14 +++- src/internal/connector/graph_connector.go | 12 ++- 5 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/internal/connector/exchange/iterators_test.go b/src/internal/connector/exchange/iterators_test.go index 0c8797d72..50c2c3d47 100644 --- a/src/internal/connector/exchange/iterators_test.go +++ b/src/internal/connector/exchange/iterators_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" + "github.com/alcionai/corso/internal/connector/graph" "github.com/alcionai/corso/internal/connector/mockconnector" "github.com/alcionai/corso/internal/connector/support" "github.com/alcionai/corso/internal/tester" @@ -137,6 +138,13 @@ func (suite *ExchangeIteratorSuite) TestIterativeFunctions() { &service.adapter, test.transformer) require.NoError(t, err) + + qp := graph.QueryParams{ + User: userID, + Scope: test.scope, + Credentials: service.credentials, + FailFast: false, + } // Create collection for iterate test collections := make(map[string]*Collection) var errs error @@ -144,10 +152,8 @@ func (suite *ExchangeIteratorSuite) TestIterativeFunctions() { // with corresponding item IDs. New collections are created for each directory callbackFunc := test.iterativeFunction( ctx, - userID, - test.scope, - errs, false, - service.credentials, + qp, + errs, collections, nil) diff --git a/src/internal/connector/exchange/service_iterators.go b/src/internal/connector/exchange/service_iterators.go index 7fcdbf50e..a6cb09566 100644 --- a/src/internal/connector/exchange/service_iterators.go +++ b/src/internal/connector/exchange/service_iterators.go @@ -9,7 +9,6 @@ import ( "github.com/alcionai/corso/internal/connector/graph" "github.com/alcionai/corso/internal/connector/support" - "github.com/alcionai/corso/pkg/account" "github.com/alcionai/corso/pkg/selectors" ) @@ -31,11 +30,8 @@ type displayable interface { // @returns a callback func that works with msgraphgocore.PageIterator.Iterate function type GraphIterateFunc func( ctx context.Context, - user string, - scope selectors.ExchangeScope, + qp graph.QueryParams, errs error, - failFast bool, - credentials account.M365Config, collections map[string]*Collection, graphStatusChannel chan<- *support.ConnectorOperationStatus, ) func(any) bool @@ -46,11 +42,8 @@ type GraphIterateFunc func( // placed into a Collection based on the parent folder func IterateSelectAllDescendablesForCollections( ctx context.Context, - user string, - scope selectors.ExchangeScope, + qp graph.QueryParams, errs error, - failFast bool, - credentials account.M365Config, collections map[string]*Collection, statusCh chan<- *support.ConnectorOperationStatus, ) func(any) bool { @@ -63,12 +56,12 @@ func IterateSelectAllDescendablesForCollections( return func(pageItem any) bool { // Defines the type of collection being created within the function if !isCategorySet { - if scope.IncludesCategory(selectors.ExchangeMail) { + if qp.Scope.IncludesCategory(selectors.ExchangeMail) { collectionType = messages category = mailCategory } - if scope.IncludesCategory(selectors.ExchangeContact) { + if qp.Scope.IncludesCategory(selectors.ExchangeContact) { collectionType = contacts category = contactsCategory } @@ -78,21 +71,21 @@ func IterateSelectAllDescendablesForCollections( entry, ok := pageItem.(descendable) if !ok { - errs = support.WrapAndAppendf(user, errors.New("descendable conversion failure"), errs) + errs = support.WrapAndAppendf(qp.User, errors.New("descendable conversion failure"), errs) return true } // Saving to messages to list. Indexed by folder directory := *entry.GetParentFolderId() if _, ok = collections[directory]; !ok { - service, err := createService(credentials, failFast) + service, err := createService(qp.Credentials, qp.FailFast) if err != nil { - errs = support.WrapAndAppend(user, err, errs) + errs = support.WrapAndAppend(qp.User, err, errs) return true } edc := NewCollection( - user, - []string{credentials.TenantID, user, category, directory}, + qp.User, + []string{qp.Credentials.TenantID, qp.User, category, directory}, collectionType, service, statusCh, @@ -112,11 +105,8 @@ func IterateSelectAllDescendablesForCollections( // the calendarID which originates from M365. func IterateSelectAllEventsForCollections( ctx context.Context, - user string, - scope selectors.ExchangeScope, + qp graph.QueryParams, errs error, - failFast bool, - credentials account.M365Config, collections map[string]*Collection, statusCh chan<- *support.ConnectorOperationStatus, ) func(any) bool { @@ -124,7 +114,7 @@ func IterateSelectAllEventsForCollections( event, ok := eventItem.(models.Eventable) if !ok { errs = support.WrapAndAppend( - user, + qp.User, errors.New("event iteration failure"), errs, ) @@ -137,7 +127,7 @@ func IterateSelectAllEventsForCollections( value, ok := adtl["calendar@odata.associationLink"] if !ok { errs = support.WrapAndAppend( - user, + qp.User, fmt.Errorf("%s: does not support calendar look up", *event.GetId()), errs, ) @@ -148,7 +138,7 @@ func IterateSelectAllEventsForCollections( link, ok := value.(*string) if !ok || link == nil { errs = support.WrapAndAppend( - user, + qp.User, fmt.Errorf("%s: unable to obtain calendar event data", *event.GetId()), errs, ) @@ -160,7 +150,7 @@ func IterateSelectAllEventsForCollections( directory, err := parseCalendarIDFromEvent(*link) if err != nil { errs = support.WrapAndAppend( - user, + qp.User, errors.Wrap(err, *event.GetId()), errs, ) @@ -169,15 +159,15 @@ func IterateSelectAllEventsForCollections( } if _, ok := collections[directory]; !ok { - service, err := createService(credentials, failFast) + service, err := createService(qp.Credentials, qp.FailFast) if err != nil { - errs = support.WrapAndAppend(user, err, errs) + errs = support.WrapAndAppend(qp.User, err, errs) return true } edc := NewCollection( - user, - []string{credentials.TenantID, user, eventsCategory, directory}, + qp.User, + []string{qp.Credentials.TenantID, qp.User, eventsCategory, directory}, events, service, statusCh, @@ -196,11 +186,8 @@ func IterateSelectAllEventsForCollections( // into a Collection. Messages outside of those directories are omitted. func IterateAndFilterMessagesForCollections( ctx context.Context, - user string, - scope selectors.ExchangeScope, + qp graph.QueryParams, errs error, - failFast bool, - credentials account.M365Config, collections map[string]*Collection, statusCh chan<- *support.ConnectorOperationStatus, ) func(any) bool { @@ -210,15 +197,12 @@ func IterateAndFilterMessagesForCollections( if !isFilterSet { err := CollectMailFolders( ctx, - scope, - user, + qp, collections, - credentials, - failFast, statusCh, ) if err != nil { - errs = support.WrapAndAppend(user, err, errs) + errs = support.WrapAndAppend(qp.User, err, errs) return false } @@ -227,7 +211,7 @@ func IterateAndFilterMessagesForCollections( message, ok := messageItem.(descendable) if !ok { - errs = support.WrapAndAppend(user, errors.New("message iteration failure"), errs) + errs = support.WrapAndAppend(qp.User, errors.New("message iteration failure"), errs) return true } // Saving only messages for the created directories @@ -244,11 +228,8 @@ func IterateAndFilterMessagesForCollections( func IterateFilterFolderDirectoriesForCollections( ctx context.Context, - user string, - scope selectors.ExchangeScope, + qp graph.QueryParams, errs error, - failFast bool, - credentials account.M365Config, collections map[string]*Collection, statusCh chan<- *support.ConnectorOperationStatus, ) func(any) bool { @@ -261,7 +242,7 @@ func IterateFilterFolderDirectoriesForCollections( folder, ok := folderItem.(displayable) if !ok { errs = support.WrapAndAppend( - user, + qp.User, errors.New("unable to transform folderable item"), errs, ) @@ -273,19 +254,19 @@ func IterateFilterFolderDirectoriesForCollections( return true } - if !scope.Matches(selectors.ExchangeMailFolder, *folder.GetDisplayName()) { + if !qp.Scope.Matches(selectors.ExchangeMailFolder, *folder.GetDisplayName()) { return true } directory := *folder.GetId() - service, err = createService(credentials, failFast) + service, err = createService(qp.Credentials, qp.FailFast) if err != nil { errs = support.WrapAndAppend( *folder.GetDisplayName(), errors.Wrap( err, - "unable to create service a folder query service for "+user, + "unable to create service a folder query service for "+qp.User, ), errs, ) @@ -294,8 +275,8 @@ func IterateFilterFolderDirectoriesForCollections( } temp := NewCollection( - user, - []string{credentials.TenantID, user, mailCategory, directory}, + qp.User, + []string{qp.Credentials.TenantID, qp.User, mailCategory, directory}, messages, service, statusCh, diff --git a/src/internal/connector/exchange/service_query.go b/src/internal/connector/exchange/service_query.go index a5d5c04b6..09763e7fd 100644 --- a/src/internal/connector/exchange/service_query.go +++ b/src/internal/connector/exchange/service_query.go @@ -11,8 +11,6 @@ import ( "github.com/alcionai/corso/internal/connector/graph" "github.com/alcionai/corso/internal/connector/support" - "github.com/alcionai/corso/pkg/account" - "github.com/alcionai/corso/pkg/selectors" ) // GraphQuery represents functions which perform exchange-specific queries @@ -126,23 +124,20 @@ func RetrieveMessageDataForUser(gs graph.Service, user, m365ID string) (absser.P func CollectMailFolders( ctx context.Context, - scope selectors.ExchangeScope, - user string, + qp graph.QueryParams, collections map[string]*Collection, - credentials account.M365Config, - failFast bool, statusCh chan<- *support.ConnectorOperationStatus, ) error { - queryService, err := createService(credentials, failFast) + queryService, err := createService(qp.Credentials, qp.FailFast) if err != nil { - return errors.New("unable to create a mail folder query service for " + user) + return errors.New("unable to create a mail folder query service for " + qp.User) } - query, err := GetAllFolderNamesForUser(queryService, user) + query, err := GetAllFolderNamesForUser(queryService, qp.User) if err != nil { return fmt.Errorf( "unable to query mail folder for %s: details: %s", - user, + qp.User, support.ConnectorStackErrorTrace(err), ) } @@ -158,18 +153,15 @@ func CollectMailFolders( callbackFunc := IterateFilterFolderDirectoriesForCollections( ctx, - user, - scope, + qp, err, - failFast, - credentials, collections, statusCh, ) iterateFailure := pageIterator.Iterate(callbackFunc) if iterateFailure != nil { - err = support.WrapAndAppend(user+" iterate failure", iterateFailure, err) + err = support.WrapAndAppend(qp.User+" iterate failure", iterateFailure, err) } return err diff --git a/src/internal/connector/graph/service.go b/src/internal/connector/graph/service.go index d04aa5576..9b28cc6e6 100644 --- a/src/internal/connector/graph/service.go +++ b/src/internal/connector/graph/service.go @@ -1,6 +1,18 @@ package graph -import msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" +import ( + msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" + + "github.com/alcionai/corso/pkg/account" + "github.com/alcionai/corso/pkg/selectors" +) + +type QueryParams struct { + User string + Scope selectors.ExchangeScope + Credentials account.M365Config + FailFast bool +} type Service interface { // Client() returns msgraph Service client that can be used to process and execute diff --git a/src/internal/connector/graph_connector.go b/src/internal/connector/graph_connector.go index 901523a21..fe343cc49 100644 --- a/src/internal/connector/graph_connector.go +++ b/src/internal/connector/graph_connector.go @@ -332,14 +332,20 @@ func (gc *GraphConnector) createCollections( allCollections := make([]*exchange.Collection, 0) // Create collection of ExchangeDataCollection for _, user := range users { + qp := graph.QueryParams{ + User: user, + Scope: scope, + FailFast: gc.failFast, + Credentials: gc.credentials, + } collections := make(map[string]*exchange.Collection) - response, err := query(&gc.graphService, user) + response, err := query(&gc.graphService, qp.User) if err != nil { return nil, errors.Wrapf( err, "user %s M365 query: %s", - user, support.ConnectorStackErrorTrace(err)) + qp.User, support.ConnectorStackErrorTrace(err)) } pageIterator, err := msgraphgocore.NewPageIterator(response, &gc.graphService.adapter, transformer) @@ -350,7 +356,7 @@ func (gc *GraphConnector) createCollections( // callbackFunc iterates through all M365 object target and fills exchange.Collection.jobs[] // with corresponding item M365IDs. New collections are created for each directory. // Each directory used the M365 Identifier. The use of ID stops collisions betweens users - callbackFunc := gIter(ctx, user, scope, errs, gc.failFast, gc.credentials, collections, gc.statusCh) + callbackFunc := gIter(ctx, qp, errs, collections, gc.statusCh) iterateError := pageIterator.Iterate(callbackFunc) if iterateError != nil {