fix(github): read watched PRs without organization scopes #40747 (GitHub #40740)
main fromrefs/pull/40740/head·opened View on GitHubSupersedes #32366. I closed that PR only to stay within the 3-open-PR limit. This is the same fix, ported fresh onto current main, not a merge of the old branch. Thanks to @sebassdc for confirming the failure on a real install (watches with reads: 0 and a null head while the scope errors kept coming).
Problem#
A GitHub token with only repo, gist and workflow (no read:org) can't read a pull request's detail. PullRequestCore selects ... on Team { slug combinedSlug name avatarUrl } under reviewRequests. Every one of those fields needs read:org or read:discussion. GitHub checks scopes against the query text, so it refuses the whole query even when no team is requested, and even with reviewRequests(first: 0). The existing unreadable-reviewer fallback (#19549 / #19564) retries with first: 0, so it can't recover. A PR watch on such a token never sees checks or the review decision.
The review-thread read (ReviewThreads) fails the same way. Its Team fragment needs read:org, and so does ... on Organization { login } in the shared Reactions fragment.
Change#
PullRequestCoreandReviewThreadstake a$teamDetails: Boolean!and select Team fields under@include(if: $teamDetails). With the flag off, GitHub accepts the query (checked live).readRequestedReviewersreads withteamDetails: true. If GitHub answers only withINSUFFICIENT_SCOPES, it retries once withteamDetails: false. A successful fallback is cached for 10 minutes per credential and host, so later detail and review-thread reads skip straight to it. A comment says why the key is per credential and host, and that a token that gainsread:orgreads teams again once the entry expires. This answers the CodeRabbit note on #32366. The existing per-PR requested-reviewer refusal fallback still applies on top.hasTeamReviewRequestalso counts a request whose__typenameisTeam, so a team request still shows when its details weren't read. Individual reviewers, checks and the review decision are unchanged. Only team names and avatars are missing, and only for a credential that can't read them.Reactionsno longer selects... on Organization { login }. An organization reactor still counts intotalCount, which the mapping already uses. The fragment is shared, so the regeneratedissues.graphql.tschanges too: issue detail and comments stop selecting that field.- The generated
*.graphql.tsfiles are regenerated with the package'sgraphqlgen. - A failed retry, or any other error, still fails as before.
Left alone on purpose: the PR listing (PullRequestRow's ... on Team { slug }) and the reviewer picker (ReviewerCandidates) also refuse this token (checked live). The listing needs the flag added to two queries and their callers. The reviewer picker is next to the open #35126. Both are separate follow-ups and not part of the watch-read fix.
Scope#
This fixes the bug #32366 fixed, with the same approach, which Macroscope approved and CodeRabbit reviewed there. It's a small fix for an obvious bug: GitHub refuses a read for fields the token can't select, and nothing reaches the user except a watch that never fires. @sebassdc's report on #32366 shows the same failure on T3 Code 0.1.7. The unresolved review item from #32366 is addressed: the refusal assertion in the test checks the exact error and both retry calls. No contract, client or UI change.
Verification#
End-to-end against real GitHub (read-only). I ran the real GitHubPullRequestApi read path, composed the way driver.ts builds it (GitHubPullRequestApi.layer ← GitHubAppAccess ← GitHubRest.layerWithDependencies, with the real credential, quota, request policy and fetch HTTP client). The credential came from GH_TOKEN, the same way the server reads it, and has scopes gist, repo, workflow only. The script calls getPullRequestDetail, getPullRequestActivity and listReviewThreadComments on three public PRs. The token was never printed, and the logs are redacted.
Before, with packages/source-control-github/src/server at origin/main 11224f63d2:
target: pingdotgg/t3code#40694 on github.com
getPullRequestDetail FAILED: {"_tag":"GitHubApiResponseError","status":200,"githubErrorTypes":["INSUFFICIENT_SCOPES",...],"refusedFields":["slug needs ['read:org', 'read:discussion']","combinedSlug needs [...]","name needs [...]","avatarUrl needs [...]"]}
getPullRequestActivity OK: {"comments":2,"commits":4}
listReviewThreadComments FAILED: {"_tag":"GitHubApiResponseError","status":200,"refusedFields":["login needs ['read:org']" x4,"combinedSlug needs [...]","name needs [...]","avatarUrl needs [...]"]}
#35910 and #38514 failed the same way.
After, on this branch:
target: pingdotgg/t3code#40694 on github.com
getPullRequestDetail OK: {"number":40694,"state":"open","reviewDecision":null,"checksState":"passing","checks":45,...}
getPullRequestActivity OK: {"comments":2,"commits":4}
listReviewThreadComments OK: {..., "reviewers":1}
target: pingdotgg/t3code#35910 on github.com
getPullRequestDetail OK: {"number":35910,"state":"open","reviewDecision":"changes-requested","checksState":"passing","checks":39,...}
listReviewThreadComments OK: {..., "reviewers":3}
target: pingdotgg/t3code#38514 on github.com
getPullRequestDetail OK: {"number":38514,"state":"open","reviewDecision":"approved","checksState":"failing","checks":37,...}
listReviewThreadComments OK: {..., "reviewers":1}
Tests
- New case in
GitHubPullRequestApi.test.ts, "reads PR truth when the credential cannot select Team fields". It fails onmainwithGitHubApiResponseError: ... The 'slug' field requires one of the following scopes: ['read:org', 'read:discussion']. It passes here. It checks the failing check, the review decision, the reviewer login, the Team marker, that the fallback is cached (no second refusal),teamDetails: falseon the review-thread read, and that a later reviewer refusal still fails with the exact GitHub error after both retries. vp test run GitHubPullRequestApi.test.ts gitHubPullRequestJson.test.ts: 193 passed.vp run check:changed: format, lint, knip (both passes),graphqlgen --checkand typecheck of the 11 affected packages pass. Its tests beside the change also pass: 195 passed.
Not checked: a live PR that requests a team. I had no public PR with a pending team request to read. The unit test covers that case with GitHub's real response shape.
Written by Claude Opus 5.5 in Claude Code via T3 Code.
🤖 Generated with Claude Code
- 1
tanstack-source added labelsbug
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
No actionable comments were generated in the recent review.
Walkthrough
-
The GitHub pull-request API now retries reads without Team reviewer details after insufficient-scope errors and caches the refusal. Reaction schemas and queries no longer model Organization reactors or request their logins.
Changes: Team detail scope fallback
Layer / File(s) Summary Conditional Team reviewer fields
packages/source-control-github/src/server/pullRequestReads.graphql,packages/source-control-github/src/server/pullRequestReads.graphql.ts,packages/source-control-github/src/server/pullRequestReviews.graphql,packages/source-control-github/src/server/pullRequestReviews.graphql.tsPull-request and review-thread queries accept a teamDetailsvariable to conditionally request Team fields. Generated schemas reflect optional Team details.Team request mapping
packages/source-control-github/src/server/gitHubPullRequestJson.tsRequested reviewer data preserves __typename. Team-request detection recognizes theTeamtypename and retains the existing fallback checks.Scope retry and regression coverage
packages/source-control-github/src/server/GitHubPullRequestApi.ts,packages/source-control-github/src/server/GitHubPullRequestApi.test.tsThe API retries insufficient-scope-only responses without Team details and caches the refusal for 10 minutes by credential and host. Tests cover the retry, later reads, and reviewer-field errors. Organization reactor query changes
Layer / File(s) Summary Organization reactor schema and selections
packages/source-control-github/src/server/issues.graphql.ts,packages/source-control-github/src/server/pullRequestReviews.graphql,packages/source-control-github/src/server/pullRequestReviews.graphql.tsGenerated reactor schemas classify Organization as an other typename. Issue, review-thread, and review-comment reaction fragments no longer select Organization logins. Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Sequence Diagram(s)#
sequenceDiagram participant API as GitHubPullRequestApi participant GraphQL as GitHub GraphQL API participant Cache as Credential and host refusal cache API->>GraphQL: Request reviewer details with teamDetails true GraphQL-->>API: Return INSUFFICIENT_SCOPES errors API->>Cache: Cache Team detail refusal for 10 minutes API->>GraphQL: Retry reviewer read with teamDetails falsePossibly related PRs
- pingdotgg/t3code#32366: Describes the same insufficient-scope fallback, including retrying without Team metadata and caching the refusal by credential and host.
Suggested reviewers:
juliusmarminge,macroscopeapp
Merge Risk:
Minimal · up to94c8a-
The change lets pull-request watches work with tokens that lack organization scopes by retrying without Team reviewer details. The only residual concern is a narrow race in credential selection that could briefly hide Team details, so the merge risk is minimal.
Pre-merge checks:
4-
Check name Status Explanation Title check The title follows conventional commit format and names the main change: pull-request watch reads now work without organization scopes. The changeset adds a team-details flag, an INSUFFICIENT_SCOPES re… Description check The description follows the repository template. It has Problem, Change, Scope, and Verification sections, and each section has specific content. The Problem section explains the scope refusal and the… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Title check
-
Explanation
The title follows conventional commit format and names the main change: pull-request watch reads now work without organization scopes. The changeset adds a team-details flag, an INSUFFICIENT_SCOPES retry, and a reduced Reactions fragment, all aimed at that goal. The title is specific and matches the primary fix.
Full details: Description check
-
Explanation
The description follows the repository template. It has Problem, Change, Scope, and Verification sections, and each section has specific content. The Problem section explains the scope refusal and the affected fields. The Change section lists the code changes and the cache behavior. The Scope section links the superseded PR
#32366and justifies the small fix. The Verification section gives real-GitHub results, unit test details, and unchecked items. The template's conventional commit title guidance is met by the title. The description does not state a linked issue for the bug, but it explains why the fix needs no issue, as the template allows for small fixes. The agent footer is present, as the template asks.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: main
- File: .coderabbit.config.ts
- Commit: cd3a57a
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Advanced
- Run ID:
91a95a43-944a-4bbe-a4d1-e029a78cbf6e
Commits-
Reviewing files that changed from the base of the PR and between cd3a57ac7221b4151fb7c64a132d9ba59d98beaa and 94c8abed81bc4ad9b08bff1ff49ac7206ba700c9.
Files selected for processing: 8-
packages/source-control-github/src/server/GitHubPullRequestApi.test.tspackages/source-control-github/src/server/GitHubPullRequestApi.tspackages/source-control-github/src/server/gitHubPullRequestJson.tspackages/source-control-github/src/server/issues.graphql.tspackages/source-control-github/src/server/pullRequestReads.graphqlpackages/source-control-github/src/server/pullRequestReads.graphql.tspackages/source-control-github/src/server/pullRequestReviews.graphqlpackages/source-control-github/src/server/pullRequestReviews.graphql.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
-
Sign in to join this conversation.
copilot-pull-request-reviewer[bot](GitHub profile, opens in a new tab) reviewed
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.