From e160dd3eae3925546402ed2b1def91e756c0074e Mon Sep 17 00:00:00 2001 From: Gowtham Munukutla Date: Fri, 28 May 2021 15:41:12 +0530 Subject: [PATCH] fix listing of PRs when merged ones are searched (#3730) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mislav Marohnić --- pkg/cmd/issue/list/list.go | 7 +++++- pkg/cmd/pr/list/list.go | 7 +++++- pkg/cmd/pr/shared/params.go | 16 +++++++++++++ pkg/cmd/pr/shared/params_test.go | 41 ++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/issue/list/list.go b/pkg/cmd/issue/list/list.go index c543a84dc..f0942ab20 100644 --- a/pkg/cmd/issue/list/list.go +++ b/pkg/cmd/issue/list/list.go @@ -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, diff --git a/pkg/cmd/pr/list/list.go b/pkg/cmd/pr/list/list.go index faf7a3e24..5810e8d7c 100644 --- a/pkg/cmd/pr/list/list.go +++ b/pkg/cmd/pr/list/list.go @@ -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, diff --git a/pkg/cmd/pr/shared/params.go b/pkg/cmd/pr/shared/params.go index d8348ec2c..93a5f2f58 100644 --- a/pkg/cmd/pr/shared/params.go +++ b/pkg/cmd/pr/shared/params.go @@ -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 diff --git a/pkg/cmd/pr/shared/params_test.go b/pkg/cmd/pr/shared/params_test.go index 74115c5b6..314218754 100644 --- a/pkg/cmd/pr/shared/params_test.go +++ b/pkg/cmd/pr/shared/params_test.go @@ -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) + } +}