diff --git a/apps/roomote/src/app/api/webhooks/github/handlers/issueCommentHandler.ts b/apps/roomote/src/app/api/webhooks/github/handlers/issueCommentHandler.ts index 93e81fc0a4..d1da93bc3c 100644 --- a/apps/roomote/src/app/api/webhooks/github/handlers/issueCommentHandler.ts +++ b/apps/roomote/src/app/api/webhooks/github/handlers/issueCommentHandler.ts @@ -3,7 +3,7 @@ import { z } from 'zod'; import type { JobPayload } from '@roo-code-cloud/db'; -import { createAndEnqueueJob } from './utils'; +import { createAndEnqueueJob, fetchGitHubAPI } from './utils'; const githubIssueCommentWebhookSchema = z.object({ action: z.string(), @@ -48,9 +48,14 @@ export async function handleIssueCommentEvent(body: string) { // Handle PR comments (when comment is on a pull request) if (issue.pull_request) { - const response = await fetch(issue.pull_request.url); + const response = await fetchGitHubAPI(issue.pull_request.url); if (!response.ok) { + console.error( + `🔴 Failed to fetch pull request -> ${issue.pull_request.url}`, + `Status: ${response.status}`, + ); + return NextResponse.json({ message: 'failed_to_fetch_pull_request' }); } @@ -59,6 +64,7 @@ export async function handleIssueCommentEvent(body: string) { const pull_request = githubPullRequestWebhookSchema.parse( await response.json(), ); + console.log(`🗄️ Pull Request -> ${issue.pull_request.url}`, pull_request); const payload: JobPayload<'github.pr.comment.respond'> = { @@ -79,6 +85,7 @@ export async function handleIssueCommentEvent(body: string) { 'github.pr.comment.respond', payload, ); + return NextResponse.json({ message: 'pr_comment_job_enqueued', jobId, @@ -101,6 +108,7 @@ export async function handleIssueCommentEvent(body: string) { }; const { jobId, enqueuedJobId } = await createAndEnqueueJob(type, payload); + return NextResponse.json({ message: 'issue_comment_job_enqueued', jobId, diff --git a/apps/roomote/src/app/api/webhooks/github/handlers/utils.ts b/apps/roomote/src/app/api/webhooks/github/handlers/utils.ts index 2edd6a3bda..12b47b9bcd 100644 --- a/apps/roomote/src/app/api/webhooks/github/handlers/utils.ts +++ b/apps/roomote/src/app/api/webhooks/github/handlers/utils.ts @@ -43,3 +43,17 @@ export async function createAndEnqueueJob( return { jobId: job.id, enqueuedJobId: enqueuedJob.id }; } + +export async function fetchGitHubAPI(url: string, options: RequestInit = {}) { + const headers: Record = { + Accept: 'application/vnd.github.v3+json', + 'User-Agent': 'Roomote-Webhook-Handler', + ...((options.headers as Record) || {}), + }; + + if (process.env.GH_TOKEN) { + headers['Authorization'] = `Bearer ${process.env.GH_TOKEN}`; + } + + return fetch(url, { ...options, headers }); +}