From 5184920b52cf40dd90c88dbbc74e2605b99793ac Mon Sep 17 00:00:00 2001 From: Keepers Date: Thu, 15 Jun 2023 12:28:31 -0600 Subject: [PATCH] add foldermatcher to handler (#3565) Missed adding the foldermatcher to the handlers for onedrive and sharepoint during the api refactor. --- #### Does this PR need a docs update or release note? - [x] :no_entry: No #### Type of change - [x] :broom: Tech Debt/Cleanup #### Issue(s) * #1996 #### Test Plan - [x] :zap: Unit test - [x] :green_heart: E2E --- src/internal/m365/onedrive/backup.go | 17 +----------- src/internal/m365/onedrive/collections.go | 26 +++++++++---------- .../m365/onedrive/collections_test.go | 17 +++++------- src/internal/m365/onedrive/handlers.go | 4 +++ .../m365/onedrive/item_collector_test.go | 20 ++++---------- src/internal/m365/onedrive/item_handler.go | 12 ++++++++- src/internal/m365/onedrive/item_test.go | 6 ++++- src/internal/m365/onedrive/mock/handlers.go | 8 ++++++ src/internal/m365/sharepoint/backup.go | 15 +---------- src/internal/m365/sharepoint/backup_test.go | 15 +---------- .../m365/sharepoint/library_handler.go | 12 ++++++++- 11 files changed, 66 insertions(+), 86 deletions(-) diff --git a/src/internal/m365/onedrive/backup.go b/src/internal/m365/onedrive/backup.go index f1a47e9e1..eaedf4284 100644 --- a/src/internal/m365/onedrive/backup.go +++ b/src/internal/m365/onedrive/backup.go @@ -19,20 +19,6 @@ import ( "github.com/alcionai/corso/src/pkg/services/m365/api" ) -type odFolderMatcher struct { - scope selectors.OneDriveScope -} - -func (fm odFolderMatcher) IsAny() bool { - return fm.scope.IsAny(selectors.OneDriveFolder) -} - -func (fm odFolderMatcher) Matches(dir string) bool { - return fm.scope.Matches(selectors.OneDriveFolder, dir) -} - -// ProduceBackupCollections returns a set of DataCollection which represents the OneDrive data -// for the specified user func ProduceBackupCollections( ctx context.Context, ac api.Client, @@ -68,10 +54,9 @@ func ProduceBackupCollections( logger.Ctx(ctx).Debug("creating OneDrive collections") nc := NewCollections( - &itemBackupHandler{ac.Drives()}, + &itemBackupHandler{ac.Drives(), scope}, tenant, user.ID(), - odFolderMatcher{scope}, su, ctrlOpts) diff --git a/src/internal/m365/onedrive/collections.go b/src/internal/m365/onedrive/collections.go index 24371b22c..7122a2361 100644 --- a/src/internal/m365/onedrive/collections.go +++ b/src/internal/m365/onedrive/collections.go @@ -42,11 +42,6 @@ const ( const restrictedDirectory = "Site Pages" -type folderMatcher interface { - IsAny() bool - Matches(string) bool -} - // Collections is used to retrieve drive data for a // resource owner, which can be either a user or a sharepoint site. type Collections struct { @@ -54,7 +49,7 @@ type Collections struct { tenantID string resourceOwner string - matcher folderMatcher + statusUpdater support.StatusUpdater ctrl control.Options @@ -74,7 +69,6 @@ func NewCollections( bh BackupHandler, tenantID string, resourceOwner string, - matcher folderMatcher, statusUpdater support.StatusUpdater, ctrlOpts control.Options, ) *Collections { @@ -82,7 +76,6 @@ func NewCollections( handler: bh, tenantID: tenantID, resourceOwner: resourceOwner, - matcher: matcher, CollectionMap: map[string]map[string]*Collection{}, statusUpdater: statusUpdater, ctrl: ctrlOpts, @@ -697,7 +690,7 @@ func (c *Collections) UpdateCollections( } // Skip items that don't match the folder selectors we were given. - if shouldSkipDrive(ctx, collectionPath, c.matcher, driveName) { + if shouldSkip(ctx, collectionPath, c.handler, driveName) { logger.Ctx(ictx).Debugw("path not selected", "skipped_path", collectionPath.String()) continue } @@ -827,12 +820,17 @@ func (c *Collections) UpdateCollections( return el.Failure() } -func shouldSkipDrive(ctx context.Context, drivePath path.Path, m folderMatcher, driveName string) bool { - return !includePath(ctx, m, drivePath) || +type dirScopeChecker interface { + IsAllPass() bool + IncludesDir(dir string) bool +} + +func shouldSkip(ctx context.Context, drivePath path.Path, dsc dirScopeChecker, driveName string) bool { + return !includePath(ctx, dsc, drivePath) || (drivePath.Category() == path.LibrariesCategory && restrictedDirectory == driveName) } -func includePath(ctx context.Context, m folderMatcher, folderPath path.Path) bool { +func includePath(ctx context.Context, dsc dirScopeChecker, folderPath path.Path) bool { // Check if the folder is allowed by the scope. pb, err := path.GetDriveFolderPath(folderPath) if err != nil { @@ -842,11 +840,11 @@ func includePath(ctx context.Context, m folderMatcher, folderPath path.Path) boo // Hack for the edge case where we're looking at the root folder and can // select any folder. Right now the root folder has an empty folder path. - if len(pb.Elements()) == 0 && m.IsAny() { + if len(pb.Elements()) == 0 && dsc.IsAllPass() { return true } - return m.Matches(pb.String()) + return dsc.IncludesDir(pb.String()) } func updatePath(paths map[string]string, id, newPath string) { diff --git a/src/internal/m365/onedrive/collections_test.go b/src/internal/m365/onedrive/collections_test.go index d18ad0f4f..bc64875f4 100644 --- a/src/internal/m365/onedrive/collections_test.go +++ b/src/internal/m365/onedrive/collections_test.go @@ -743,10 +743,9 @@ func (suite *OneDriveCollectionsUnitSuite) TestUpdateCollections() { maps.Copy(outputFolderMap, tt.inputFolderMap) c := NewCollections( - &itemBackupHandler{api.Drives{}}, + &itemBackupHandler{api.Drives{}, tt.scope}, tenant, user, - testFolderMatcher{tt.scope}, nil, control.Options{ToggleFeatures: control.Toggles{}}) @@ -1238,13 +1237,12 @@ func (p *mockItemPager) ValuesIn(api.DeltaPageLinker) ([]models.DriveItemable, e func (suite *OneDriveCollectionsUnitSuite) TestGet() { var ( - anyFolder = (&selectors.OneDriveBackup{}).Folders(selectors.Any())[0] - tenant = "a-tenant" - user = "a-user" - empty = "" - next = "next" - delta = "delta1" - delta2 = "delta2" + tenant = "a-tenant" + user = "a-user" + empty = "" + next = "next" + delta = "delta1" + delta2 = "delta2" ) metadataPath, err := path.Builder{}.ToServiceCategoryMetadataPath( @@ -2345,7 +2343,6 @@ func (suite *OneDriveCollectionsUnitSuite) TestGet() { mbh, tenant, user, - testFolderMatcher{anyFolder}, func(*support.ControllerOperationStatus) {}, control.Options{ToggleFeatures: control.Toggles{}}) diff --git a/src/internal/m365/onedrive/handlers.go b/src/internal/m365/onedrive/handlers.go index 78ea162ff..079bcd727 100644 --- a/src/internal/m365/onedrive/handlers.go +++ b/src/internal/m365/onedrive/handlers.go @@ -54,6 +54,10 @@ type BackupHandler interface { // provided path. FormatDisplayPath(driveName string, parentPath *path.Builder) string NewLocationIDer(driveID string, elems ...string) details.LocationIDer + + // scope wrapper funcs + IsAllPass() bool + IncludesDir(dir string) bool } type GetItemPermissioner interface { diff --git a/src/internal/m365/onedrive/item_collector_test.go b/src/internal/m365/onedrive/item_collector_test.go index 65e9bf5fe..a935cc802 100644 --- a/src/internal/m365/onedrive/item_collector_test.go +++ b/src/internal/m365/onedrive/item_collector_test.go @@ -390,7 +390,10 @@ func (suite *OneDriveIntgSuite) TestCreateGetDeleteFolder() { for _, test := range table { suite.Run(test.name, func() { t := suite.T() - bh := itemBackupHandler{suite.ac.Drives()} + bh := itemBackupHandler{ + suite.ac.Drives(), + (&selectors.OneDriveBackup{}).Folders(selectors.Any())[0], + } pager := suite.ac.Drives().NewUserDrivePager(suite.userID, nil) ctx, flush := tester.NewContext(t) @@ -415,18 +418,6 @@ func (suite *OneDriveIntgSuite) TestCreateGetDeleteFolder() { } } -type testFolderMatcher struct { - scope selectors.OneDriveScope -} - -func (fm testFolderMatcher) IsAny() bool { - return fm.scope.IsAny(selectors.OneDriveFolder) -} - -func (fm testFolderMatcher) Matches(p string) bool { - return fm.scope.Matches(selectors.OneDriveFolder, p) -} - func (suite *OneDriveIntgSuite) TestOneDriveNewCollections() { creds, err := tester.NewM365Account(suite.T()).M365Config() require.NoError(suite.T(), err, clues.ToCore(err)) @@ -459,10 +450,9 @@ func (suite *OneDriveIntgSuite) TestOneDriveNewCollections() { ) colls := NewCollections( - &itemBackupHandler{suite.ac.Drives()}, + &itemBackupHandler{suite.ac.Drives(), scope}, creds.AzureTenantID, test.user, - testFolderMatcher{scope}, service.updateStatus, control.Options{ ToggleFeatures: control.Toggles{}, diff --git a/src/internal/m365/onedrive/item_handler.go b/src/internal/m365/onedrive/item_handler.go index a95791237..904a20bd6 100644 --- a/src/internal/m365/onedrive/item_handler.go +++ b/src/internal/m365/onedrive/item_handler.go @@ -12,6 +12,7 @@ import ( odConsts "github.com/alcionai/corso/src/internal/m365/onedrive/consts" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/path" + "github.com/alcionai/corso/src/pkg/selectors" "github.com/alcionai/corso/src/pkg/services/m365/api" ) @@ -22,7 +23,8 @@ import ( var _ BackupHandler = &itemBackupHandler{} type itemBackupHandler struct { - ac api.Drives + ac api.Drives + scope selectors.OneDriveScope } func (h itemBackupHandler) Get( @@ -108,6 +110,14 @@ func (h itemBackupHandler) GetItem( return h.ac.GetItem(ctx, driveID, itemID) } +func (h itemBackupHandler) IsAllPass() bool { + return h.scope.IsAny(selectors.OneDriveFolder) +} + +func (h itemBackupHandler) IncludesDir(dir string) bool { + return h.scope.Matches(selectors.OneDriveFolder, dir) +} + // --------------------------------------------------------------------------- // Restore // --------------------------------------------------------------------------- diff --git a/src/internal/m365/onedrive/item_test.go b/src/internal/m365/onedrive/item_test.go index 5fd36d345..d862e6edf 100644 --- a/src/internal/m365/onedrive/item_test.go +++ b/src/internal/m365/onedrive/item_test.go @@ -17,6 +17,7 @@ import ( "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/pkg/control/testdata" "github.com/alcionai/corso/src/pkg/fault" + "github.com/alcionai/corso/src/pkg/selectors" "github.com/alcionai/corso/src/pkg/services/m365/api" ) @@ -118,7 +119,10 @@ func (suite *ItemIntegrationSuite) TestItemReader_oneDrive() { suite.user, suite.userDriveID) - bh := itemBackupHandler{suite.service.ac.Drives()} + bh := itemBackupHandler{ + suite.service.ac.Drives(), + (&selectors.OneDriveBackup{}).Folders(selectors.Any())[0], + } // Read data for the file itemData, err := downloadItem(ctx, bh, driveItem) diff --git a/src/internal/m365/onedrive/mock/handlers.go b/src/internal/m365/onedrive/mock/handlers.go index 0b48ffa6c..bafda5022 100644 --- a/src/internal/m365/onedrive/mock/handlers.go +++ b/src/internal/m365/onedrive/mock/handlers.go @@ -184,6 +184,14 @@ var defaultSharePointLocationIDer = func(driveID string, elems ...string) detail return details.NewSharePointLocationIDer(driveID, elems...) } +func (h BackupHandler) IsAllPass() bool { + return true +} + +func (h BackupHandler) IncludesDir(string) bool { + return true +} + // --------------------------------------------------------------------------- // Get Itemer // --------------------------------------------------------------------------- diff --git a/src/internal/m365/sharepoint/backup.go b/src/internal/m365/sharepoint/backup.go index 79765bfdb..0596707d3 100644 --- a/src/internal/m365/sharepoint/backup.go +++ b/src/internal/m365/sharepoint/backup.go @@ -220,10 +220,9 @@ func collectLibraries( var ( collections = []data.BackupCollection{} colls = onedrive.NewCollections( - &libraryBackupHandler{ad}, + &libraryBackupHandler{ad, scope}, tenantID, site.ID(), - folderMatcher{scope}, updater.UpdateStatus, ctrlOpts) ) @@ -301,15 +300,3 @@ func collectPages( return spcs, el.Failure() } - -type folderMatcher struct { - scope selectors.SharePointScope -} - -func (fm folderMatcher) IsAny() bool { - return fm.scope.IsAny(selectors.SharePointLibraryFolder) -} - -func (fm folderMatcher) Matches(dir string) bool { - return fm.scope.Matches(selectors.SharePointLibraryFolder, dir) -} diff --git a/src/internal/m365/sharepoint/backup_test.go b/src/internal/m365/sharepoint/backup_test.go index d36e40377..ecffd5ecb 100644 --- a/src/internal/m365/sharepoint/backup_test.go +++ b/src/internal/m365/sharepoint/backup_test.go @@ -29,18 +29,6 @@ var testBaseDrivePath = path.Builder{}.Append( "driveID1", odConsts.RootPathDir) -type testFolderMatcher struct { - scope selectors.SharePointScope -} - -func (fm testFolderMatcher) IsAny() bool { - return fm.scope.IsAny(selectors.SharePointLibraryFolder) -} - -func (fm testFolderMatcher) Matches(p string) bool { - return fm.scope.Matches(selectors.SharePointLibraryFolder, p) -} - // --------------------------------------------------------------------------- // tests // --------------------------------------------------------------------------- @@ -113,10 +101,9 @@ func (suite *LibrariesBackupUnitSuite) TestUpdateCollections() { ) c := onedrive.NewCollections( - &libraryBackupHandler{api.Drives{}}, + &libraryBackupHandler{api.Drives{}, test.scope}, tenantID, site, - testFolderMatcher{test.scope}, nil, control.Defaults()) diff --git a/src/internal/m365/sharepoint/library_handler.go b/src/internal/m365/sharepoint/library_handler.go index 4ea9e1e92..eff8a9bff 100644 --- a/src/internal/m365/sharepoint/library_handler.go +++ b/src/internal/m365/sharepoint/library_handler.go @@ -13,13 +13,15 @@ import ( odConsts "github.com/alcionai/corso/src/internal/m365/onedrive/consts" "github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/path" + "github.com/alcionai/corso/src/pkg/selectors" "github.com/alcionai/corso/src/pkg/services/m365/api" ) var _ onedrive.BackupHandler = &libraryBackupHandler{} type libraryBackupHandler struct { - ac api.Drives + ac api.Drives + scope selectors.SharePointScope } func (h libraryBackupHandler) Get( @@ -139,6 +141,14 @@ func (h libraryBackupHandler) GetItem( return h.ac.GetItem(ctx, driveID, itemID) } +func (h libraryBackupHandler) IsAllPass() bool { + return h.scope.IsAny(selectors.SharePointLibraryFolder) +} + +func (h libraryBackupHandler) IncludesDir(dir string) bool { + return h.scope.Matches(selectors.SharePointLibraryFolder, dir) +} + // --------------------------------------------------------------------------- // Restore // ---------------------------------------------------------------------------