Skip to content

Commit 4622fad

Browse files
claytonrcarterprarit
authored andcommitted
feat(milestone): Add milestone browse subcommand
1 parent 88fe18c commit 4622fad

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

cmd/milestone_browse.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/MakeNowJust/heredoc/v2"
7+
"github.com/rsteube/carapace"
8+
"github.com/spf13/cobra"
9+
"github.com/zaquestion/lab/internal/action"
10+
"github.com/zaquestion/lab/internal/gitlab"
11+
)
12+
13+
var milestoneBrowseCmd = &cobra.Command{
14+
Use: "browse [remote] [<name>]",
15+
Aliases: []string{"b"},
16+
Short: "View milestone in a browser",
17+
Example: heredoc.Doc(`
18+
lab milestone browse
19+
lab milestone browse upstream "my great milestone"`),
20+
PersistentPreRun: labPersistentPreRun,
21+
Run: func(cmd *cobra.Command, args []string) {
22+
rn, name, err := parseArgsRemoteAndProject(args)
23+
if err != nil {
24+
log.Fatal(err)
25+
}
26+
27+
var milestoneURL string
28+
if name == "" {
29+
project, err := gitlab.FindProject(rn)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
34+
milestoneURL = project.WebURL + "/-/milestones"
35+
} else {
36+
name = strings.TrimLeft(name, "%")
37+
milestone, err := gitlab.MilestoneGet(rn, name)
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
42+
milestoneURL = milestone.WebURL
43+
}
44+
45+
err = browse(milestoneURL)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
},
50+
}
51+
52+
func init() {
53+
milestoneCmd.AddCommand(milestoneBrowseCmd)
54+
carapace.Gen(milestoneCreateCmd).PositionalCompletion(
55+
action.Remotes(),
56+
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
57+
project, _, err := parseArgsRemoteAndProject(c.Args)
58+
if err != nil {
59+
return carapace.ActionMessage(err.Error())
60+
}
61+
return action.Milestones(project, action.MilestoneOpts{Active: true})
62+
}),
63+
)
64+
}

cmd/milestone_browse_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func Test_milestoneBrowse(t *testing.T) {
10+
oldBrowse := browse
11+
defer func() { browse = oldBrowse }()
12+
13+
browse = func(url string) error {
14+
require.Equal(t, "https://gitlab.com/zaquestion/test/-/milestones/1", url)
15+
return nil
16+
}
17+
18+
// milestone "1.0" has id 1
19+
milestoneBrowseCmd.Run(nil, []string{"1.0"})
20+
}

0 commit comments

Comments
 (0)