- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.5k
Google Drive - new source changes-to-files-in-drive using Changes API instead of File Subscriptions API #18397
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
          
     Merged
      
      
    
      
        
          +182
        
        
          −16
        
        
          
        
      
    
  
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      8e5b65d
              
                changes-to-files-in-drive
              
              
                michelle0927 962ab66
              
                updates
              
              
                michelle0927 5611138
              
                updates
              
              
                michelle0927 c91af00
              
                updates
              
              
                michelle0927 fc04d8a
              
                versions
              
              
                michelle0927 7c19fc7
              
                Merge branch 'master' into issue-18315
              
              
                michelle0927 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
    
  
  
    
              
              
        
          
          
            147 changes: 147 additions & 0 deletions
          
          147 
        
  components/google_drive/sources/changes-to-files-in-drive/changes-to-files-in-drive.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,147 @@ | ||
| import common from "../common-webhook.mjs"; | ||
| import { | ||
| MY_DRIVE_VALUE, | ||
| GOOGLE_DRIVE_NOTIFICATION_ADD, | ||
| GOOGLE_DRIVE_NOTIFICATION_CHANGE, | ||
| GOOGLE_DRIVE_NOTIFICATION_UPDATE, | ||
| } from "../../common/constants.mjs"; | ||
| import commonDedupeChanges from "../common-dedupe-changes.mjs"; | ||
| import { stashFile } from "../../common/utils.mjs"; | ||
|  | ||
| export default { | ||
| ...common, | ||
| key: "google_drive-changes-to-files-in-drive", | ||
| name: "Changes to Files in Drive", | ||
| description: "Emit new event when a change is made to one of the specified files. [See the documentation](https://developers.google.com/drive/api/v3/reference/changes/watch)", | ||
| version: "0.0.1", | ||
| type: "source", | ||
| dedupe: "unique", | ||
| props: { | ||
| infoAlert: { | ||
| Check warning on line 20 in components/google_drive/sources/changes-to-files-in-drive/changes-to-files-in-drive.mjs 
     | ||
| type: "alert", | ||
| alertType: "info", | ||
| content: "This source uses `changes.watch` and supports watching 10+ files. To watch for changes to fewer than 10 files, you may want to use the **Changes to Specific Files** source instead (uses `files.watch`).", | ||
| }, | ||
| ...common.props, | ||
| drive: { | ||
| type: "string", | ||
| label: "Drive", | ||
| description: "Defaults to `My Drive`. To use a [Shared Drive](https://support.google.com/a/users/answer/9310351), use the **Changes to Specific Files (Shared Drive)** source instead.", | ||
| optional: true, | ||
| default: MY_DRIVE_VALUE, | ||
| }, | ||
| files: { | ||
| type: "string[]", | ||
| label: "Files", | ||
| description: "The files you want to watch for changes.", | ||
| options({ prevContext }) { | ||
| const { nextPageToken } = prevContext; | ||
| return this.googleDrive.listFilesOptions(nextPageToken, this.getListFilesOpts()); | ||
| }, | ||
| }, | ||
| includeLink: { | ||
| label: "Include Link", | ||
| type: "boolean", | ||
| description: "Upload file to your File Stash and emit temporary download link to the file. Google Workspace documents will be converted to PDF. See [the docs](https://pipedream.com/docs/connect/components/files) to learn more about working with files in Pipedream.", | ||
| default: false, | ||
| optional: true, | ||
| }, | ||
| dir: { | ||
| type: "dir", | ||
| accessMode: "write", | ||
| optional: true, | ||
| }, | ||
| ...commonDedupeChanges.props, | ||
| }, | ||
| hooks: { | ||
| async deploy() { | ||
| 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 modifiedTime > "${timeString}" and trashed = false`, | ||
| fields: "files", | ||
| pageSize: 5, | ||
| }); | ||
|  | ||
| const { files } = await this.googleDrive.listFilesInPage(null, args); | ||
|  | ||
| await this.processChanges(files); | ||
| }, | ||
| ...common.hooks, | ||
| }, | ||
| methods: { | ||
| ...common.methods, | ||
| getUpdateTypes() { | ||
| return [ | ||
| GOOGLE_DRIVE_NOTIFICATION_ADD, | ||
| GOOGLE_DRIVE_NOTIFICATION_CHANGE, | ||
| GOOGLE_DRIVE_NOTIFICATION_UPDATE, | ||
| ]; | ||
| }, | ||
| generateMeta(data, headers) { | ||
| const { | ||
| id: fileId, | ||
| name: fileName, | ||
| modifiedTime: tsString, | ||
| } = data; | ||
| const ts = Date.parse(tsString); | ||
| const resourceState = headers && headers["x-goog-resource-state"]; | ||
|  | ||
| const summary = resourceState | ||
| ? `${resourceState.toUpperCase()} - ${fileName || "Untitled"}` | ||
| : fileName || "Untitled"; | ||
|  | ||
| return { | ||
| id: `${fileId}-${ts}`, | ||
| summary, | ||
| ts, | ||
| }; | ||
| }, | ||
| isFileRelevant(file) { | ||
| return this.files.includes(file.id); | ||
| }, | ||
| getChanges(headers) { | ||
| if (!headers) { | ||
| return { | ||
| change: { }, | ||
| }; | ||
| } | ||
| return { | ||
| change: { | ||
| state: headers["x-goog-resource-state"], | ||
| resourceURI: headers["x-goog-resource-uri"], | ||
| changed: headers["x-goog-changed"], // "Additional details about the changes. Possible values: content, parents, children, permissions" | ||
| }, | ||
| }; | ||
| }, | ||
| async processChange(file, headers) { | ||
| const changes = this.getChanges(headers); | ||
| const fileInfo = await this.googleDrive.getFile(file.id); | ||
| if (this.includeLink) { | ||
| fileInfo.file = await stashFile(file, this.googleDrive, this.dir); | ||
| } | ||
| const eventToEmit = { | ||
| file: fileInfo, | ||
| ...changes, | ||
| }; | ||
| const meta = this.generateMeta(fileInfo, headers); | ||
| this.$emit(eventToEmit, meta); | ||
| }, | ||
| async processChanges(changedFiles, headers) { | ||
| console.log(`Processing ${changedFiles.length} changed files`); | ||
| console.log(`Changed files: ${JSON.stringify(changedFiles, null, 2)}!!!`); | ||
| console.log(`Files: ${this.files}!!!`); | ||
|  | ||
| const filteredFiles = this.checkMinimumInterval(changedFiles); | ||
| for (const file of filteredFiles) { | ||
| if (!this.isFileRelevant(file)) { | ||
| console.log(`Skipping event for irrelevant file ${file.id}`); | ||
| continue; | ||
| } | ||
| await this.processChange(file, headers); | ||
| } | ||
| }, | ||
| }, | ||
| }; | ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
      
      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.