Skip to content

Commit f9ab282

Browse files
authored
Use GH_TOKEN to fetch pull request data (#186)
1 parent 495d1bd commit f9ab282

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

apps/roomote/src/app/api/webhooks/github/handlers/issueCommentHandler.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { z } from 'zod';
33

44
import type { JobPayload } from '@roo-code-cloud/db';
55

6-
import { createAndEnqueueJob } from './utils';
6+
import { createAndEnqueueJob, fetchGitHubAPI } from './utils';
77

88
const githubIssueCommentWebhookSchema = z.object({
99
action: z.string(),
@@ -48,9 +48,14 @@ export async function handleIssueCommentEvent(body: string) {
4848

4949
// Handle PR comments (when comment is on a pull request)
5050
if (issue.pull_request) {
51-
const response = await fetch(issue.pull_request.url);
51+
const response = await fetchGitHubAPI(issue.pull_request.url);
5252

5353
if (!response.ok) {
54+
console.error(
55+
`🔴 Failed to fetch pull request -> ${issue.pull_request.url}`,
56+
`Status: ${response.status}`,
57+
);
58+
5459
return NextResponse.json({ message: 'failed_to_fetch_pull_request' });
5560
}
5661

@@ -59,6 +64,7 @@ export async function handleIssueCommentEvent(body: string) {
5964
const pull_request = githubPullRequestWebhookSchema.parse(
6065
await response.json(),
6166
);
67+
6268
console.log(`🗄️ Pull Request -> ${issue.pull_request.url}`, pull_request);
6369

6470
const payload: JobPayload<'github.pr.comment.respond'> = {
@@ -79,6 +85,7 @@ export async function handleIssueCommentEvent(body: string) {
7985
'github.pr.comment.respond',
8086
payload,
8187
);
88+
8289
return NextResponse.json({
8390
message: 'pr_comment_job_enqueued',
8491
jobId,
@@ -101,6 +108,7 @@ export async function handleIssueCommentEvent(body: string) {
101108
};
102109

103110
const { jobId, enqueuedJobId } = await createAndEnqueueJob(type, payload);
111+
104112
return NextResponse.json({
105113
message: 'issue_comment_job_enqueued',
106114
jobId,

apps/roomote/src/app/api/webhooks/github/handlers/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ export async function createAndEnqueueJob<T extends JobType>(
4343

4444
return { jobId: job.id, enqueuedJobId: enqueuedJob.id };
4545
}
46+
47+
export async function fetchGitHubAPI(url: string, options: RequestInit = {}) {
48+
const headers: Record<string, string> = {
49+
Accept: 'application/vnd.github.v3+json',
50+
'User-Agent': 'Roomote-Webhook-Handler',
51+
...((options.headers as Record<string, string>) || {}),
52+
};
53+
54+
if (process.env.GH_TOKEN) {
55+
headers['Authorization'] = `Bearer ${process.env.GH_TOKEN}`;
56+
}
57+
58+
return fetch(url, { ...options, headers });
59+
}

0 commit comments

Comments
 (0)