From 27ecdcb269b4381f6ac86b59a0cb7a116c5e59ee Mon Sep 17 00:00:00 2001 From: Vaibhav Kamra Date: Thu, 4 May 2023 18:13:25 -0700 Subject: [PATCH] Parse weburl to construct drive name for OneDrive --- .../connector/onedrive/collections.go | 2 +- src/internal/connector/onedrive/drive.go | 25 ++++++++++ src/internal/connector/onedrive/drive_test.go | 48 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/internal/connector/onedrive/collections.go b/src/internal/connector/onedrive/collections.go index 8594e4a6f..ae461be57 100644 --- a/src/internal/connector/onedrive/collections.go +++ b/src/internal/connector/onedrive/collections.go @@ -305,7 +305,7 @@ func (c *Collections) Get( for _, d := range drives { var ( driveID = ptr.Val(d.GetId()) - driveName = ptr.Val(d.GetName()) + driveName = driveName(d) prevDelta = prevDeltas[driveID] oldPaths = oldPathsByDriveID[driveID] numOldDelta = 0 diff --git a/src/internal/connector/onedrive/drive.go b/src/internal/connector/onedrive/drive.go index 99487c66b..648d6ada8 100644 --- a/src/internal/connector/onedrive/drive.go +++ b/src/internal/connector/onedrive/drive.go @@ -3,6 +3,7 @@ package onedrive import ( "context" "fmt" + "path" "strings" "github.com/alcionai/clues" @@ -26,6 +27,12 @@ const ( nextLinkKey = "@odata.nextLink" itemChildrenRawURLFmt = "https://graph.microsoft.com/v1.0/drives/%s/items/%s/children" itemNotFoundErrorCode = "itemNotFound" + + // https://learn.microsoft.com/en-us/graph/api/resources/drive?view=graph-rest-1.0#properties + // OneDrive personal drives will return personal. OneDrive for Business will return business. + // SharePoint document libraries will return documentLibrary. + oneDriveDriveType = "business" + spDriveDriveType = "documentLibrary" ) // DeltaUpdate holds the results of a current delta token. It normally @@ -343,3 +350,21 @@ func DeleteItem( return nil } + +// driveName returns the drive name for a drive +// For OneDrive - Graph always sets d.Name to `OneDrive` so +// instead we parse the webURL and use the last element +// e.g. if it is "https:///personal/_onmicrosoft_com/Documents", +// we return `Documents` +func driveName(d models.Driveable) string { + dt := ptr.Val(d.GetDriveType()) + switch dt { + case oneDriveDriveType: + if webURL := ptr.Val(d.GetWebUrl()); webURL != "" { + return path.Base(webURL) + } + fallthrough + default: + return ptr.Val(d.GetName()) + } +} diff --git a/src/internal/connector/onedrive/drive_test.go b/src/internal/connector/onedrive/drive_test.go index d2f1a68b6..bd5b404a4 100644 --- a/src/internal/connector/onedrive/drive_test.go +++ b/src/internal/connector/onedrive/drive_test.go @@ -277,6 +277,46 @@ func (suite *OneDriveUnitSuite) TestDrives() { } } +func (suite *OneDriveUnitSuite) Test_driveName() { + _, flush := tester.NewContext() + defer flush() + + webURL := "https:///personal/_onmicrosoft_com/Documents" + + driveOneDrive := models.NewDrive() + driveOneDrive.SetDriveType(ptr.To(oneDriveDriveType)) + driveOneDrive.SetWebUrl(&webURL) + driveOneDrive.SetName(ptr.To("anewdrivename")) + + driveSharePoint := models.NewDrive() + driveSharePoint.SetDriveType(ptr.To(spDriveDriveType)) + driveSharePoint.SetWebUrl(&webURL) + driveSharePoint.SetName(ptr.To("anewdrivename")) + + table := []struct { + name string + d models.Driveable + expected string + }{ + { + "OneDrive", + driveOneDrive, + "Documents", + }, + { + "Sharepoint", + driveSharePoint, + "anewdrivename", + }, + } + for _, test := range table { + suite.Run(test.name, func() { + t := suite.T() + assert.Equal(t, test.expected, driveName(test.d)) + }) + } +} + // Integration tests type OneDriveSuite struct { @@ -315,6 +355,14 @@ func (suite *OneDriveSuite) TestCreateGetDeleteFolder() { require.NoError(t, err, clues.ToCore(err)) require.NotEmpty(t, drives) + // Verify drive type/name + for _, d := range drives { + assert.Equal(t, oneDriveDriveType, ptr.Val(d.GetDriveType())) + // Graph sets all OneDrive drive names to `OneDrive` + // Verify we're using the webURL instead + assert.NotEqual(t, "OneDrive", driveName(d)) + } + // TODO: Verify the intended drive driveID := ptr.Val(drives[0].GetId())