diff --git a/packages/backend/src/apps/google-sheets/assets/favicon.svg b/packages/backend/src/apps/google-sheets/assets/favicon.svg new file mode 100644 index 00000000..bd5d938c --- /dev/null +++ b/packages/backend/src/apps/google-sheets/assets/favicon.svg @@ -0,0 +1,89 @@ + + + + Sheets-icon + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/backend/src/apps/google-sheets/auth/generate-auth-url.ts b/packages/backend/src/apps/google-sheets/auth/generate-auth-url.ts new file mode 100644 index 00000000..ea89f8a4 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/auth/generate-auth-url.ts @@ -0,0 +1,24 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; +import authScope from '../common/auth-scope'; + +export default async function generateAuthUrl($: IGlobalVariable) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId as string, + redirect_uri: redirectUri, + prompt: 'select_account', + scope: authScope.join(' '), + response_type: 'code', + access_type: 'offline', + }); + + const url = `https://accounts.google.com/o/oauth2/v2/auth?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/google-sheets/auth/index.ts b/packages/backend/src/apps/google-sheets/auth/index.ts new file mode 100644 index 00000000..5e52e35a --- /dev/null +++ b/packages/backend/src/apps/google-sheets/auth/index.ts @@ -0,0 +1,48 @@ +import generateAuthUrl from './generate-auth-url'; +import verifyCredentials from './verify-credentials'; +import refreshToken from './refresh-token'; +import isStillVerified from './is-still-verified'; + +export default { + fields: [ + { + key: 'oAuthRedirectUrl', + label: 'OAuth Redirect URL', + type: 'string' as const, + required: true, + readOnly: true, + value: '{WEB_APP_URL}/app/google-sheets/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Google Cloud, enter the URL above.', + clickToCopy: true, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + { + key: 'clientSecret', + label: 'Client Secret', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, + refreshToken, +}; diff --git a/packages/backend/src/apps/google-sheets/auth/is-still-verified.ts b/packages/backend/src/apps/google-sheets/auth/is-still-verified.ts new file mode 100644 index 00000000..c46fa18b --- /dev/null +++ b/packages/backend/src/apps/google-sheets/auth/is-still-verified.ts @@ -0,0 +1,9 @@ +import { IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; + +const isStillVerified = async ($: IGlobalVariable) => { + const currentUser = await getCurrentUser($); + return !!currentUser.resourceName; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/google-sheets/auth/refresh-token.ts b/packages/backend/src/apps/google-sheets/auth/refresh-token.ts new file mode 100644 index 00000000..17d0c4cc --- /dev/null +++ b/packages/backend/src/apps/google-sheets/auth/refresh-token.ts @@ -0,0 +1,26 @@ +import { URLSearchParams } from 'node:url'; +import { IGlobalVariable } from '@automatisch/types'; +import authScope from '../common/auth-scope'; + +const refreshToken = async ($: IGlobalVariable) => { + const params = new URLSearchParams({ + client_id: $.auth.data.clientId as string, + client_secret: $.auth.data.clientSecret as string, + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken as string, + }); + + const { data } = await $.http.post( + 'https://oauth2.googleapis.com/token', + params.toString() + ); + + await $.auth.set({ + accessToken: data.access_token, + expiresIn: data.expires_in, + scope: authScope.join(' '), + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/google-sheets/auth/verify-credentials.ts b/packages/backend/src/apps/google-sheets/auth/verify-credentials.ts new file mode 100644 index 00000000..124e73c1 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/auth/verify-credentials.ts @@ -0,0 +1,57 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; + +type TUser = { + displayName: string; + metadata: { + primary: boolean; + }; +}; + +type TEmailAddress = { + value: string; + metadata: { + primary: boolean; + }; +}; + +const verifyCredentials = async ($: IGlobalVariable) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const { data } = await $.http.post(`https://oauth2.googleapis.com/token`, { + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + code: $.auth.data.code, + grant_type: 'authorization_code', + redirect_uri: redirectUri, + }); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + const currentUser = await getCurrentUser($); + + const { displayName } = currentUser.names.find( + (name: TUser) => name.metadata.primary + ); + const { value: email } = currentUser.emailAddresses.find( + (emailAddress: TEmailAddress) => emailAddress.metadata.primary + ); + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: $.auth.data.scope, + idToken: data.id_token, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + resourceName: currentUser.resourceName, + screenName: `${displayName} - ${email}`, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/google-sheets/common/add-auth-header.ts b/packages/backend/src/apps/google-sheets/common/add-auth-header.ts new file mode 100644 index 00000000..8e7798b8 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/common/add-auth-header.ts @@ -0,0 +1,11 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if ($.auth.data?.accessToken) { + requestConfig.headers.Authorization = `${$.auth.data.tokenType} ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/google-sheets/common/auth-scope.ts b/packages/backend/src/apps/google-sheets/common/auth-scope.ts new file mode 100644 index 00000000..b4b8fdb3 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/common/auth-scope.ts @@ -0,0 +1,8 @@ +const authScope: string[] = [ + 'https://www.googleapis.com/auth/drive', + 'https://www.googleapis.com/auth/spreadsheets', + 'https://www.googleapis.com/auth/userinfo.email', + 'https://www.googleapis.com/auth/userinfo.profile', +]; + +export default authScope; diff --git a/packages/backend/src/apps/google-sheets/common/get-current-user.ts b/packages/backend/src/apps/google-sheets/common/get-current-user.ts new file mode 100644 index 00000000..724fe1ac --- /dev/null +++ b/packages/backend/src/apps/google-sheets/common/get-current-user.ts @@ -0,0 +1,10 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const getCurrentUser = async ($: IGlobalVariable) => { + const { data: currentUser } = await $.http.get( + 'https://people.googleapis.com/v1/people/me?personFields=names,emailAddresses' + ); + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/google-sheets/dynamic-data/index.ts b/packages/backend/src/apps/google-sheets/dynamic-data/index.ts new file mode 100644 index 00000000..f570d9a5 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/dynamic-data/index.ts @@ -0,0 +1,3 @@ +import listDrives from './list-drives'; + +export default [listDrives]; \ No newline at end of file diff --git a/packages/backend/src/apps/google-sheets/dynamic-data/list-drives/index.ts b/packages/backend/src/apps/google-sheets/dynamic-data/list-drives/index.ts new file mode 100644 index 00000000..ea68c599 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/dynamic-data/list-drives/index.ts @@ -0,0 +1,38 @@ +import { IGlobalVariable, IJSONObject } from '@automatisch/types'; + +export default { + name: 'List drives', + key: 'listDrives', + + async run($: IGlobalVariable) { + const drives: { + data: IJSONObject[]; + } = { + data: [{ value: null, name: 'My Google Drive' }], + }; + + const params = { + pageSize: 100, + pageToken: undefined as unknown as string, + }; + + do { + const { data } = await $.http.get( + `https://www.googleapis.com/drive/v3/drives`, + { params } + ); + params.pageToken = data.nextPageToken; + + if (data.drives) { + for (const drive of data.drives) { + drives.data.push({ + value: drive.id, + name: drive.name, + }); + } + } + } while (params.pageToken); + + return drives; + }, +}; diff --git a/packages/backend/src/apps/google-sheets/index.d.ts b/packages/backend/src/apps/google-sheets/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/backend/src/apps/google-sheets/index.ts b/packages/backend/src/apps/google-sheets/index.ts new file mode 100644 index 00000000..5232b5f6 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/index.ts @@ -0,0 +1,20 @@ +import defineApp from '../../helpers/define-app'; +import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; +import triggers from './triggers'; +import dynamicData from './dynamic-data'; + +export default defineApp({ + name: 'Google Sheets', + key: 'google-sheets', + baseUrl: 'https://docs.google.com/spreadsheets', + apiBaseUrl: 'https://sheets.googleapis.com', + iconUrl: '{BASE_URL}/apps/google-sheets/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/google-sheets/connection', + primaryColor: '0F9D58', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, + triggers, + dynamicData, +}); diff --git a/packages/backend/src/apps/google-sheets/triggers/index.ts b/packages/backend/src/apps/google-sheets/triggers/index.ts new file mode 100644 index 00000000..c2c22ebe --- /dev/null +++ b/packages/backend/src/apps/google-sheets/triggers/index.ts @@ -0,0 +1,3 @@ +import newSpreadsheets from './new-spreadsheets'; + +export default [newSpreadsheets]; \ No newline at end of file diff --git a/packages/backend/src/apps/google-sheets/triggers/new-spreadsheets/index.ts b/packages/backend/src/apps/google-sheets/triggers/new-spreadsheets/index.ts new file mode 100644 index 00000000..afb08a3c --- /dev/null +++ b/packages/backend/src/apps/google-sheets/triggers/new-spreadsheets/index.ts @@ -0,0 +1,33 @@ +import defineTrigger from '../../../../helpers/define-trigger'; +import newSpreadsheets from './new-spreadsheets' + +export default defineTrigger({ + name: 'New Spreadsheets', + key: 'newSpreadsheets', + pollInterval: 15, + description: 'Triggers when you create a new spreadsheet.', + arguments: [ + { + label: 'Drive', + key: 'driveId', + type: 'dropdown' as const, + required: false, + description: 'The Google Drive where your spreadsheet resides. If nothing is selected, then your personal Google Drive will be used.', + variables: false, + source: { + type: 'query', + name: 'getDynamicData', + arguments: [ + { + name: 'key', + value: 'listDrives', + }, + ], + }, + }, + ], + + async run($) { + await newSpreadsheets($); + }, +}); \ No newline at end of file diff --git a/packages/backend/src/apps/google-sheets/triggers/new-spreadsheets/new-spreadsheets.ts b/packages/backend/src/apps/google-sheets/triggers/new-spreadsheets/new-spreadsheets.ts new file mode 100644 index 00000000..e054beb7 --- /dev/null +++ b/packages/backend/src/apps/google-sheets/triggers/new-spreadsheets/new-spreadsheets.ts @@ -0,0 +1,33 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const newSpreadsheets = async ($: IGlobalVariable) => { + const params = { + pageToken: undefined as unknown as string, + orderBy: 'createdTime desc', + q: `mimeType='application/vnd.google-apps.spreadsheet'`, + fields: '*', + pageSize: 1000, + driveId: $.step.parameters.driveId, + }; + + do { + const { data } = await $.http.get( + 'https://www.googleapis.com/drive/v3/files', + { params } + ); + params.pageToken = data.nextPageToken; + + if (data.files?.length) { + for (const file of data.files) { + $.pushTriggerItem({ + raw: file, + meta: { + internalId: file.id, + }, + }); + } + } + } while (params.pageToken); +}; + +export default newSpreadsheets; diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 44a51012..1118ac41 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -68,15 +68,6 @@ export default defineConfig({ { text: 'Connection', link: '/apps/flickr/connection' }, ], }, - { - text: 'Google Drive', - collapsible: true, - collapsed: true, - items: [ - { text: 'Triggers', link: '/apps/google-drive/triggers' }, - { text: 'Connection', link: '/apps/google-drive/connection' }, - ], - }, { text: 'Github', collapsible: true, @@ -87,6 +78,15 @@ export default defineConfig({ { text: 'Connection', link: '/apps/github/connection' }, ], }, + { + text: 'Google Drive', + collapsible: true, + collapsed: true, + items: [ + { text: 'Triggers', link: '/apps/google-drive/triggers' }, + { text: 'Connection', link: '/apps/google-drive/connection' }, + ], + }, { text: 'Google Forms', collapsible: true, @@ -96,6 +96,15 @@ export default defineConfig({ { text: 'Connection', link: '/apps/google-forms/connection' }, ], }, + { + text: 'Google Sheets', + collapsible: true, + collapsed: true, + items: [ + { text: 'Triggers', link: '/apps/google-sheets/triggers' }, + { text: 'Connection', link: '/apps/google-sheets/connection' }, + ], + }, { text: 'HTTP Request', collapsible: true, diff --git a/packages/docs/pages/apps/google-sheets/connection.md b/packages/docs/pages/apps/google-sheets/connection.md new file mode 100644 index 00000000..79afb68c --- /dev/null +++ b/packages/docs/pages/apps/google-sheets/connection.md @@ -0,0 +1,28 @@ +# Google Sheets + +:::info +This page explains the steps you need to follow to set up the Google Sheets +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to the [Google Cloud Console](https://console.cloud.google.com) to create a project. +2. Click on the project drop-down menu at the top of the page, and click on the **New Project** button. +3. Enter a name for your project and click on the **Create** button. +4. Go to [API Library](https://console.cloud.google.com/apis/library) in Google Cloud console. +5. Search for **People API** in the search bar and click on it. +6. Click on the **Enable** button to enable the API. +7. Repeat steps 5 and 6 for the **Google Drive API** and **Google Sheets API**. +8. Go to [OAuth consent screen](https://console.cloud.google.com/apis/credentials/consent) in Google Cloud console. +9. Select **External** here for starting your app in testing mode at first. Click on the **Create** button. +10. Fill **App Name**, **User Support Email**, and **Developer Contact Information**. Click on the **Save and Continue** button. +11. Skip adding or removing scopes and click on the **Save and Continue** button. +12. Click on the **Add Users** button and add a test email because only test users can access the app while publishing status is set to "Testing". +13. Click on the **Save and Continue** button and now you have configured the consent screen. +14. Go to [Credentials](https://console.cloud.google.com/apis/credentials) in Google Cloud console. +15. Click on the **Create Credentials** button and select the **OAuth client ID** option. +16. Select the application type as **Web application** and fill the **Name** field. +17. Copy **OAuth Redirect URL** from Automatisch to **Authorized redirect URIs** field, and click on the **Create** button. +18. Copy the **Your Client ID** value from the following popup to the `Client ID` field on Automatisch. +19. Copy the **Your Client Secret** value from the following popup to the `Client Secret` field on Automatisch. +20. Click **Submit** button on Automatisch. +21. Congrats! Start using your new Google Sheets connection within the flows. diff --git a/packages/docs/pages/apps/google-sheets/triggers.md b/packages/docs/pages/apps/google-sheets/triggers.md new file mode 100644 index 00000000..2189ccb9 --- /dev/null +++ b/packages/docs/pages/apps/google-sheets/triggers.md @@ -0,0 +1,12 @@ +--- +favicon: /favicons/google-sheets.svg +items: + - name: New Spreadsheets + desc: Triggers when you create a new spreadsheet +--- + + + + diff --git a/packages/docs/pages/public/favicons/google-sheets.svg b/packages/docs/pages/public/favicons/google-sheets.svg new file mode 100644 index 00000000..9bdc9721 --- /dev/null +++ b/packages/docs/pages/public/favicons/google-sheets.svg @@ -0,0 +1,89 @@ + + + + Sheets-icon + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/web/src/components/ControlledAutocomplete/index.tsx b/packages/web/src/components/ControlledAutocomplete/index.tsx index bad45fc0..76d86671 100644 --- a/packages/web/src/components/ControlledAutocomplete/index.tsx +++ b/packages/web/src/components/ControlledAutocomplete/index.tsx @@ -69,7 +69,7 @@ function ControlledAutocomplete( }, fieldState, }) => ( -
+
{/* encapsulated with an element such as div to vertical spacing delegated from parent */} (
  • {option.label}