Skip to content

Commit d268b9a

Browse files
authored
Google Sheets - watch drive instead of file (#18631)
* watch drive instead of file * pnpm-lock.yaml * package.json version * add optional watchDrive prop, updates * version * update for shared drives
1 parent 480a514 commit d268b9a

File tree

10 files changed

+102
-13
lines changed

10 files changed

+102
-13
lines changed

components/google_sheets/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/google_sheets",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "Pipedream Google_sheets Components",
55
"main": "google_sheets.app.mjs",
66
"keywords": [
@@ -11,7 +11,7 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"dependencies": {
1313
"@googleapis/sheets": "^0.3.0",
14-
"@pipedream/google_drive": "^1.1.0",
14+
"@pipedream/google_drive": "^1.1.1",
1515
"@pipedream/platform": "^3.1.0",
1616
"lodash": "^4.17.21",
1717
"uuidv4": "^6.2.6",

components/google_sheets/sources/common/http-based/base.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export default {
3131
props: {
3232
googleSheets,
3333
db: "$.service.db",
34-
http: "$.interface.http",
34+
http: {
35+
type: "$.interface.http",
36+
customResponse: true,
37+
},
3538
timer: {
3639
label: "Push notification renewal schedule",
3740
description:

components/google_sheets/sources/common/http-based/drive.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ export default {
135135
return;
136136
}
137137

138+
this.http.respond({
139+
status: 200,
140+
});
141+
138142
const spreadsheet = await this.getSpreadsheetToProcess(event);
139143

140144
if (!spreadsheet) {

components/google_sheets/sources/common/http-based/sheet.mjs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,39 @@
55
import { v4 as uuid } from "uuid";
66

77
import base from "./base.mjs";
8+
import drive from "./drive.mjs";
89

910
/**
1011
* This source watches for changes to a specific spreadsheet in the user's Google Drive.
1112
*/
1213
export default {
1314
...base,
15+
props: {
16+
...base.props,
17+
watchDrive: {
18+
type: "boolean",
19+
label: "Watch Drive",
20+
description: "Set to `true` to watch the drive for changes. May reduce rate limiting.",
21+
optional: true,
22+
},
23+
},
1424
methods: {
25+
...drive.methods,
1526
...base.methods,
27+
_getChangeToken() {
28+
return this.db.get("changeToken");
29+
},
30+
_setChangeToken(changeToken) {
31+
this.db.set("changeToken", changeToken);
32+
},
1633
async activateHook(channelID) {
34+
if (this.watchDrive) {
35+
return this.googleSheets.activateHook(
36+
channelID,
37+
this.http.endpoint,
38+
this.googleSheets.getDriveId(this.watchedDrive),
39+
);
40+
}
1741
return this.googleSheets.activateFileHook(
1842
channelID,
1943
this.http.endpoint,
@@ -27,6 +51,28 @@ export default {
2751
.filter(({ sheetType }) => sheetType === "GRID")
2852
.map(({ sheetId }) => (sheetId.toString()));
2953
},
54+
async isSheetRelevant() {
55+
const pageToken = this._getChangeToken() || this._getPageToken();
56+
const drive = this.googleSheets.drive();
57+
const params = {
58+
pageToken,
59+
};
60+
const driveId = this.getDriveId(this.watchedDrive);
61+
if (driveId) {
62+
Object.assign(params, {
63+
driveId,
64+
supportsAllDrives: true,
65+
includeItemsFromAllDrives: true,
66+
corpora: "drive",
67+
});
68+
}
69+
const { data } = await drive.changes.list(params);
70+
const {
71+
changes, newStartPageToken,
72+
} = data;
73+
this._setChangeToken(newStartPageToken);
74+
return changes.some((change) => change.file.id === this.getSheetId());
75+
},
3076
async renewSubscription() {
3177
// Assume subscription & channelID may all be undefined at
3278
// this point Handle their absence appropriately.
@@ -51,10 +97,37 @@ export default {
5197
});
5298
this._setChannelID(newChannelID);
5399
},
100+
async renewDriveSubscription() {
101+
const subscription = this._getSubscription();
102+
const channelID = this._getChannelID() || uuid();
103+
104+
const {
105+
expiration,
106+
resourceId,
107+
newChannelID,
108+
newPageToken,
109+
} = await this.googleSheets.renewSubscription(
110+
this.watchedDrive,
111+
subscription,
112+
this.http.endpoint,
113+
channelID,
114+
this._getPageToken(),
115+
);
116+
117+
this._setSubscription({
118+
expiration,
119+
resourceId,
120+
});
121+
this._setChannelID(newChannelID);
122+
this._setPageToken(newPageToken);
123+
},
54124
},
55125
async run(event) {
56126
if (event.timestamp) {
57127
// Component was invoked by timer
128+
if (this.watchDrive) {
129+
return this.renewDriveSubscription();
130+
}
58131
return this.renewSubscription();
59132
}
60133

@@ -63,6 +136,15 @@ export default {
63136
return;
64137
}
65138

139+
this.http.respond({
140+
status: 200,
141+
});
142+
143+
if (this.watchDrive && !(await this.isSheetRelevant(event))) {
144+
console.log("Change to unwatched file, exiting early");
145+
return;
146+
}
147+
66148
const spreadsheet = await this.googleSheets.getSpreadsheet(this.sheetID);
67149

68150
return this.processSpreadsheet(spreadsheet);

components/google_sheets/sources/new-comment/new-comment.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "google_sheets-new-comment",
77
name: "New Comment (Instant)",
88
description: "Emit new event each time a comment is added to a spreadsheet.",
9-
version: "0.1.1",
9+
version: "0.1.2",
1010
dedupe: "unique",
1111
type: "source",
1212
methods: {

components/google_sheets/sources/new-row-added-polling/new-row-added-polling.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
key: "google_sheets-new-row-added-polling",
99
name: "New Row Added",
1010
description: "Emit new event each time a row or rows are added to the bottom of a spreadsheet.",
11-
version: "0.1.1",
11+
version: "0.1.2",
1212
dedupe: "unique",
1313
type: "source",
1414
props: {

components/google_sheets/sources/new-row-added/new-row-added.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
key: "google_sheets-new-row-added",
99
name: "New Row Added (Instant)",
1010
description: "Emit new event each time a row or rows are added to the bottom of a spreadsheet.",
11-
version: "0.2.1",
11+
version: "0.2.2",
1212
dedupe: "unique",
1313
type: "source",
1414
props: {

components/google_sheets/sources/new-updates/new-updates.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
type: "source",
1010
name: "New Updates (Instant)",
1111
description: "Emit new event each time a row or cell is updated in a spreadsheet.",
12-
version: "0.3.1",
12+
version: "0.3.2",
1313
dedupe: "unique",
1414
props: {
1515
...httpBase.props,

components/google_sheets/sources/new-worksheet/new-worksheet.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
type: "source",
1010
name: "New Worksheet (Instant)",
1111
description: "Emit new event each time a new worksheet is created in a spreadsheet.",
12-
version: "0.2.1",
12+
version: "0.2.2",
1313
dedupe: "unique",
1414
hooks: {
1515
...httpBase.hooks,

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)