2025-05-12 19:10:01 +00:00
|
|
|
'use server';
|
|
|
|
|
|
|
|
|
|
import { NextRequest } from "next/server";
|
|
|
|
|
import { App, Octokit } from "octokit";
|
|
|
|
|
import { WebhookEventDefinition} from "@octokit/webhooks/types";
|
|
|
|
|
import { EndpointDefaults } from "@octokit/types";
|
|
|
|
|
import { env } from "@/env.mjs";
|
|
|
|
|
import { processGitHubPullRequest } from "@/features/agents/review-agent/app";
|
|
|
|
|
import { throttling } from "@octokit/plugin-throttling";
|
|
|
|
|
import fs from "fs";
|
|
|
|
|
import { GitHubPullRequest } from "@/features/agents/review-agent/types";
|
2025-06-02 18:16:01 +00:00
|
|
|
import { createLogger } from "@sourcebot/logger";
|
|
|
|
|
|
|
|
|
|
const logger = createLogger('github-webhook');
|
2025-05-12 19:10:01 +00:00
|
|
|
|
|
|
|
|
let githubApp: App | undefined;
|
2025-10-22 03:17:28 +00:00
|
|
|
if (env.GITHUB_REVIEW_AGENT_APP_ID && env.GITHUB_REVIEW_AGENT_APP_WEBHOOK_SECRET && env.GITHUB_REVIEW_AGENT_APP_PRIVATE_KEY_PATH) {
|
2025-05-12 19:10:01 +00:00
|
|
|
try {
|
2025-10-22 03:17:28 +00:00
|
|
|
const privateKey = fs.readFileSync(env.GITHUB_REVIEW_AGENT_APP_PRIVATE_KEY_PATH, "utf8");
|
2025-05-12 19:10:01 +00:00
|
|
|
|
|
|
|
|
const throttledOctokit = Octokit.plugin(throttling);
|
|
|
|
|
githubApp = new App({
|
2025-10-22 03:17:28 +00:00
|
|
|
appId: env.GITHUB_REVIEW_AGENT_APP_ID,
|
2025-05-12 19:10:01 +00:00
|
|
|
privateKey: privateKey,
|
|
|
|
|
webhooks: {
|
2025-10-22 03:17:28 +00:00
|
|
|
secret: env.GITHUB_REVIEW_AGENT_APP_WEBHOOK_SECRET,
|
2025-05-12 19:10:01 +00:00
|
|
|
},
|
|
|
|
|
Octokit: throttledOctokit,
|
|
|
|
|
throttle: {
|
|
|
|
|
onRateLimit: (retryAfter: number, options: Required<EndpointDefaults>, octokit: Octokit, retryCount: number) => {
|
|
|
|
|
if (retryCount > 3) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.warn(`Rate limit exceeded: ${retryAfter} seconds`);
|
2025-05-12 19:10:01 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.error(`Error initializing GitHub app: ${error}`);
|
2025-05-12 19:10:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isPullRequestEvent(eventHeader: string, payload: unknown): payload is WebhookEventDefinition<"pull-request-opened"> | WebhookEventDefinition<"pull-request-synchronize"> {
|
|
|
|
|
return eventHeader === "pull_request" && typeof payload === "object" && payload !== null && "action" in payload && typeof payload.action === "string" && (payload.action === "opened" || payload.action === "synchronize");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isIssueCommentEvent(eventHeader: string, payload: unknown): payload is WebhookEventDefinition<"issue-comment-created"> {
|
|
|
|
|
return eventHeader === "issue_comment" && typeof payload === "object" && payload !== null && "action" in payload && typeof payload.action === "string" && payload.action === "created";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const POST = async (request: NextRequest) => {
|
|
|
|
|
const body = await request.json();
|
|
|
|
|
const headers = Object.fromEntries(request.headers.entries());
|
|
|
|
|
|
|
|
|
|
const githubEvent = headers['x-github-event'] || headers['X-GitHub-Event'];
|
|
|
|
|
if (githubEvent) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.info('GitHub event received:', githubEvent);
|
2025-05-12 19:10:01 +00:00
|
|
|
|
|
|
|
|
if (!githubApp) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.warn('Received GitHub webhook event but GitHub app env vars are not set');
|
2025-05-12 19:10:01 +00:00
|
|
|
return Response.json({ status: 'ok' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isPullRequestEvent(githubEvent, body)) {
|
|
|
|
|
if (env.REVIEW_AGENT_AUTO_REVIEW_ENABLED === "false") {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.info('Review agent auto review (REVIEW_AGENT_AUTO_REVIEW_ENABLED) is disabled, skipping');
|
2025-05-12 19:10:01 +00:00
|
|
|
return Response.json({ status: 'ok' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!body.installation) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.error('Received github pull request event but installation is not present');
|
2025-05-12 19:10:01 +00:00
|
|
|
return Response.json({ status: 'ok' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const installationId = body.installation.id;
|
|
|
|
|
const octokit = await githubApp.getInstallationOctokit(installationId);
|
|
|
|
|
|
|
|
|
|
const pullRequest = body.pull_request as GitHubPullRequest;
|
|
|
|
|
await processGitHubPullRequest(octokit, pullRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isIssueCommentEvent(githubEvent, body)) {
|
|
|
|
|
const comment = body.comment.body;
|
|
|
|
|
if (!comment) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.warn('Received issue comment event but comment body is empty');
|
2025-05-12 19:10:01 +00:00
|
|
|
return Response.json({ status: 'ok' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (comment === `/${env.REVIEW_AGENT_REVIEW_COMMAND}`) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.info('Review agent review command received, processing');
|
2025-05-12 19:10:01 +00:00
|
|
|
|
|
|
|
|
if (!body.installation) {
|
2025-06-02 18:16:01 +00:00
|
|
|
logger.error('Received github issue comment event but installation is not present');
|
2025-05-12 19:10:01 +00:00
|
|
|
return Response.json({ status: 'ok' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pullRequestNumber = body.issue.number;
|
|
|
|
|
const repositoryName = body.repository.name;
|
|
|
|
|
const owner = body.repository.owner.login;
|
|
|
|
|
|
|
|
|
|
const octokit = await githubApp.getInstallationOctokit(body.installation.id);
|
|
|
|
|
const { data: pullRequest } = await octokit.rest.pulls.get({
|
|
|
|
|
owner,
|
|
|
|
|
repo: repositoryName,
|
|
|
|
|
pull_number: pullRequestNumber,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await processGitHubPullRequest(octokit, pullRequest);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Response.json({ status: 'ok' });
|
|
|
|
|
}
|