Skip to content

Commit bb0fe46

Browse files
committed
total match count in title
1 parent 94ef4bb commit bb0fe46

File tree

7 files changed

+78
-63
lines changed

7 files changed

+78
-63
lines changed

api/queries_issue.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
171171
return &payload, nil
172172
}
173173

174-
func IssueList(client *Client, repo ghrepo.Interface, state string, labels []string, assigneeString string, limit int) ([]Issue, error) {
174+
func IssueList(client *Client, repo ghrepo.Interface, state string, labels []string, assigneeString string, limit int) (*IssuesAndTotalCount, error) {
175175
var states []string
176176
switch state {
177177
case "open", "":
@@ -189,7 +189,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
189189
repository(owner: $owner, name: $repo) {
190190
hasIssuesEnabled
191191
issues(first: $limit, orderBy: {field: CREATED_AT, direction: DESC}, states: $states, labels: $labels, filterBy: {assignee: $assignee}) {
192-
nodes {
192+
totalCount
193+
nodes {
193194
...issue
194195
}
195196
}
@@ -213,7 +214,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
213214
var resp struct {
214215
Repository struct {
215216
Issues struct {
216-
Nodes []Issue
217+
Nodes []Issue
218+
TotalCount int
217219
}
218220
HasIssuesEnabled bool
219221
}
@@ -228,7 +230,8 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
228230
return nil, fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(repo))
229231
}
230232

231-
return resp.Repository.Issues.Nodes, nil
233+
res := IssuesAndTotalCount{Issues: resp.Repository.Issues.Nodes, TotalCount: resp.Repository.Issues.TotalCount}
234+
return &res, nil
232235
}
233236

234237
func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, error) {

api/queries_pr.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
432432
return &result.CreatePullRequest.PullRequest, nil
433433
}
434434

435-
func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]PullRequest, error) {
435+
func PullRequestList(client *Client, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) {
436436
type prBlock struct {
437437
Edges []struct {
438438
Node PullRequest
@@ -441,6 +441,8 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
441441
HasNextPage bool
442442
EndCursor string
443443
}
444+
TotalCount int
445+
IssueCount int
444446
}
445447
type response struct {
446448
Repository struct {
@@ -483,23 +485,25 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
483485
first: $limit,
484486
after: $endCursor,
485487
orderBy: {field: CREATED_AT, direction: DESC}
486-
) {
487-
edges {
488-
node {
489-
...pr
490-
}
491-
}
492-
pageInfo {
493-
hasNextPage
494-
endCursor
495-
}
496-
}
497-
}
488+
) {
489+
totalCount
490+
edges {
491+
node {
492+
...pr
493+
}
494+
}
495+
pageInfo {
496+
hasNextPage
497+
endCursor
498+
}
499+
}
500+
}
498501
}`
499502

500503
prs := []PullRequest{}
501504
pageLimit := min(limit, 100)
502505
variables := map[string]interface{}{}
506+
res := PullRequestAndTotalCount{}
503507

504508
// If assignee was specified, use the `search` API rather than
505509
// `Repository.pullRequests`, but this mode doesn't support multiple labels
@@ -511,6 +515,7 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
511515
$endCursor: String,
512516
) {
513517
search(query: $q, type: ISSUE, first: $limit, after: $endCursor) {
518+
issueCount
514519
edges {
515520
node {
516521
...pr
@@ -564,8 +569,10 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
564569
return nil, err
565570
}
566571
prData := data.Repository.PullRequests
572+
res.TotalCount = prData.TotalCount
567573
if _, ok := variables["q"]; ok {
568574
prData = data.Search
575+
res.TotalCount = prData.IssueCount
569576
}
570577

571578
for _, edge := range prData.Edges {
@@ -583,8 +590,8 @@ func PullRequestList(client *Client, vars map[string]interface{}, limit int) ([]
583590
done:
584591
break
585592
}
586-
587-
return prs, nil
593+
res.PullRequests = prs
594+
return &res, nil
588595
}
589596

590597
func min(a, b int) int {

command/issue.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/cli/cli/pkg/githubtemplate"
1717
"github.com/cli/cli/utils"
1818
"github.com/spf13/cobra"
19-
"github.com/spf13/pflag"
2019
)
2120

2221
func init() {
@@ -108,31 +107,15 @@ func issueList(cmd *cobra.Command, args []string) error {
108107
return err
109108
}
110109

111-
issues, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit)
110+
issuesData, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit)
112111
if err != nil {
113112
return err
114113
}
114+
issues := issuesData.Issues
115+
issueCount := issuesData.TotalCount
115116

116-
title := func (msg string) string {
117-
return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo))
118-
}
119-
120-
if len(issues) == 0 {
121-
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
122-
msg := "There are no open issues"
123-
124-
userSetFlags := false
125-
cmd.Flags().Visit(func(f *pflag.Flag) {
126-
userSetFlags = true
127-
})
128-
if userSetFlags {
129-
msg = "No issues match your search"
130-
}
131-
fmt.Fprintf(colorErr, title(msg))
132-
return nil
133-
}
134-
135-
fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(issues), "issue")))
117+
title := utils.GetTitle(cmd, "issue", limit, issueCount, baseRepo)
118+
fmt.Fprintf(colorableErr(cmd), title)
136119

137120
out := cmd.OutOrStdout()
138121
table := utils.NewTablePrinter(out)

command/pr.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/cli/cli/internal/ghrepo"
1414
"github.com/cli/cli/utils"
1515
"github.com/spf13/cobra"
16-
"github.com/spf13/pflag"
1716
)
1817

1918
func init() {
@@ -135,7 +134,6 @@ func prList(cmd *cobra.Command, args []string) error {
135134
return err
136135
}
137136

138-
139137
limit, err := cmd.Flags().GetInt("limit")
140138
if err != nil {
141139
return err
@@ -186,32 +184,17 @@ func prList(cmd *cobra.Command, args []string) error {
186184
params["assignee"] = assignee
187185
}
188186

189-
prs, err := api.PullRequestList(apiClient, params, limit)
187+
prsData, err := api.PullRequestList(apiClient, params, limit)
190188
if err != nil {
191189
return err
192190
}
193191

194-
title := func (msg string) string {
195-
return fmt.Sprintf("\n%s in %s\n\n", msg, ghrepo.FullName(*baseRepo))
196-
}
197-
198-
if len(prs) == 0 {
199-
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
200-
msg := "There are no open pull requests"
192+
prs := prsData.PullRequests
193+
prCount := prsData.TotalCount
201194

202-
userSetFlags := false
203-
cmd.Flags().Visit(func(f *pflag.Flag) {
204-
userSetFlags = true
205-
})
206-
if userSetFlags {
207-
msg = "No pull requests match your search"
208-
}
209-
fmt.Fprintf(colorErr, title(msg))
210-
return nil
211-
}
195+
title := utils.GetTitle(cmd, "pull request", limit, prCount, baseRepo)
196+
fmt.Fprintf(colorableErr(cmd), title)
212197

213-
fmt.Fprintf(colorableErr(cmd), title(utils.Pluralize(len(prs), "pull request")))
214-
215198
table := utils.NewTablePrinter(cmd.OutOrStdout())
216199
for _, pr := range prs {
217200
prNum := strconv.Itoa(pr.Number)

test/fixtures/issueList.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"repository": {
44
"hasIssuesEnabled": true,
55
"issues": {
6+
"totalCount": 3,
67
"nodes": [
78
{
89
"number": 1,

test/fixtures/prList.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"data": {
33
"repository": {
44
"pullRequests": {
5+
"totalCount": 3,
56
"edges": [
67
{
78
"node": {

utils/utils.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/cli/cli/internal/ghrepo"
89
"github.com/cli/cli/pkg/browser"
10+
"github.com/spf13/cobra"
11+
"github.com/spf13/pflag"
912
md "github.com/vilmibm/go-termd"
1013
)
1114

@@ -77,3 +80,37 @@ func FuzzyAgo(ago time.Duration) string {
7780

7881
return fmtDuration(int(ago.Hours()/24/365), "year")
7982
}
83+
84+
func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo *ghrepo.Interface) string {
85+
userSetFlagCounter := 0
86+
limitSet := false
87+
88+
cmd.Flags().Visit(func(f *pflag.Flag) {
89+
userSetFlagCounter += 1
90+
if f.Name == "limit" {
91+
limitSet = true
92+
}
93+
})
94+
95+
title := "\n%s in %s\n\n"
96+
if matchCount == 0 {
97+
msg := fmt.Sprintf("There are no open %ss", cmdType)
98+
99+
if userSetFlagCounter > 0 {
100+
msg = fmt.Sprintf("No %ss match your search", cmdType)
101+
}
102+
return fmt.Sprintf(title, msg, ghrepo.FullName(*baseRepo))
103+
}
104+
105+
if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) {
106+
title = "\n%s match your search in %s\n\n"
107+
}
108+
109+
out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(*baseRepo))
110+
111+
if limit < matchCount {
112+
out = out + fmt.Sprintln(Gray(fmt.Sprintf("Showing %d/%d results\n", limit, matchCount)))
113+
}
114+
115+
return out
116+
}

0 commit comments

Comments
 (0)