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

Commit

Permalink
botway(core): support botway CE for bots, thanks @coder
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Jun 2, 2023
1 parent 82a18c7 commit f97ba38
Show file tree
Hide file tree
Showing 13 changed files with 677 additions and 177 deletions.
103 changes: 103 additions & 0 deletions core/app/api/ce/enable/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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: password } = await jwtDecrypt(body.password, 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;

const query = `
mutation {
templateDeploy(input: {
services: [
{
hasDomain: true
isPrivate: true
owner: "${ghu.login}"
name: "bwce-${body.slug}-${faker.number.int({ max: 100 })}"
serviceName: "CE"
template: "https://github.com/botwayorg/ce"
variables: {GIT_REPO: "https://github.com/${
body.repo
}" GITHUB_TOKEN: "${githubApiToken.data}" PASSWORD: "${
password.data
}"}
}
]
projectId: "${projectId.data}"
}) {
projectId
}
}
`;

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

if (enable.errors) {
console.log(enable.errors);

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

const { error } = await supabase
.from("projects")
.update({
enable_ce: true,
})
.eq("railway_project_id", body.projectId);

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

return NextResponse.json({ message: "Success" });
}
103 changes: 103 additions & 0 deletions core/app/api/ce/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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: railwayApiToken } = await jwtDecrypt(
user?.user_metadata["railwayApiToken"],
BW_SECRET_KEY
);

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

const query = `
query {
project(id: "${projectId.data}") {
services {
edges {
node {
id
serviceInstances {
edges {
node {
domains {
customDomains {
domain
}
serviceDomains {
domain
}
}
source {
repo
template {
serviceSource
}
}
}
}
}
}
}
}
}
}
`;

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

if (check.errors) {
console.log(check.errors);

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

const domainNode = check.data.project.services.edges.find((srv: any) =>
srv.node.serviceInstances.edges.find(
(si: any) =>
si.node.source.template?.serviceSource ===
"https://github.com/botwayorg/ce"
)
).node.serviceInstances.edges[0].node.domains;

let domain;

if (domainNode.customDomains.length != 0) {
domain = domainNode.customDomains[0].domain;
} else {
domain = domainNode.serviceDomains[0].domain;
}

return NextResponse.json({ message: "Success", domain });
}
3 changes: 0 additions & 3 deletions core/app/api/projects/config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { BW_SECRET_KEY } from "@/tools/tokens";
import { fetcher } from "@/tools/fetch";
import { Octokit } from "octokit";
import createClient from "@/supabase/server";
import { exec } from "child_process";
import { stringify } from "ajv";

// export const runtime = "edge";
export const revalidate = 0;

export async function POST(request: Request) {
Expand Down
2 changes: 1 addition & 1 deletion core/app/api/projects/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function POST(request: Request) {
query: `
mutation serviceCreate {
serviceCreate(input: {
name: "main",
name: "Main",
projectId: "${createRailwayProject.data.projectCreate.id}"
}) {
id
Expand Down
Loading

0 comments on commit f97ba38

Please sign in to comment.