Skip to content

Commit

Permalink
mr_list: Support search
Browse files Browse the repository at this point in the history
Search is as useful for merge requests as for issues, so support
the same option as `lab issue list`.
  • Loading branch information
fmuellner committed Jan 21, 2021
1 parent c59b143 commit 8251fd6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
30 changes: 24 additions & 6 deletions cmd/mr_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"

"github.com/pkg/errors"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
Expand All @@ -24,6 +25,7 @@ var (
mrReady bool
mrConflicts bool
mrNoConflicts bool
mrExactMatch bool
assigneeID *int
mrAssignee string
order string
Expand All @@ -32,11 +34,15 @@ var (

// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list [remote]",
Aliases: []string{"ls"},
Short: "List merge requests",
Long: ``,
Args: cobra.MaximumNArgs(1),
Use: "list [remote] [search]",
Aliases: []string{"ls", "search"},
Short: "List merge requests",
Long: ``,
Args: cobra.MaximumNArgs(2),
Example: `lab mr list
lab mr list "search terms" # search merge requests for "search terms"
lab mr search "search terms" # same as above
lab mr list remote "search terms" # search "remote" for merge requests with "search terms"`,
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
mrs, err := mrList(args)
Expand All @@ -51,7 +57,7 @@ var listCmd = &cobra.Command{
}

func mrList(args []string) ([]*gitlab.MergeRequest, error) {
rn, _, err := parseArgsRemoteAndID(args)
rn, search, err := parseArgsRemoteAndProject(args)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,6 +121,17 @@ func mrList(args []string) ([]*gitlab.MergeRequest, error) {
opts.WIP = gitlab.String("no")
}

if mrExactMatch {
if search == "" {
return nil, errors.New("Exact match requested, but no search terms provided")
}
search = "\"" + search + "\""
}

if search != "" {
opts.Search = &search
}

mrs, err := lab.MRList(rn, opts, num)
if err != nil {
return mrs, err
Expand Down Expand Up @@ -161,6 +178,7 @@ func init() {
listCmd.Flags().SortFlags = false
listCmd.Flags().BoolVar(&mrNoConflicts, "no-conflicts", false, "list only MRs that can be merged")
listCmd.Flags().BoolVar(&mrConflicts, "conflicts", false, "list only MRs that cannot be merged")
listCmd.Flags().BoolVarP(&mrExactMatch, "exact-match", "x", false, "match on the exact (case-insensitive) search terms")

mrCmd.AddCommand(listCmd)
carapace.Gen(listCmd).FlagCompletion(carapace.ActionMap{
Expand Down
16 changes: 16 additions & 0 deletions cmd/mr_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,19 @@ func Test_mrListCreatedDescending(t *testing.T) {
t.Log(mrs)
require.Equal(t, latestCreatedTestMR, mrs[0])
}

func Test_mrListSearch(t *testing.T) {
t.Parallel()
repo := copyTestRepo(t)
cmd := exec.Command(labBinaryPath, "mr", "list", "emoji")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
}

mrs := strings.Split(string(b), "\n")
t.Log(mrs)
require.Contains(t, mrs, "!6 test award emoji")
}

0 comments on commit 8251fd6

Please sign in to comment.