@@ -2,7 +2,7 @@ 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"
5+ import { type JobType , type JobPayload , githubWebhookSchema } from "@/types"
66import { db , cloudJobs } from "@/db"
77import { enqueue } from "@/lib"
88
@@ -15,59 +15,58 @@ function verifySignature(body: string, signature: string, secret: string): boole
1515export async function POST ( request : NextRequest ) {
1616 try {
1717 const signature = request . headers . get ( "x-hub-signature-256" )
18- const event = request . headers . get ( "x-github-event" )
1918
2019 if ( ! signature ) {
21- return NextResponse . json ( { error : "Missing signature " } , { status : 400 } )
20+ return NextResponse . json ( { error : "missing_signature " } , { status : 400 } )
2221 }
2322
2423 const body = await request . text ( )
2524
2625 if ( ! verifySignature ( body , signature , process . env . GITHUB_WEBHOOK_SECRET ! ) ) {
27- return NextResponse . json ( { error : "Invalid signature " } , { status : 401 } )
26+ return NextResponse . json ( { error : "invalid_signature " } , { status : 401 } )
2827 }
2928
30- console . log ( "✅ Signature verified" )
31- console . log ( "📋 Event ->" , event )
29+ const event = request . headers . get ( "x-github-event" )
3230
3331 if ( event !== "issues" ) {
3432 return NextResponse . json ( { message : "event_ignored" } )
3533 }
3634
3735 const data = githubWebhookSchema . parse ( JSON . parse ( body ) )
36+ console . log ( "🗄️ Webhook ->" , data )
37+ const { action, repository, issue } = data
3838
39- console . log ( "🗄️ Data ->" , data )
40-
41- if ( data . action !== "opened" ) {
39+ if ( action !== "opened" ) {
4240 return NextResponse . json ( { message : "action_ignored" } )
4341 }
4442
4543 const type : JobType = "github.issue.fix"
46- const status : JobStatus = "pending"
4744
4845 const payload : JobPayload < typeof type > = {
49- repo : data . repository . full_name ,
50- issue : data . issue . number ,
51- title : data . issue . title ,
52- body : data . issue . body || "" ,
53- labels : data . issue . labels . map ( ( label ) => label . name ) ,
46+ repo : repository . full_name ,
47+ issue : issue . number ,
48+ title : issue . title ,
49+ body : issue . body || "" ,
50+ labels : issue . labels . map ( ( { name } ) => name ) ,
5451 }
5552
56- const [ job ] = await db . insert ( cloudJobs ) . values ( { type, status , payload } ) . returning ( )
53+ const [ job ] = await db . insert ( cloudJobs ) . values ( { type, payload , status : "pending" } ) . returning ( )
5754
5855 if ( ! job ) {
59- throw new Error ( "Failed to create job " )
56+ throw new Error ( "Failed to create `cloudJobs` record. " )
6057 }
6158
62- await enqueue ( { jobId : job . id , type, payload } )
63- return NextResponse . json ( { message : "Job created successfully" , jobId : job . id } )
59+ const enqueuedJob = await enqueue ( { jobId : job . id , type, payload } )
60+ console . log ( "🔗 Enqueued job ->" , enqueuedJob )
61+
62+ return NextResponse . json ( { message : "job_enqueued" , jobId : job . id , enqueuedJobId : enqueuedJob . id } )
6463 } catch ( error ) {
65- console . error ( "GitHub webhook error :" , error )
64+ console . error ( "GitHub Webhook Error :" , error )
6665
6766 if ( error instanceof z . ZodError ) {
68- return NextResponse . json ( { error : "Invalid webhook payload " , details : error . errors } , { status : 400 } )
67+ return NextResponse . json ( { error : "bad_request " , details : error . errors } , { status : 400 } )
6968 }
7069
71- return NextResponse . json ( { error : "Internal server error " } , { status : 500 } )
70+ return NextResponse . json ( { error : "internal_server_error " } , { status : 500 } )
7271 }
7372}
0 commit comments