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

Commit

Permalink
botway(core): finish from integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed May 19, 2023
1 parent aef5b6c commit 3a4d892
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 200 deletions.
97 changes: 97 additions & 0 deletions core/app/api/integrations/add/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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";
import { Octokit } from "octokit";
import { faker } from "@faker-js/faker";

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
);

const { payload: projectId } = await jwtDecrypt(
body.projectId,
BW_SECRET_KEY
);

const octokit = new Octokit({
auth: githubApiToken.data,
});

const ghu = await (await octokit.request("GET /user", {})).data;

let vars;

if (body.vars) {
if (body.vars.k) {
vars = `variables: {${body.vars.k}: "${body.vars.v}"}`;
} else {
vars = `variables: {${body.vars.k1}: "${body.vars.v1}" ${body.vars.k2}: "${body.vars.v2}"}`;
}
}

const query = `
mutation {
templateDeploy(input: {
services: [
{
hasDomain: true
isPrivate: true
owner: "${ghu.login}"
name: "${
body.slug + "-" + faker.word.sample() + "-" + faker.word.sample()
}"
serviceName: "${
body.slug + "-" + faker.word.sample() + "-" + faker.word.sample()
}"
template: "https://github.com/${body.template_repo}"
${vars}
}
]
projectId: "${projectId.data}"
}) {
projectId
}
}`;

const deploy = await fetcher("https://backboard.railway.app/graphql/v2", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${railwayApiToken.data}`,
},
body: JSON.stringify({
query,
}),
});

if (deploy.errors) {
return NextResponse.json({ message: deploy.errors[0].message });
}

return NextResponse.json({ message: "Success" });
}
34 changes: 0 additions & 34 deletions core/app/api/integrations/new/route.ts

This file was deleted.

16 changes: 13 additions & 3 deletions core/app/project/[id]/integrations/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ const Project = ({ user, projectId, slug }: any) => {

if (int.variables) {
if (int.variables.length === 1) {
vars = { v1: formData.v1 };
vars = { v: formData.v1, k: int.variables[0].name };
} else {
vars = { v1: formData.v1, v2: formData.v2 };
vars = {
v1: formData.v1,
k1: int.variables[0].name,
v2: formData.v2,
k2: int.variables[1].name,
};
}
}

Expand All @@ -92,10 +97,11 @@ const Project = ({ user, projectId, slug }: any) => {
template_repo: int.template_repo,
template_code: int.template_code,
is_plugin: int.is_plugin,
projectId: project?.railway_project_id,
vars,
};

const newBot = await fetcher("/api/integrations/new", {
const newBot = await fetcher("/api/integrations/add", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
Expand All @@ -106,8 +112,12 @@ const Project = ({ user, projectId, slug }: any) => {
"You have successfully created a new bot project",
toastStyle
);

setOpen(false);
} else {
toast.error(newBot.error, toastStyle);

setOpen(false);
}
} catch (e: any) {
toast.error(e.message, toastStyle);
Expand Down
9 changes: 5 additions & 4 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@faker-js/faker": "^8.0.1",
"@headlessui/react": "^1.7.14",
"@primer/octicons-react": "^19.1.0",
"@supabase/auth-helpers-nextjs": "^0.6.1",
Expand All @@ -28,8 +29,8 @@
"jose": "^4.14.4",
"marked": "^5.0.2",
"mini-svg-data-uri": "^1.4.4",
"next": "13.4.2",
"octokit": "^2.0.14",
"next": "13.4.3",
"octokit": "^2.0.16",
"postcss": "8.4.23",
"postcss-nested": "^6.0.1",
"react": "18.2.0",
Expand All @@ -46,8 +47,8 @@
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
"@types/bcryptjs": "^2.4.2",
"@types/marked": "^4.3.0",
"@types/node": "^20.1.7",
"@types/marked": "^5.0.0",
"@types/node": "^20.2.1",
"@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 @@ -69,7 +69,7 @@ require (
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
github.com/microcosm-cc/bluemonday v1.0.24 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
Expand All @@ -89,7 +89,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/stretchr/testify v1.8.3 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyex
github.com/microcosm-cc/bluemonday v1.0.21/go.mod h1:ytNkv4RrDrLJ2pqlsSI46O6IVXmZOBBD4SaJyDwwTkM=
github.com/microcosm-cc/bluemonday v1.0.23 h1:SMZe2IGa0NuHvnVNAZ+6B38gsTbi5e4sViiWJyDDqFY=
github.com/microcosm-cc/bluemonday v1.0.23/go.mod h1:mN70sk7UkkF8TUr2IGBpNN0jAgStuPzlK76QuruE/z4=
github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO6R7uFTPlw=
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
Expand Down Expand Up @@ -360,6 +362,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"packageManager": "[email protected]",
"devDependencies": {
"prettier": "^2.8.8",
"turbo": "^1.9.6"
"turbo": "^1.9.8"
}
}
Loading

0 comments on commit 3a4d892

Please sign in to comment.