Skip to content

fix(issue): avoid fetching unnecessary fields for discovery#12884

Merged
williammartin merged 7 commits intotrunkfrom
babakks/use-min-discovery-fields-for-issue-create
Mar 25, 2026
Merged

fix(issue): avoid fetching unnecessary fields for discovery#12884
williammartin merged 7 commits intotrunkfrom
babakks/use-min-discovery-fields-for-issue-create

Conversation

@babakks
Copy link
Copy Markdown
Member

@babakks babakks commented Mar 9, 2026

Fixes #12798

Add a new IssueRepoInfo API function that fetches only the minimal fields needed for issue operations (id, name, owner, hasIssuesEnabled, viewerPermission), and switch both issue create and issue transfer to use it instead of GitHubRepo, which also fetches defaultBranchRef and 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 RepositoryInfo to IssueRepositoryInfo and using the new StubIssueRepoInfoResponse helper. The core logic change is minimal: a new IssueRepoInfo function in api/queries_repo.go and a one-line swap in each of issue create and issue transfer. Each test stub was individually reviewed to confirm the response body only contains fields within IssueRepoInfo's scope and no adjustments beyond the query name were needed.

Alternative approach (#12873)

An alternative fix in #12873 uses FetchRepository with only hasIssuesEnabled, but this misses id (needed by the IssueCreate mutation for repositoryId) and viewerPermission (needed by ViewerCanTriage() to decide whether to show the metadata prompt in interactive mode). A dedicated function like IssueRepoInfo avoids this kind of issue by encapsulating the exact fields required, rather than pushing that responsibility onto each caller.

babakks and others added 6 commits March 9, 2026 11:30
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>
@babakks babakks marked this pull request as ready for review March 9, 2026 12:08
@babakks babakks requested a review from a team as a code owner March 9, 2026 12:08
@babakks babakks requested review from Copilot and williammartin March 9, 2026 12:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 IssueRepoInfo API function in api/queries_repo.go with a narrower GraphQL query (IssueRepositoryInfo) that avoids permission-sensitive fields.
  • Switched issue create and issue transfer from api.GitHubRepo to api.IssueRepoInfo.
  • Updated all test stubs to match the new query name and added unit tests for the new function, plus a StubIssueRepoInfoResponse test 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 GitHubRepoIssueRepoInfo
pkg/cmd/issue/create/create_test.go Update mock query names and stub helpers
pkg/cmd/issue/transfer/transfer.go Swap GitHubRepoIssueRepoInfo
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>
Copy link
Copy Markdown
Member

@williammartin williammartin left a comment

Choose a reason for hiding this comment

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

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

@williammartin williammartin merged commit 69d89a6 into trunk Mar 25, 2026
11 checks passed
@williammartin williammartin deleted the babakks/use-min-discovery-fields-for-issue-create branch March 25, 2026 14:39
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.

gh issue create fails with fine-grained PAT that has Issues:Write but not Contents:Read

3 participants