Skip to content

Commit 8251fd6

Browse files
committed
mr_list: Support search
Search is as useful for merge requests as for issues, so support the same option as `lab issue list`.
1 parent c59b143 commit 8251fd6

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

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)