diff --git a/internal/action/action.go b/internal/action/action.go index cccb44fe..7e264501 100644 --- a/internal/action/action.go +++ b/internal/action/action.go @@ -2,10 +2,14 @@ package action import ( "strconv" + "strings" + "time" "github.com/rsteube/carapace" + "github.com/rsteube/carapace/pkg/cache" "github.com/xanzy/go-gitlab" "github.com/zaquestion/lab/internal/git" + lab "github.com/zaquestion/lab/internal/gitlab" ) // Remotes returns a carapace.Action containing all possible remote values @@ -86,3 +90,52 @@ func MergeRequests(mrList func(args []string) ([]*gitlab.MergeRequest, error)) c return carapace.ActionValuesDescribed(values...) }) } + +// Labels returns a carapace.Action containing all possible labels +func Labels(project string) carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + labels, err := lab.LabelList(project) + if err != nil { + return carapace.ActionMessage(err.Error()) + } + + values := make([]string, len(labels)*2) + for index, label := range labels { + values[index*2] = label.Name + values[index*2+1] = label.Description + } + return carapace.ActionValuesDescribed(values...) + }).Cache(5*time.Minute, cache.String(project)) +} + +// MilestoneOpts store filtering information for the milestones to be +// completed by Milestones(). +type MilestoneOpts struct { + Active bool +} + +func (o MilestoneOpts) format() string { + if o.Active { + return "active" + } + return "closed" +} + +// Milestones returns a carapace.Action containing all possible milestones with +// their description +func Milestones(project string, opts MilestoneOpts) carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + state := opts.format() + milestones, err := lab.MilestoneList(project, &gitlab.ListMilestonesOptions{State: &state}) + if err != nil { + return carapace.ActionMessage(err.Error()) + } + + 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())) +} diff --git a/internal/action/label.go b/internal/action/label.go deleted file mode 100644 index 77da7a9e..00000000 --- a/internal/action/label.go +++ /dev/null @@ -1,26 +0,0 @@ -package action - -import ( - "time" - - "github.com/rsteube/carapace" - "github.com/rsteube/carapace/pkg/cache" - lab "github.com/zaquestion/lab/internal/gitlab" -) - -// Labels returns a carapace.Action containing all possible labels -func Labels(project string) carapace.Action { - return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - labels, err := lab.LabelList(project) - if err != nil { - return carapace.ActionMessage(err.Error()) - } - - values := make([]string, len(labels)*2) - for index, label := range labels { - values[index*2] = label.Name - values[index*2+1] = label.Description - } - return carapace.ActionValuesDescribed(values...) - }).Cache(5*time.Minute, cache.String(project)) -} diff --git a/internal/action/milestone.go b/internal/action/milestone.go deleted file mode 100644 index d0add207..00000000 --- a/internal/action/milestone.go +++ /dev/null @@ -1,43 +0,0 @@ -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" -) - -// MilestoneOpts store filtering information for the milestones to be -// completed by Milestones(). -type MilestoneOpts struct { - Active bool -} - -func (o MilestoneOpts) format() string { - if o.Active { - return "active" - } - return "closed" -} - -// Milestones returns a carapace.Action containing all possible milestones with -// their description -func Milestones(project string, opts MilestoneOpts) carapace.Action { - return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - state := opts.format() - milestones, err := lab.MilestoneList(project, &gitlab.ListMilestonesOptions{State: &state}) - if err != nil { - return carapace.ActionMessage(err.Error()) - } - - 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())) -}