fix(issue): avoid fetching unnecessary fields for discovery#12884
fix(issue): avoid fetching unnecessary fields for discovery#12884williammartin merged 7 commits intotrunkfrom
Conversation
Add a new IssueRepoInfo function that fetches only the fields needed for issue creation (id, name, owner, hasIssuesEnabled, viewerPermission), avoiding defaultBranchRef and other fields that require Contents:Read. Also add StubIssueRepoInfoResponse helper to httpmock for testing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add success, not-found, and edge case tests for both GitHubRepo and IssueRepoInfo, covering field population, parent repo handling, viewer permission checks, and issues-disabled scenarios. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… permission Switch issue create from GitHubRepo to IssueRepoInfo so that gh issue create works with fine-grained PATs that only have Issues:Write and Metadata:Read permissions. Fixes #12798 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Update test mocks to match the renamed GraphQL query used by IssueRepoInfo, and switch to StubIssueRepoInfoResponse helper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…sues Only the destination repo ID is needed for issue transfer. Switch from GitHubRepo to IssueRepoInfo to use minimal fields appropriate for issues. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes #12798 where gh issue create fails with fine-grained PATs that have Issues:Write but not Contents:Read permissions. The root cause was that GitHubRepo fetches defaultBranchRef and merge-strategy fields via GraphQL, which require Contents:Read. The fix introduces a minimal IssueRepoInfo function that only fetches the fields actually needed for issue operations (id, name, owner, hasIssuesEnabled, viewerPermission).
Changes:
- Added new
IssueRepoInfoAPI function inapi/queries_repo.gowith a narrower GraphQL query (IssueRepositoryInfo) that avoids permission-sensitive fields. - Switched
issue createandissue transferfromapi.GitHubRepotoapi.IssueRepoInfo. - Updated all test stubs to match the new query name and added unit tests for the new function, plus a
StubIssueRepoInfoResponsetest helper.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| api/queries_repo.go | New IssueRepoInfo function with minimal GraphQL query |
| api/queries_repo_test.go | Tests for IssueRepoInfo (not found, success, issues disabled) and improved GitHubRepo tests |
| pkg/cmd/issue/create/create.go | Swap GitHubRepo → IssueRepoInfo |
| pkg/cmd/issue/create/create_test.go | Update mock query names and stub helpers |
| pkg/cmd/issue/transfer/transfer.go | Swap GitHubRepo → IssueRepoInfo |
| pkg/cmd/issue/transfer/transfer_test.go | Update mock query name |
| pkg/httpmock/legacy.go | New StubIssueRepoInfoResponse helper |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
williammartin
left a comment
There was a problem hiding this comment.
Passes all the issue acceptance tests (after I rebased on main):
➜ pr-12884 git:(babakks/use-min-discovery-fields-for-issue-create) GH_ACCEPTANCE_HOST=github.com GH_ACCEPTANCE_ORG=gh-acceptance-testing GH_ACCEPTANCE_TOKEN=$(gh auth token) go test -tags=acceptance -run ^TestIssues$ ./acceptance
ok github.com/cli/cli/v2/acceptance 24.048s
Fixes #12798
Add a new
IssueRepoInfoAPI function that fetches only the minimal fields needed for issue operations (id,name,owner,hasIssuesEnabled,viewerPermission), and switch bothissue createandissue transferto use it instead ofGitHubRepo, which also fetchesdefaultBranchRefand merge-strategy fields that require additional token permissions beyond what issue commands need.Review notes
Most of the changes in this PR are test stub updates — switching mock query names from
RepositoryInfotoIssueRepositoryInfoand using the newStubIssueRepoInfoResponsehelper. The core logic change is minimal: a newIssueRepoInfofunction inapi/queries_repo.goand a one-line swap in each ofissue createandissue transfer. Each test stub was individually reviewed to confirm the response body only contains fields withinIssueRepoInfo's scope and no adjustments beyond the query name were needed.Alternative approach (#12873)
An alternative fix in #12873 uses
FetchRepositorywith onlyhasIssuesEnabled, but this missesid(needed by theIssueCreatemutation forrepositoryId) andviewerPermission(needed byViewerCanTriage()to decide whether to show the metadata prompt in interactive mode). A dedicated function likeIssueRepoInfoavoids this kind of issue by encapsulating the exact fields required, rather than pushing that responsibility onto each caller.