X Tutup
Skip to content

Commit d14715f

Browse files
committed
Convert bool to string early for pr list draft flag
1 parent 6a34f53 commit d14715f

File tree

6 files changed

+38
-45
lines changed

6 files changed

+38
-45
lines changed

pkg/cmd/pr/list/http.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func shouldUseSearch(filters prShared.FilterOptions) bool {
14-
return filters.Draft != nil || filters.Author != "" || filters.Assignee != "" || filters.Search != "" || len(filters.Labels) > 0
14+
return filters.Draft != "" || filters.Author != "" || filters.Assignee != "" || filters.Search != "" || len(filters.Labels) > 0
1515
}
1616

1717
func listPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters prShared.FilterOptions, limit int) (*api.PullRequestAndTotalCount, error) {
@@ -181,8 +181,8 @@ func searchPullRequests(httpClient *http.Client, repo ghrepo.Interface, filters
181181
q.SetBaseBranch(filters.BaseBranch)
182182
}
183183

184-
if filters.Draft != nil {
185-
q.SetDraft(*filters.Draft)
184+
if filters.Draft != "" {
185+
q.SetDraft(filters.Draft)
186186
}
187187

188188
pageLimit := min(limit, 100)

pkg/cmd/pr/list/list.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ type ListOptions struct {
3737
Author string
3838
Assignee string
3939
Search string
40-
Draft *bool
40+
Draft string
4141
}
4242

4343
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
44-
draft := false
4544
opts := &ListOptions{
4645
IO: f.IOStreams,
4746
HttpClient: f.HttpClient,
4847
Browser: f.Browser,
49-
Draft: &draft,
5048
}
5149

50+
var draft bool
51+
5252
cmd := &cobra.Command{
5353
Use: "list",
5454
Short: "List and filter pull requests in this repository",
@@ -77,8 +77,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
7777
return &cmdutil.FlagError{Err: fmt.Errorf("invalid value for --limit: %v", opts.LimitResults)}
7878
}
7979

80-
if !cmd.Flags().Changed("draft") {
81-
opts.Draft = nil
80+
if cmd.Flags().Changed("draft") {
81+
opts.Draft = strconv.FormatBool(draft)
8282
}
8383

8484
if runF != nil {
@@ -99,7 +99,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
9999
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
100100
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
101101
cmd.Flags().StringVarP(&opts.Search, "search", "S", "", "Search pull requests with `query`")
102-
cmd.Flags().BoolVar(opts.Draft, "draft", false, "Filter by draft/non-draft")
102+
cmd.Flags().BoolVarP(&draft, "draft", "d", false, "Filter by draft state")
103103

104104
cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.PullRequestFields)
105105

pkg/cmd/pr/list/list_test.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,16 @@ func TestPRList_filteringDraft(t *testing.T) {
183183
expectedQuery string
184184
}{
185185
{
186-
"draft",
187-
"--draft=1",
188-
`repo:OWNER/REPO is:pr is:open draft:true`,
186+
name: "draft",
187+
cli: "--draft",
188+
expectedQuery: `repo:OWNER/REPO is:pr is:open draft:true`,
189189
},
190190
{
191-
"non-draft",
192-
"--draft=false",
193-
`repo:OWNER/REPO is:pr is:open draft:false`,
194-
}}
191+
name: "non-draft",
192+
cli: "--draft=false",
193+
expectedQuery: `repo:OWNER/REPO is:pr is:open draft:false`,
194+
},
195+
}
195196

196197
for _, test := range tests {
197198
t.Run(test.name, func(t *testing.T) {
@@ -210,16 +211,13 @@ func TestPRList_filteringDraft(t *testing.T) {
210211
}
211212
})
212213
}
213-
214214
}
215215

216216
func TestPRList_withInvalidLimitFlag(t *testing.T) {
217217
http := initFakeHTTP()
218218
defer http.Verify(t)
219-
220219
_, err := runCommand(http, true, `--limit=0`)
221-
assert.NotNil(t, err)
222-
assert.Equal(t, err.Error(), "invalid value for --limit: 0")
220+
assert.EqualError(t, err, "invalid value for --limit: 0")
223221
}
224222

225223
func TestPRList_web(t *testing.T) {
@@ -229,19 +227,19 @@ func TestPRList_web(t *testing.T) {
229227
expectedBrowserURL string
230228
}{
231229
{
232-
"test",
233-
"-a peter -l bug -l docs -L 10 -s merged -B trunk",
234-
"https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Amerged+assignee%3Apeter+label%3Abug+label%3Adocs+base%3Atrunk",
230+
name: "filters",
231+
cli: "-a peter -l bug -l docs -L 10 -s merged -B trunk",
232+
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Amerged+assignee%3Apeter+label%3Abug+label%3Adocs+base%3Atrunk",
235233
},
236234
{
237-
"draft",
238-
"--draft=true",
239-
"https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Aopen+draft%3Atrue",
235+
name: "draft",
236+
cli: "--draft=true",
237+
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Aopen+draft%3Atrue",
240238
},
241239
{
242-
"non-draft",
243-
"--draft=0",
244-
"https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Aopen+draft%3Afalse",
240+
name: "non-draft",
241+
cli: "--draft=0",
242+
expectedBrowserURL: "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Aopen+draft%3Afalse",
245243
},
246244
}
247245

@@ -263,5 +261,4 @@ func TestPRList_web(t *testing.T) {
263261
assert.Equal(t, test.expectedBrowserURL, output.BrowsedURL)
264262
})
265263
}
266-
267264
}

pkg/cmd/pr/shared/params.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ type FilterOptions struct {
157157
Mention string
158158
Milestone string
159159
Search string
160-
// filter by draft/non-draft, `nil` means "no filter"
161-
Draft *bool
162-
163-
Fields []string
160+
Draft string
161+
Fields []string
164162
}
165163

166164
func (opts *FilterOptions) IsDefault() bool {
@@ -243,8 +241,8 @@ func SearchQueryBuild(options FilterOptions) string {
243241
if options.Search != "" {
244242
q.AddQuery(options.Search)
245243
}
246-
if options.Draft != nil {
247-
q.SetDraft(*options.Draft)
244+
if options.Draft != "" {
245+
q.SetDraft(options.Draft)
248246
}
249247
return q.String()
250248
}

pkg/cmd/pr/shared/params_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ func Test_listURLWithQuery(t *testing.T) {
1616
listURL string
1717
options FilterOptions
1818
}
19-
draft := true
20-
noDraft := false
2119

2220
tests := []struct {
2321
name string
@@ -44,7 +42,7 @@ func Test_listURLWithQuery(t *testing.T) {
4442
options: FilterOptions{
4543
Entity: "pr",
4644
State: "open",
47-
Draft: &draft,
45+
Draft: "true",
4846
},
4947
},
5048
want: "https://example.com/path?q=is%3Apr+is%3Aopen+draft%3Atrue",
@@ -57,7 +55,7 @@ func Test_listURLWithQuery(t *testing.T) {
5755
options: FilterOptions{
5856
Entity: "pr",
5957
State: "open",
60-
Draft: &noDraft,
58+
Draft: "false",
6159
},
6260
},
6361
want: "https://example.com/path?q=is%3Apr+is%3Aopen+draft%3Afalse",

pkg/githubsearch/query.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Query struct {
5454
forkState string
5555
visibility string
5656
isArchived *bool
57-
isDraft *bool
57+
draft string
5858
}
5959

6060
func (q *Query) InRepository(nameWithOwner string) {
@@ -140,8 +140,8 @@ func (q *Query) SetArchived(isArchived bool) {
140140
q.isArchived = &isArchived
141141
}
142142

143-
func (q *Query) SetDraft(isDraft bool) {
144-
q.isDraft = &isDraft
143+
func (q *Query) SetDraft(draft string) {
144+
q.draft = draft
145145
}
146146

147147
func (q *Query) String() string {
@@ -203,8 +203,8 @@ func (q *Query) String() string {
203203
if q.headBranch != "" {
204204
qs += fmt.Sprintf("head:%s ", quote(q.headBranch))
205205
}
206-
if q.isDraft != nil {
207-
qs += fmt.Sprintf("draft:%v ", *q.isDraft)
206+
if q.draft != "" {
207+
qs += fmt.Sprintf("draft:%v ", q.draft)
208208
}
209209

210210
if q.sort != "" {

0 commit comments

Comments
 (0)
X Tutup