From b3178d1b8aacfa90b8a68554a186f3b26f3190ba Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Wed, 16 Nov 2022 00:23:41 +0100 Subject: [PATCH] feat: add slack_bot token on connecting workspace to slack --- backend/sqlx-data.json | 35 +++++++++++ backend/windmill-api/src/oauth2.rs | 33 +++++++++++ .../src/routes/oauth/callback_slack.svelte | 9 +-- frontend/src/routes/workspace_settings.svelte | 59 ++++++++++--------- 4 files changed, 105 insertions(+), 31 deletions(-) diff --git a/backend/sqlx-data.json b/backend/sqlx-data.json index 83c658bf94..13cdb60279 100644 --- a/backend/sqlx-data.json +++ b/backend/sqlx-data.json @@ -210,6 +210,23 @@ }, "query": "INSERT INTO usr\n (workspace_id, email, username, is_admin)\n VALUES ($1, $2, $3, $4)" }, + "0c3a39eafc349870be019318d6925922558ac20fdd76b042d69ccd8a527e3ff5": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Left": [ + "Varchar", + "Varchar", + "Jsonb", + "Text", + "Varchar", + "Bool" + ] + } + }, + "query": "INSERT INTO resource\n (workspace_id, path, value, description, resource_type, is_oauth)\n VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (workspace_id, path) DO UPDATE SET value = $3" + }, "0d6412bc3ebb1d58bdd9cbcef774dacf9016fa402af5c1b4e339b9a3d7163d5e": { "describe": { "columns": [ @@ -1289,6 +1306,24 @@ }, "query": "DELETE FROM usr_to_group WHERE group_ = $1 AND workspace_id = $2" }, + "52c8b4350235bdaab4df79e517d5e42a61a4e1e209d120b2c8bb31ebb7ce1e56": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Left": [ + "Varchar", + "Varchar", + "Varchar", + "Bool", + "Varchar", + "Int4", + "Bool" + ] + } + }, + "query": "INSERT INTO variable\n (workspace_id, path, value, is_secret, description, account, is_oauth)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (workspace_id, path) DO UPDATE SET value = $3" + }, "541ebd3bac65431237cf3b882dfdcd61ca97c253d9754d05bba59fda89841067": { "describe": { "columns": [ diff --git a/backend/windmill-api/src/oauth2.rs b/backend/windmill-api/src/oauth2.rs index 8754a54159..6cde5d8033 100644 --- a/backend/windmill-api/src/oauth2.rs +++ b/backend/windmill-api/src/oauth2.rs @@ -610,6 +610,39 @@ async fn connect_slack_callback( ) .execute(&mut tx) .await?; + + let token_path = "g/slack/bot_token"; + let mc = build_crypt(&mut tx, &w_id).await?; + let value = encrypt(&mc, &token.bot.bot_access_token); + sqlx::query!( + "INSERT INTO variable + (workspace_id, path, value, is_secret, description, account, is_oauth) + VALUES ($1, $2, $3, $4, $5, $6, $7) + ON CONFLICT (workspace_id, path) DO UPDATE SET value = $3", + &w_id, + token_path, + value, + true, + "The slack bot token to act on behalf of the installed app of the connected workspace", + None::, + true, + ) + .execute(&mut tx) + .await?; + + sqlx::query!( + "INSERT INTO resource + (workspace_id, path, value, description, resource_type, is_oauth) + VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (workspace_id, path) DO UPDATE SET value = $3", + w_id, + token_path, + serde_json::json!({ "token": format!("$var:{token_path}") }), + "The slack bot token to act on behalf of the installed app of the connected workspace", + "slack", + true + ) + .execute(&mut tx) + .await?; tx.commit().await?; Ok("slack workspace connected".to_string()) } diff --git a/frontend/src/routes/oauth/callback_slack.svelte b/frontend/src/routes/oauth/callback_slack.svelte index 9489683681..664fb9f017 100644 --- a/frontend/src/routes/oauth/callback_slack.svelte +++ b/frontend/src/routes/oauth/callback_slack.svelte @@ -5,8 +5,6 @@ import { onMount } from 'svelte' import { OauthService } from '$lib/gen' import { workspaceStore, oauthStore } from '$lib/stores' - import Icon from 'svelte-awesome' - import { faSpinner } from '@fortawesome/free-solid-svg-icons' import CenteredPage from '$lib/components/CenteredPage.svelte' import PageHeader from '$lib/components/PageHeader.svelte' import WindmillIcon from '$lib/components/icons/WindmillIcon.svelte' @@ -19,11 +17,14 @@ if (error) { sendUserToast(`Error trying to add slack connection: ${error}`, true) } else if (code && state) { - await OauthService.connectSlackCallback({ + const token = await OauthService.connectSlackCallback({ workspace: $workspaceStore!, requestBody: { code, state } }) - sendUserToast('Slack workspace connected to your Windmill workspace') + oauthStore.set({ access_token: token }) + sendUserToast( + 'Slack workspace connected to your Windmill workspace and slack token saved at `g/slack/bot_token`.' + ) } else { sendUserToast('Missing code or state as query params', true) } diff --git a/frontend/src/routes/workspace_settings.svelte b/frontend/src/routes/workspace_settings.svelte index 6a884a7410..f6ba26ed2d 100644 --- a/frontend/src/routes/workspace_settings.svelte +++ b/frontend/src/routes/workspace_settings.svelte @@ -18,7 +18,7 @@ import type { User } from '$lib/gen' import { sendUserToast, msToSec } from '$lib/utils' import PageHeader from '$lib/components/PageHeader.svelte' - import { userStore, usersWorkspaceStore, workspaceStore, oauthStore } from '$lib/stores' + import { userStore, usersWorkspaceStore, workspaceStore } from '$lib/stores' import CenteredPage from '$lib/components/CenteredPage.svelte' import { faSlack } from '@fortawesome/free-brands-svg-icons' import TableCustom from '$lib/components/TableCustom.svelte' @@ -26,9 +26,9 @@ import InviteUser from '$lib/components/InviteUser.svelte' import ScriptPicker from '$lib/components/ScriptPicker.svelte' import AppConnect from '$lib/components/AppConnect.svelte' - import { onMount } from 'svelte' import { Button } from '$lib/components/common' import Tooltip from '$lib/components/Tooltip.svelte' + import { faScroll, faWind } from '@fortawesome/free-solid-svg-icons' let users: User[] = [] let invites: WorkspaceInvite[] = [] @@ -37,7 +37,6 @@ let scriptPath: string let team_name: string | undefined - let appConnect: AppConnect const fuseOptions = { includeScore: false, keys: ['username', 'email'] @@ -101,15 +100,8 @@ loadSlack() } } - - onMount(() => { - if ($oauthStore) { - appConnect.openFromOauth('slack') - } - }) - {#if $userStore?.is_admin} @@ -206,26 +198,39 @@

Status: {#if team_name}Connected to slack workspace {team_name}{:else}Not connected{/if}

-
+ + {#if team_name} +
+ + + +
+ {:else} -
- {#if team_name} - {/if}

Script to run on /windmill command