Skip to content

Commit

Permalink
Updated the routes (#9)
Browse files Browse the repository at this point in the history
* Updated the routes

* Pushing small changes

* Um

* Updated Libraries
  • Loading branch information
patela22 authored May 20, 2024
1 parent 9a3b1f9 commit 1f5bd73
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/node_modules
/.pnp
.pnp.js
bun.lockb
*.lockb

# testing
/coverage
Expand Down Expand Up @@ -42,4 +42,4 @@ yarn-error.log*
# typescript
*.tsbuildinfo

bun.lockb
*.lockb
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,6 +15,7 @@ export const appRouter = createTRPCRouter({
github: gitHubRouter,
greeting: greeting,
database: database,
webhook: githubWebhookRouter,
});

// export type definition of API
Expand Down
63 changes: 63 additions & 0 deletions src/server/api/routers/prisma.ts
Original file line number Diff line number Diff line change
@@ -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 };
}),
});

0 comments on commit 1f5bd73

Please sign in to comment.