-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,174 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Context, MiddlewareHandler } from "hono"; | ||
import { getCookie } from "hono/cookie"; | ||
import type { GetServerSidePropsContext } from "next"; | ||
import type { DefaultSession } from "next-auth"; | ||
import { getServerAuthSession } from "server/common/get-server-auth-session"; | ||
import type { UserSession } from "types/next-auth"; | ||
|
||
export async function getHonoSession(c: Context) { | ||
return await getServerAuthSession({ | ||
req: { | ||
...c.req.raw.clone(), | ||
cookies: getCookie(c), | ||
} as unknown as GetServerSidePropsContext["req"], | ||
res: { | ||
...c.res, | ||
getHeader: (h: string) => c.req.header(h), | ||
setHeader: (h: string, v: string) => c.header(h, v), | ||
} as unknown as GetServerSidePropsContext["res"], | ||
}); | ||
} | ||
|
||
export const authMiddleware: MiddlewareHandler<{ | ||
Variables: { | ||
user: UserSession & DefaultSession["user"]; | ||
}; | ||
}> = async (c, next) => { | ||
const session = await getHonoSession(c); | ||
if (!session || !session.user) { | ||
return c.json({ error: "Unauthorized" }, { status: 401 }); | ||
} | ||
c.set("user", session.user); | ||
return next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { zValidator } from "@hono/zod-validator"; | ||
import { authMiddleware } from "api/helpers"; | ||
import { Hono } from "hono"; | ||
import { prisma } from "server/db/client"; | ||
import { z } from "zod"; | ||
import { githubApp } from "server/common/github-app"; | ||
import type { GithubIntegrationSettings } from "server/common/integrations"; | ||
|
||
export function makeIntegrationsRoute() { | ||
return new Hono() | ||
.get( | ||
"/github", | ||
zValidator( | ||
"query", | ||
z.object({ | ||
projectId: z.string(), | ||
}) | ||
), | ||
authMiddleware, | ||
async (c) => { | ||
const user = c.get("user"); | ||
const { projectId } = c.req.valid("query"); | ||
const project = await prisma.project.findFirst({ | ||
where: { id: projectId, users: { some: { userId: user.id } } }, | ||
include: { integrations: true }, | ||
}); | ||
const referrer = new URL(c.req.header("Referer") ?? "/"); | ||
|
||
if (!project) { | ||
referrer.searchParams.set("error", "Unauthorized"); | ||
return c.redirect(referrer.toString()); | ||
} | ||
if (project.integrations.some((i) => i.type === "GITHUB")) { | ||
referrer.searchParams.set("error", "Integration already exists"); | ||
return c.redirect(referrer.toString()); | ||
} | ||
|
||
const searchParams = new URLSearchParams(); | ||
searchParams.set("projectId", projectId); | ||
|
||
return c.redirect( | ||
await githubApp.getInstallationUrl({ state: searchParams.toString() }) | ||
); | ||
} | ||
) | ||
.get( | ||
"/github/setup", | ||
zValidator( | ||
"query", | ||
z.object({ | ||
installation_id: z.string().transform(Number), | ||
setup_action: z.enum(["install", "update"]), | ||
state: z.string().transform((s) => { | ||
const url = new URLSearchParams(s); | ||
const projectId = url.get("projectId"); | ||
if (!projectId) { | ||
throw new Error("projectId not found in state"); | ||
} | ||
return { projectId }; | ||
}), | ||
}) | ||
), | ||
async (c) => { | ||
const { installation_id, setup_action, state } = c.req.valid("query"); | ||
if (setup_action === "update") { | ||
return c.json({ message: "Update not implemented" }, { status: 501 }); | ||
} | ||
|
||
const project = await prisma.project.findFirst({ | ||
where: { id: state.projectId }, | ||
include: { integrations: true }, | ||
}); | ||
if (!project) { | ||
return c.json( | ||
{ message: "Project not found" }, | ||
{ | ||
status: 404, | ||
} | ||
); | ||
} | ||
await prisma.integration.create({ | ||
data: { | ||
type: "GITHUB", | ||
projectId: project.id, | ||
settings: { | ||
installationId: installation_id, | ||
repositoryIds: [], | ||
} satisfies GithubIntegrationSettings, | ||
}, | ||
}); | ||
return c.redirect(`/projects/${project.id}/settings`); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.