Skip to content

Commit

Permalink
feat: add slack_bot token on connecting workspace to slack
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed Nov 15, 2022
1 parent 381b036 commit b3178d1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 31 deletions.
35 changes: 35 additions & 0 deletions backend/sqlx-data.json
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down
33 changes: 33 additions & 0 deletions backend/windmill-api/src/oauth2.rs
Expand Up @@ -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::<i32>,
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())
}
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/routes/oauth/callback_slack.svelte
Expand Up @@ -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'
Expand All @@ -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)
}
Expand Down
59 changes: 32 additions & 27 deletions frontend/src/routes/workspace_settings.svelte
Expand Up @@ -18,17 +18,17 @@
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'
import { goto } from '$app/navigation'
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[] = []
Expand All @@ -37,7 +37,6 @@
let scriptPath: string
let team_name: string | undefined
let appConnect: AppConnect
const fuseOptions = {
includeScore: false,
keys: ['username', 'email']
Expand Down Expand Up @@ -101,15 +100,8 @@
loadSlack()
}
}
onMount(() => {
if ($oauthStore) {
appConnect.openFromOauth('slack')
}
})
</script>

<AppConnect bind:this={appConnect} />
<CenteredPage>
{#if $userStore?.is_admin}
<PageHeader title="Workspace Settings of {$workspaceStore}" />
Expand Down Expand Up @@ -206,26 +198,39 @@
<p class="text-xs text-gray-700 my-1">
Status: {#if team_name}Connected to slack workspace {team_name}{:else}Not connected{/if}
</p>
<div class="flex justify-start">

{#if team_name}
<div class="flex flex-col gap-2 max-w-sm">
<Button
size="sm"
endIcon={{ icon: faSlack }}
btnClasses="mt-2"
variant="border"
on:click={async () => {
await OauthService.disconnectSlack({
workspace: $workspaceStore ?? ''
})
loadSlack()
sendUserToast('Disconnected Slack')
}}
>
Disconnect Slack
</Button>
<Button
size="sm"
endIcon={{ icon: faScroll }}
href="/scripts/add?hub=hub%2F314%2Fslack%2Fexample_of_responding_to_a_slack_command_slack"
>
Create a script to handle slack commands
</Button>
<Button size="sm" endIcon={{ icon: faWind }} href="/flows/add?hub=28">
Create a flow to handle slack commands
</Button>
</div>
{:else}
<Button size="sm" endIcon={{ icon: faSlack }} href="/api/oauth/connect_slack">
Connect to Slack
</Button>
</div>
{#if team_name}
<Button
size="sm"
endIcon={{ icon: faSlack }}
btnClasses="mt-2"
on:click={async () => {
await OauthService.disconnectSlack({
workspace: $workspaceStore ?? ''
})
loadSlack()
sendUserToast('Disconnected Slack')
}}
>
Disconnect Slack
</Button>
{/if}
<h3 class="mt-5 text-gray-700"
>Script to run on /windmill command <Tooltip>
Expand Down

0 comments on commit b3178d1

Please sign in to comment.