Skip to content

Commit

Permalink
Merge pull request #1064 from activepieces/chore/fix-google-sheets-gmail
Browse files Browse the repository at this point in the history
fix: google sheets sample & gmail permissions
  • Loading branch information
abuaboud committed Apr 14, 2023
2 parents 46077fb + de0313e commit 6a338c9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/pieces/gmail/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-gmail",
"version": "0.2.5"
"version": "0.2.6"
}
8 changes: 2 additions & 6 deletions packages/pieces/gmail/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { createPiece } from '@activepieces/pieces-framework';
import { gmailGetEmail } from './lib/actions/get-mail-action';
import { gmailGetThread } from './lib/actions/get-thread-action';
import { gmailSearchMail } from './lib/actions/search-email-action';
import { gmailSendEmailAction } from './lib/actions/send-email-action';
import { gmailNewEmailTrigger } from './lib/triggers/new-email';
import packageJson from '../package.json';

export const gmail = createPiece({
name: 'gmail',
logoUrl: 'https://cdn.activepieces.com/pieces/gmail.png',
actions: [gmailSendEmailAction, gmailGetEmail, gmailSearchMail, gmailGetThread],
actions: [gmailSendEmailAction],
displayName: 'Gmail',
authors: ['AbdulTheActivePiecer', 'kanarelo'],
triggers: [gmailNewEmailTrigger],
triggers: [],
version: packageJson.version,
});
3 changes: 2 additions & 1 deletion packages/pieces/gmail/src/lib/common/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const GmailProps = {
authUrl: "https://accounts.google.com/o/oauth2/auth",
tokenUrl: "https://oauth2.googleapis.com/token",
required: true,
scope: ["https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/gmail.readonly"]
// TODO add https://www.googleapis.com/auth/gmail.readonly when we have the permission
scope: ["https://www.googleapis.com/auth/gmail.send"]
}),
from: Property.ShortText({
displayName: 'Email sender',
Expand Down
2 changes: 1 addition & 1 deletion packages/pieces/google-sheets/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-google-sheets",
"version": "0.2.4"
"version": "0.2.5"
}
88 changes: 43 additions & 45 deletions packages/pieces/google-sheets/src/lib/triggers/new-row-added.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { createTrigger } from '@activepieces/pieces-framework';
import { OAuth2PropertyValue, createTrigger } from '@activepieces/pieces-framework';
import { TriggerStrategy } from "@activepieces/pieces-framework";
import { googleSheetsCommon } from '../common/common';
import { DedupeStrategy, Polling, pollingHelper } from '@activepieces/pieces-common';

const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const sampleData = Array.from(alphabet).map(c => `${c} Value`);
Array.from(alphabet).forEach(c => sampleData.push(`${c}${c} Value`));

const alphabet ='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const sampleData = Array.from(alphabet).map(c=>`${c} Value`);
Array.from(alphabet).forEach(c=>sampleData.push(`${c}${c} Value`));
export const newRowAdded = createTrigger({
name: 'new_row_added',
displayName: 'New Row',
Expand All @@ -14,52 +16,48 @@ export const newRowAdded = createTrigger({
spreadsheet_id: googleSheetsCommon.spreadsheet_id,
sheet_id: googleSheetsCommon.sheet_id
},
type: TriggerStrategy.POLLING,
sampleData: {
"value": sampleData,
"rowId": 1
},
type: TriggerStrategy.POLLING,
async test(context) {
const sheetId = context.propsValue['sheet_id'];
const accessToken = context.propsValue['authentication']['access_token'];
const spreadSheetId = context.propsValue['spreadsheet_id'];
const allValues = await googleSheetsCommon.getValues(spreadSheetId, accessToken, sheetId);
if(!allValues)
{
return [];
}
return allValues.slice(Math.max(allValues.length-5,0));
onEnable: async (context) => {
await pollingHelper.onEnable(polling, {
store: context.store,
propsValue: context.propsValue,
})
},
async onEnable(context) {
const sheetId = context.propsValue['sheet_id'];
const accessToken = context.propsValue['authentication']['access_token'];
const spreadSheetId = context.propsValue['spreadsheet_id'];
const currentValues = await googleSheetsCommon.getValues(spreadSheetId, accessToken, sheetId);
console.log(`The spreadsheet ${spreadSheetId} started with ${currentValues.length} rows`);
context.store?.put("rowCount", currentValues.length);
onDisable: async (context) => {
await pollingHelper.onDisable(polling, {
store: context.store,
propsValue: context.propsValue,
})
},
run: async (context) => {
return await pollingHelper.poll(polling, {
store: context.store,
propsValue: context.propsValue,
});
},
test: async (context) => {
return await pollingHelper.poll(polling, {
store: context.store,
propsValue: context.propsValue,
});
},
async onDisable(context) {
console.log("Disabling new google sheets trigger");
},
async run(context) {
const sheetId = context.propsValue['sheet_id'];
const accessToken = context.propsValue['authentication']['access_token'];
const spreadSheetId = context.propsValue['spreadsheet_id'];
const rowCount = (await context.store?.get<number>("rowCount")) ?? 0;
const currentValues = await googleSheetsCommon.getValues(spreadSheetId, accessToken, sheetId)
let payloads: unknown[] = [];
console.log(`The spreadsheet ${spreadSheetId} has now ${currentValues.length} rows, previous # of rows ${rowCount}`);
if (currentValues.length > rowCount) {
payloads = currentValues.slice(rowCount).map((value, index) => {
const rowIndex = rowCount + index;
return {
value: value,
rowId: rowIndex
}
});
}
context.store?.put("rowCount", currentValues.length);
return payloads;
},
});


const polling: Polling<{ authentication: OAuth2PropertyValue, spreadsheet_id: string, sheet_id: number}> = {
strategy: DedupeStrategy.LAST_ITEM,
items: async ({ propsValue }) => {
const currentValues = await googleSheetsCommon.getValues(propsValue.spreadsheet_id, propsValue.authentication.access_token, propsValue.sheet_id)
return currentValues.reverse().map((item, index) => ({
id: currentValues.length - index,
data: {
value: item,
rowId: currentValues.length - index
},
}));
}
};

0 comments on commit 6a338c9

Please sign in to comment.