Skip to content

Commit 619333a

Browse files
committed
Avoid using error values to pass information about the search cap
1 parent e8b015b commit 619333a

File tree

10 files changed

+27
-67
lines changed

10 files changed

+27
-67
lines changed

api/client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package api
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io"
98
"io/ioutil"
@@ -17,10 +16,6 @@ import (
1716
"github.com/shurcooL/graphql"
1817
)
1918

20-
// The Search API returns a max of 1000 results for each search.
21-
// (https://docs.github.com/en/rest/reference/search#about-the-search-api)
22-
var ErrSearchAPIMaxLimit = errors.New("limit was set to >1000 but Github search API returns max 1000 results")
23-
2419
// ClientOption represents an argument to NewClient
2520
type ClientOption = func(http.RoundTripper) http.RoundTripper
2621

api/queries_issue.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ type IssuesPayload struct {
1616
}
1717

1818
type IssuesAndTotalCount struct {
19-
Issues []Issue
20-
TotalCount int
19+
Issues []Issue
20+
TotalCount int
21+
SearchCapped bool
2122
}
2223

2324
type Issue struct {

api/queries_pr.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type PullRequestsPayload struct {
2626
type PullRequestAndTotalCount struct {
2727
TotalCount int
2828
PullRequests []PullRequest
29+
SearchCapped bool
2930
}
3031

3132
type PullRequest struct {

pkg/cmd/issue/list/http.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ loop:
126126
}
127127

128128
res := api.IssuesAndTotalCount{Issues: issues, TotalCount: totalCount}
129-
130-
if limit > 1000 {
131-
return &res, api.ErrSearchAPIMaxLimit
132-
}
133-
134129
return &res, nil
135130
}
136131

@@ -176,7 +171,7 @@ func searchIssues(client *api.Client, repo ghrepo.Interface, filters prShared.Fi
176171
"query": searchQuery,
177172
}
178173

179-
ic := api.IssuesAndTotalCount{}
174+
ic := api.IssuesAndTotalCount{SearchCapped: limit > 1000}
180175

181176
loop:
182177
for {

pkg/cmd/issue/list/list.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ func listRun(opts *ListOptions) error {
151151
filterOptions.Fields = opts.Exporter.Fields()
152152
}
153153

154-
listResult, listErr := issueList(httpClient, baseRepo, filterOptions, opts.LimitResults)
155-
if listErr != nil && listErr != api.ErrSearchAPIMaxLimit {
156-
return listErr
154+
listResult, err := issueList(httpClient, baseRepo, filterOptions, opts.LimitResults)
155+
if err != nil {
156+
return err
157157
}
158158

159159
err = opts.IO.StartPager()
@@ -166,16 +166,12 @@ func listRun(opts *ListOptions) error {
166166
return opts.Exporter.Write(opts.IO, listResult.Issues)
167167
}
168168

169+
if listResult.SearchCapped {
170+
fmt.Fprintln(opts.IO.ErrOut, "warning: this query uses the Search API which is capped at 1000 results maximum")
171+
}
169172
if isTerminal {
170173
title := prShared.ListHeader(ghrepo.FullName(baseRepo), "issue", len(listResult.Issues), listResult.TotalCount, !filterOptions.IsDefault())
171-
out := fmt.Sprintf("\n%s\n", title)
172-
173-
if listErr == api.ErrSearchAPIMaxLimit {
174-
icon := opts.IO.ColorScheme().WarningIcon()
175-
out = fmt.Sprintf("%s%s warning: %s\n", out, icon, listErr.Error())
176-
}
177-
178-
fmt.Fprintf(opts.IO.Out, "%s\n", out)
174+
fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title)
179175
}
180176

181177
issueShared.PrintIssues(opts.IO, "", len(listResult.Issues), listResult.Issues)

pkg/cmd/pr/list/http.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ func searchPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters
182182
"q": q.String(),
183183
}
184184

185-
res := api.PullRequestAndTotalCount{}
186-
185+
res := api.PullRequestAndTotalCount{SearchCapped: limit > 1000}
187186
var check = make(map[int]struct{})
188187
client := api.NewClientFromHTTP(httpClient)
189188

@@ -218,10 +217,6 @@ loop:
218217
}
219218
}
220219

221-
if limit > 1000 {
222-
return &res, api.ErrSearchAPIMaxLimit
223-
}
224-
225220
return &res, nil
226221
}
227222

pkg/cmd/pr/list/http_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,6 @@ func Test_listPullRequests(t *testing.T) {
144144
}))
145145
},
146146
},
147-
{
148-
name: "with labels and limit above 1000",
149-
args: args{
150-
repo: ghrepo.New("OWNER", "REPO"),
151-
limit: 2000,
152-
filters: prShared.FilterOptions{
153-
Labels: []string{"hello", "one world"},
154-
},
155-
},
156-
wantErr: true,
157-
},
158147
}
159148
for _, tt := range tests {
160149
t.Run(tt.name, func(t *testing.T) {

pkg/cmd/pr/list/list.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ func listRun(opts *ListOptions) error {
151151
return opts.Browser.Browse(openURL)
152152
}
153153

154-
listResult, listErr := listPullRequests(httpClient, baseRepo, filters, opts.LimitResults)
155-
if listErr != nil && listErr != api.ErrSearchAPIMaxLimit {
156-
return listErr
154+
listResult, err := listPullRequests(httpClient, baseRepo, filters, opts.LimitResults)
155+
if err != nil {
156+
return err
157157
}
158158

159159
err = opts.IO.StartPager()
@@ -166,16 +166,12 @@ func listRun(opts *ListOptions) error {
166166
return opts.Exporter.Write(opts.IO, listResult.PullRequests)
167167
}
168168

169+
if listResult.SearchCapped {
170+
fmt.Fprintln(opts.IO.ErrOut, "warning: this query uses the Search API which is capped at 1000 results maximum")
171+
}
169172
if opts.IO.IsStdoutTTY() {
170173
title := shared.ListHeader(ghrepo.FullName(baseRepo), "pull request", len(listResult.PullRequests), listResult.TotalCount, !filters.IsDefault())
171-
out := fmt.Sprintf("\n%s\n", title)
172-
173-
if listErr == api.ErrSearchAPIMaxLimit {
174-
icon := opts.IO.ColorScheme().WarningIcon()
175-
out = fmt.Sprintf("%s%s warning: %s\n", out, icon, listErr.Error())
176-
}
177-
178-
fmt.Fprintln(opts.IO.Out, out)
174+
fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title)
179175
}
180176

181177
cs := opts.IO.ColorScheme()

pkg/cmd/repo/list/http.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ pagination:
175175
variables["endCursor"] = githubv4.String(result.Search.PageInfo.EndCursor)
176176
}
177177

178-
if limit > 1000 {
179-
return &listResult, api.ErrSearchAPIMaxLimit
180-
}
181-
182178
return &listResult, nil
183179
}
184180

pkg/cmd/repo/list/list.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ func listRun(opts *ListOptions) error {
130130
return err
131131
}
132132

133-
listResult, listErr := listRepos(httpClient, host, opts.Limit, opts.Owner, filter)
134-
if listErr != nil {
135-
return listErr
133+
listResult, err := listRepos(httpClient, host, opts.Limit, opts.Owner, filter)
134+
if err != nil {
135+
return err
136136
}
137137

138138
if err := opts.IO.StartPager(); err != nil {
@@ -171,17 +171,13 @@ func listRun(opts *ListOptions) error {
171171
tp.EndRow()
172172
}
173173

174+
if listResult.FromSearch && opts.Limit > 1000 {
175+
fmt.Fprintln(opts.IO.ErrOut, "warning: this query uses the Search API which is capped at 1000 results maximum")
176+
}
174177
if opts.IO.IsStdoutTTY() {
175178
hasFilters := filter.Visibility != "" || filter.Fork || filter.Source || filter.Language != "" || filter.Topic != ""
176179
title := listHeader(listResult.Owner, len(listResult.Repositories), listResult.TotalCount, hasFilters)
177-
out := fmt.Sprintf("\n%s\n", title)
178-
179-
if listErr == api.ErrSearchAPIMaxLimit {
180-
icon := opts.IO.ColorScheme().WarningIcon()
181-
out = fmt.Sprintf("%s%s warning: %s\n", out, icon, listErr.Error())
182-
}
183-
184-
fmt.Fprintln(opts.IO.Out, out)
180+
fmt.Fprintf(opts.IO.Out, "\n%s\n\n", title)
185181
}
186182

187183
return tp.Render()

0 commit comments

Comments
 (0)