Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
botway(core): add integrations api
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed May 17, 2023
1 parent 318b708 commit aef5b6c
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 130 deletions.
34 changes: 34 additions & 0 deletions core/app/api/integrations/new/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { NextResponse } from "next/server";
import { fetcher } from "@/tools/fetch";
import { jwtDecrypt } from "jose";
import { BW_SECRET_KEY } from "@/tools/tokens";
import createClient from "@/supabase/server";

export const revalidate = 0;

export async function POST(request: Request) {
const body = await request.json();

const supabase = createClient();

const {
data: { user: user },
error: userError,
} = await supabase.auth.getUser();

if (userError) {
return NextResponse.json({ error: userError });
}

const { payload: githubApiToken } = await jwtDecrypt(
user?.user_metadata["githubApiToken"],
BW_SECRET_KEY
);

const { payload: railwayApiToken } = await jwtDecrypt(
user?.user_metadata["railwayApiToken"],
BW_SECRET_KEY
);

return NextResponse.json({ message: "Success" });
}
182 changes: 180 additions & 2 deletions core/app/project/[id]/integrations/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ import {
} from "@primer/octicons-react";
import { marked } from "marked";
import { fetcher } from "@/tools/fetch";
import { Fragment, useState } from "react";
import { toast } from "react-hot-toast";
import { toastStyle } from "@/tools/toast-style";
import { Dialog, Transition } from "@headlessui/react";
import { Field, Form, Formik } from "formik";
import { Button } from "@/components/Button";

export const revalidate = 0;

const queryClient = new QueryClient();

const Project = ({ user, projectId, slug }: any) => {
const [isLoading, setIsLoading] = useState(false);
const [open, setOpen] = useState(false);

const fetchProject = async () => {
const { data: project } = await supabase
.from("projects")
Expand Down Expand Up @@ -63,6 +72,64 @@ const Project = ({ user, projectId, slug }: any) => {
}
);

async function addIntegration(formData: any) {
try {
setIsLoading(true);

let vars = {};

if (int.variables) {
if (int.variables.length === 1) {
vars = { v1: formData.v1 };
} else {
vars = { v1: formData.v1, v2: formData.v2 };
}
}

const body = {
name: int.name,
slug: int.slug,
template_repo: int.template_repo,
template_code: int.template_code,
is_plugin: int.is_plugin,
vars,
};

const newBot = await fetcher("/api/integrations/new", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});

if (newBot.message === "Success") {
toast.success(
"You have successfully created a new bot project",
toastStyle
);
} else {
toast.error(newBot.error, toastStyle);
}
} catch (e: any) {
toast.error(e.message, toastStyle);
} finally {
setIsLoading(false);
}
}

const check = (int: any) => {
if (!int.is_plugin) {
setOpen(true);
} else {
addIntegration({
name: int.title,
slug: int.slug,
template_repo: int.template_repo,
template_code: int.template_code,
is_plugin: int.is_plugin,
});
}
};

return (
<>
{projectIsLoading || integrationsIsLoading ? (
Expand Down Expand Up @@ -94,7 +161,10 @@ const Project = ({ user, projectId, slug }: any) => {
</p>
<p className="text-base text-gray-400">{int?.desc}</p>
</div>
<a className="flex md:!hidden items-center justify-center border transition-all duration-200 active:scale-95 outline-none focus:outline-none bg-blue-700 border-gray-800 text-white hover:opacity-90 h-[42px] py-2 px-3 rounded-lg text-base leading-6 space-x-3">
<a
onClick={() => check(int)}
className="flex md:!hidden cursor-pointer items-center justify-center border transition-all duration-200 active:scale-95 outline-none focus:outline-none bg-blue-700 border-gray-800 text-white hover:opacity-90 h-[42px] py-2 px-3 rounded-lg text-base leading-6 space-x-3"
>
<span className="inline-block">Add {int?.name}</span>
</a>
</div>
Expand All @@ -112,7 +182,10 @@ const Project = ({ user, projectId, slug }: any) => {
</div>
</div>
<div className="w-full lg:w-3/12 lg:mt-6 lg:sticky lg:top-[48px] align-self[flex-start] flex flex-col">
<a className="hidden md:flex items-center justify-center border transition-all duration-200 active:scale-95 outline-none focus:outline-none lg:!flex bg-blue-700 border-gray-800 text-white hover:opacity-90 h-[42px] py-2 px-3 rounded-lg text-base leading-6 space-x-3">
<a
onClick={() => check(int)}
className="hidden md:flex cursor-pointer items-center justify-center border transition-all duration-200 active:scale-95 outline-none focus:outline-none lg:!flex bg-blue-700 border-gray-800 text-white hover:opacity-90 h-[42px] py-2 px-3 rounded-lg text-base leading-6 space-x-3"
>
<span className="inline-block">Add {int?.name}</span>
</a>
<div className="mt-16 flex flex-col space-y-6">
Expand Down Expand Up @@ -172,6 +245,111 @@ const Project = ({ user, projectId, slug }: any) => {
</div>
</div>
</div>

<Transition.Root show={open} as={Fragment}>
<Dialog as="div" className="relative z-10" onClose={setOpen}>
<Transition.Child
as={Fragment}
enter="ease-in-out duration-500"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in-out duration-500"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-bwdefualt bg-opacity-50 transition-opacity" />
</Transition.Child>

<div className="fixed inset-0 overflow-hidden">
<div className="absolute inset-0 overflow-hidden">
<div className="pointer-events-none fixed inset-y-0 right-0 flex max-w-full pl-10">
<Transition.Child
as={Fragment}
enter="transform transition ease-in-out duration-500 sm:duration-700"
enterFrom="translate-x-full"
enterTo="translate-x-0"
leave="transform transition ease-in-out duration-500 sm:duration-700"
leaveFrom="translate-x-0"
leaveTo="translate-x-full"
>
<Dialog.Panel className="pointer-events-auto relative w-screen max-w-md">
<Transition.Child
as={Fragment}
enter="ease-in-out duration-200"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in-out duration-500"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="absolute left-0 top-0 -ml-8 flex pr-2 pt-4 sm:-ml-10 sm:pr-4"></div>
</Transition.Child>
<div className="flex h-full flex-col overflow-y-scroll bg-secondary border-l border-gray-800 py-4 shadow-xl">
<div className="px-4 border-b border-gray-800 sm:px-6">
<Dialog.Title className="text-lg font-semibold text-white leading-6 pb-4">
Add Integration To My Project
</Dialog.Title>
</div>
<div className="relative mt-4 flex-1 px-4 sm:px-6">
<div className="my-4 max-w-4xl space-y-8">
<Formik
initialValues={{
slug: int.slug,
v1: "",
v2: "",
}}
onSubmit={addIntegration}
>
{({ errors }) => (
<>
<Form className="column w-full">
<div>
{int.variables ? (
int.variables.map((varx: any) => (
<>
<label className="text-white col-span-12 text-base lg:col-span-5">
{varx.name}
</label>

<div className="pt-2" />

<Field
className="input"
id={`v${varx.index}`}
name={`v${varx.index}`}
type="text"
/>

<div className="pb-2" />
</>
))
) : (
<></>
)}
</div>
<br />

<Button
htmlType="submit"
type="success"
loading={isLoading}
>
Add
</Button>
</Form>
</>
)}
</Formik>
</div>
</div>
</div>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</div>
</Dialog>
</Transition.Root>
</ProjectLayout>
)}
</>
Expand Down
6 changes: 3 additions & 3 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"@headlessui/react": "^1.7.14",
"@primer/octicons-react": "^19.1.0",
"@supabase/auth-helpers-nextjs": "^0.6.1",
"@supabase/supabase-js": "^2.21.0",
"@tanstack/react-query": "^4.29.5",
"@supabase/supabase-js": "^2.22.0",
"@tanstack/react-query": "^4.29.7",
"ajv": "^8.12.0",
"autoprefixer": "10.4.14",
"bcryptjs": "^2.4.3",
Expand Down Expand Up @@ -47,7 +47,7 @@
"@tailwindcss/typography": "^0.5.9",
"@types/bcryptjs": "^2.4.2",
"@types/marked": "^4.3.0",
"@types/node": "^20.1.3",
"@types/node": "^20.1.7",
"@types/react": "18.2.6",
"@types/react-dom": "18.2.4",
"@types/slug": "^5.0.3",
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/cli/shurcooL-graphql v0.0.3 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.9.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
Expand Down Expand Up @@ -85,7 +85,7 @@ require (
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9 // indirect
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.9.0 h1:pTK/l/3qYIKaRXuHnEnIf7Y5NxfRPfpb7dis6/gdlVI=
github.com/dlclark/regexp2 v1.9.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand All @@ -136,6 +138,7 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
Expand Down Expand Up @@ -239,6 +242,7 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -317,6 +321,7 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
Expand All @@ -330,6 +335,8 @@ github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"build": "turbo run build",
"dev": "turbo run dev --parallel",
"start": "pnpm build && turbo run start --parallel",
"format": "prettier *.json *.yaml *.md ./core ./assets ./create-botway-bot ./packages -w"
"format": "prettier *.json *.yaml *.md ./core ./assets ./create-botway-bot ./packages ./strg -w"
},
"packageManager": "[email protected]",
"devDependencies": {
"prettier": "^2.8.8",
"turbo": "^1.9.4"
"turbo": "^1.9.6"
}
}
Loading

0 comments on commit aef5b6c

Please sign in to comment.