fix listing of PRs when merged ones are searched (#3730)

Co-authored-by: Mislav Marohnić <mislav@github.com>
This commit is contained in:
Gowtham Munukutla 2021-05-28 15:41:12 +05:30 committed by GitHub
parent 35e5c758b5
commit e160dd3eae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 2 deletions

View file

@ -112,9 +112,14 @@ func listRun(opts *ListOptions) error {
return err
}
issueState := strings.ToLower(opts.State)
if issueState == "open" && shared.QueryHasStateClause(opts.Search) {
issueState = ""
}
filterOptions := prShared.FilterOptions{
Entity: "issue",
State: strings.ToLower(opts.State),
State: issueState,
Assignee: opts.Assignee,
Labels: opts.Labels,
Author: opts.Author,

View file

@ -116,9 +116,14 @@ func listRun(opts *ListOptions) error {
return err
}
prState := strings.ToLower(opts.State)
if prState == "open" && shared.QueryHasStateClause(opts.Search) {
prState = ""
}
filters := shared.FilterOptions{
Entity: "pr",
State: strings.ToLower(opts.State),
State: prState,
Author: opts.Author,
Assignee: opts.Assignee,
Labels: opts.Labels,

View file

@ -2,6 +2,7 @@ package shared
import (
"fmt"
"github.com/google/shlex"
"net/url"
"strings"
@ -243,6 +244,21 @@ func SearchQueryBuild(options FilterOptions) string {
return q.String()
}
func QueryHasStateClause(searchQuery string) bool {
argv, err := shlex.Split(searchQuery)
if err != nil {
return false
}
for _, arg := range argv {
if arg == "is:closed" || arg == "is:merged" || arg == "state:closed" || arg == "state:merged" || strings.HasPrefix(arg, "merged:") || strings.HasPrefix(arg, "closed:") {
return true
}
}
return false
}
// MeReplacer resolves usages of `@me` to the handle of the currently logged in user.
type MeReplacer struct {
apiClient *api.Client

View file

@ -1,6 +1,7 @@
package shared
import (
"github.com/stretchr/testify/assert"
"net/http"
"reflect"
"testing"
@ -151,3 +152,43 @@ func TestMeReplacer_Replace(t *testing.T) {
})
}
}
func Test_QueryHasStateClause(t *testing.T) {
tests := []struct {
searchQuery string
hasState bool
}{
{
searchQuery: "is:closed is:merged",
hasState: true,
},
{
searchQuery: "author:mislav",
hasState: false,
},
{
searchQuery: "assignee:g14a mentions:vilmibm",
hasState: false,
},
{
searchQuery: "merged:>2021-05-20",
hasState: true,
},
{
searchQuery: "state:merged state:open",
hasState: true,
},
{
searchQuery: "assignee:g14a is:closed",
hasState: true,
},
{
searchQuery: "state:closed label:bug",
hasState: true,
},
}
for _, tt := range tests {
gotState := QueryHasStateClause(tt.searchQuery)
assert.Equal(t, tt.hasState, gotState)
}
}