Conversation
…ynn/code-reverb into chore/octokit-app-upgrade pull remote
📝 WalkthroughWalkthroughThe PR refactors account resolution in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/api/webhooks/github/route.tslib/ai/actions/index.ts
💤 Files with no reviewable changes (1)
- app/api/webhooks/github/route.ts
🧰 Additional context used
🧬 Code graph analysis (1)
lib/ai/actions/index.ts (1)
lib/auth.ts (1)
auth(6-24)
| const session = await auth.api.getSession({ headers: await headers() }); | ||
|
|
||
| if (!session) throw new Error("Unauthorized"); | ||
| const account = await prisma.account.findFirst({ | ||
| where: { | ||
| userId: session.user.id, | ||
| providerId: "github", | ||
| }, | ||
| }); | ||
|
|
||
| if (!account?.accessToken) throw new Error("No GitHub access token found"); |
There was a problem hiding this comment.
Session-based auth breaks webhook-triggered calls.
According to the AI summary, this function is called from a webhook handler when a PR is opened. Webhook requests from GitHub are server-to-server calls that don't carry user session cookies. The auth.api.getSession() call will return null for all webhook-triggered invocations, causing every PR summary request to fail with "Unauthorized".
The previous design (passing accountId as a parameter) allowed the webhook handler to resolve the account from webhook payload data (e.g., installationId). Consider one of:
- Restore the
accountIdparameter for webhook contexts and keep session auth only for user-initiated calls. - Resolve account from
installationIdwithin this function instead of relying on user session. - Create separate functions for webhook-triggered vs. user-triggered flows.
🔎 Example: Resolve account from installationId instead
export async function generatePullRequestSummary(
owner: string,
repoName: string,
prNumber: number,
title: string,
description: string,
installationId: number,
baseSha: string,
headSha: string,
changedFiles: number,
additions: number,
- deletions: number
+ deletions: number,
+ accountId?: string // Optional: passed by webhook handler
) {
try {
- const session = await auth.api.getSession({ headers: await headers() });
-
- if (!session) throw new Error("Unauthorized");
- const account = await prisma.account.findFirst({
- where: {
- userId: session.user.id,
- providerId: "github",
- },
- });
-
- if (!account?.accessToken) throw new Error("No GitHub access token found");
+ let resolvedAccountId = accountId;
+
+ // If no accountId provided, try to resolve from session (user-initiated)
+ if (!resolvedAccountId) {
+ const session = await auth.api.getSession({ headers: await headers() });
+ if (!session) throw new Error("Unauthorized");
+
+ const account = await prisma.account.findFirst({
+ where: {
+ userId: session.user.id,
+ providerId: "github",
+ },
+ });
+ if (!account?.accessToken) throw new Error("No GitHub access token found");
+ resolvedAccountId = account.accountId;
+ }
await inngest.send({
name: "pr.summary.requested",
data: {
owner,
repo: repoName,
prNumber,
title: title ?? "",
description: description ?? "",
- accountId: account.accountId,
+ accountId: resolvedAccountId,
installationId: installationId ?? null,Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In lib/ai/actions/index.ts around lines 155-165, session-based auth via
auth.api.getSession() breaks webhook-triggered calls (no cookies) and causes
"Unauthorized"; restore support for an explicit accountId parameter (or
installationId) so the caller can pass the account context for server-to-server
webhooks, and keep the session-based path as a fallback: first try to use a
provided accountId/installationId to look up the account (resolve installationId
-> account if needed), and only if none provided, use auth.api.getSession() to
derive the user and then fetch the account; ensure error messages reflect which
lookup failed.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.