Skip to content

fix(pr): remove numberFieldOnly optimization that skips API validation#13327

Merged
babakks merged 1 commit into
trunkfrom
wm/fix-pr-view-number-only-optimization
May 2, 2026
Merged

fix(pr): remove numberFieldOnly optimization that skips API validation#13327
babakks merged 1 commit into
trunkfrom
wm/fix-pr-view-number-only-optimization

Conversation

@williammartin
Copy link
Copy Markdown
Member

@williammartin williammartin commented May 1, 2026

Summary

Fixes #13319

Removes the numberFieldOnly optimization in pkg/cmd/pr/shared/finder.go that caused gh pr view <N> --json number to return exit 0 even when N was an issue number (not a PR).

Problem

The optimization short-circuited the API call when only the number field was requested, returning a synthetic PullRequest struct without validating that the number actually corresponds to a PR. Since f.prNumber is set by parsing the selector as an integer, any valid number (including issue numbers) would succeed.

Fix

Remove the optimization entirely. It saved a single API call for an extremely uncommon query - the caller already knows the number from the argument they passed. The correctness cost is not worth the marginal performance gain.

Copilot AI review requested due to automatic review settings May 1, 2026 15:19
@williammartin williammartin requested a review from a team as a code owner May 1, 2026 15:19
@williammartin williammartin requested a review from babakks May 1, 2026 15:19
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@williammartin williammartin force-pushed the wm/fix-pr-view-number-only-optimization branch from 96066b7 to 8ff70e6 Compare May 1, 2026 15:23
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

Removes the numberFieldOnly fast-path in the PR finder so gh pr view <N> --json number still performs an API lookup and fails when <N> is not a pull request number (e.g., it’s an Issue number).

Changes:

  • Removed the numberFieldOnly optimization that returned a synthetic PR without API validation.
  • Updated the "number only" finder test to stub the GraphQL request now that the API call always happens.
Show a summary per file
File Description
pkg/cmd/pr/shared/finder.go Removes the early-return path so PR numbers are always validated via API lookup.
pkg/cmd/pr/shared/finder_test.go Updates the "number only" test to include a GraphQL stub because the lookup is no longer skipped.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comments suppressed due to low confidence (1)

pkg/cmd/pr/shared/finder_test.go:350

  • The updated "number only" test now stubs the GraphQL call, but there still isn’t a regression test for the reported bug: passing an Issue number (non‑PR) with fields: []string{"number"} should return an error. Consider adding a test case that stubs the PullRequestByNumber query to return the not-found GraphQL error response and asserts wantErr: true, so this behavior is locked in.
			name: "pr number zero",
			args: args{
				selector:   "0",
				fields:     []string{"number"},
				baseRepoFn: stubBaseRepoFn(ghrepo.New("ORIGINOWNER", "REPO"), nil),
				branchFn: func() (string, error) {
					return "blueberries", nil
				},
				gitConfigClient: stubGitConfigClient{
					readBranchConfigFn:  stubBranchConfig(git.BranchConfig{}, nil),
					pushDefaultFn:       stubPushDefault(git.PushDefaultSimple, nil),
					remotePushDefaultFn: stubRemotePushDefault("", nil),
				},
			},
			wantErr: true,
		},
		{
			name: "number with hash argument",
			args: args{
				selector:   "#13",
				fields:     []string{"id", "number"},
  • Files reviewed: 2/2 changed files
  • Comments generated: 0

Copy link
Copy Markdown
Member

@babakks babakks left a comment

Choose a reason for hiding this comment

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

Good catch!

@babakks babakks merged commit 50d6008 into trunk May 2, 2026
18 checks passed
@babakks babakks deleted the wm/fix-pr-view-number-only-optimization branch May 2, 2026 06:53
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 pr view --json number returns exit 0 for Issue numbers due to numberFieldOnly optimization skipping API validation

3 participants