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] 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 +}