Skip to content
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

feat(integration): add google-drive integration #1569

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion apps/console/download-component-icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });

async function main() {
try {
console.log("Downloading AI component icons...");
console.log("Downloading component icons...");
await getComponentIcons("ai");
await getComponentIcons("data");
await getComponentIcons("application");
Expand Down Expand Up @@ -73,6 +73,7 @@ async function getComponentIcons(
}).then((res) => res.json())) as GitHubContent[] | undefined;

if (!componentFolders || !componentFolders.length) {
console.warn(`No component folders found: ${folderPath}`);
return [];
}

Expand All @@ -88,6 +89,9 @@ async function getComponentIcons(
}).then((res) => res.json())) as GitHubContent[] | undefined;

if (!componentFolderContent || !componentFolderContent.length) {
console.warn(
`No component folder content found: ${componentFolder.url}`,
);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/console/public/icons/anthropic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
171 changes: 171 additions & 0 deletions apps/console/public/icons/google-drive.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions apps/console/public/icons/http.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 13 additions & 9 deletions apps/console/public/icons/instill-app.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/toolkit/src/constant/integration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const AvailableOAuthIntegration = ["github"];
export const AvailableOAuthIntegration = ["github", "slack", "google-drive"];
export const OAuthCallbackConnectionIdQueryParam = "connectionId";
export const OAuthCallbackIntegrationIdQueryParam = "integrationId";
export const OAuthCallbackStatusQueryParam = "status";
100 changes: 73 additions & 27 deletions packages/toolkit/src/lib/integrations/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "instill-sdk";
import NextAuth from "next-auth";
import GitHubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";

import { getPrefilledOAuthIntegrationConnectionId } from "./helpers";

Expand All @@ -27,6 +28,12 @@ const slackScopes = [
"users:read.email",
];

const googleDriveScopes = [
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
];

const githubScopes = ["repo", "write:repo_hook", "user:email", "read:user"];

export function getAuthHandler({
Expand All @@ -40,8 +47,8 @@ export function getAuthHandler({
id: "slack",
name: "Slack",
type: "oauth",
clientId: String(process.env.SLACK_CLIENT_ID),
clientSecret: String(process.env.SLACK_CLIENT_SECRET),
clientId: String(process.env.INTEGRATION_SLACK_CLIENT_ID),
clientSecret: String(process.env.INTEGRATION_SLACK_CLIENT_SECRET),
authorization: {
url: "https://slack.com/oauth/v2/authorize",
params: {
Expand All @@ -53,7 +60,7 @@ export function getAuthHandler({
token: "https://slack.com/api/oauth.v2.access",
userinfo: {
url: "https://slack.com/api/users.info",
async request({ tokens }: { tokens: any; provider: any }) {
async request({ tokens }: { tokens: any }) {
const profile = await fetch(
`https://slack.com/api/users.info?user=${tokens.authed_user.id}`,
{
Expand All @@ -68,9 +75,25 @@ export function getAuthHandler({
},
},
},
GoogleProvider({
id: "google-drive",
clientId: String(process.env.INTEGRATION_GOOGLE_DRIVE_CLIENT_ID),
clientSecret: String(
process.env.INTEGRATION_GOOGLE_DRIVE_CLIENT_SECRET,
),
authorization: {
url: "https://accounts.google.com/o/oauth2/auth",
params: {
scope: googleDriveScopes.join(" "),
prompt: "consent",
access_type: "offline",
response_type: "code",
},
},
}),
GitHubProvider({
clientId: String(process.env.GITHUB_CLIENT_ID),
clientSecret: String(process.env.GITHUB_CLIENT_SECRET),
clientId: String(process.env.INTEGRATION_GITHUB_CLIENT_ID),
clientSecret: String(process.env.INTEGRATION_GITHUB_CLIENT_SECRET),
authorization: {
url: "https://github.com/login/oauth/authorize",
params: {
Expand All @@ -96,32 +119,53 @@ export function getAuthHandler({
) {
let payload: Nullable<CreateIntegrationConnectionRequest> = null;

let identity =
profile.email ??
profile.name ??
(profile.login as string | undefined) ??
profile.id;

switch (account.provider) {
case "google": {
// payload = {
// integrationId: "google",
// method: "METHOD_OAUTH",
// setup: {
// token: account.access_token,
// },
// namespaceId,
// id: connectionId,
// oAuthAccessDetails: account,
// };
payload = null;
case "google-drive": {
const identity = profile.email ?? profile.name;

if (!identity) {
throw new Error(
"Instill Integration Error: Google Drive user not found, can't get the identity",
);
}

const prefilledIntegrationConnectionId =
getPrefilledOAuthIntegrationConnectionId({
provider: "google-drive",
connectionIdentity: identity,
});

payload = {
integrationId: "google-drive",
method: "METHOD_OAUTH",
setup: {
token: account.access_token,
"refresh-token": account.refresh_token,
},
namespaceId,
id: prefilledIntegrationConnectionId,
oAuthAccessDetails: {
...account,
...profile,
},
identity: identity,
scopes: googleDriveScopes,
};
break;
}
case "github": {
const identity = profile.login as string | undefined;

if (!identity) {
throw new Error(
"Instill Integration Error: GitHub user not found, can't get the identity",
);
}

const prefilledIntegrationConnectionId =
getPrefilledOAuthIntegrationConnectionId({
provider: "github",
connectionIdentity: profile.login as string,
connectionIdentity: identity,
});

payload = {
Expand All @@ -136,13 +180,13 @@ export function getAuthHandler({
...account,
...profile,
},
identity: identity ?? undefined,
identity: identity,
scopes: githubScopes,
};
break;
}
case "slack": {
identity =
const identity =
// the profile we get from slack is not typed
(profile.profile as any).email ??
profile.real_name ??
Expand Down Expand Up @@ -179,7 +223,7 @@ export function getAuthHandler({
...account,
...profile,
},
identity: identity ?? undefined,
identity: identity,
scopes: slackScopes,
};
break;
Expand All @@ -195,6 +239,8 @@ export function getAuthHandler({
apiToken: instillAccessToken,
});

console.log("payload", payload);

if (payload) {
await client.core.integration.createIntegrationConnection(
payload,
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/lib/integrations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export type OnOAuthCallbackProps = {

export type OnOAuthCallback = (props: OnOAuthCallbackProps) => void;

export type IntegrationProvider = "google" | "slack" | "github";
export type IntegrationProvider = "google-drive" | "slack" | "github";
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const ConnectableIntegration = ({
case "slack":
provider = "slack";
break;
case "google-drive":
provider = "google-drive";
break;
default:
break;
}
Expand All @@ -70,15 +73,6 @@ export const ConnectableIntegration = ({
return;
}

if (integration.id === "slack" && namespaceId) {
initializeIntegrationConnection({
provider: "slack",
namespaceId,
integrationId: integration.id,
});
return;
}

setEditingDialogIsOpen(true);
}}
>
Expand Down
12 changes: 6 additions & 6 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
"NEXT_SERVER_API_GATEWAY_URL",
"NEXT_PUBLIC_CONSOLE_BASE_URL",
"INSTILL_API_VERSION",
"GOOGLE_CLIENT_ID",
"GOOGLE_CLIENT_SECRET",
"GITHUB_CLIENT_ID",
"GITHUB_CLIENT_SECRET",
"SLACK_CLIENT_ID",
"SLACK_CLIENT_SECRET",
"INTEGRATION_GOOGLE_DRIVE_CLIENT_ID",
"INTEGRATION_GOOGLE_DRIVE_CLIENT_SECRET",
"INTEGRATION_GITHUB_CLIENT_ID",
"INTEGRATION_GITHUB_CLIENT_SECRET",
"INTEGRATION_SLACK_CLIENT_ID",
"INTEGRATION_SLACK_CLIENT_SECRET",
"NEXT_PUBLIC_APPLICATION_API_VERSION"
]
}
Loading