route.ts126 lines, 3.2 KB
| 1 | import { NextRequest, NextResponse } from "next/server"; |
| 2 | import crypto from "crypto"; |
| 3 | import { db } from "@/server/db"; |
| 4 | import { inngest } from "@/server/inngest"; |
| 5 | |
| 6 | interface PullRequestPayload { |
| 7 | action: string; |
| 8 | number: number; |
| 9 | pull_request: { |
| 10 | id: number; |
| 11 | number: number; |
| 12 | title: string; |
| 13 | html_url: string; |
| 14 | state: string; |
| 15 | draft: boolean; |
| 16 | }; |
| 17 | repository: { |
| 18 | id: number; |
| 19 | full_name: string; |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | function verifySignature(payload: string, signature: string | null): boolean { |
| 24 | const secret = process.env.GITHUB_WEBHOOK_SECRET; |
| 25 | if (!secret) { |
| 26 | console.warn("GITHUB_WEBHOOK_SECRET not set, skipping verification"); |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | if (!signature) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | const hmac = crypto.createHmac("sha256", secret); |
| 35 | const digest = "sha256=" + hmac.update(payload).digest("hex"); |
| 36 | |
| 37 | return crypto.timingSafeEqual(Buffer.from(digest), Buffer.from(signature)); |
| 38 | } |
| 39 | |
| 40 | export async function POST(request: NextRequest) { |
| 41 | const payload = await request.text(); |
| 42 | const signature = request.headers.get("x-hub-signature-256"); |
| 43 | const event = request.headers.get("x-github-event"); |
| 44 | |
| 45 | // Verify the webhook signature |
| 46 | if (!verifySignature(payload, signature)) { |
| 47 | return NextResponse.json({ error: "Invalid signature" }, { status: 401 }); |
| 48 | } |
| 49 | |
| 50 | // Only handle pull_request events |
| 51 | if (event !== "pull_request") { |
| 52 | return NextResponse.json({ message: "Event ignored" }, { status: 200 }); |
| 53 | } |
| 54 | |
| 55 | const data = JSON.parse(payload) as PullRequestPayload; |
| 56 | |
| 57 | // Only trigger on open, synchronize (new commits), or reopen |
| 58 | if (!["opened", "synchronize", "reopened"].includes(data.action)) { |
| 59 | return NextResponse.json( |
| 60 | { message: `Action '${data.action}' ignored` }, |
| 61 | { status: 200 }, |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | // Skip draft PRs |
| 66 | if (data.pull_request.draft) { |
| 67 | return NextResponse.json({ message: "Draft PR ignored" }, { status: 200 }); |
| 68 | } |
| 69 | |
| 70 | // Find the repository in our database |
| 71 | const repository = await db.repository.findUnique({ |
| 72 | where: { githubId: data.repository.id }, |
| 73 | include: { user: true }, |
| 74 | }); |
| 75 | |
| 76 | if (!repository) { |
| 77 | return NextResponse.json( |
| 78 | { message: "Repository not connected" }, |
| 79 | { status: 200 }, |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | // Check if there's already a review in progress |
| 84 | const existingReview = await db.review.findFirst({ |
| 85 | where: { |
| 86 | repositoryId: repository.id, |
| 87 | prNumber: data.pull_request.number, |
| 88 | status: { in: ["PENDING", "PROCESSING"] }, |
| 89 | }, |
| 90 | }); |
| 91 | |
| 92 | if (existingReview) { |
| 93 | return NextResponse.json( |
| 94 | { message: "Review already in progress" }, |
| 95 | { status: 200 }, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | // Create a new review record |
| 100 | const review = await db.review.create({ |
| 101 | data: { |
| 102 | repositoryId: repository.id, |
| 103 | userId: repository.userId, |
| 104 | prNumber: data.pull_request.number, |
| 105 | prTitle: data.pull_request.title, |
| 106 | prUrl: data.pull_request.html_url, |
| 107 | status: "PENDING", |
| 108 | }, |
| 109 | }); |
| 110 | |
| 111 | // Trigger the Inngest job |
| 112 | await inngest.send({ |
| 113 | name: "review/pr.requested", |
| 114 | data: { |
| 115 | reviewId: review.id, |
| 116 | repositoryId: repository.id, |
| 117 | prNumber: data.pull_request.number, |
| 118 | userId: repository.userId, |
| 119 | }, |
| 120 | }); |
| 121 | |
| 122 | return NextResponse.json( |
| 123 | { message: "Review triggered", reviewId: review.id }, |
| 124 | { status: 200 }, |
| 125 | ); |
| 126 | } |