Skip to content

Commit e160dd3

Browse files
g14amislav
andauthored
fix listing of PRs when merged ones are searched (cli#3730)
Co-authored-by: Mislav Marohnić <mislav@github.com>
1 parent 35e5c75 commit e160dd3

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

pkg/cmd/issue/list/list.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,14 @@ func listRun(opts *ListOptions) error {
112112
return err
113113
}
114114

115+
issueState := strings.ToLower(opts.State)
116+
if issueState == "open" && shared.QueryHasStateClause(opts.Search) {
117+
issueState = ""
118+
}
119+
115120
filterOptions := prShared.FilterOptions{
116121
Entity: "issue",
117-
State: strings.ToLower(opts.State),
122+
State: issueState,
118123
Assignee: opts.Assignee,
119124
Labels: opts.Labels,
120125
Author: opts.Author,

pkg/cmd/pr/list/list.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,14 @@ func listRun(opts *ListOptions) error {
116116
return err
117117
}
118118

119+
prState := strings.ToLower(opts.State)
120+
if prState == "open" && shared.QueryHasStateClause(opts.Search) {
121+
prState = ""
122+
}
123+
119124
filters := shared.FilterOptions{
120125
Entity: "pr",
121-
State: strings.ToLower(opts.State),
126+
State: prState,
122127
Author: opts.Author,
123128
Assignee: opts.Assignee,
124129
Labels: opts.Labels,

pkg/cmd/pr/shared/params.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package shared
22

33
import (
44
"fmt"
5+
"github.com/google/shlex"
56
"net/url"
67
"strings"
78

@@ -243,6 +244,21 @@ func SearchQueryBuild(options FilterOptions) string {
243244
return q.String()
244245
}
245246

247+
func QueryHasStateClause(searchQuery string) bool {
248+
argv, err := shlex.Split(searchQuery)
249+
if err != nil {
250+
return false
251+
}
252+
253+
for _, arg := range argv {
254+
if arg == "is:closed" || arg == "is:merged" || arg == "state:closed" || arg == "state:merged" || strings.HasPrefix(arg, "merged:") || strings.HasPrefix(arg, "closed:") {
255+
return true
256+
}
257+
}
258+
259+
return false
260+
}
261+
246262
// MeReplacer resolves usages of `@me` to the handle of the currently logged in user.
247263
type MeReplacer struct {
248264
apiClient *api.Client

pkg/cmd/pr/shared/params_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shared
22

33
import (
4+
"github.com/stretchr/testify/assert"
45
"net/http"
56
"reflect"
67
"testing"
@@ -151,3 +152,43 @@ func TestMeReplacer_Replace(t *testing.T) {
151152
})
152153
}
153154
}
155+
156+
func Test_QueryHasStateClause(t *testing.T) {
157+
tests := []struct {
158+
searchQuery string
159+
hasState bool
160+
}{
161+
{
162+
searchQuery: "is:closed is:merged",
163+
hasState: true,
164+
},
165+
{
166+
searchQuery: "author:mislav",
167+
hasState: false,
168+
},
169+
{
170+
searchQuery: "assignee:g14a mentions:vilmibm",
171+
hasState: false,
172+
},
173+
{
174+
searchQuery: "merged:>2021-05-20",
175+
hasState: true,
176+
},
177+
{
178+
searchQuery: "state:merged state:open",
179+
hasState: true,
180+
},
181+
{
182+
searchQuery: "assignee:g14a is:closed",
183+
hasState: true,
184+
},
185+
{
186+
searchQuery: "state:closed label:bug",
187+
hasState: true,
188+
},
189+
}
190+
for _, tt := range tests {
191+
gotState := QueryHasStateClause(tt.searchQuery)
192+
assert.Equal(t, tt.hasState, gotState)
193+
}
194+
}

0 commit comments

Comments
 (0)