fix(pr): remove numberFieldOnly optimization that skips API validation#13327
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
96066b7 to
8ff70e6
Compare
Contributor
There was a problem hiding this comment.
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
numberFieldOnlyoptimization 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 thePullRequestByNumberquery to return the not-found GraphQL error response and assertswantErr: 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #13319
Removes the
numberFieldOnlyoptimization inpkg/cmd/pr/shared/finder.gothat causedgh pr view <N> --json numberto return exit 0 even whenNwas an issue number (not a PR).Problem
The optimization short-circuited the API call when only the
numberfield was requested, returning a syntheticPullRequeststruct without validating that the number actually corresponds to a PR. Sincef.prNumberis 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.