-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[ACTION] Google Drive - Polling-based components as an alternative to webhook-based ones #19126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jcortes
wants to merge
1
commit into
master
Choose a base branch
from
google-drive-polling-sources-alternative-to-webhook-sources
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
components/google_drive/sources/new-files-instant-polling/new-files-instant-polling.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; | ||
| import googleDrive from "../../google_drive.app.mjs"; | ||
| import { getListFilesOpts } from "../../common/utils.mjs"; | ||
| import { GOOGLE_DRIVE_FOLDER_MIME_TYPE } from "../../common/constants.mjs"; | ||
| import sampleEmit from "./test-event.mjs"; | ||
|
|
||
| export default { | ||
| key: "google_drive-new-files-instant-polling", | ||
| name: "New Files (Polling)", | ||
| description: "Emit new event when a new file is added in your linked Google Drive", | ||
| version: "0.0.1", | ||
| type: "source", | ||
| dedupe: "unique", | ||
| props: { | ||
| googleDrive, | ||
| db: "$.service.db", | ||
| timer: { | ||
| type: "$.interface.timer", | ||
| default: { | ||
| intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, | ||
| }, | ||
| }, | ||
| drive: { | ||
| propDefinition: [ | ||
| googleDrive, | ||
| "watchedDrive", | ||
| ], | ||
| description: "Defaults to My Drive. To select a [Shared Drive](https://support.google.com/a/users/answer/9310351) instead, select it from this list.", | ||
| optional: false, | ||
| }, | ||
| folders: { | ||
| propDefinition: [ | ||
| googleDrive, | ||
| "folderId", | ||
| ({ drive }) => ({ | ||
| drive, | ||
| baseOpts: { | ||
| q: `mimeType = '${GOOGLE_DRIVE_FOLDER_MIME_TYPE}' and trashed = false`, | ||
| }, | ||
| }), | ||
| ], | ||
| type: "string[]", | ||
| label: "Folders", | ||
| description: "The specific folder(s) to watch for new files. Leave blank to watch all files in the Drive.", | ||
| optional: true, | ||
| default: [], | ||
| }, | ||
| }, | ||
| hooks: { | ||
| async deploy() { | ||
| // Get initial page token for change tracking | ||
| const driveId = this.getDriveId(); | ||
| const startPageToken = await this.googleDrive.getPageToken(driveId); | ||
| this._setPageToken(startPageToken); | ||
|
|
||
| this._setLastRunTimestamp(Date.now()); | ||
|
|
||
| // Emit sample files from the last 30 days | ||
| const daysAgo = new Date(); | ||
| daysAgo.setDate(daysAgo.getDate() - 30); | ||
| const timeString = daysAgo.toISOString(); | ||
|
|
||
| const args = this.getListFilesOpts({ | ||
| q: `mimeType != "application/vnd.google-apps.folder" and createdTime > "${timeString}" and trashed = false`, | ||
| orderBy: "createdTime desc", | ||
| fields: "*", | ||
| pageSize: 5, | ||
| }); | ||
|
|
||
| const { files } = await this.googleDrive.listFilesInPage(null, args); | ||
|
|
||
| for (const file of files) { | ||
| if (this.shouldProcess(file)) { | ||
| await this.emitFile(file); | ||
| } | ||
| } | ||
| }, | ||
| }, | ||
| methods: { | ||
| _getPageToken() { | ||
| return this.db.get("pageToken"); | ||
| }, | ||
| _setPageToken(pageToken) { | ||
| this.db.set("pageToken", pageToken); | ||
| }, | ||
| _getLastRunTimestamp() { | ||
| return this.db.get("lastRunTimestamp"); | ||
| }, | ||
| _setLastRunTimestamp(timestamp) { | ||
| this.db.set("lastRunTimestamp", timestamp); | ||
| }, | ||
jcortes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| getDriveId(drive = this.drive) { | ||
| return this.googleDrive.getDriveId(drive); | ||
| }, | ||
| getListFilesOpts(args = {}) { | ||
| return getListFilesOpts(this.drive, { | ||
| q: "mimeType != 'application/vnd.google-apps.folder' and trashed = false", | ||
| ...args, | ||
| }); | ||
| }, | ||
| shouldProcess(file) { | ||
| // Skip folders | ||
| if (file.mimeType === GOOGLE_DRIVE_FOLDER_MIME_TYPE) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if specific folders are being watched | ||
| if (this.folders?.length > 0) { | ||
| const watchedFolders = new Set(this.folders); | ||
| if (!file.parents || !file.parents.some((p) => watchedFolders.has(p))) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
jcortes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| generateMeta(file) { | ||
| const { | ||
| id: fileId, | ||
| name: summary, | ||
| createdTime: tsString, | ||
| } = file; | ||
| const ts = Date.parse(tsString); | ||
|
|
||
| return { | ||
| id: `${fileId}-${ts}`, | ||
| summary, | ||
| ts, | ||
| }; | ||
| }, | ||
| async emitFile(file) { | ||
| const meta = this.generateMeta(file); | ||
| this.$emit(file, meta); | ||
| }, | ||
| }, | ||
| async run() { | ||
| const currentRunTimestamp = Date.now(); | ||
| const lastRunTimestamp = this._getLastRunTimestamp(); | ||
|
|
||
| const pageToken = this._getPageToken(); | ||
| const driveId = this.getDriveId(); | ||
|
|
||
| const changedFilesStream = this.googleDrive.listChanges(pageToken, driveId); | ||
|
|
||
| for await (const changedFilesPage of changedFilesStream) { | ||
| console.log("Changed files page:", changedFilesPage); | ||
| const { | ||
| changedFiles, | ||
| nextPageToken, | ||
| } = changedFilesPage; | ||
|
|
||
| console.log(changedFiles.length | ||
| ? `Processing ${changedFiles.length} changed files` | ||
| : "No changed files since last run"); | ||
|
|
||
| for (const file of changedFiles) { | ||
| // Skip folders | ||
| if (file.mimeType === GOOGLE_DRIVE_FOLDER_MIME_TYPE) { | ||
| continue; | ||
| } | ||
|
|
||
| // Get full file metadata including parents | ||
| const fullFile = await this.googleDrive.getFile(file.id, { | ||
| fields: "*", | ||
| }); | ||
|
|
||
| // Check if it's a new file (created after last run) | ||
| const fileCreatedTime = Date.parse(fullFile.createdTime); | ||
| if (fileCreatedTime <= lastRunTimestamp) { | ||
| console.log(`Skipping existing file ${fullFile.name || fullFile.id}`); | ||
| continue; | ||
| } | ||
|
|
||
| if (!this.shouldProcess(fullFile)) { | ||
| console.log(`Skipping file ${fullFile.name || fullFile.id}`); | ||
| continue; | ||
| } | ||
|
|
||
| await this.emitFile(fullFile); | ||
| } | ||
|
|
||
| // Save the next page token after successfully processing | ||
| this._setPageToken(nextPageToken); | ||
| } | ||
|
|
||
| // Update the last run timestamp after processing all changes | ||
| this._setLastRunTimestamp(currentRunTimestamp); | ||
| }, | ||
| sampleEmit, | ||
| }; | ||
99 changes: 99 additions & 0 deletions
99
components/google_drive/sources/new-files-instant-polling/test-event.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| export default { | ||
| "kind": "drive#file", | ||
| "copyRequiresWriterPermission": false, | ||
| "writersCanShare": true, | ||
| "viewedByMe": true, | ||
| "mimeType": "text/plain", | ||
| "parents": [ | ||
| "0ANs73yKKVA" | ||
| ], | ||
| "iconLink": "https://drive-thirdparty.googleusercontent.com/16/type/text/plain", | ||
| "shared": false, | ||
| "lastModifyingUser": { | ||
| "displayName": "John Doe", | ||
| "kind": "drive#user", | ||
| "me": true, | ||
| "permissionId": "077423361841532483", | ||
| "emailAddress": "john@doe.com", | ||
| "photoLink": "https://lh3.googleusercontent.com/a/default-user=s64" | ||
| }, | ||
| "owners": [ | ||
| { | ||
| "displayName": "John Doe", | ||
| "kind": "drive#user", | ||
| "me": true, | ||
| "permissionId": "07742336189483", | ||
| "emailAddress": "john@doe.com", | ||
| "photoLink": "https://lh3.googleusercontent.com/a/default-user=s64" | ||
| } | ||
| ], | ||
| "webViewLink": "https://drive.google.com/file/d/1LmXTIQ0wqKP7T3-l9r127Maw4Pt2BViTOo/view?usp=drivesdk", | ||
| "size": "1024", | ||
| "viewersCanCopyContent": true, | ||
| "permissions": [ | ||
| { | ||
| "id": "07742336184153259483", | ||
| "displayName": "John Doe", | ||
| "type": "user", | ||
| "kind": "drive#permission", | ||
| "photoLink": "https://lh3.googleusercontent.com/a/default-user=s64", | ||
| "emailAddress": "john@doe.com", | ||
| "role": "owner", | ||
| "deleted": false, | ||
| "pendingOwner": false | ||
| } | ||
| ], | ||
| "spaces": [ | ||
| "drive" | ||
| ], | ||
| "id": "1LmXTIQ0wqKP7T3-l9r127Maw4Pt2BViTOo", | ||
| "name": "New File.txt", | ||
| "starred": false, | ||
| "trashed": false, | ||
| "explicitlyTrashed": false, | ||
| "createdTime": "2023-06-28T12:52:54.570Z", | ||
| "modifiedTime": "2023-06-28T12:52:54.570Z", | ||
| "modifiedByMeTime": "2023-06-28T12:52:54.570Z", | ||
| "viewedByMeTime": "2023-06-29T06:30:58.441Z", | ||
| "quotaBytesUsed": "1024", | ||
| "version": "1", | ||
| "ownedByMe": true, | ||
| "isAppAuthorized": false, | ||
| "capabilities": { | ||
| "canChangeViewersCanCopyContent": true, | ||
| "canEdit": true, | ||
| "canCopy": true, | ||
| "canComment": true, | ||
| "canAddChildren": false, | ||
| "canDelete": true, | ||
| "canDownload": true, | ||
| "canListChildren": false, | ||
| "canRemoveChildren": false, | ||
| "canRename": true, | ||
| "canTrash": true, | ||
| "canReadRevisions": true, | ||
| "canChangeCopyRequiresWriterPermission": true, | ||
| "canMoveItemIntoTeamDrive": true, | ||
| "canUntrash": true, | ||
| "canModifyContent": true, | ||
| "canMoveItemOutOfDrive": true, | ||
| "canAddMyDriveParent": false, | ||
| "canRemoveMyDriveParent": true, | ||
| "canMoveItemWithinDrive": true, | ||
| "canShare": true, | ||
| "canMoveChildrenWithinDrive": false, | ||
| "canModifyContentRestriction": true, | ||
| "canChangeSecurityUpdateEnabled": false, | ||
| "canAcceptOwnership": false, | ||
| "canReadLabels": true, | ||
| "canModifyLabels": true | ||
| }, | ||
| "modifiedByMe": true, | ||
| "permissionIds": [ | ||
| "07742336184153259483" | ||
| ], | ||
| "linkShareMetadata": { | ||
| "securityUpdateEligible": false, | ||
| "securityUpdateEnabled": true | ||
| } | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.