Skip to content

Commit e67df2b

Browse files
authored
Merge pull request #560 from fmuellner/mr-search
mr_list: Support search
2 parents 102b81b + 8251fd6 commit e67df2b

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

cmd/issue_list.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66

7+
"github.com/pkg/errors"
78
"github.com/rsteube/carapace"
89
"github.com/spf13/cobra"
910
gitlab "github.com/xanzy/go-gitlab"
@@ -12,12 +13,13 @@ import (
1213
)
1314

1415
var (
15-
issueLabels []string
16-
issueMilestone string
17-
issueState string
18-
issueSearch string
19-
issueNumRet int
20-
issueAll bool
16+
issueLabels []string
17+
issueMilestone string
18+
issueState string
19+
issueSearch string
20+
issueNumRet int
21+
issueAll bool
22+
issueExactMatch bool
2123
)
2224

2325
var issueListCmd = &cobra.Command{
@@ -70,6 +72,13 @@ func issueList(args []string) ([]*gitlab.Issue, error) {
7072
OrderBy: gitlab.String("updated_at"),
7173
}
7274

75+
if issueExactMatch {
76+
if issueSearch == "" {
77+
return nil, errors.New("Exact match requested, but no search terms provided")
78+
}
79+
issueSearch = "\"" + issueSearch + "\""
80+
}
81+
7382
if issueSearch != "" {
7483
opts.Search = &issueSearch
7584
}
@@ -97,6 +106,9 @@ func init() {
97106
issueListCmd.Flags().StringVar(
98107
&issueMilestone, "milestone", "",
99108
"filter issues by milestone")
109+
issueListCmd.Flags().BoolVarP(
110+
&issueExactMatch, "exact-match", "x", false,
111+
"match on the exact (case-insensitive) search terms")
100112

101113
issueCmd.AddCommand(issueListCmd)
102114
carapace.Gen(issueListCmd).FlagCompletion(carapace.ActionMap{

cmd/mr_list.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66

7+
"github.com/pkg/errors"
78
"github.com/rsteube/carapace"
89
"github.com/spf13/cobra"
910
gitlab "github.com/xanzy/go-gitlab"
@@ -24,6 +25,7 @@ var (
2425
mrReady bool
2526
mrConflicts bool
2627
mrNoConflicts bool
28+
mrExactMatch bool
2729
assigneeID *int
2830
mrAssignee string
2931
order string
@@ -32,11 +34,15 @@ var (
3234

3335
// listCmd represents the list command
3436
var listCmd = &cobra.Command{
35-
Use: "list [remote]",
36-
Aliases: []string{"ls"},
37-
Short: "List merge requests",
38-
Long: ``,
39-
Args: cobra.MaximumNArgs(1),
37+
Use: "list [remote] [search]",
38+
Aliases: []string{"ls", "search"},
39+
Short: "List merge requests",
40+
Long: ``,
41+
Args: cobra.MaximumNArgs(2),
42+
Example: `lab mr list
43+
lab mr list "search terms" # search merge requests for "search terms"
44+
lab mr search "search terms" # same as above
45+
lab mr list remote "search terms" # search "remote" for merge requests with "search terms"`,
4046
PersistentPreRun: LabPersistentPreRun,
4147
Run: func(cmd *cobra.Command, args []string) {
4248
mrs, err := mrList(args)
@@ -51,7 +57,7 @@ var listCmd = &cobra.Command{
5157
}
5258

5359
func mrList(args []string) ([]*gitlab.MergeRequest, error) {
54-
rn, _, err := parseArgsRemoteAndID(args)
60+
rn, search, err := parseArgsRemoteAndProject(args)
5561
if err != nil {
5662
return nil, err
5763
}
@@ -115,6 +121,17 @@ func mrList(args []string) ([]*gitlab.MergeRequest, error) {
115121
opts.WIP = gitlab.String("no")
116122
}
117123

124+
if mrExactMatch {
125+
if search == "" {
126+
return nil, errors.New("Exact match requested, but no search terms provided")
127+
}
128+
search = "\"" + search + "\""
129+
}
130+
131+
if search != "" {
132+
opts.Search = &search
133+
}
134+
118135
mrs, err := lab.MRList(rn, opts, num)
119136
if err != nil {
120137
return mrs, err
@@ -161,6 +178,7 @@ func init() {
161178
listCmd.Flags().SortFlags = false
162179
listCmd.Flags().BoolVar(&mrNoConflicts, "no-conflicts", false, "list only MRs that can be merged")
163180
listCmd.Flags().BoolVar(&mrConflicts, "conflicts", false, "list only MRs that cannot be merged")
181+
listCmd.Flags().BoolVarP(&mrExactMatch, "exact-match", "x", false, "match on the exact (case-insensitive) search terms")
164182

165183
mrCmd.AddCommand(listCmd)
166184
carapace.Gen(listCmd).FlagCompletion(carapace.ActionMap{

cmd/mr_list_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,19 @@ func Test_mrListCreatedDescending(t *testing.T) {
214214
t.Log(mrs)
215215
require.Equal(t, latestCreatedTestMR, mrs[0])
216216
}
217+
218+
func Test_mrListSearch(t *testing.T) {
219+
t.Parallel()
220+
repo := copyTestRepo(t)
221+
cmd := exec.Command(labBinaryPath, "mr", "list", "emoji")
222+
cmd.Dir = repo
223+
224+
b, err := cmd.CombinedOutput()
225+
if err != nil {
226+
t.Fatal(err)
227+
}
228+
229+
mrs := strings.Split(string(b), "\n")
230+
t.Log(mrs)
231+
require.Contains(t, mrs, "!6 test award emoji")
232+
}

0 commit comments

Comments
 (0)