Apply wsl linter to onedrive package (#911)

## Description

Finish enabling wsl linter on Corso repo by removing the exception for onedrive package

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [x] 🐹 Trivial/Minor

## Issue(s)

* closes #481

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2022-09-20 13:02:35 -07:00 committed by GitHub
parent 714e7e3a9b
commit 7850272a5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 8 deletions

View File

@ -6,11 +6,11 @@ linters:
- gci - gci
- gofmt - gofmt
- gofumpt - gofumpt
- errcheck
- lll - lll
- misspell - misspell
- revive - revive
- wsl - wsl
- errcheck
linters-settings: linters-settings:
gci: gci:
@ -80,6 +80,3 @@ issues:
linters: linters:
- revive - revive
text: "import-shadowing:.*'suite' shadows" text: "import-shadowing:.*'suite' shadows"
# Temporarily skip linting wsl on `connector` package until fixes are merged.
- path: internal/connector/onedrive
linters: wsl

View File

@ -64,6 +64,7 @@ func NewCollection(
} }
// Allows tests to set a mock populator // Allows tests to set a mock populator
c.itemReader = driveItemReader c.itemReader = driveItemReader
return c return c
} }
@ -105,16 +106,21 @@ func (od *Item) Info() details.ItemInfo {
// populateItems iterates through items added to the collection // populateItems iterates through items added to the collection
// and uses the collection `itemReader` to read the item // and uses the collection `itemReader` to read the item
func (oc *Collection) populateItems(ctx context.Context) { func (oc *Collection) populateItems(ctx context.Context) {
var errs error var (
itemsRead := 0 errs error
itemsRead = 0
)
for _, itemID := range oc.driveItemIDs { for _, itemID := range oc.driveItemIDs {
// Read the item // Read the item
itemName, itemData, err := oc.itemReader(ctx, oc.service, oc.driveID, itemID) itemName, itemData, err := oc.itemReader(ctx, oc.service, oc.driveID, itemID)
if err != nil { if err != nil {
errs = support.WrapAndAppendf(itemID, err, errs) errs = support.WrapAndAppendf(itemID, err, errs)
if oc.service.ErrPolicy() { if oc.service.ErrPolicy() {
break break
} }
continue continue
} }
// Item read successfully, add to collection // Item read successfully, add to collection
@ -129,7 +135,9 @@ func (oc *Collection) populateItems(ctx context.Context) {
}, },
} }
} }
close(oc.data) close(oc.data)
status := support.CreateStatus(ctx, support.Backup, status := support.CreateStatus(ctx, support.Backup,
len(oc.driveItemIDs), // items to read len(oc.driveItemIDs), // items to read
itemsRead, // items read successfully itemsRead, // items read successfully

View File

@ -49,6 +49,7 @@ func (suite *OneDriveCollectionSuite) testStatusUpdater(
return func(s *support.ConnectorOperationStatus) { return func(s *support.ConnectorOperationStatus) {
suite.T().Logf("Update status %v, count %d, success %d", s, s.ObjectCount, s.Successful) suite.T().Logf("Update status %v, count %d, success %d", s, s.ObjectCount, s.Successful)
*statusToUpdate = *s *statusToUpdate = *s
wg.Done() wg.Done()
} }
} }
@ -78,11 +79,15 @@ func (suite *OneDriveCollectionSuite) TestOneDriveCollection() {
// Read items from the collection // Read items from the collection
wg.Add(1) wg.Add(1)
readItems := []data.Stream{} readItems := []data.Stream{}
for item := range coll.Items() { for item := range coll.Items() {
readItems = append(readItems, item) readItems = append(readItems, item)
} }
wg.Wait() wg.Wait()
// Expect only 1 item // Expect only 1 item
require.Len(t, readItems, 1) require.Len(t, readItems, 1)
require.Equal(t, 1, collStatus.ObjectCount) require.Equal(t, 1, collStatus.ObjectCount)
@ -105,8 +110,8 @@ func (suite *OneDriveCollectionSuite) TestOneDriveCollection() {
func (suite *OneDriveCollectionSuite) TestOneDriveCollectionReadError() { func (suite *OneDriveCollectionSuite) TestOneDriveCollectionReadError() {
t := suite.T() t := suite.T()
wg := sync.WaitGroup{}
collStatus := support.ConnectorOperationStatus{} collStatus := support.ConnectorOperationStatus{}
wg := sync.WaitGroup{}
wg.Add(1) wg.Add(1)
folderPath, err := getCanonicalPath("folderPath", "a-tenant", "a-user") folderPath, err := getCanonicalPath("folderPath", "a-tenant", "a-user")

View File

@ -90,10 +90,12 @@ func (c *Collections) updateCollections(ctx context.Context, driveID string, ite
if err != nil { if err != nil {
return err return err
} }
if item.GetRoot() != nil { if item.GetRoot() != nil {
// Skip the root item // Skip the root item
continue continue
} }
if item.GetParentReference() == nil || item.GetParentReference().GetPath() == nil { if item.GetParentReference() == nil || item.GetParentReference().GetPath() == nil {
return errors.Errorf("item does not have a parent reference. item name : %s", *item.GetName()) return errors.Errorf("item does not have a parent reference. item name : %s", *item.GetName())
} }
@ -116,6 +118,7 @@ func (c *Collections) updateCollections(ctx context.Context, driveID string, ite
c.statusUpdater, c.statusUpdater,
) )
} }
switch { switch {
case item.GetFolder() != nil, item.GetPackage() != nil: case item.GetFolder() != nil, item.GetPackage() != nil:
// For folders and packages we also create a collection to represent those // For folders and packages we also create a collection to represent those
@ -148,6 +151,7 @@ func (c *Collections) updateCollections(ctx context.Context, driveID string, ite
return errors.Errorf("item type not supported. item name : %s", *item.GetName()) return errors.Errorf("item type not supported. item name : %s", *item.GetName())
} }
} }
return nil return nil
} }
@ -162,6 +166,8 @@ func (c *Collections) stats(item models.DriveItemable) error {
default: default:
return errors.Errorf("item type not supported. item name : %s", *item.GetName()) return errors.Errorf("item type not supported. item name : %s", *item.GetName())
} }
c.numItems++ c.numItems++
return nil return nil
} }

View File

@ -123,6 +123,7 @@ func (suite *OneDriveCollectionsSuite) TestUpdateCollections() {
expectedPackageCount: 1, expectedPackageCount: 1,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
suite.T().Run(tt.testCase, func(t *testing.T) { suite.T().Run(tt.testCase, func(t *testing.T) {
c := NewCollections(tenant, user, &MockGraphService{}, nil) c := NewCollections(tenant, user, &MockGraphService{}, nil)
@ -157,6 +158,7 @@ func driveItem(name string, path string, isFile, isFolder, isPackage bool) model
case isPackage: case isPackage:
item.SetPackage(models.NewPackage_escaped()) item.SetPackage(models.NewPackage_escaped())
} }
return item return item
} }

View File

@ -30,6 +30,7 @@ func drives(ctx context.Context, service graph.Service, user string) ([]models.D
return nil, errors.Wrapf(err, "failed to retrieve user drives. user: %s, details: %s", return nil, errors.Wrapf(err, "failed to retrieve user drives. user: %s, details: %s",
user, support.ConnectorStackErrorTrace(err)) user, support.ConnectorStackErrorTrace(err))
} }
logger.Ctx(ctx).Debugf("Found %d drives for user %s", len(r.GetValue()), user) logger.Ctx(ctx).Debugf("Found %d drives for user %s", len(r.GetValue()), user)
return r.GetValue(), nil return r.GetValue(), nil
@ -50,6 +51,7 @@ func collectItems(
// https://docs.microsoft.com/en-us/graph/api/driveitem-delta? // https://docs.microsoft.com/en-us/graph/api/driveitem-delta?
// view=graph-rest-1.0&tabs=http#example-4-retrieving-delta-results-using-a-timestamp // view=graph-rest-1.0&tabs=http#example-4-retrieving-delta-results-using-a-timestamp
builder := service.Client().DrivesById(driveID).Root().Delta() builder := service.Client().DrivesById(driveID).Root().Delta()
for { for {
r, err := builder.Get() r, err := builder.Get()
if err != nil { if err != nil {
@ -69,10 +71,12 @@ func collectItems(
if _, found := r.GetAdditionalData()[nextLinkKey]; !found { if _, found := r.GetAdditionalData()[nextLinkKey]; !found {
break break
} }
nextLink := r.GetAdditionalData()[nextLinkKey].(*string) nextLink := r.GetAdditionalData()[nextLinkKey].(*string)
logger.Ctx(ctx).Debugf("Found %s nextLink", *nextLink) logger.Ctx(ctx).Debugf("Found %s nextLink", *nextLink)
builder = delta.NewDeltaRequestBuilder(*nextLink, service.Adapter()) builder = delta.NewDeltaRequestBuilder(*nextLink, service.Adapter())
} }
return nil return nil
} }

View File

@ -44,6 +44,7 @@ func driveItemReader(
return "", nil, errors.Errorf("file does not have a download URL. ID: %s, %#v", return "", nil, errors.Errorf("file does not have a download URL. ID: %s, %#v",
itemID, item.GetAdditionalData()) itemID, item.GetAdditionalData())
} }
downloadURL := item.GetAdditionalData()[downloadURLKey].(*string) downloadURL := item.GetAdditionalData()[downloadURLKey].(*string)
// TODO: We should use the `msgraphgocore` http client which has the right // TODO: We should use the `msgraphgocore` http client which has the right

View File

@ -41,6 +41,7 @@ func TestItemIntegrationSuite(t *testing.T) {
); err != nil { ); err != nil {
t.Skip(err) t.Skip(err)
} }
suite.Run(t, new(ItemIntegrationSuite)) suite.Run(t, new(ItemIntegrationSuite))
} }
@ -85,6 +86,7 @@ func (suite *ItemIntegrationSuite) TestItemReader() {
break break
} }
} }
return nil return nil
} }
err = collectItems(ctx, suite, driveID, itemCollector) err = collectItems(ctx, suite, driveID, itemCollector)
@ -104,6 +106,7 @@ func (suite *ItemIntegrationSuite) TestItemReader() {
name, itemData, err := driveItemReader(ctx, suite, driveID, driveItemID) name, itemData, err := driveItemReader(ctx, suite, driveID, driveItemID)
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
require.NotEmpty(suite.T(), name) require.NotEmpty(suite.T(), name)
size, err := io.Copy(io.Discard, itemData) size, err := io.Copy(io.Discard, itemData)
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
require.NotZero(suite.T(), size) require.NotZero(suite.T(), size)

View File

@ -56,7 +56,6 @@ func RestoreCollections(ctx context.Context, service graph.Service, dcs []data.C
// Iterate through the data collections and restore the contents of each // Iterate through the data collections and restore the contents of each
for _, dc := range dcs { for _, dc := range dcs {
directory := dc.FullPath() directory := dc.FullPath()
drivePath, err := toOneDrivePath(directory) drivePath, err := toOneDrivePath(directory)
@ -86,6 +85,7 @@ func RestoreCollections(ctx context.Context, service graph.Service, dcs []data.C
// Restore items from the collection // Restore items from the collection
exit := false exit := false
items := dc.Items() items := dc.Items()
for !exit { for !exit {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -123,6 +123,7 @@ func createRestoreFolders(ctx context.Context, service graph.Service, driveID st
support.ConnectorStackErrorTrace(err), support.ConnectorStackErrorTrace(err),
) )
} }
logger.Ctx(ctx).Debugf("Found Root for Drive %s with ID %s", driveID, *driveRoot.GetId()) logger.Ctx(ctx).Debugf("Found Root for Drive %s with ID %s", driveID, *driveRoot.GetId())
parentFolderID := *driveRoot.GetId() parentFolderID := *driveRoot.GetId()
@ -131,8 +132,10 @@ func createRestoreFolders(ctx context.Context, service graph.Service, driveID st
if err == nil { if err == nil {
parentFolderID = *folderItem.GetId() parentFolderID = *folderItem.GetId()
logger.Ctx(ctx).Debugf("Found %s with ID %s", folder, parentFolderID) logger.Ctx(ctx).Debugf("Found %s with ID %s", folder, parentFolderID)
continue continue
} }
if err != errFolderNotFound { if err != errFolderNotFound {
return "", errors.Wrapf(err, "folder %s not found in drive(%s) parentFolder(%s)", folder, driveID, parentFolderID) return "", errors.Wrapf(err, "folder %s not found in drive(%s) parentFolder(%s)", folder, driveID, parentFolderID)
} }