From 33fd95c83d5cb444954347ac264d274d6c84dbb8 Mon Sep 17 00:00:00 2001 From: Itzaprado <132521881+Itzaprado@users.noreply.github.com> Date: Mon, 3 Mar 2025 14:45:25 -0600 Subject: [PATCH] fix: add logic to getDrives for paginated drives (#654) [ ] change getDrives logic to be silimar as the pagedFileList to obtain more than 50 drives listed --- lib/actions/google/drive/google_drive.d.ts | 5 +--- lib/actions/google/drive/google_drive.js | 22 +++++++++-------- src/actions/google/drive/google_drive.ts | 28 ++++++++++++++-------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/lib/actions/google/drive/google_drive.d.ts b/lib/actions/google/drive/google_drive.d.ts index 00da62178..8d9ea2371 100644 --- a/lib/actions/google/drive/google_drive.d.ts +++ b/lib/actions/google/drive/google_drive.d.ts @@ -29,10 +29,7 @@ export declare class GoogleDriveAction extends Hub.OAuthAction { oauthCheck(request: Hub.ActionRequest): Promise; oauth2Client(redirectUri: string | undefined): OAuth2Client; sendData(filename: string, request: Hub.ActionRequest, drive: Drive): Promise>; - getDrives(drive: Drive): Promise<{ - name: string; - label: string; - }[]>; + getDrives(drive: Drive, accumulatedFolders: drive_v3.Schema$Drive[], response: GaxiosResponse): Promise; getMimeType(request: Hub.ActionRequest): string | undefined; sanitizeGaxiosError(err: any): void; protected getAccessTokenCredentialsFromCode(redirect: string, code: string): Promise; diff --git a/lib/actions/google/drive/google_drive.js b/lib/actions/google/drive/google_drive.js index 3972357e4..daa2b082f 100644 --- a/lib/actions/google/drive/google_drive.js +++ b/lib/actions/google/drive/google_drive.js @@ -100,7 +100,10 @@ class GoogleDriveAction extends Hub.OAuthAction { throw "Domain Verification Failed"; }); const drive = await this.driveClientFromRequest(stateJson.redirect, stateJson.tokens); - const driveSelections = await this.getDrives(drive); + const paginatedDrives = await this.getDrives(drive, [], await drive.drives.list({ pageSize: 50 })); + const driveSelections = paginatedDrives.filter((_drive) => (!(_drive.id === undefined) && !(_drive.name === undefined))) + .map((folder) => ({ name: folder.id, label: folder.name })); + driveSelections.unshift({ name: "mydrive", label: "My Drive" }); form.fields.push({ description: "Google Drive where your file will be saved", label: "Select Drive to save file", @@ -291,15 +294,14 @@ class GoogleDriveAction extends Hub.OAuthAction { }); }); } - async getDrives(drive) { - const driveList = [{ name: "mydrive", label: "My Drive" }]; - const drives = await drive.drives.list({ - pageSize: 50, - }); - if (drives.data.drives) { - drives.data.drives.forEach((d) => { - driveList.push({ name: d.id, label: d.name }); - }); + async getDrives(drive, accumulatedFolders, response) { + const driveList = accumulatedFolders.concat(response.data.drives); + if (response.data.nextPageToken) { + const pageOptions = { + pageSize: 50, + pageToken: response.data.nextPageToken, + }; + return this.getDrives(drive, driveList, await drive.drives.list(pageOptions)); } return driveList; } diff --git a/src/actions/google/drive/google_drive.ts b/src/actions/google/drive/google_drive.ts index 17e2f7262..d39ee0cfa 100644 --- a/src/actions/google/drive/google_drive.ts +++ b/src/actions/google/drive/google_drive.ts @@ -126,7 +126,12 @@ export class GoogleDriveAction extends Hub.OAuthAction { const drive = await this.driveClientFromRequest(stateJson.redirect, stateJson.tokens) - const driveSelections = await this.getDrives(drive) + const paginatedDrives = await this.getDrives(drive, [], await drive.drives.list({pageSize: 50})) + const driveSelections = paginatedDrives.filter((_drive) => ( + !(_drive.id === undefined) && !(_drive.name === undefined))) + .map((folder) => ({name: folder.id!, label: folder.name!})) + driveSelections.unshift({name: "mydrive", label: "My Drive"}) + form.fields.push({ description: "Google Drive where your file will be saved", label: "Select Drive to save file", @@ -338,16 +343,19 @@ export class GoogleDriveAction extends Hub.OAuthAction { }) } - async getDrives(drive: Drive) { - const driveList = [{name: "mydrive", label: "My Drive"}] - const drives: GaxiosResponse = await drive.drives.list({ - pageSize: 50, - }) - if (drives.data.drives) { - drives.data.drives.forEach((d) => { - driveList.push({name: d.id!, label: d.name!}) - }) + async getDrives(drive: Drive, + accumulatedFolders: drive_v3.Schema$Drive[], + response: GaxiosResponse): Promise { + const driveList = accumulatedFolders.concat(response.data.drives!) + + if (response.data.nextPageToken) { + const pageOptions = { + pageSize: 50, + pageToken: response.data.nextPageToken, + } + return this.getDrives(drive, driveList, await drive.drives.list(pageOptions)) } + return driveList }