Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@bill3tt
Copy link
Contributor

@bill3tt bill3tt commented Oct 23, 2020

Hello πŸ‘‹ This is my first PR being raised here, so please let me know if I'm getting something wrong πŸ˜„

Currently, you can't view closed PRs by branch. This is due to a states: OPEN filter in the graphql query.

However, viewing the closed PR by number does work as the query does not have this condition here.

This PR removes this condition from the PullRequestForBranch function.

Before this change

➜  cli git:(c8b7dad) go run cmd/gh/main.go pr view quiye:issue-report -R cli/cli --web
no open pull requests found for branch "quiye:issue-report"

After this change

➜  cli git:(view-closed-prs) go run cmd/gh/main.go pr view quiye:issue-report -R cli/cli --web
Opening github.com/cli/cli/pull/2262 in your browser.

This closes #2270

@bill3tt bill3tt changed the title Allow gh pr view to view closed PRs Allow gh pr view <closed_pr_branch_name> to view closed PRs Oct 23, 2020
@samcoe
Copy link
Contributor

samcoe commented Oct 26, 2020

Thanks for contributing! I think this is a good start but the solution runs into some issues with other uses of PullRequestForBranch. For example, PullRequestForBranch, is used in pr/create command to determine if there is already an open PR for the current branch. This change would break that behavior and return the incorrect result if there was a closed PR for the current branch. What I think we need to do is add a state argument to PullRequestForBranch so that the callers can decide if they want to look for open or closed PRs.

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.

Good find! Thanks for taking this on πŸ™‡

query PullRequestForBranch($owner: String!, $repo: String!, $headRefName: String!) {
repository(owner: $owner, name: $repo) {
pullRequests(headRefName: $headRefName, states: OPEN, first: 30) {
pullRequests(headRefName: $headRefName, first: 30) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This fixes an immediate problem, but causes another one.

PullRequestForBranch is called in two places:

  1. find the PR for a branch, e.g. in pr view;
  2. check if there are open PRs for a branch during pr create.

This change would break the feature described in (2).

I think the solution should be to have PullRequestForBranch accept an argument that dictates whether we're searching through open PRs or all PRs regardless of state. Also, when searching over all PRs, precedence should still be given to an open PR if one exists, even if a closed PR for the same branch exists and is chronologically newer.

return client.REST(repo.RepoHost(), "DELETE", path, nil, nil)
}

// sortPRsByPrecedence in the following order: OPEN, MERGED, CLOSED
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is a neater way of doing this (for example in the GraphQL query) then I am all ears! πŸ‘‚

Copy link
Contributor

Choose a reason for hiding this comment

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

Our GraphQL API doesn't allow sorting by state, so doing it in memory like this is fine!

@bill3tt
Copy link
Contributor Author

bill3tt commented Oct 26, 2020

@samcoe / @mislav - thank you both for the swift & helpful review πŸ™Œ I have attempted to address both of your comments in the subsequent commits.

Couple of things to point out:

  • No client-side validation of the filters passed into the function. This feels reasonable as these cannot be supplied by the user.
  • Order of precedence I have gone with for PRs is OPEN, MERGED, CLOSED - do shout if you other preferences for this ordering.

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.

Thanks for the updates! Minor notes:

return client.REST(repo.RepoHost(), "DELETE", path, nil, nil)
}

// sortPRsByPrecedence in the following order: OPEN, MERGED, CLOSED
Copy link
Contributor

Choose a reason for hiding this comment

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

Our GraphQL API doesn't allow sorting by state, so doing it in memory like this is fine!

Comment on lines 1159 to 1161
if (prs[b].State == "MERGED" && prs[a].State =="CLOSED") ||
(prs[b].State == "OPEN" && prs[a].State =="CLOSED") ||
(prs[b].State == "OPEN" && prs[a].State == "MERGED") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you're only sorting by open-first (other states do not matter), I think you can get away with simply:

return prs[a].State == "OPEN"

In this case, you might want to use SliceStable instead of Slice to preserve the original order of records that rank the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense :) Updated this function, test & comment to reflect this.

Copy link
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

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

Thanks for making the requested changes. This looks good to me, had just one small naming comment πŸ‘

}

// sortPRsByPrecedence ensures that OPEN PRs precede non-open states (MERGED, CLOSED)
func sortPRsByPrecedence(prs []PullRequest) []PullRequest {
Copy link
Contributor

Choose a reason for hiding this comment

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

One small nit is that can we name this sortPullRequestsByState?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

of course 😁

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.

Thank you for your hard work! <3

@mislav mislav merged commit e093d23 into cli:trunk Oct 28, 2020
@bill3tt
Copy link
Contributor Author

bill3tt commented Oct 28, 2020

Thanks to you both for the swift & helpful reviews πŸ™Œ

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 <closed_pr_branch> should show the closed PR

3 participants