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: svix integration and create_webhook page #3814

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
63 changes: 35 additions & 28 deletions frontend/src/lib/components/ScriptPicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
export let kinds: Script['kind'][] = ['script']
export let disabled = false
export let allowRefresh = false
export let allowEdit = true

let items: { value: string; label: string }[] = []
let drawerViewer: Drawer
Expand Down Expand Up @@ -126,14 +127,16 @@
{#if scriptPath !== undefined && scriptPath !== ''}
{#if itemKind == 'flow'}
<div class="flex gap-2">
<Button
endIcon={{ icon: ExternalLink }}
target="_blank"
color="light"
size="xs"
variant="border"
href="/flows/edit/{scriptPath}">Edit</Button
>
{#if allowEdit}
<Button
endIcon={{ icon: ExternalLink }}
target="_blank"
color="light"
size="xs"
variant="border"
href="/flows/edit/{scriptPath}">Edit</Button
>
{/if}
<Button
color="light"
size="xs"
Expand All @@ -147,16 +150,18 @@
</div>
{:else if itemKind == 'app'}
<div class="flex gap-2">
<Button
startIcon={{ icon: Pen }}
target="_blank"
color="light"
size="xs"
href="/apps/edit/{scriptPath}"
variant="border"
>
Edit
</Button>
{#if allowEdit}
<Button
startIcon={{ icon: Pen }}
target="_blank"
color="light"
size="xs"
href="/apps/edit/{scriptPath}"
variant="border"
>
Edit
</Button>
{/if}
<Button
color="light"
size="xs"
Expand All @@ -170,16 +175,18 @@
</div>
{:else}
<div class="flex gap-2">
<Button
startIcon={{ icon: Pen }}
target="_blank"
color="light"
size="xs"
href="/scripts/edit/{scriptPath}"
variant="border"
>
Edit
</Button>
{#if allowEdit}
<Button
startIcon={{ icon: Pen }}
target="_blank"
color="light"
size="xs"
href="/scripts/edit/{scriptPath}"
variant="border"
>
Edit
</Button>
{/if}
<Button
color="light"
size="xs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<script lang="ts">
import PageHeader from '$lib/components/PageHeader.svelte'
import ScriptPicker from '$lib/components/ScriptPicker.svelte'
import CenteredPage from '$lib/components/CenteredPage.svelte'
import { Button } from '$lib/components/common'
import { page } from '$app/stores'
import { userStore, workspaceStore } from '$lib/stores'
import { sendUserToast } from '$lib/toast'
import UserSettings from '$lib/components/UserSettings.svelte'
import { generateRandomString } from '$lib/utils'
import Tooltip from '$lib/components/Tooltip.svelte'

let itemPath: string | undefined = undefined
let itemKind: 'script' | 'flow' | 'app' = 'script'
let clientName = $page.url.searchParams.get('name') ?? undefined
let redirectURI = $page.url.searchParams.get('redirect_uri')

let userSettings: UserSettings
let token: string | undefined = undefined
let scopes: string[] = []

$: updateTokenAndScope(itemPath)

async function createWebhook(path: string, kind: 'script' | 'flow' | 'app', token: string) {
try {
let webhook: string
switch (kind) {
case 'script':
webhook = `${$page.url.origin}/api/w/${$workspaceStore}/jobs/run/p/${path}`
redirectSuccess(webhook, token)
break
case 'flow':
webhook = `${$page.url.origin}/api/w/${$workspaceStore}/jobs/run/f/${path}`
redirectSuccess(webhook, token)
break
default:
sendUserToast('Invalid item type', true)
break
}
} catch (error) {
sendUserToast(error, true)
}
}

function redirectSuccess(webhook: string, token: string) {
const webhook_url = new URL(webhook)
webhook_url.searchParams.append('token', token)
const url = new URL(redirectURI!)
url.searchParams.append('webhook_url', webhook_url.toString())
window.location.href = url.toString()
}

function redirectCancel(e: string): void {
const url = new URL(redirectURI!)
url.searchParams.append('error', e)
window.location.href = url.toString()
}

function updateTokenAndScope(scriptPath: string | undefined) {
if (!scriptPath) {
scopes = []
token = undefined
return
}

token = undefined
scopes = [`run:${itemKind}/${scriptPath}`]
}
</script>

<UserSettings
bind:this={userSettings}
on:tokenCreated={(e) => {
token = e.detail
}}
newTokenLabel={`webhook-${$userStore?.username ?? 'superadmin'}-${generateRandomString(4)}`}
{scopes}
/>

<CenteredPage>
<div class="flex items-center justify-center h-screen">
<div class="flex flex-col gap-5">
<div class="flex flex-row gap-3 items-center justify-center">
<PageHeader title={`${clientName} wants to create a webhook`} />
</div>
<div class="flex flex-row gap-3 items-center">
<div
class="flex items-center justify-center w-10 h-10 border-2 border-blue-500 text-blue-500 font-bold rounded-full"
>
1
</div>
<h2>Select the script or flow to be triggered by the webhook</h2>
</div>
<div class="flex flex-row items-center justify-center w-100%">
<div class="flex flex-col items-center gap-3 w-full">
<ScriptPicker
allowEdit={false}
allowFlow={true}
allowRefresh={true}
bind:scriptPath={itemPath}
bind:itemKind
/>
<h4>or</h4>
<div class="flex flex-row gap-2">
<Button size="xs" color="light" variant="border" target="_blank" href="/scripts/add">Create New Script</Button>
<Button size="xs" color="light" variant="border" target="_blank" href="/flows/add?nodraft=true">Create New Flow</Button>
</div>
</div>
</div>
<div class="flex flex-row gap-3 items-center mt-3">
<div
class="flex items-center justify-center w-10 h-10 border-2 border-blue-500 text-blue-500 font-bold rounded-full"
>
2
</div>
<h2>Create a token for this webhook</h2>
<Tooltip customSize="150%">
If you generate a token, it will have a scope such that it can only be used to
trigger this {itemKind}. It is safe to share as it cannot be used to impersonate you.
Otherwise you may enter another token but beware that it will be shared with the external
application.
</Tooltip>
</div>
<div class="flex flex-row items-center justify-center">
<div class="flex flex-col items-center gap-5 w-100%">
<div class="flex flex-row items-center gap-2">
<input
bind:value={token}
disabled={itemPath == undefined}
placeholder="enter or generate token"
class="!text-md"
/>
<Button size="md" disabled={itemPath == undefined} on:click={userSettings.openDrawer}>
Generate webhook-specific Token
</Button>
</div>
<div class="flex flex-row gap-2 mt-10">
<Button
disabled={!itemPath || !token || token === ''}
on:click={() => itemPath && token && createWebhook(itemPath, itemKind, token)}
>Approve and share with {clientName}</Button
>
<Button color="red" on:click={() => redirectCancel('user_canceled')}>Cancel</Button>
</div>
</div>
</div>
</div>
</div>
</CenteredPage>
Loading