nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: cli/cli
Pull request #10729 (2025-04-04 08:21:46)

Fix panic on `gh pr view 0`
Handle the case where PR number parse fail with invalid number while no base branch is set.

Wasn't sure if this is the way gh cli prefer to handle this check, but it worked for me and I'll be happy to update / align to a better way.

Close https://github.com/cli/cli/issues/10728



Patch:
diff --git a/pkg/cmd/pr/shared/finder.go b/pkg/cmd/pr/shared/finder.go
index e6bb7d66a..7cbde9b27 100644
--- a/pkg/cmd/pr/shared/finder.go
+++ b/pkg/cmd/pr/shared/finder.go
@@ -213,6 +213,7 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err
 
 	var pr *api.PullRequest
 	if f.prNumber > 0 {
+		// If we have a PR number, let's look it up
 		if numberFieldOnly {
 			// avoid hitting the API if we already have all the information
 			return &api.PullRequest{Number: f.prNumber}, f.baseRefRepo, nil
@@ -221,11 +222,16 @@ func (f *finder) Find(opts FindOptions) (*api.PullRequest, ghrepo.Interface, err
 		if err != nil {
 			return pr, f.baseRefRepo, err
 		}
-	} else {
+	} else if prRefs.BaseRepo() != nil && f.branchName != "" {
+		// No PR number, but we have a base repo and branch name.
 		pr, err = findForRefs(httpClient, prRefs, opts.States, fields.ToSlice())
 		if err != nil {
 			return pr, f.baseRefRepo, err
 		}
+	} else {
+		// If we don't have a PR number or a base repo and branch name,
+		// we can't do anything
+		return nil, f.baseRefRepo, &NotFoundError{fmt.Errorf("no pull requests found")}
 	}
 
 	g, _ := errgroup.WithContext(context.Background())


Test patch:
diff --git a/pkg/cmd/pr/shared/finder_test.go b/pkg/cmd/pr/shared/finder_test.go
index e1aae16b1..abc754d1a 100644
--- a/pkg/cmd/pr/shared/finder_test.go
+++ b/pkg/cmd/pr/shared/finder_test.go
@@ -165,6 +165,23 @@ func TestFind(t *testing.T) {
 			wantPR:   13,
 			wantRepo: "https://github.com/ORIGINOWNER/REPO",
 		},
+		{
+			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{