8000 Fix panic on `gh pr view 0` by nopcoder · Pull Request #10729 · cli/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix panic on gh pr view 0 #10729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 2, 2025
Merged

Fix panic on gh pr view 0 #10729

merged 12 commits into from
May 2, 2025

Conversation

nopcoder
Copy link
Contributor
@nopcoder nopcoder commented Apr 4, 2025

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 #10728

@nopcoder nopcoder requested a review from a team as a code owner April 4, 2025 08:21
@nopcoder nopcoder requested a review from BagToad April 4, 2025 08:21
@nopcoder nopcoder temporarily deployed to cli-automation April 4, 2025 08:21 — with GitHub Actions Inactive
@nopcoder nopcoder changed the title Fix panic on gh pr view 0 Fix panic on gh pr view 0 Apr 4, 2025
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Apr 4, 2025
@BagToad
Copy link
Member
BagToad commented Apr 7, 2025

👋 Hi @nopcoder!

Thanks for working on this! We currently have some significant changes to gh pr create underway in #10513, so we are going to review this after that work is completed.

At a glance though, it would be good to add a test case for this ❤️

@nopcoder
Copy link
Contributor Author
nopcoder commented Apr 7, 2025

Thanks @BagToad for the fast feedback, I'll look into test case and wait for the big change and rebase.

@BagToad BagToad added blocked and removed blocked labels Apr 9, 2025
@BagToad
Copy link
Member
BagToad commented Apr 15, 2025

👋 Hi @nopcoder

This PR is no longer blocked - please feel free to rebase your branch or merge in changes from trunk.

Please @ mention me when you're ready for a review 😁

@nopcoder
Copy link
Contributor Author
nopcoder commented Apr 17, 2025

@BagToad updated the code with the latest changes. Let me know if this is the preferred way to handle this edge case.

@BagToad
Copy link
Member
BagToad commented Apr 28, 2025

👋 Hey @nopcoder, apologies for the delay reviewing this.

I'm looking through it, and I can confirm that this does solve the problem. I can also confirm that this change passes acceptance tests.

That said, I am wondering if we could make it more clear why this change fixes the problem. I think we could improve the code to make it clear what each expected state is.

Here's sort of what I was thinking:

	if f.prNumber > 0 {
		// We have a PR number, let's look up by the PR number.

		if numberFieldOnly {
			// avoid hitting the API if we already have all the information
			return &api.PullRequest{Number: f.prNumber}, f.baseRefRepo, nil
		}
		pr, err = findByNumber(httpClient, f.baseRefRepo, f.prNumber, fields.ToSlice())
		if err != nil {
			return pr, f.baseRefRepo, err
		}
	} 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 and we don't have a base repo,
		// we can't do anything.

		return nil, nil, &NotFoundError{fmt.Errorf("no pull requests found")}
	}

What do you think?

I'll probably also get a second set of eyes on this after we talk about it, since gh pr related logic is very complex and I don't want us to miss anything! 😁

@nopcoder
Copy link
Contributor Author

@BagToad Thanks! updated the code and the test by your suggestion. much better than doing a lookup that will always fail.

Copy link
Member
@BagToad BagToad left a comment

Choose a reason for hiding this comment

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

@nopcoder Sorry for the delay - I was having a colleague check our work here 😁

I also re-ran our Acceptance Tests and they look good 👍

Test results

❯ GH_ACCEPTANCE_TOKEN=$(gh auth token) GH_ACCEPTANCE_HOST=github.com GH_ACCEPTANCE_ORG=gh-acceptance-testing go test -count=1 -tags=acceptance -run ^TestPullRequests$ ./acceptance -v --json | tparse --all
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │                                   TEST                                    │             PACKAGE               │
│─────────┼─────────┼───────────────────────────────────────────────────────────────────────────┼───────────────────────────────────│
│  PASS   │   17.88 │ TestPullRequests/pr-checkout-with-url-from-fork                           │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   16.81 │ TestPullRequests/pr-view-status-respects-remote-pushdefault               │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   16.57 │ TestPullRequests/pr-view-status-respects-branch-pushremote                │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   16.05 │ TestPullRequests/pr-create-edit-with-project                              │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   14.87 │ TestPullRequests/pr-view-same-org-fork                                    │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   12.57 │ TestPullRequests/pr-create-from-issue-develop-base                        │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   11.73 │ TestPullRequests/pr-comment-edit-last-with-comments                       │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   11.45 │ TestPullRequests/pr-merge-merge-strategy                                  │ github.com/cli/cli/v2/acceptance  │
│  PASS   │   10.83 │ TestPullRequests/pr-merge-rebase-strategy                                 │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    9.77 │ TestPullRequests/pr-create-respects-simple-pushdefault                    │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    9.26 │ TestPullRequests/pr-create-with-metadata                                  │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    8.53 │ TestPullRequests/pr-comment-new                                           │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    8.46 │ TestPullRequests/pr-create-from-manual-merge-base                         │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    8.44 │ TestPullRequests/pr-view-status-respects-push-destination                 │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    8.26 │ TestPullRequests/pr-comment-edit-last-without-comments-creates            │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    8.16 │ TestPullRequests/pr-view                                                  │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    8.14 │ TestPullRequests/pr-list                                                  │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    8.00 │ TestPullRequests/pr-view-status-respects-simple-pushdefault               │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    7.98 │ TestPullRequests/pr-comment-edit-last-without-comments-errors             │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    7.98 │ TestPullRequests/pr-checkout-by-number                                    │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    7.68 │ TestPullRequests/pr-create-without-upstream-config                        │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    7.21 │ TestPullRequests/pr-checkout                                              │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    6.90 │ TestPullRequests/pr-create-no-local-repo                                  │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    6.76 │ TestPullRequests/pr-view-outside-repo                                     │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    6.72 │ TestPullRequests/pr-create-basic                                          │ github.com/cli/cli/v2/acceptance  │
│  PASS   │    0.00 │ TestPullRequests                                                          │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.01 │ TestPullRequests/pr-create-guesses-remote-from-sha-with-branch-name-slash │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.01 │ TestPullRequests/pr-create-guesses-remote-from-sha                        │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.04 │ TestPullRequests/pr-create-push-default-upstream-no-merge-ref-fork        │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.04 │ TestPullRequests/pr-create-push-default-upstream-no-merge-ref             │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.04 │ TestPullRequests/pr-create-remote-ref-with-branch-name-slash              │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.04 │ TestPullRequests/pr-create-respects-branch-pushremote                     │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.04 │ TestPullRequests/pr-create-respects-push-destination                      │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.01 │ TestPullRequests/pr-create-respects-remote-pushdefault                    │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.01 │ TestPullRequests/pr-create-respects-user-colon-branch-syntax              │ github.com/cli/cli/v2/acceptance  │
│  SKIP   │    0.01 │ TestPullRequests/pr-status-respects-cross-org                             │ github.com/cli/cli/v2/acceptance  │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌────────────────────────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │             PACKAGE              │ COVER │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼──────────────────────────────────┼───────┼──────┼──────┼───────│
│  PASS   │ 36.60s  │ github.com/cli/cli/v2/acceptance │  --   │  26  │  0   │  10   │
└────────────────────────────────────────────────────────────────────────────────────┘

@BagToad BagToad merged commit 7b86830 into cli:trunk May 2, 2025
9 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request May 20, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://github.com/cli/cli) | minor | `v2.72.0` -> `v2.73.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.73.0`](https://github.com/cli/cli/releases/tag/v2.73.0): GitHub CLI 2.73.0

[Compare Source](cli/cli@v2.72.0...v2.73.0)

#### :copilot: Copilot Coding Agent Support

You can now assign issues to GitHub Copilot directly from `gh`, just as you would assign them to a teammate. Use `gh issue edit <number> --add-assignee @&#8203;copilot` to assign the GitHub Copilot coding agent, and Copilot will work in the background to understand the issue, propose a solution, and open a pull request when it's ready for your review. If you run `gh issue edit` interactively, `Copilot (AI)` will be displayed as a potential assignee. This feature is available for GitHub Copilot Pro+ and Copilot Enterprise subscribers. For more details, refer to [the full changelog post for Copilot coding agent](https://github.blog/changelog/2025-05-19-github-copilot-coding-agent-in-public-preview/).

#### What's Changed

##### ✨ Features

-   Copilot is assignable to issues and pull requests with `issue edit` and `pr edit` by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10992
    -   `gh issue edit`: actors are assignable to issues by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10960
    -   `gh pr edit`: Assign actors to pull requests by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10984
    -   `issue edit`, `pr edit`: handle display names in interactive assignee editing   by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10990
    -   `issue edit`, `pr edit`: Support special non-interactive (flags) assignee name `@copilot` by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10991
-   \[gh issue/pr comment] Add support for last comment delete for issues and MRs by [@&#8203;sinansonmez](https://github.com/sinansonmez) in cli/cli#10596
-   \[gh issue view] Expose `closedByPullRequestsReferences` JSON field by [@&#8203;iamazeem](https://github.com/iamazeem) in cli/cli#10941
-   Accessible prompter always displays selection defaults in a format readable by a screen reader by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10937

##### 🐛 Fixes

-   Fix `StatusJSONResponse` usage by [@&#8203;babakks](https://github.com/babakks) in cli/cli#10810
-   Fix panic on `gh pr view 0` by [@&#8203;nopcoder](https://github.com/nopcoder) in cli/cli#10729
-   Fix flakey test for accessible prompter by [@&#8203;BagToad](https://github.com/BagToad) in cli/cli#10918
-   Fix accessible prompter flaky tests by [@&#8203;babakks](https://github.com/babakks) in cli/cli#10977
-   Handle missing archive URLs on release download by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10947
-   Fix bug when removing all MR reviewers by [@&#8203;babakks](https://github.com/babakks) in cli/cli#10975

##### 📚 Docs & Chores

-   Feature detect v1 projects on pr view by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10821
-   Feature detect v1 projects on non-interactive pr create by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10909
-   Feature detect v1 projects on web mode pr create by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10911
-   Feature detect v1 projects on interactive `pr create` by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10915
-   Feature detect v1 projects on pr edit by [@&#8203;williammartin](https://github.com/williammartin) in cli/cli#10942
-   Move predicate type filtering in `gh attestation verify` by [@&#8203;malancas](https://github.com/malancas) in cli/cli#10670
-   Improve assertion for disabled echo mode by [@&#8203;babakks](https://github.com/babakks) in cli/cli#10927

##### :dependabot: Dependencies

-   chore(deps): bump actions/attest-build-provenance from 2.2.2 to 2.3.0 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10886
-   chore(deps): bump github.com/cpuguy83/go-md2man/v2 from 2.0.6 to 2.0.7 by [@&#8203;dependabot](https://github.com/dependabot) in cli/cli#10869

#### What's Changed

#### New Contributors

-   [@&#8203;sinansonmez](https://github.com/sinansonmez) made their first contribution in cli/cli#10596
-   [@&#8203;nopcoder](https://github.com/nopcoder) made their first contribution in cli/cli#10729

**Full Changelog**: cli/cli@v2.72.0...v2.73.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4xNS4wIiwidXBkYXRlZEluVmVyIjoiNDAuMTUuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external pull request originating outside of the CLI core team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gh pr view 0 panic
3 participants
0