Skip to content

fix(web): use REST API endpoint for fetching PR diff instead of web diff_url#1302

Open
AasheeshLikePanner wants to merge 2 commits into
sourcebot-dev:mainfrom
AasheeshLikePanner:fix/review-agent-private-repo-diff-1277
Open

fix(web): use REST API endpoint for fetching PR diff instead of web diff_url#1302
AasheeshLikePanner wants to merge 2 commits into
sourcebot-dev:mainfrom
AasheeshLikePanner:fix/review-agent-private-repo-diff-1277

Conversation

@AasheeshLikePanner

@AasheeshLikePanner AasheeshLikePanner commented Jun 11, 2026

Copy link
Copy Markdown

Description

Fixes the Review Agent failing on private GitHub repositories when fetching the PR diff. The githubPrParser was using pullRequest.diff_url from the GitHub API, which returns a github.com web URL (e.g. https://github.com/owner/repo/pull/123.diff). GitHub App installation tokens are only accepted by the REST API at api.github.com, so requests to the web domain return 404 for private repositories.

The fix replaces the web URL with the REST API endpoint GET /repos/{owner}/{repo}/pulls/{pull_number} using mediaType: { format: 'diff' }, which correctly authenticates with the installation token and returns the diff for both public and private repos.

Related Issue(s)

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • Changed githubPrParser.ts to fetch the PR diff via octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', { mediaType: { format: 'diff' } }) instead of octokit.request(pullRequest.diff_url)
  • Updated the test in githubPrParser.test.ts to verify the REST API endpoint is called with correct parameters
  • Added CHANGELOG entry under [Unreleased] > Fixed

Testing

  • Unit tests added/updated
  • All existing tests pass (1250/1250 across 4 packages)
  • Verified via Octokit endpoint parsing that the fix changes the URL from github.com to api.github.com

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • All tests pass
  • CHANGELOG entry added

Additional Notes

The root cause was traced through Octokit's endpoint.parse() (@octokit/endpoint/dist-src/parse.js:20): when an absolute URL is passed (like the diff_url), the baseUrl is never prepended, so the request goes to github.com instead of api.github.com where the installation token is valid. The existing code in webhook/route.ts:189 already uses the correct REST API pattern (octokit.rest.pulls.get) for fetching PR data from comment

Summary by CodeRabbit

  • Bug Fixes

    • Fixed Review Agent failing when processing pull requests from private GitHub repositories; PR diffs are now fetched reliably.
  • New Features

    • PR payload now includes the head commit SHA for more accurate review context.
  • Tests

    • Improved test coverage for PR parsing, diff handling, and error propagation.

…iff_url

The Review Agent's githubPrParser was using pullRequest.diff_url to
fetch the PR diff, which points to a github.com web URL. GitHub App
installation tokens are only accepted on the REST API (api.github.com),
so requests to the web domain fail with 404 for private repositories.

Fix by using the REST API endpoint GET /repos/{owner}/{repo}/pulls/{pull_number}
with mediaType: { format: 'diff' }, which correctly authenticates with
the installation token and works for both public and private repos.

Fixes sourcebot-dev#1277
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 071e1d72-25c6-4920-8f3a-5bad0946b8b2

📥 Commits

Reviewing files that changed from the base of the PR and between 143ba90 and 7ad1c69.

📒 Files selected for processing (1)
  • CHANGELOG.md
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md

Walkthrough

The Review Agent's PR diff parser now uses the GitHub REST API's diff media type instead of the inaccessible diff_url field; tests were updated to assert the REST call and cover diff-parsing scenarios, and CHANGELOG documents the fix.

Changes

GitHub PR Diff Fetching via REST API

Layer / File(s) Summary
Core implementation change to REST API diff fetching
packages/web/src/features/agents/review-agent/nodes/githubPrParser.ts
githubPrParser replaces pullRequest.diff_url with an explicit Octokit REST call to GET /repos/{owner}/{repo}/pulls/{pull_number} with mediaType.format: "diff". The returned diff is parsed via parse(diff.data as unknown as string); per-file/per-chunk transformation logic remains and the return payload now includes head_sha: pullRequest.head.sha.
Test suite: core behavior and REST API verification
packages/web/src/features/agents/review-agent/nodes/githubPrParser.test.ts
Tests verify PR metadata mapping, null body handling, and validate the new REST API call signature with mediaType.format: "diff". Adds makePullRequest helper and ensures request failures propagate.
Test suite: diff parsing edge cases and scenarios
packages/web/src/features/agents/review-agent/nodes/githubPrParser.test.ts
Coverage for empty-diff behavior, unified diff parsing (added/context/deleted lines), correct newSnippet/oldSnippet extraction, and multiple-file diffs.
Changelog documentation
CHANGELOG.md
Unreleased "Fixed" entry documents the Review Agent's switch to REST API diff fetching for private repository support and references PR #1302.

Sequence Diagram(s)

sequenceDiagram
  participant githubPrParser
  participant Octokit
  participant parseDiff as parse-diff
  participant Sourcebot

  githubPrParser->>Octokit: GET /repos/{owner}/{repo}/pulls/{pull_number} (mediaType: diff)
  Octokit-->>githubPrParser: diff text (diff.data)
  githubPrParser->>parseDiff: parse(diff.data as string)
  parseDiff-->>githubPrParser: parsed file/chunk structures
  githubPrParser->>Sourcebot: sourcebot_pr_payload (includes head_sha, file_diffs)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested reviewers

  • msukkari
  • brendan-kellam
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: replacing web diff_url with REST API endpoint for fetching PR diffs.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/web/src/features/agents/review-agent/nodes/githubPrParser.test.ts (1)

16-51: 💤 Low value

Consider removing the unused diff_url field.

The makePullRequest helper includes a diff_url field (lines 24, 34, 42) that is no longer accessed by the implementation. Since the parser now uses the REST API endpoint directly, this field is dead code in the test factory.

However, keeping it might be intentional to maintain the complete shape of a GitHubPullRequest object for test fidelity.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/web/src/features/agents/review-agent/nodes/githubPrParser.test.ts`
around lines 16 - 51, The makePullRequest test helper includes an unused
diff_url property on the overrides type, opts default object and returned
GitHubPullRequest shape; remove diff_url from the overrides Partial type, from
the opts defaults, and from the returned object in makePullRequest (function
name: makePullRequest) so the helper matches the fields actually consumed by the
parser (GitHubPullRequest) — or if you want to preserve full PR shape for
fidelity, keep it but add a comment clarifying it’s unused.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@CHANGELOG.md`:
- Around line 10-12: Update the CHANGELOG entry that currently reads "Fixed
Review Agent failing on private GitHub repositories when fetching the PR
diff..." to reference the pull request instead of the issue: replace the
existing issue link
[`#1277`](https://github.com/sourcebot-dev/sourcebot/issues/1277) with the PR link
for PR `#1302` formatted as
[`#1302`](https://github.com/sourcebot-dev/sourcebot/pull/1302) so the line ends
with the correct PR reference.

---

Nitpick comments:
In `@packages/web/src/features/agents/review-agent/nodes/githubPrParser.test.ts`:
- Around line 16-51: The makePullRequest test helper includes an unused diff_url
property on the overrides type, opts default object and returned
GitHubPullRequest shape; remove diff_url from the overrides Partial type, from
the opts defaults, and from the returned object in makePullRequest (function
name: makePullRequest) so the helper matches the fields actually consumed by the
parser (GitHubPullRequest) — or if you want to preserve full PR shape for
fidelity, keep it but add a comment clarifying it’s unused.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6659c80f-fa12-4a11-8805-b17185f00cd8

📥 Commits

Reviewing files that changed from the base of the PR and between 1387c46 and 143ba90.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • packages/web/src/features/agents/review-agent/nodes/githubPrParser.test.ts
  • packages/web/src/features/agents/review-agent/nodes/githubPrParser.ts

Comment thread CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Review Agent fails on private GitHub repositories when fetching PR diff

1 participant