Skip to content

Commit

Permalink
internal/action: move Labels and Milestones actions to a single file
Browse files Browse the repository at this point in the history
IMHO there isn't a good reason why only Labels and Milestones had their
own action files while other objects like Issues didn't. This patch
moves both Labels and Milestones actions to the action.go generic file.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Jun 23, 2021
1 parent b6864c2 commit 4b2a727
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 69 deletions.
53 changes: 53 additions & 0 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))
}
26 changes: 0 additions & 26 deletions internal/action/label.go

This file was deleted.

43 changes: 0 additions & 43 deletions internal/action/milestone.go

This file was deleted.

0 comments on commit 4b2a727

Please sign in to comment.