Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/google_drive/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/google_drive",
"version": "1.0.7",
"version": "1.1.0",
"description": "Pipedream Google_drive Components",
"main": "google_drive.app.mjs",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
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",

Check warning on line 14 in components/google_drive/sources/changes-to-files-in-drive/changes-to-files-in-drive.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Source names should start with "New". See https://pipedream.com/docs/components/guidelines/#source-name
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

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop infoAlert must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 20 in components/google_drive/sources/changes-to-files-in-drive/changes-to-files-in-drive.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop infoAlert must have a label. See https://pipedream.com/docs/components/guidelines/#props
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 eventId = headers && headers["x-goog-message-number"];
const resourceState = headers && headers["x-goog-resource-state"];

const summary = resourceState
? `${resourceState.toUpperCase()} - ${fileName || "Untitled"}`
: fileName || "Untitled";

return {
id: `${fileId}-${eventId || 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);
if (this.includeLink) {
file.file = await stashFile(file, this.googleDrive, this.dir);
}
const fileInfo = await this.googleDrive.getFile(file.id);
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);
}
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
export default {
...common,
key: "google_drive-changes-to-specific-files-shared-drive",
name: "Changes to Specific Files (Shared Drive)",

Check warning on line 29 in components/google_drive/sources/changes-to-specific-files-shared-drive/changes-to-specific-files-shared-drive.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Source names should start with "New". See https://pipedream.com/docs/components/guidelines/#source-name
description: "Watches for changes to specific files in a shared drive, emitting an event when a change is made to one of those files",

Check warning on line 30 in components/google_drive/sources/changes-to-specific-files-shared-drive/changes-to-specific-files-shared-drive.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Source descriptions should start with "Emit new". See https://pipedream.com/docs/components/guidelines/#source-description
version: "0.3.0",
version: "0.3.1",
type: "source",
// Dedupe events based on the "x-goog-message-number" header for the target channel:
// https://developers.google.com/drive/api/v3/push#making-watch-requests
Expand Down Expand Up @@ -127,11 +127,12 @@
if (this.includeLink) {
file.file = await stashFile(file, this.googleDrive, this.dir);
}
const fileInfo = await this.googleDrive.getFile(file.id);
const eventToEmit = {
file,
file: fileInfo,
...changes,
};
const meta = this.generateMeta(file, headers);
const meta = this.generateMeta(fileInfo, headers);
this.$emit(eventToEmit, meta);
},
async processChanges(changedFiles, headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { v4 as uuid } from "uuid";
import { MY_DRIVE_VALUE } from "../../common/constants.mjs";
import changesToSpecificFiles from "../changes-to-specific-files-shared-drive/changes-to-specific-files-shared-drive.mjs";
import { ConfigurationError } from "@pipedream/platform";
import sampleEmit from "./test-event.mjs";

/**
Expand All @@ -13,14 +14,19 @@
export default {
...changesToSpecificFiles,
key: "google_drive-changes-to-specific-files",
name: "Changes to Specific Files",

Check warning on line 17 in components/google_drive/sources/changes-to-specific-files/changes-to-specific-files.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Source names should start with "New". See https://pipedream.com/docs/components/guidelines/#source-name
description: "Watches for changes to specific files, emitting an event when a change is made to one of those files. To watch for changes to [shared drive](https://support.google.com/a/users/answer/9310351) files, use the **Changes to Specific Files (Shared Drive)** source instead.",

Check warning on line 18 in components/google_drive/sources/changes-to-specific-files/changes-to-specific-files.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Source descriptions should start with "Emit new". See https://pipedream.com/docs/components/guidelines/#source-description
version: "0.3.0",
version: "0.3.1",
type: "source",
// Dedupe events based on the "x-goog-message-number" header for the target channel:
// https://developers.google.com/drive/api/v3/push#making-watch-requests
dedupe: "unique",
props: {
infoAlert: {

Check warning on line 25 in components/google_drive/sources/changes-to-specific-files/changes-to-specific-files.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop infoAlert must have a description. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 25 in components/google_drive/sources/changes-to-specific-files/changes-to-specific-files.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop infoAlert must have a label. See https://pipedream.com/docs/components/guidelines/#props
type: "alert",
alertType: "info",
content: "This source uses `files.watch` and supports up to 10 file subscriptions. To watch for changes to more than 10 files, use the **Changes to Files in Drive** source instead (uses `changes.watch`).",
},
...changesToSpecificFiles.props,
drive: {
type: "string",
Expand Down Expand Up @@ -50,6 +56,12 @@
},
hooks: {
...changesToSpecificFiles.hooks,
async deploy() {
if (this.files.length > 10) {
throw new ConfigurationError("This source only supports up to 10 files");
}
await changesToSpecificFiles.hooks.deploy.bind(this)();
},
async activate() {
// Called when a component is created or updated. Handles all the logic
// for starting and stopping watch notifications tied to the desired
Expand Down
Loading