Skip to content

Conversation

AyushRawal
Copy link
Contributor

Fixes #4263

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Looks good! Do you think it will be possible to cover this behavior with a test scoped to findForBranch?

@@ -314,7 +318,9 @@ func findForBranch(httpClient *http.Client, repo ghrepo.Interface, baseBranch, h

for _, pr := range prs {
if pr.HeadLabel() == headBranch && (baseBranch == "" || pr.BaseRefName == baseBranch) {
return &pr, nil
if pr.State == "OPEN" || resp.Repository.DefaultBranchRef.Name != headBranch {
Copy link
Contributor

Choose a reason for hiding this comment

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

This condition looks good but could be merged with the if condition that precedes it. Something like:

if pr.HeadLabel() == headBranch && (baseBranch == "" || pr.BaseRefName == baseBranch) && (pr.State == "OPEN" || resp.Repository.DefaultBranchRef.Name != headBranch) {

Feel free to split the condition into multiple lines if that looks better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am of 2 minds 😅. I can do it however you like.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well then just put it on 1-line if condition and then we'll see. As long as it's not nested ifs 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@AyushRawal
Copy link
Contributor Author

Looks good! Do you think it will be possible to cover this behavior with a test scoped to findForBranch?

I can't think of any way to do that, but I don't have much experience writing tests. If you have any ideas, please help me out.

@mislav mislav self-assigned this Sep 7, 2021
@mislav
Copy link
Contributor

mislav commented Sep 7, 2021

No worries. We'll try to add a test before this merges ✌️

@andrewhsu
Copy link
Contributor

I had a look at this fix and from the existing code coverage html report:

bash$ go test github.com/cli/cli/v2/pkg/cmd/pr/shared -run TestFind -coverprofile=coverage.out
ok      github.com/cli/cli/v2/pkg/cmd/pr/shared 0.015s  coverage: 9.7% of statements
bash$ go tool cover -html=coverage.out -o coverage.html

it seems just this line is not covered after :

return nil, &NotFoundError{fmt.Errorf("no pull requests found for branch %q", headBranch)}

so a test case that can get to there would effectively run through the new if condition.

I'd be willing to create a patch for finder_test.go if it hasn't already been done. Let me know if that would be desirable.

@andrewhsu
Copy link
Contributor

I've created a commit andrewhsu@22d689f with the test. Feel free to cherry-pick or let me know where I should create a PR for this.

The commit adds a test case to finder_test.go. I basically duplicated the test case no argument reads current branch and tweaked it. I added defaultBranchRef so the value would match headBranch in the new if condition. Here is a diff of what I changed:

2c2
<                       name: "no argument reads current branch",
---
>                       name: "current branch with merged pr",
23c23
<                                                               "state": "OPEN",
---
>                                                               "state": "MERGED",
29c29,32
<                                               ]}
---
>                                               ]},
>                                               "defaultBranchRef":{
>                                                       "name": "blueberries"
>                                               }
32,33c35
<                       wantPR:   13,
<                       wantRepo: "https://github.com/OWNER/REPO",
---
>                       wantErr: true,

I found https://docs.github.com/en/graphql/overview/explorer helpful because this is my first time diving into GraphQL. Let me know if I've missed the pool. 🏊🏼‍♂️

@mislav mislav added the external pull request originating outside of the CLI core team label Dec 20, 2021
andrewhsu and others added 2 commits December 20, 2021 19:29
To ensure only open PRs are shown on default branch when running `gh pr
view`.
@AyushRawal AyushRawal requested a review from a team as a code owner December 20, 2021 18:31
@AyushRawal AyushRawal requested review from mislav and removed request for a team December 20, 2021 18:31
@mislav
Copy link
Contributor

mislav commented Dec 20, 2021

@andrewhsu Much appreciated!

@AyushRawal Thank you for your patience 🙇

@mislav mislav enabled auto-merge (squash) December 20, 2021 18:34
@mislav mislav merged commit ca25026 into cli:trunk Dec 20, 2021
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.

Prevent pr commands without arguments from accidentally applying to defunct, closed PRs
3 participants