Allow gh pr view <closed_pr_branch_name> to view closed PRs#2276
Conversation
gh pr view to view closed PRsgh pr view <closed_pr_branch_name> to view closed PRs
|
Thanks for contributing! I think this is a good start but the solution runs into some issues with other uses of |
mislav
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
This fixes an immediate problem, but causes another one.
PullRequestForBranch is called in two places:
- find the PR for a branch, e.g. in
pr view; - 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 |
There was a problem hiding this comment.
If there is a neater way of doing this (for example in the GraphQL query) then I am all ears! 👂
There was a problem hiding this comment.
Our GraphQL API doesn't allow sorting by state, so doing it in memory like this is fine!
|
@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:
|
mislav
left a comment
There was a problem hiding this comment.
Thanks for the updates! Minor notes:
| return client.REST(repo.RepoHost(), "DELETE", path, nil, nil) | ||
| } | ||
|
|
||
| // sortPRsByPrecedence in the following order: OPEN, MERGED, CLOSED |
There was a problem hiding this comment.
Our GraphQL API doesn't allow sorting by state, so doing it in memory like this is fine!
| 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") { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Makes sense :) Updated this function, test & comment to reflect this.
samcoe
left a comment
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
One small nit is that can we name this sortPullRequestsByState?
This method doesn't necessarily search through open pull requests.
1c24307 to
fb621d2
Compare
mislav
left a comment
There was a problem hiding this comment.
Thank you for your hard work! <3
|
Thanks to you both for the swift & helpful reviews 🙌 |
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: OPENfilter 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
PullRequestForBranchfunction.Before this change
After this change
This closes #2270