@@ -2,24 +2,12 @@ import { NextRequest, NextResponse } from "next/server"
22import { createHmac } from "crypto"
33import { z } from "zod"
44
5+ import { type JobType , type JobStatus , type JobPayload , githubWebhookSchema } from "@/types"
56import { db , cloudJobs } from "@/db"
67import { enqueue } from "@/lib"
78
8- const githubWebhookSchema = z . object ( {
9- action : z . string ( ) ,
10- issue : z . object ( {
11- number : z . number ( ) ,
12- title : z . string ( ) ,
13- body : z . string ( ) . nullable ( ) ,
14- labels : z . array ( z . object ( { name : z . string ( ) } ) ) ,
15- } ) ,
16- repository : z . object ( {
17- full_name : z . string ( ) ,
18- } ) ,
19- } )
20-
21- function verifySignature ( payload : string , signature : string , secret : string ) : boolean {
22- const expectedSignature = createHmac ( "sha256" , secret ) . update ( payload , "utf8" ) . digest ( "hex" )
9+ function verifySignature ( body : string , signature : string , secret : string ) : boolean {
10+ const expectedSignature = createHmac ( "sha256" , secret ) . update ( body , "utf8" ) . digest ( "hex" )
2311 const receivedSignature = signature . replace ( "sha256=" , "" )
2412 return expectedSignature === receivedSignature
2513}
@@ -33,40 +21,45 @@ export async function POST(request: NextRequest) {
3321 return NextResponse . json ( { error : "Missing signature" } , { status : 400 } )
3422 }
3523
36- const payload = await request . text ( )
24+ const body = await request . text ( )
3725
38- if ( ! verifySignature ( payload , signature , process . env . GITHUB_WEBHOOK_SECRET ! ) ) {
26+ if ( ! verifySignature ( body , signature , process . env . GITHUB_WEBHOOK_SECRET ! ) ) {
3927 return NextResponse . json ( { error : "Invalid signature" } , { status : 401 } )
4028 }
4129
30+ console . log ( "✅ Signature verified" )
31+ console . log ( "📋 Event ->" , event )
32+
4233 if ( event !== "issues" ) {
43- return NextResponse . json ( { message : "Event ignored " } )
34+ return NextResponse . json ( { message : "event_ignored " } )
4435 }
4536
46- const data = githubWebhookSchema . parse ( JSON . parse ( payload ) )
37+ const data = githubWebhookSchema . parse ( JSON . parse ( body ) )
38+
39+ console . log ( "🗄️ Data ->" , data )
4740
4841 if ( data . action !== "opened" ) {
49- return NextResponse . json ( { message : "Action ignored " } )
42+ return NextResponse . json ( { message : "action_ignored " } )
5043 }
5144
52- const jobPayload = {
45+ const type : JobType = "github.issue.fix"
46+ const status : JobStatus = "pending"
47+
48+ const payload : JobPayload < typeof type > = {
5349 repo : data . repository . full_name ,
5450 issue : data . issue . number ,
5551 title : data . issue . title ,
5652 body : data . issue . body || "" ,
5753 labels : data . issue . labels . map ( ( label ) => label . name ) ,
5854 }
5955
60- const [ job ] = await db
61- . insert ( cloudJobs )
62- . values ( { type : "github.issue.fix" , status : "pending" , payload : jobPayload } )
63- . returning ( )
56+ const [ job ] = await db . insert ( cloudJobs ) . values ( { type, status, payload } ) . returning ( )
6457
6558 if ( ! job ) {
6659 throw new Error ( "Failed to create job" )
6760 }
6861
69- await enqueue ( "github.issue.fix" , jobPayload , job . id )
62+ await enqueue ( { jobId : job . id , type , payload } )
7063 return NextResponse . json ( { message : "Job created successfully" , jobId : job . id } )
7164 } catch ( error ) {
7265 console . error ( "GitHub webhook error:" , error )
0 commit comments