From 1f5bd7377e0c5696bdd871e5701ae503e7b1f641 Mon Sep 17 00:00:00 2001 From: Ansh P <39590072+patela22@users.noreply.github.com> Date: Mon, 20 May 2024 19:47:48 -0400 Subject: [PATCH] Updated the routes (#9) * Updated the routes * Pushing small changes * Um * Updated Libraries --- .gitignore | 4 +- package.json | 3 +- src/server/api/root.ts | 2 + src/server/api/routers/prisma.ts | 63 ++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/server/api/routers/prisma.ts diff --git a/.gitignore b/.gitignore index a0768bd..1a35e8e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ /node_modules /.pnp .pnp.js -bun.lockb +*.lockb # testing /coverage @@ -42,4 +42,4 @@ yarn-error.log* # typescript *.tsbuildinfo -bun.lockb \ No newline at end of file +*.lockb \ No newline at end of file diff --git a/package.json b/package.json index 7a9cc5e..2082349 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "dependencies": { "@auth/prisma-adapter": "^1.4.0", "@aws-sdk/client-rds": "^3.572.0", + "@octokit/rest": "^20.1.1", "@prisma/client": "^5.10.2", "@t3-oss/env-nextjs": "^0.10.1", "@tanstack/react-query": "^5.25.0", @@ -31,10 +32,10 @@ "install": "^0.13.0", "next": "^14.2.1", "next-auth": "^4.24.6", + "nextjs-cors": "^2.2.0", "octokit": "^4.0.2", "parse-git-config": "^3.0.0", "parse-github-url": "^1.0.2", - "nextjs-cors": "^2.2.0", "react": "18.2.0", "react-dom": "18.2.0", "server-only": "^0.0.1", diff --git a/src/server/api/root.ts b/src/server/api/root.ts index 4a74563..32ac5a9 100644 --- a/src/server/api/root.ts +++ b/src/server/api/root.ts @@ -3,6 +3,7 @@ import { gitHubRouter } from "./routers/github-router"; import { createCallerFactory, createTRPCRouter } from "~/server/api/trpc"; import { greeting } from "./routers/greeting"; import { database } from "./routers/databases"; +import { githubWebhookRouter } from "./routers/prisma"; /** * This is the primary router for your server. @@ -14,6 +15,7 @@ export const appRouter = createTRPCRouter({ github: gitHubRouter, greeting: greeting, database: database, + webhook: githubWebhookRouter, }); // export type definition of API diff --git a/src/server/api/routers/prisma.ts b/src/server/api/routers/prisma.ts new file mode 100644 index 0000000..0627631 --- /dev/null +++ b/src/server/api/routers/prisma.ts @@ -0,0 +1,63 @@ +import { z } from "zod"; +import { createTRPCRouter, publicProcedure } from "~/server/api/trpc"; +import { Octokit } from "@octokit/rest"; +import { exec } from "child_process"; +import { promisify } from "util"; +import { writeFile } from "fs/promises"; +const execAsync = promisify(exec); + +// Initialize Octokit with an access token +const octokit = new Octokit({ + auth: process.env.GITHUB_ACCESS_TOKEN, +}); + +export const githubWebhookRouter = createTRPCRouter({ + handlePush: publicProcedure + .input( + z.object({ + ref: z.string(), + after: z.string(), + repository: z.object({ + owner: z.string(), + name: z.string(), + }), + installation: z.object({ + id: z.number(), + }), + }), + ) + .mutation(async ({ input }) => { + if (input.ref === "refs/heads/main") { + try { + // Fetch the content of the Prisma schema file using Octokit + const response = await octokit.repos.getContent({ + owner: input.repository.owner, + repo: input.repository.name, + path: "prisma/schema.prisma", + mediaType: { + format: "raw", + }, + }); + + const schemaContent = response.data as unknown as string; + + // Save schemaContent to a local file + await writeFile("prisma/schema.prisma", schemaContent, "utf8"); + + // Apply changes using Prisma + const { stdout, stderr } = await execAsync("npx prisma db push"); + console.log(stdout); + if (stderr) { + console.error("Error during database push:", stderr); + throw new Error("Failed to update database schema"); + } + return { success: true }; + } catch (error) { + console.error("Failed to handle GitHub push:", error); + throw new Error("Failed to handle GitHub push"); + } + } + + return { ignored: true }; + }), +});