mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-21 22:05:23 +02:00
90 lines
2.3 KiB
TypeScript
90 lines
2.3 KiB
TypeScript
import { createGithub, validateRequest } from "@dokploy/server";
|
|
import { db } from "@dokploy/server/db";
|
|
import { hasPermission } from "@dokploy/server/services/permission";
|
|
import { eq } from "drizzle-orm";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { Octokit } from "octokit";
|
|
import { github } from "@/server/db/schema";
|
|
|
|
type Query = {
|
|
code: string;
|
|
state: string;
|
|
installation_id: string;
|
|
setup_action: string;
|
|
};
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
) {
|
|
const { code, state, installation_id }: Query = req.query as Query;
|
|
|
|
if (!code) {
|
|
return res.status(400).json({ error: "Missing code parameter" });
|
|
}
|
|
|
|
const { user, session } = await validateRequest(req);
|
|
if (!user || !session?.activeOrganizationId) {
|
|
return res.status(401).json({ error: "Unauthorized" });
|
|
}
|
|
const ctx = {
|
|
user: { id: user.id },
|
|
session: { activeOrganizationId: session.activeOrganizationId },
|
|
};
|
|
|
|
const [action] = state?.split(":") ?? [];
|
|
if (!(await hasPermission(ctx, { gitProviders: ["create"] }))) {
|
|
return res.status(403).json({ error: "Forbidden" });
|
|
}
|
|
|
|
if (action === "gh_init") {
|
|
const octokit = new Octokit({});
|
|
const { data } = await octokit.request(
|
|
"POST /app-manifests/{code}/conversions",
|
|
{
|
|
code: code as string,
|
|
},
|
|
);
|
|
|
|
await createGithub(
|
|
{
|
|
name: data.name,
|
|
githubAppName: data.html_url,
|
|
githubAppId: data.id,
|
|
githubClientId: data.client_id,
|
|
githubClientSecret: data.client_secret,
|
|
githubWebhookSecret: data.webhook_secret,
|
|
githubPrivateKey: data.pem,
|
|
},
|
|
session.activeOrganizationId,
|
|
user.id,
|
|
);
|
|
} else if (action === "gh_setup") {
|
|
const githubId = state?.split(":")[1];
|
|
if (!githubId) {
|
|
return res.status(400).json({ error: "Missing github provider id" });
|
|
}
|
|
|
|
const provider = await db.query.github.findFirst({
|
|
where: eq(github.githubId, githubId),
|
|
with: { gitProvider: true },
|
|
});
|
|
if (
|
|
!provider ||
|
|
provider.gitProvider.organizationId !== session.activeOrganizationId
|
|
) {
|
|
return res.status(404).json({ error: "Github provider not found" });
|
|
}
|
|
|
|
await db
|
|
.update(github)
|
|
.set({
|
|
githubInstallationId: installation_id,
|
|
})
|
|
.where(eq(github.githubId, githubId))
|
|
.returning();
|
|
}
|
|
|
|
res.redirect(307, "/dashboard/settings/git-providers");
|
|
}
|