Skip to content

Commit 944aeb5

Browse files
committed
add rate limit throttling to octokit
1 parent ef978fd commit 944aeb5

File tree

1 file changed

+17
-2
lines changed
  • packages/web/src/app/api/(server)/webhook

1 file changed

+17
-2
lines changed

packages/web/src/app/api/(server)/webhook/route.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
'use server';
22

33
import { NextRequest } from "next/server";
4-
import { App } from "octokit";
5-
import { WebhookEventDefinition } from "@octokit/webhooks/types";
4+
import { App, Octokit } from "octokit";
5+
import { WebhookEventDefinition} from "@octokit/webhooks/types";
6+
import { EndpointDefaults } from "@octokit/types";
67
import { env } from "@/env.mjs";
78
import { processGitHubPullRequest } from "@/features/agents/review-agent/app";
9+
import { throttling } from "@octokit/plugin-throttling";
810
import fs from "fs";
911

1012
let githubApp: App | undefined;
1113
if (env.GITHUB_APP_ID && env.GITHUB_APP_WEBHOOK_SECRET && env.GITHUB_APP_PRIVATE_KEY_PATH) {
1214
try {
1315
const privateKey = fs.readFileSync(env.GITHUB_APP_PRIVATE_KEY_PATH, "utf8");
16+
17+
const throttledOctokit = Octokit.plugin(throttling);
1418
githubApp = new App({
1519
appId: env.GITHUB_APP_ID,
1620
privateKey: privateKey,
1721
webhooks: {
1822
secret: env.GITHUB_APP_WEBHOOK_SECRET,
1923
},
24+
Octokit: throttledOctokit,
25+
throttle: {
26+
onRateLimit: (retryAfter: number, options: Required<EndpointDefaults>, octokit: Octokit, retryCount: number) => {
27+
if (retryCount > 3) {
28+
console.log(`Rate limit exceeded: ${retryAfter} seconds`);
29+
return false;
30+
}
31+
32+
return true;
33+
},
34+
}
2035
});
2136
} catch (error) {
2237
console.error(`Error initializing GitHub app: ${error}`);

0 commit comments

Comments
 (0)