Skip to content

Commit

Permalink
added milestone completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Mar 6, 2021
1 parent 3ffb5d9 commit 7e5ce92
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 3 deletions.
11 changes: 11 additions & 0 deletions cmd/issue_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ func init() {
issueCreateCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")

issueCmd.AddCommand(issueCreateCmd)

carapace.Gen(issueCreateCmd).FlagCompletion(carapace.ActionMap{
"milestone": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
})

carapace.Gen(issueCreateCmd).PositionalCompletion(
action.Remotes(),
)
Expand Down
11 changes: 11 additions & 0 deletions cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ func init() {
issueEditCmd.Flags().SortFlags = false

issueCmd.AddCommand(issueEditCmd)

carapace.Gen(issueEditCmd).FlagCompletion(carapace.ActionMap{
"milestone": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
})

carapace.Gen(issueEditCmd).PositionalCompletion(
action.Remotes(),
action.Issues(issueList),
Expand Down
7 changes: 7 additions & 0 deletions cmd/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ func init() {

issueCmd.AddCommand(issueListCmd)
carapace.Gen(issueListCmd).FlagCompletion(carapace.ActionMap{
"milestone": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
"state": carapace.ActionValues("all", "opened", "closed"),
})
carapace.Gen(issueListCmd).PositionalCompletion(
Expand Down
9 changes: 8 additions & 1 deletion cmd/milestone_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ var milestoneCreateCmd = &cobra.Command{
func init() {
milestoneCreateCmd.Flags().String("description", "", "description of the new milestone")
milestoneCmd.AddCommand(milestoneCreateCmd)
carapace.Gen(milestoneCmd).PositionalCompletion(
carapace.Gen(milestoneCreateCmd).PositionalCompletion(
action.Remotes(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
)
}
9 changes: 8 additions & 1 deletion cmd/milestone_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ var milestoneDeleteCmd = &cobra.Command{

func init() {
milestoneCmd.AddCommand(milestoneDeleteCmd)
carapace.Gen(milestoneCmd).PositionalCompletion(
carapace.Gen(milestoneDeleteCmd).PositionalCompletion(
action.Remotes(),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
)
}
7 changes: 6 additions & 1 deletion cmd/milestone_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ lab milestone list remote "search term" # search "remote" for milestones with "
func init() {
milestoneListCmd.Flags().StringP("state", "s", "active", "filter milestones by state (active/closed)")
milestoneCmd.AddCommand(milestoneListCmd)
carapace.Gen(milestoneCmd).PositionalCompletion(

carapace.Gen(milestoneListCmd).FlagCompletion(carapace.ActionMap{
"state": carapace.ActionValues("active", "closed"),
})

carapace.Gen(milestoneListCmd).PositionalCompletion(
action.Remotes(),
)
}
11 changes: 11 additions & 0 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ func init() {
mergeRequestCmd.Flags().AddFlagSet(mrCreateCmd.Flags())

mrCmd.AddCommand(mrCreateCmd)

carapace.Gen(mrCreateCmd).FlagCompletion(carapace.ActionMap{
"milestone": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
})

carapace.Gen(mrCreateCmd).PositionalCompletion(
action.Remotes(),
action.RemoteBranches(0),
Expand Down
11 changes: 11 additions & 0 deletions cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ func init() {
mrEditCmd.Flags().SortFlags = false

mrCmd.AddCommand(mrEditCmd)

carapace.Gen(mrEditCmd).FlagCompletion(carapace.ActionMap{
"milestone": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
})

carapace.Gen(mrEditCmd).PositionalCompletion(
action.Remotes(),
action.MergeRequests(mrList),
Expand Down
7 changes: 7 additions & 0 deletions cmd/mr_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ func init() {

mrCmd.AddCommand(listCmd)
carapace.Gen(listCmd).FlagCompletion(carapace.ActionMap{
"milestone": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if project, _, err := parseArgsRemoteAndProject(c.Args); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return action.Milestones(project, action.MilestoneOpts{Active: true})
}
}),
"state": carapace.ActionValues("all", "opened", "closed", "merged"),
})

Expand Down
39 changes: 39 additions & 0 deletions internal/action/milestone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package action

import (
"strings"
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/cache"
"github.com/xanzy/go-gitlab"
lab "github.com/zaquestion/lab/internal/gitlab"
)

type MilestoneOpts struct {
Active bool
}

func (o MilestoneOpts) format() string {
if o.Active {
return "active"
} else {
return "closed"
}
}

func Milestones(project string, opts MilestoneOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
state := opts.format()
if milestones, err := lab.MilestoneList(project, &gitlab.ListMilestonesOptions{State: &state}); err != nil {
return carapace.ActionMessage(err.Error())
} else {
values := make([]string, len(milestones)*2)
for index, milestone := range milestones {
values[index*2] = milestone.Title
values[index*2+1] = strings.SplitN(milestone.Description, "\n", 2)[0]
}
return carapace.ActionValuesDescribed(values...)
}
}).Cache(5*time.Minute, cache.String(project, opts.format()))
}

0 comments on commit 7e5ce92

Please sign in to comment.