From de77e78ef57b0764d87ecf8de5d6aaa1ceb81afa Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Fri, 14 Feb 2020 14:15:07 +0500 Subject: [PATCH 01/13] added pr count string before printing PRs --- command/pr.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/command/pr.go b/command/pr.go index d1cb7e5be..917964d3b 100644 --- a/command/pr.go +++ b/command/pr.go @@ -206,6 +206,9 @@ func prList(cmd *cobra.Command, args []string) error { printMessage(colorErr, msg) return nil } + + prCountMsg := fmt.Sprintf("%d pull requests match your search\n", len(prs)) + printMessage(colorableErr(cmd), prCountMsg) table := utils.NewTablePrinter(cmd.OutOrStdout()) for _, pr := range prs { From 906f49659fa85e3496425454371519302cec36ed Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Sat, 15 Feb 2020 22:37:13 +0500 Subject: [PATCH 02/13] updated title and tests --- command/issue.go | 8 +++++--- command/issue_test.go | 3 +-- command/pr.go | 10 +++++----- command/pr_test.go | 3 +-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/command/issue.go b/command/issue.go index 6caf81c12..b8f424311 100644 --- a/command/issue.go +++ b/command/issue.go @@ -108,13 +108,13 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(colorableErr(cmd), "\nIssues for %s\n\n", ghrepo.FullName(*baseRepo)) - issues, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit) if err != nil { return err } + title := fmt.Sprintf("\n%s in %s\n\n", "%s", ghrepo.FullName(*baseRepo)) + if len(issues) == 0 { colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open issues" message is actually an issue msg := "There are no open issues" @@ -126,10 +126,12 @@ func issueList(cmd *cobra.Command, args []string) error { if userSetFlags { msg = "No issues match your search" } - printMessage(colorErr, msg) + fmt.Fprintf(colorErr, title, msg) return nil } + fmt.Fprintf(colorableErr(cmd), title, utils.Pluralize(len(issues), "issue")) + out := cmd.OutOrStdout() table := utils.NewTablePrinter(out) for _, issue := range issues { diff --git a/command/issue_test.go b/command/issue_test.go index c09de2c62..ba36101d4 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -142,9 +142,8 @@ func TestIssueList_withFlags(t *testing.T) { eq(t, output.String(), "") eq(t, output.Stderr(), ` -Issues for OWNER/REPO +No issues match your search in OWNER/REPO -No issues match your search `) bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body) diff --git a/command/pr.go b/command/pr.go index 917964d3b..999036607 100644 --- a/command/pr.go +++ b/command/pr.go @@ -135,7 +135,6 @@ func prList(cmd *cobra.Command, args []string) error { return err } - fmt.Fprintf(colorableErr(cmd), "\nPull requests for %s\n\n", ghrepo.FullName(*baseRepo)) limit, err := cmd.Flags().GetInt("limit") if err != nil { @@ -192,6 +191,8 @@ func prList(cmd *cobra.Command, args []string) error { return err } + title := fmt.Sprintf("\n%s in %s\n\n", "%s", ghrepo.FullName(*baseRepo)) + if len(prs) == 0 { colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open prs" message is acually a pr msg := "There are no open pull requests" @@ -203,13 +204,12 @@ func prList(cmd *cobra.Command, args []string) error { if userSetFlags { msg = "No pull requests match your search" } - printMessage(colorErr, msg) + fmt.Fprintf(colorErr, title, msg) return nil } - - prCountMsg := fmt.Sprintf("%d pull requests match your search\n", len(prs)) - printMessage(colorableErr(cmd), prCountMsg) + fmt.Fprintf(colorableErr(cmd), title, utils.Pluralize(len(prs), "pull request")) + table := utils.NewTablePrinter(cmd.OutOrStdout()) for _, pr := range prs { prNum := strconv.Itoa(pr.Number) diff --git a/command/pr_test.go b/command/pr_test.go index d621d447b..66c59d4d8 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -192,9 +192,8 @@ func TestPRList_filtering(t *testing.T) { eq(t, output.String(), "") eq(t, output.Stderr(), ` -Pull requests for OWNER/REPO +No pull requests match your search in OWNER/REPO -No pull requests match your search `) bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body) From c50f6f324e97c9b4192f2ccb7aaad9ceec67ee18 Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Sat, 15 Feb 2020 23:06:08 +0500 Subject: [PATCH 03/13] updated list tests --- command/issue_test.go | 5 +++++ command/pr_test.go | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/command/issue_test.go b/command/issue_test.go index ba36101d4..e67e62fb2 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -109,6 +109,11 @@ func TestIssueList(t *testing.T) { t.Errorf("error running command `issue list`: %v", err) } + eq(t, output.Stderr(), ` +3 issues in OWNER/REPO + +`) + expectedIssues := []*regexp.Regexp{ regexp.MustCompile(`(?m)^1\t.*won`), regexp.MustCompile(`(?m)^2\t.*too`), diff --git a/command/pr_test.go b/command/pr_test.go index 66c59d4d8..7f3555733 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -171,6 +171,10 @@ func TestPRList(t *testing.T) { t.Fatal(err) } + eq(t, output.Stderr(), ` +3 pull requests in OWNER/REPO + +`) eq(t, output.String(), `32 New feature feature 29 Fixed bad bug hubot:bug-fix 28 Improve documentation docs From 94ef4bb3774bb8ee9b8641c5478287bde827383c Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Sun, 16 Feb 2020 12:26:36 +0500 Subject: [PATCH 04/13] switched title to fn literal --- command/issue.go | 8 +++++--- command/pr.go | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/command/issue.go b/command/issue.go index b8f424311..a80a0de45 100644 --- a/command/issue.go +++ b/command/issue.go @@ -113,7 +113,9 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - title := fmt.Sprintf("\n%s in %s\n\n", "%s", ghrepo.FullName(*baseRepo)) + title := func (msg string) string { + return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo)) + } if len(issues) == 0 { colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open issues" message is actually an issue @@ -126,11 +128,11 @@ func issueList(cmd *cobra.Command, args []string) error { if userSetFlags { msg = "No issues match your search" } - fmt.Fprintf(colorErr, title, msg) + fmt.Fprintf(colorErr, title(msg)) return nil } - fmt.Fprintf(colorableErr(cmd), title, utils.Pluralize(len(issues), "issue")) + fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(issues), "issue"))) out := cmd.OutOrStdout() table := utils.NewTablePrinter(out) diff --git a/command/pr.go b/command/pr.go index 999036607..8e51bb2a0 100644 --- a/command/pr.go +++ b/command/pr.go @@ -191,7 +191,9 @@ func prList(cmd *cobra.Command, args []string) error { return err } - title := fmt.Sprintf("\n%s in %s\n\n", "%s", ghrepo.FullName(*baseRepo)) + title := func (msg string) string { + return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo)) + } if len(prs) == 0 { colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open prs" message is acually a pr @@ -204,11 +206,11 @@ func prList(cmd *cobra.Command, args []string) error { if userSetFlags { msg = "No pull requests match your search" } - fmt.Fprintf(colorErr, title, msg) + fmt.Fprintf(colorErr, title(msg)) return nil } - fmt.Fprintf(colorableErr(cmd), title, utils.Pluralize(len(prs), "pull request")) + fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(prs), "pull request"))) table := utils.NewTablePrinter(cmd.OutOrStdout()) for _, pr := range prs { From bb0fe46db6df745d32e64bb7ca337044ab3748a7 Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Thu, 20 Feb 2020 01:54:51 +0500 Subject: [PATCH 05/13] total match count in title --- api/queries_issue.go | 11 +++++++---- api/queries_pr.go | 37 +++++++++++++++++++++--------------- command/issue.go | 27 +++++--------------------- command/pr.go | 27 +++++--------------------- test/fixtures/issueList.json | 1 + test/fixtures/prList.json | 1 + utils/utils.go | 37 ++++++++++++++++++++++++++++++++++++ 7 files changed, 78 insertions(+), 63 deletions(-) diff --git a/api/queries_issue.go b/api/queries_issue.go index 934467e05..ec125129a 100644 --- a/api/queries_issue.go +++ b/api/queries_issue.go @@ -171,7 +171,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string) return &payload, nil } -func IssueList(client *Client, repo ghrepo.Interface, state string, labels []string, assigneeString string, limit int) ([]Issue, error) { +func IssueList(client *Client, repo ghrepo.Interface, state string, labels []string, assigneeString string, limit int) (*IssuesAndTotalCount, error) { var states []string switch state { case "open", "": @@ -189,7 +189,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str repository(owner: $owner, name: $repo) { hasIssuesEnabled issues(first: $limit, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) { - nodes { + totalCount + nodes { ...issue } } @@ -213,7 +214,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str var resp struct { Repository struct { Issues struct { - Nodes []Issue + Nodes []Issue + TotalCount int } HasIssuesEnabled bool } @@ -228,7 +230,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo)) } - return resp.Repository.Issues.Nodes, nil + res := IssuesAndTotalCount{Issues: resp.Repository.Issues.Nodes, TotalCount: resp.Repository.Issues.TotalCount} + return &res, nil } func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, error) { diff --git a/api/queries_pr.go b/api/queries_pr.go index 734245acb..3b057d5e7 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -432,7 +432,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter return &result.CreatePullRequest.PullRequest, nil } -func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]PullRequest, error) { +func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) { type prBlock struct { Edges []struct { Node PullRequest @@ -441,6 +441,8 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([] HasNextPage bool EndCursor string } + TotalCount int + IssueCount int } type response struct { Repository struct { @@ -483,23 +485,25 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([] first: $limit, after: $endCursor, orderBy: {field: CREATED_AT, direction: DESC} - ) { - edges { - node { - ...pr - } - } - pageInfo { - hasNextPage - endCursor - } - } - } + ) { + totalCount + edges { + node { + ...pr + } + } + pageInfo { + hasNextPage + endCursor + } + } + } }` prs := []PullRequest{} pageLimit := min(limit, 100) variables := map[string]interface{}{} + res := PullRequestAndTotalCount{} // If assignee was specified, use the `search` API rather than // `Repository.pullRequests`, but this mode doesn't support multiple labels @@ -511,6 +515,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([] $endCursor: String, ) { search(query: $q, type: ISSUE, first: $limit, after: $endCursor) { + issueCount edges { node { ...pr @@ -564,8 +569,10 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([] return nil, err } prData := data.Repository.PullRequests + res.TotalCount = prData.TotalCount if _, ok := variables["q"]; ok { prData = data.Search + res.TotalCount = prData.IssueCount } for _, edge := range prData.Edges { @@ -583,8 +590,8 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([] done: break } - - return prs, nil + res.PullRequests = prs + return &res, nil } func min(a, b int) int { diff --git a/command/issue.go b/command/issue.go index a80a0de45..3eea9252f 100644 --- a/command/issue.go +++ b/command/issue.go @@ -16,7 +16,6 @@ import ( "github.com/cli/cli/pkg/githubtemplate" "github.com/cli/cli/utils" "github.com/spf13/cobra" - "github.com/spf13/pflag" ) func init() { @@ -108,31 +107,15 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - issues, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit) + issuesData, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit) if err != nil { return err } + issues := issuesData.Issues + issueCount := issuesData.TotalCount - title := func (msg string) string { - return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo)) - } - - if len(issues) == 0 { - colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open issues" message is actually an issue - msg := "There are no open issues" - - userSetFlags := false - cmd.Flags().Visit(func(f *pflag.Flag) { - userSetFlags = true - }) - if userSetFlags { - msg = "No issues match your search" - } - fmt.Fprintf(colorErr, title(msg)) - return nil - } - - fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(issues), "issue"))) + title := utils.GetTitle(cmd, "issue", limit, issueCount, baseRepo) + fmt.Fprintf(colorableErr(cmd), title) out := cmd.OutOrStdout() table := utils.NewTablePrinter(out) diff --git a/command/pr.go b/command/pr.go index 8e51bb2a0..91d7309da 100644 --- a/command/pr.go +++ b/command/pr.go @@ -13,7 +13,6 @@ import ( "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/utils" "github.com/spf13/cobra" - "github.com/spf13/pflag" ) func init() { @@ -135,7 +134,6 @@ func prList(cmd *cobra.Command, args []string) error { return err } - limit, err := cmd.Flags().GetInt("limit") if err != nil { return err @@ -186,32 +184,17 @@ func prList(cmd *cobra.Command, args []string) error { params["assignee"] = assignee } - prs, err := api.PullRequestList(apiClient, params, limit) + prsData, err := api.PullRequestList(apiClient, params, limit) if err != nil { return err } - title := func (msg string) string { - return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo)) - } + prs := prsData.PullRequests + prCount := prsData.TotalCount - if len(prs) == 0 { - colorErr := colorableErr(cmd) // Send to stderr because otherwise when piping this command it would seem like the "no open prs" message is acually a pr - msg := "There are no open pull requests" + title := utils.GetTitle(cmd, "pull request", limit, prCount, baseRepo) + fmt.Fprintf(colorableErr(cmd), title) - userSetFlags := false - cmd.Flags().Visit(func(f *pflag.Flag) { - userSetFlags = true - }) - if userSetFlags { - msg = "No pull requests match your search" - } - fmt.Fprintf(colorErr, title(msg)) - return nil - } - - fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(prs), "pull request"))) - table := utils.NewTablePrinter(cmd.OutOrStdout()) for _, pr := range prs { prNum := strconv.Itoa(pr.Number) diff --git a/test/fixtures/issueList.json b/test/fixtures/issueList.json index 40537f12d..4b19f3930 100644 --- a/test/fixtures/issueList.json +++ b/test/fixtures/issueList.json @@ -3,6 +3,7 @@ "repository": { "hasIssuesEnabled": true, "issues": { + "totalCount": 3, "nodes": [ { "number": 1, diff --git a/test/fixtures/prList.json b/test/fixtures/prList.json index 2808a5a8e..abd0e4ee9 100644 --- a/test/fixtures/prList.json +++ b/test/fixtures/prList.json @@ -2,6 +2,7 @@ "data": { "repository": { "pullRequests": { + "totalCount": 3, "edges": [ { "node": { diff --git a/utils/utils.go b/utils/utils.go index fbd7e84de..f9a18d20c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,7 +5,10 @@ import ( "fmt" "time" + "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/browser" + "github.com/spf13/cobra" + "github.com/spf13/pflag" md "github.com/vilmibm/go-termd" ) @@ -77,3 +80,37 @@ func FuzzyAgo(ago time.Duration) string { return fmtDuration(int(ago.Hours()/24/365), "year") } + +func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo *ghrepo.Interface) string { + userSetFlagCounter := 0 + limitSet := false + + cmd.Flags().Visit(func(f *pflag.Flag) { + userSetFlagCounter += 1 + if f.Name == "limit" { + limitSet = true + } + }) + + title := "\n%s in %s\n\n" + if matchCount == 0 { + msg := fmt.Sprintf("There are no open %ss", cmdType) + + if userSetFlagCounter > 0 { + msg = fmt.Sprintf("No %ss match your search", cmdType) + } + return fmt.Sprintf(title, msg, ghrepo.FullName(*baseRepo)) + } + + if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) { + title = "\n%s match your search in %s\n\n" + } + + out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(*baseRepo)) + + if limit < matchCount { + out = out + fmt.Sprintln(Gray(fmt.Sprintf("Showing %d/%d results\n", limit, matchCount))) + } + + return out +} From 7589da4c2bf7653ec2cdc8460e02b684e213741c Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Thu, 20 Feb 2020 02:03:13 +0500 Subject: [PATCH 06/13] removed extra import --- utils/utils.go | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 1313a7ab7..db95ab3d0 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -10,7 +10,6 @@ import ( "github.com/cli/cli/pkg/browser" "github.com/spf13/cobra" "github.com/spf13/pflag" - md "github.com/vilmibm/go-termd" ) // OpenInBrowser opens the url in a web browser based on OS and $BROWSER environment variable From 3c687ed11113bf5508579f1b4d21c7663c7edb3a Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Thu, 27 Feb 2020 00:57:42 +0500 Subject: [PATCH 07/13] moved getTitle to common; changed title string --- command/common.go | 43 +++++++++++++++++++++++++++++++++++++++++++ command/issue.go | 4 ++-- command/issue_test.go | 2 +- command/pr.go | 4 ++-- command/pr_test.go | 2 +- utils/utils.go | 37 ------------------------------------- 6 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 command/common.go diff --git a/command/common.go b/command/common.go new file mode 100644 index 000000000..a0cb37822 --- /dev/null +++ b/command/common.go @@ -0,0 +1,43 @@ +package command + +import ( + "fmt" + + "github.com/cli/cli/internal/ghrepo" + "github.com/cli/cli/utils" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +func getTitle(cmd *cobra.Command, cmdType string, matchCount int, totalMatchCount int, baseRepo ghrepo.Interface) string { + userSetFlagCounter := 0 + limitSet := false + + cmd.Flags().Visit(func(f *pflag.Flag) { + userSetFlagCounter += 1 + if f.Name == "limit" { + limitSet = true + } + }) + + title := "\n%s in %s\n\n" + if totalMatchCount == 0 { + msg := fmt.Sprintf("There are no open %ss", cmdType) + + if userSetFlagCounter > 0 { + msg = fmt.Sprintf("No %ss match your search", cmdType) + } + return fmt.Sprintf(title, msg, ghrepo.FullName(baseRepo)) + } + + title = "\nShowing %d of %s in %s" + if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) { + title += " that match your search\n\n" + } else { + title += "\n\n" + } + + out := fmt.Sprintf(title, matchCount, utils.Pluralize(totalMatchCount, cmdType), ghrepo.FullName(baseRepo)) + + return out +} diff --git a/command/issue.go b/command/issue.go index 6fc07e0fd..a67756273 100644 --- a/command/issue.go +++ b/command/issue.go @@ -113,9 +113,9 @@ func issueList(cmd *cobra.Command, args []string) error { return err } issues := issuesData.Issues - issueCount := issuesData.TotalCount + totalIssueCount := issuesData.TotalCount - title := utils.GetTitle(cmd, "issue", limit, issueCount, baseRepo) + title := getTitle(cmd, "issue", len(issues), totalIssueCount, baseRepo) fmt.Fprintf(colorableErr(cmd), title) out := cmd.OutOrStdout() diff --git a/command/issue_test.go b/command/issue_test.go index 71641221e..daac4867d 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -111,7 +111,7 @@ func TestIssueList(t *testing.T) { } eq(t, output.Stderr(), ` -3 issues in OWNER/REPO +Showing 3 of 3 issues in OWNER/REPO `) diff --git a/command/pr.go b/command/pr.go index 4747b2038..7c595e8b9 100644 --- a/command/pr.go +++ b/command/pr.go @@ -191,9 +191,9 @@ func prList(cmd *cobra.Command, args []string) error { } prs := prsData.PullRequests - prCount := prsData.TotalCount + totalPrCount := prsData.TotalCount - title := utils.GetTitle(cmd, "pull request", limit, prCount, baseRepo) + title := getTitle(cmd, "pull request", len(prs), totalPrCount, baseRepo) fmt.Fprintf(colorableErr(cmd), title) // Send to stderr because otherwise when piping this command it would seem like the "no open prs" message is actually a pr table := utils.NewTablePrinter(cmd.OutOrStdout()) diff --git a/command/pr_test.go b/command/pr_test.go index 26691e577..5305c2ab1 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -172,7 +172,7 @@ func TestPRList(t *testing.T) { } eq(t, output.Stderr(), ` -3 pull requests in OWNER/REPO +Showing 3 of 3 pull requests in OWNER/REPO `) eq(t, output.String(), `32 New feature feature diff --git a/utils/utils.go b/utils/utils.go index 1d23d7d89..a3af92fcb 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,10 +6,7 @@ import ( "time" "github.com/charmbracelet/glamour" - "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/browser" - "github.com/spf13/cobra" - "github.com/spf13/pflag" ) // OpenInBrowser opens the url in a web browser based on OS and $BROWSER environment variable @@ -62,37 +59,3 @@ func FuzzyAgo(ago time.Duration) string { return fmtDuration(int(ago.Hours()/24/365), "year") } - -func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo ghrepo.Interface) string { - userSetFlagCounter := 0 - limitSet := false - - cmd.Flags().Visit(func(f *pflag.Flag) { - userSetFlagCounter += 1 - if f.Name == "limit" { - limitSet = true - } - }) - - title := "\n%s in %s\n\n" - if matchCount == 0 { - msg := fmt.Sprintf("There are no open %ss", cmdType) - - if userSetFlagCounter > 0 { - msg = fmt.Sprintf("No %ss match your search", cmdType) - } - return fmt.Sprintf(title, msg, ghrepo.FullName(baseRepo)) - } - - if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) { - title = "\n%s match your search in %s\n\n" - } - - out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(baseRepo)) - - if limit < matchCount { - out = out + fmt.Sprintln(Gray(fmt.Sprintf("Showing %d/%d results\n", limit, matchCount))) - } - - return out -} From 0c9909c2307e266987574f28a7b2da4986a96bfa Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Thu, 27 Feb 2020 01:44:35 +0500 Subject: [PATCH 08/13] pluralize match if total == 1 --- command/common.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/command/common.go b/command/common.go index a0cb37822..299ed030d 100644 --- a/command/common.go +++ b/command/common.go @@ -32,7 +32,11 @@ func getTitle(cmd *cobra.Command, cmdType string, matchCount int, totalMatchCoun title = "\nShowing %d of %s in %s" if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) { - title += " that match your search\n\n" + matchStr := "match" + if totalMatchCount == 1 { + matchStr = "matches" + } + title += fmt.Sprintf(" that %s your search\n\n", matchStr) } else { title += "\n\n" } From 77a6caa84ddedd2d466bcf2731dc903b8b7f6559 Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Wed, 4 Mar 2020 01:47:30 +0500 Subject: [PATCH 09/13] test for all possible titles --- command/common.go | 4 +- command/issue_test.go | 143 ++++++++++++++++++++++++++++++++++++++++++ command/pr_test.go | 141 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 286 insertions(+), 2 deletions(-) diff --git a/command/common.go b/command/common.go index 299ed030d..21726fc93 100644 --- a/command/common.go +++ b/command/common.go @@ -20,8 +20,8 @@ func getTitle(cmd *cobra.Command, cmdType string, matchCount int, totalMatchCoun } }) - title := "\n%s in %s\n\n" if totalMatchCount == 0 { + title := "\n%s in %s\n\n" msg := fmt.Sprintf("There are no open %ss", cmdType) if userSetFlagCounter > 0 { @@ -30,7 +30,7 @@ func getTitle(cmd *cobra.Command, cmdType string, matchCount int, totalMatchCoun return fmt.Sprintf(title, msg, ghrepo.FullName(baseRepo)) } - title = "\nShowing %d of %s in %s" + title := "\nShowing %d of %s in %s" if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) { matchStr := "match" if totalMatchCount == 1 { diff --git a/command/issue_test.go b/command/issue_test.go index f803572e3..ab1b43c29 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -129,6 +129,149 @@ Showing 3 of 3 issues in OWNER/REPO } } +func TestIssueList_limit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/issueList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(issueListCmd, "issue list -L 10") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 3 of 3 issues in OWNER/REPO + +`) +} + +func TestIssueList_smallLimit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/issueList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(issueListCmd, "issue list -L 2") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 2 of 3 issues in OWNER/REPO + +`) +} + +func TestIssueList_multipleFilterWithLimit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/issueList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(issueListCmd, "issue list -s open -l web -L 2") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 2 of 3 issues in OWNER/REPO that match your search + +`) +} + +func TestIssueList_multipleFilterWithoutLimit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/issueList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(issueListCmd, "issue list -s open -l web") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 3 of 3 issues in OWNER/REPO that match your search + +`) +} + +func TestIssueList_singleFilter(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/issueList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(issueListCmd, "issue list -s open") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 3 of 3 issues in OWNER/REPO that match your search + +`) +} + +func TestIssueList_singleResultWithFilter(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + respBody := bytes.NewBufferString(`{ + "data": { + "repository": { + "hasIssuesEnabled": true, + "issues": { + "totalCount": 1, + "nodes": [ + { + "number": 1, + "title": "number won", + "url": "https://wow.com", + "labels": { + "nodes": [ + { + "name": "label" + } + ], + "totalCount": 1 + } + } + ] + } + } + } + }`) + http.StubResponse(200, respBody) + + output, err := RunCommand(issueListCmd, "issue list -s open") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 1 of 1 issue in OWNER/REPO that matches your search + +`) +} + func TestIssueList_withFlags(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() diff --git a/command/pr_test.go b/command/pr_test.go index 86be2d564..09bb9c852 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -181,6 +181,147 @@ Showing 3 of 3 pull requests in OWNER/REPO `) } +func TestPRList_limit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prListCmd, "pr list -L 10") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 3 of 3 pull requests in OWNER/REPO + +`) +} + +func TestPRList_smallLimit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prListCmd, "pr list -L 2") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 2 of 3 pull requests in OWNER/REPO + +`) +} + +func TestPRList_multipleFilterWithLimit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prListCmd, "pr list -s open -l one -L 2") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 2 of 3 pull requests in OWNER/REPO that match your search + +`) +} + +func TestPRList_multipleFilterWithoutLimit(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prListCmd, "pr list -s open -l one") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 3 of 3 pull requests in OWNER/REPO that match your search + +`) +} + +func TestPRList_singleFilter(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + jsonFile, _ := os.Open("../test/fixtures/prList.json") + defer jsonFile.Close() + http.StubResponse(200, jsonFile) + + output, err := RunCommand(prListCmd, "pr list -s open") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 3 of 3 pull requests in OWNER/REPO that match your search + +`) +} + +func TestPRList_singleResultWithFilter(t *testing.T) { + initBlankContext("OWNER/REPO", "master") + http := initFakeHTTP() + http.StubRepoResponse("OWNER", "REPO") + + respBody := bytes.NewBufferString(`{ + "data": { + "repository": { + "pullRequests": { + "totalCount": 1, + "edges": [ + { + "node": { + "number": 32, + "title": "New feature", + "url": "https://github.com/monalisa/hello/pull/32", + "headRefName": "feature" + } + } + ], + "pageInfo": { + "hasNextPage": false, + "endCursor": "" + } + } + } + } + }`) + http.StubResponse(200, respBody) + + output, err := RunCommand(prListCmd, "pr list -s open -B develop") + if err != nil { + t.Fatal(err) + } + + eq(t, output.Stderr(), ` +Showing 1 of 1 pull request in OWNER/REPO that matches your search + +`) +} + func TestPRList_filtering(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() From debedf98cf28c9963c82aa1c947dc974c96c111a Mon Sep 17 00:00:00 2001 From: UmairShahzad <18100099@lums.edu.pk> Date: Wed, 4 Mar 2020 02:36:38 +0500 Subject: [PATCH 10/13] include int in command reset to reset 'Limit' --- command/pr_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/pr_test.go b/command/pr_test.go index 09bb9c852..ad092a267 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -57,7 +57,7 @@ func RunCommand(cmd *cobra.Command, args string) (*cmdOut, error) { v.Replace([]string{}) default: switch v.Type() { - case "bool", "string": + case "bool", "string", "int": v.Set(f.DefValue) } } From 5518c013f3776be4d2b8f2b5aaf986a1ef83bd16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 5 Mar 2020 13:07:10 +0100 Subject: [PATCH 11/13] Indicate that the default `issue list` state is "open" --- command/issue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/issue.go b/command/issue.go index d576c98d0..8427ee9e7 100644 --- a/command/issue.go +++ b/command/issue.go @@ -34,7 +34,7 @@ func init() { issueCmd.AddCommand(issueListCmd) issueListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") issueListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label") - issueListCmd.Flags().StringP("state", "s", "", "Filter by state: {open|closed|all}") + issueListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|all}") issueListCmd.Flags().IntP("limit", "L", 30, "Maximum number of issues to fetch") issueViewCmd.Flags().BoolP("preview", "p", false, "Display preview of issue content") From 3d48d40b69b28a7f5c5887b8cc3427984062c76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 5 Mar 2020 13:08:21 +0100 Subject: [PATCH 12/13] Simplify `getTitle` and rename it to `listHeader` --- command/common.go | 47 ----------------------------------------------- command/issue.go | 39 +++++++++++++++++++++++++++++++++------ command/pr.go | 19 +++++++++++++------ 3 files changed, 46 insertions(+), 59 deletions(-) delete mode 100644 command/common.go diff --git a/command/common.go b/command/common.go deleted file mode 100644 index 21726fc93..000000000 --- a/command/common.go +++ /dev/null @@ -1,47 +0,0 @@ -package command - -import ( - "fmt" - - "github.com/cli/cli/internal/ghrepo" - "github.com/cli/cli/utils" - "github.com/spf13/cobra" - "github.com/spf13/pflag" -) - -func getTitle(cmd *cobra.Command, cmdType string, matchCount int, totalMatchCount int, baseRepo ghrepo.Interface) string { - userSetFlagCounter := 0 - limitSet := false - - cmd.Flags().Visit(func(f *pflag.Flag) { - userSetFlagCounter += 1 - if f.Name == "limit" { - limitSet = true - } - }) - - if totalMatchCount == 0 { - title := "\n%s in %s\n\n" - msg := fmt.Sprintf("There are no open %ss", cmdType) - - if userSetFlagCounter > 0 { - msg = fmt.Sprintf("No %ss match your search", cmdType) - } - return fmt.Sprintf(title, msg, ghrepo.FullName(baseRepo)) - } - - title := "\nShowing %d of %s in %s" - if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) { - matchStr := "match" - if totalMatchCount == 1 { - matchStr = "matches" - } - title += fmt.Sprintf(" that %s your search\n\n", matchStr) - } else { - title += "\n\n" - } - - out := fmt.Sprintf(title, matchCount, utils.Pluralize(totalMatchCount, cmdType), ghrepo.FullName(baseRepo)) - - return out -} diff --git a/command/issue.go b/command/issue.go index 8427ee9e7..84b66964f 100644 --- a/command/issue.go +++ b/command/issue.go @@ -17,6 +17,7 @@ import ( "github.com/cli/cli/pkg/text" "github.com/cli/cli/utils" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func init() { @@ -108,19 +109,26 @@ func issueList(cmd *cobra.Command, args []string) error { return err } - issuesData, err := api.IssueList(apiClient, baseRepo, state, labels, assignee, limit) + listResult, err := api.IssueList(apiClient, baseRepo, state, labels, assignee, limit) if err != nil { return err } - issues := issuesData.Issues - totalIssueCount := issuesData.TotalCount - title := getTitle(cmd, "issue", len(issues), totalIssueCount, baseRepo) - fmt.Fprintf(colorableErr(cmd), title) + hasFilters := false + cmd.Flags().Visit(func(f *pflag.Flag) { + switch f.Name { + case "state", "label", "assignee": + hasFilters = true + } + }) + + title := listHeader(ghrepo.FullName(baseRepo), "issue", len(listResult.Issues), listResult.TotalCount, hasFilters) + // TODO: avoid printing header if piped to a script + fmt.Fprintf(colorableErr(cmd), "\n%s\n\n", title) out := cmd.OutOrStdout() table := utils.NewTablePrinter(out) - for _, issue := range issues { + for _, issue := range listResult.Issues { issueNum := strconv.Itoa(issue.Number) if table.IsTTY() { issueNum = "#" + issueNum @@ -229,6 +237,25 @@ func issueView(cmd *cobra.Command, args []string) error { } +func listHeader(repoName string, itemName string, matchCount int, totalMatchCount int, hasFilters bool) string { + if totalMatchCount == 0 { + if hasFilters { + return fmt.Sprintf("No %ss match your search in %s", itemName, repoName) + } + return fmt.Sprintf("There are no open %ss in %s", itemName, repoName) + } + + if hasFilters { + matchVerb := "match" + if totalMatchCount == 1 { + matchVerb = "matches" + } + return fmt.Sprintf("Showing %d of %s in %s that %s your search", matchCount, utils.Pluralize(totalMatchCount, itemName), repoName, matchVerb) + } + + return fmt.Sprintf("Showing %d of %s in %s", matchCount, utils.Pluralize(totalMatchCount, itemName), repoName) +} + func printIssuePreview(out io.Writer, issue *api.Issue) error { coloredLabels := labelList(*issue) if coloredLabels != "" { diff --git a/command/pr.go b/command/pr.go index 665da0b75..781b1d914 100644 --- a/command/pr.go +++ b/command/pr.go @@ -14,6 +14,7 @@ import ( "github.com/cli/cli/pkg/text" "github.com/cli/cli/utils" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func init() { @@ -186,19 +187,25 @@ func prList(cmd *cobra.Command, args []string) error { params["assignee"] = assignee } - prsData, err := api.PullRequestList(apiClient, params, limit) + listResult, err := api.PullRequestList(apiClient, params, limit) if err != nil { return err } - prs := prsData.PullRequests - totalPrCount := prsData.TotalCount + hasFilters := false + cmd.Flags().Visit(func(f *pflag.Flag) { + switch f.Name { + case "state", "label", "base", "assignee": + hasFilters = true + } + }) - title := getTitle(cmd, "pull request", len(prs), totalPrCount, baseRepo) - fmt.Fprintf(colorableErr(cmd), title) // Send to stderr because otherwise when piping this command it would seem like the "no open prs" message is actually a pr + title := listHeader(ghrepo.FullName(baseRepo), "pull request", len(listResult.PullRequests), listResult.TotalCount, hasFilters) + // TODO: avoid printing header if piped to a script + fmt.Fprintf(colorableErr(cmd), "\n%s\n\n", title) table := utils.NewTablePrinter(cmd.OutOrStdout()) - for _, pr := range prs { + for _, pr := range listResult.PullRequests { prNum := strconv.Itoa(pr.Number) if table.IsTTY() { prNum = "#" + prNum From 9cd53b8f190af984a91b6343c6d090bae92570de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 5 Mar 2020 13:16:53 +0100 Subject: [PATCH 13/13] Simplify `listHeader` tests --- command/issue_test.go | 254 ++++++++++++++++++------------------------ command/pr_test.go | 141 ----------------------- 2 files changed, 111 insertions(+), 284 deletions(-) diff --git a/command/issue_test.go b/command/issue_test.go index ab1b43c29..dd9581984 100644 --- a/command/issue_test.go +++ b/command/issue_test.go @@ -129,149 +129,6 @@ Showing 3 of 3 issues in OWNER/REPO } } -func TestIssueList_limit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/issueList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(issueListCmd, "issue list -L 10") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 3 of 3 issues in OWNER/REPO - -`) -} - -func TestIssueList_smallLimit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/issueList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(issueListCmd, "issue list -L 2") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 2 of 3 issues in OWNER/REPO - -`) -} - -func TestIssueList_multipleFilterWithLimit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/issueList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(issueListCmd, "issue list -s open -l web -L 2") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 2 of 3 issues in OWNER/REPO that match your search - -`) -} - -func TestIssueList_multipleFilterWithoutLimit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/issueList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(issueListCmd, "issue list -s open -l web") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 3 of 3 issues in OWNER/REPO that match your search - -`) -} - -func TestIssueList_singleFilter(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/issueList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(issueListCmd, "issue list -s open") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 3 of 3 issues in OWNER/REPO that match your search - -`) -} - -func TestIssueList_singleResultWithFilter(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - respBody := bytes.NewBufferString(`{ - "data": { - "repository": { - "hasIssuesEnabled": true, - "issues": { - "totalCount": 1, - "nodes": [ - { - "number": 1, - "title": "number won", - "url": "https://wow.com", - "labels": { - "nodes": [ - { - "name": "label" - } - ], - "totalCount": 1 - } - } - ] - } - } - } - }`) - http.StubResponse(200, respBody) - - output, err := RunCommand(issueListCmd, "issue list -s open") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 1 of 1 issue in OWNER/REPO that matches your search - -`) -} - func TestIssueList_withFlags(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP() @@ -702,3 +559,114 @@ func TestIssueCreate_webTitleBody(t *testing.T) { eq(t, url, "https://github.com/OWNER/REPO/issues/new?title=mytitle&body=mybody") eq(t, output.String(), "Opening github.com/OWNER/REPO/issues/new in your browser.\n") } + +func Test_listHeader(t *testing.T) { + type args struct { + repoName string + itemName string + matchCount int + totalMatchCount int + hasFilters bool + } + tests := []struct { + name string + args args + want string + }{ + { + name: "no results", + args: args{ + repoName: "REPO", + itemName: "table", + matchCount: 0, + totalMatchCount: 0, + hasFilters: false, + }, + want: "There are no open tables in REPO", + }, + { + name: "no matches after filters", + args: args{ + repoName: "REPO", + itemName: "Luftballon", + matchCount: 0, + totalMatchCount: 0, + hasFilters: true, + }, + want: "No Luftballons match your search in REPO", + }, + { + name: "one result", + args: args{ + repoName: "REPO", + itemName: "genie", + matchCount: 1, + totalMatchCount: 23, + hasFilters: false, + }, + want: "Showing 1 of 23 genies in REPO", + }, + { + name: "one result after filters", + args: args{ + repoName: "REPO", + itemName: "tiny cup", + matchCount: 1, + totalMatchCount: 23, + hasFilters: true, + }, + want: "Showing 1 of 23 tiny cups in REPO that match your search", + }, + { + name: "one result in total", + args: args{ + repoName: "REPO", + itemName: "chip", + matchCount: 1, + totalMatchCount: 1, + hasFilters: false, + }, + want: "Showing 1 of 1 chip in REPO", + }, + { + name: "one result in total after filters", + args: args{ + repoName: "REPO", + itemName: "spicy noodle", + matchCount: 1, + totalMatchCount: 1, + hasFilters: true, + }, + want: "Showing 1 of 1 spicy noodle in REPO that matches your search", + }, + { + name: "multiple results", + args: args{ + repoName: "REPO", + itemName: "plant", + matchCount: 4, + totalMatchCount: 23, + hasFilters: false, + }, + want: "Showing 4 of 23 plants in REPO", + }, + { + name: "multiple results after filters", + args: args{ + repoName: "REPO", + itemName: "boomerang", + matchCount: 4, + totalMatchCount: 23, + hasFilters: true, + }, + want: "Showing 4 of 23 boomerangs in REPO that match your search", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := listHeader(tt.args.repoName, tt.args.itemName, tt.args.matchCount, tt.args.totalMatchCount, tt.args.hasFilters); got != tt.want { + t.Errorf("listHeader() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/command/pr_test.go b/command/pr_test.go index 5fccd7583..6ed9106ea 100644 --- a/command/pr_test.go +++ b/command/pr_test.go @@ -211,147 +211,6 @@ Showing 3 of 3 pull requests in OWNER/REPO `) } -func TestPRList_limit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/prList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(prListCmd, "pr list -L 10") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 3 of 3 pull requests in OWNER/REPO - -`) -} - -func TestPRList_smallLimit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/prList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(prListCmd, "pr list -L 2") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 2 of 3 pull requests in OWNER/REPO - -`) -} - -func TestPRList_multipleFilterWithLimit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/prList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(prListCmd, "pr list -s open -l one -L 2") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 2 of 3 pull requests in OWNER/REPO that match your search - -`) -} - -func TestPRList_multipleFilterWithoutLimit(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/prList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(prListCmd, "pr list -s open -l one") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 3 of 3 pull requests in OWNER/REPO that match your search - -`) -} - -func TestPRList_singleFilter(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - jsonFile, _ := os.Open("../test/fixtures/prList.json") - defer jsonFile.Close() - http.StubResponse(200, jsonFile) - - output, err := RunCommand(prListCmd, "pr list -s open") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 3 of 3 pull requests in OWNER/REPO that match your search - -`) -} - -func TestPRList_singleResultWithFilter(t *testing.T) { - initBlankContext("OWNER/REPO", "master") - http := initFakeHTTP() - http.StubRepoResponse("OWNER", "REPO") - - respBody := bytes.NewBufferString(`{ - "data": { - "repository": { - "pullRequests": { - "totalCount": 1, - "edges": [ - { - "node": { - "number": 32, - "title": "New feature", - "url": "https://github.com/monalisa/hello/pull/32", - "headRefName": "feature" - } - } - ], - "pageInfo": { - "hasNextPage": false, - "endCursor": "" - } - } - } - } - }`) - http.StubResponse(200, respBody) - - output, err := RunCommand(prListCmd, "pr list -s open -B develop") - if err != nil { - t.Fatal(err) - } - - eq(t, output.Stderr(), ` -Showing 1 of 1 pull request in OWNER/REPO that matches your search - -`) -} - func TestPRList_filtering(t *testing.T) { initBlankContext("OWNER/REPO", "master") http := initFakeHTTP()