From b7893b1fd3f13a16ece16bd0c9bbda59c0a41918 Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Sat, 22 Feb 2020 16:54:30 +0900 Subject: [PATCH 1/6] Query pullRequests of the current branch regardless of its state --- api/queries_pr.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/queries_pr.go b/api/queries_pr.go index 734245acb..a5f9f2ea2 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -150,6 +150,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu fragment pr on PullRequest { number title + state url headRefName headRepositoryOwner { @@ -185,7 +186,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix := ` query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { - pullRequests(headRefName: $headRefName, states: OPEN, first: $per_page) { + pullRequests(headRefName: $headRefName, first: $per_page) { totalCount edges { node { From b9656835944f22b2deb22d797ec2a79fbd8b3119 Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Sat, 22 Feb 2020 16:56:30 +0900 Subject: [PATCH 2/6] Append a pull request status for the current branch --- command/pr.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/command/pr.go b/command/pr.go index 9d3e2487e..1080a1351 100644 --- a/command/pr.go +++ b/command/pr.go @@ -98,7 +98,7 @@ func prStatus(cmd *cobra.Command, args []string) error { printHeader(out, "Current branch") if prPayload.CurrentPR != nil { - printPrs(out, 0, *prPayload.CurrentPR) + printPrs(out, 0, true, *prPayload.CurrentPR) } else { message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]")) printMessage(out, message) @@ -107,7 +107,7 @@ func prStatus(cmd *cobra.Command, args []string) error { printHeader(out, "Created by you") if prPayload.ViewerCreated.TotalCount > 0 { - printPrs(out, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...) + printPrs(out, prPayload.ViewerCreated.TotalCount, false, prPayload.ViewerCreated.PullRequests...) } else { printMessage(out, " You have no open pull requests") } @@ -115,7 +115,7 @@ func prStatus(cmd *cobra.Command, args []string) error { printHeader(out, "Requesting a code review from you") if prPayload.ReviewRequested.TotalCount > 0 { - printPrs(out, prPayload.ReviewRequested.TotalCount, prPayload.ReviewRequested.PullRequests...) + printPrs(out, prPayload.ReviewRequested.TotalCount, false, prPayload.ReviewRequested.PullRequests...) } else { printMessage(out, " You have no pull requests to review") } @@ -382,10 +382,13 @@ func prSelectorForCurrentBranch(ctx context.Context) (prNumber int, prHeadRef st return } -func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) { +func printPrs(w io.Writer, totalCount int, printStatus bool, prs ...api.PullRequest) { for _, pr := range prs { prNumber := fmt.Sprintf("#%d", pr.Number) fmt.Fprintf(w, " %s %s %s", utils.Green(prNumber), text.Truncate(50, replaceExcessiveWhitespace(pr.Title)), utils.Cyan("["+pr.HeadLabel()+"]")) + if printStatus { + fmt.Fprintf(w, " (%s)", pr.State) + } checks := pr.ChecksStatus() reviews := pr.ReviewStatus() From 28460e4b7e5048430fb92457380d1d8a92a943d3 Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Tue, 25 Feb 2020 01:26:07 +0900 Subject: [PATCH 3/6] Change a PR state color --- command/pr.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/command/pr.go b/command/pr.go index 1080a1351..a55193d64 100644 --- a/command/pr.go +++ b/command/pr.go @@ -387,7 +387,13 @@ func printPrs(w io.Writer, totalCount int, printStatus bool, prs ...api.PullRequ prNumber := fmt.Sprintf("#%d", pr.Number) fmt.Fprintf(w, " %s %s %s", utils.Green(prNumber), text.Truncate(50, replaceExcessiveWhitespace(pr.Title)), utils.Cyan("["+pr.HeadLabel()+"]")) if printStatus { - fmt.Fprintf(w, " (%s)", pr.State) + if pr.State == "OPEN" { + fmt.Fprintf(w, " %s", utils.Green("(OPEN)")) + } else if pr.State == "MERGED" { + fmt.Fprintf(w, " %s", utils.Magenta("(MERGED)")) + } else if pr.State == "CLOSED" { + fmt.Fprintf(w, " %s", utils.Red("(CLOSED)")) + } } checks := pr.ChecksStatus() From 9c44eaeb6ec93937b8b62fd74e9954557c5f2aa6 Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Tue, 25 Feb 2020 01:27:19 +0900 Subject: [PATCH 4/6] Define an order clause to sort PRs newest first --- api/queries_pr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/queries_pr.go b/api/queries_pr.go index a5f9f2ea2..4976dd1fc 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -186,7 +186,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu queryPrefix := ` query($owner: String!, $repo: String!, $headRefName: String!, $viewerQuery: String!, $reviewerQuery: String!, $per_page: Int = 10) { repository(owner: $owner, name: $repo) { - pullRequests(headRefName: $headRefName, first: $per_page) { + pullRequests(headRefName: $headRefName, first: $per_page, orderBy: { field: CREATED_AT, direction: DESC }) { totalCount edges { node { From 957adc1cff90e0b6c6be4aaa6b068530871db6e1 Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Tue, 25 Feb 2020 14:33:39 +0900 Subject: [PATCH 5/6] Change a PR number's color based on its PR status --- command/pr.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/command/pr.go b/command/pr.go index a55193d64..8e9015f6f 100644 --- a/command/pr.go +++ b/command/pr.go @@ -98,7 +98,7 @@ func prStatus(cmd *cobra.Command, args []string) error { printHeader(out, "Current branch") if prPayload.CurrentPR != nil { - printPrs(out, 0, true, *prPayload.CurrentPR) + printPrs(out, 0, *prPayload.CurrentPR) } else { message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]")) printMessage(out, message) @@ -107,7 +107,7 @@ func prStatus(cmd *cobra.Command, args []string) error { printHeader(out, "Created by you") if prPayload.ViewerCreated.TotalCount > 0 { - printPrs(out, prPayload.ViewerCreated.TotalCount, false, prPayload.ViewerCreated.PullRequests...) + printPrs(out, prPayload.ViewerCreated.TotalCount, prPayload.ViewerCreated.PullRequests...) } else { printMessage(out, " You have no open pull requests") } @@ -115,7 +115,7 @@ func prStatus(cmd *cobra.Command, args []string) error { printHeader(out, "Requesting a code review from you") if prPayload.ReviewRequested.TotalCount > 0 { - printPrs(out, prPayload.ReviewRequested.TotalCount, false, prPayload.ReviewRequested.PullRequests...) + printPrs(out, prPayload.ReviewRequested.TotalCount, prPayload.ReviewRequested.PullRequests...) } else { printMessage(out, " You have no pull requests to review") } @@ -382,19 +382,17 @@ func prSelectorForCurrentBranch(ctx context.Context) (prNumber int, prHeadRef st return } -func printPrs(w io.Writer, totalCount int, printStatus bool, prs ...api.PullRequest) { +func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) { for _, pr := range prs { prNumber := fmt.Sprintf("#%d", pr.Number) - fmt.Fprintf(w, " %s %s %s", utils.Green(prNumber), text.Truncate(50, replaceExcessiveWhitespace(pr.Title)), utils.Cyan("["+pr.HeadLabel()+"]")) - if printStatus { - if pr.State == "OPEN" { - fmt.Fprintf(w, " %s", utils.Green("(OPEN)")) - } else if pr.State == "MERGED" { - fmt.Fprintf(w, " %s", utils.Magenta("(MERGED)")) - } else if pr.State == "CLOSED" { - fmt.Fprintf(w, " %s", utils.Red("(CLOSED)")) - } + if pr.State == "OPEN" { + fmt.Fprintf(w, " %s", utils.Green(prNumber)) + } else if pr.State == "MERGED" { + fmt.Fprintf(w, " %s", utils.Magenta(prNumber)) + } else if pr.State == "CLOSED" { + fmt.Fprintf(w, " %s", utils.Red(prNumber)) } + fmt.Fprintf(w, " %s %s", text.Truncate(50, replaceExcessiveWhitespace(pr.Title)), utils.Cyan("["+pr.HeadLabel()+"]")) checks := pr.ChecksStatus() reviews := pr.ReviewStatus() From 5f152a349dff5617a51638d352f7c7ad8596503d Mon Sep 17 00:00:00 2001 From: Toshiya Doi Date: Thu, 27 Feb 2020 00:07:48 +0900 Subject: [PATCH 6/6] Print merged and closed PRs of the current branch along with an opening PR --- api/queries_pr.go | 12 +++++++----- command/pr.go | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/api/queries_pr.go b/api/queries_pr.go index 4dc79acbd..fcfb3c914 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -10,7 +10,7 @@ import ( type PullRequestsPayload struct { ViewerCreated PullRequestAndTotalCount ReviewRequested PullRequestAndTotalCount - CurrentPR *PullRequest + CurrentPRs []PullRequest } type PullRequestAndTotalCount struct { @@ -262,11 +262,13 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu reviewRequested = append(reviewRequested, edge.Node) } - var currentPR = resp.Repository.PullRequest - if currentPR == nil { + var currentPRs []PullRequest + if resp.Repository.PullRequest != nil { + currentPRs = append(currentPRs, *resp.Repository.PullRequest) + } else { for _, edge := range resp.Repository.PullRequests.Edges { if edge.Node.HeadLabel() == currentPRHeadRef { - currentPR = &edge.Node + currentPRs = append(currentPRs, edge.Node) } } } @@ -280,7 +282,7 @@ func PullRequests(client *Client, repo ghrepo.Interface, currentPRNumber int, cu PullRequests: reviewRequested, TotalCount: resp.ReviewRequested.TotalCount, }, - CurrentPR: currentPR, + CurrentPRs: currentPRs, } return &payload, nil diff --git a/command/pr.go b/command/pr.go index 1515e88a7..7cd12796a 100644 --- a/command/pr.go +++ b/command/pr.go @@ -97,8 +97,8 @@ func prStatus(cmd *cobra.Command, args []string) error { fmt.Fprintln(out, "") printHeader(out, "Current branch") - if prPayload.CurrentPR != nil { - printPrs(out, 0, *prPayload.CurrentPR) + if prPayload.CurrentPRs != nil { + printPrs(out, 0, prPayload.CurrentPRs...) } else { message := fmt.Sprintf(" There is no pull request associated with %s", utils.Cyan("["+currentPRHeadRef+"]")) printMessage(out, message)