github.com/zaquestion/lab@v0.25.1/internal/action/action.go (about)

     1  package action
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/rsteube/carapace"
     9  	"github.com/rsteube/carapace/pkg/cache"
    10  	"github.com/xanzy/go-gitlab"
    11  	"github.com/zaquestion/lab/internal/git"
    12  	lab "github.com/zaquestion/lab/internal/gitlab"
    13  )
    14  
    15  // Remotes returns a carapace.Action containing all possible remote values
    16  func Remotes() carapace.Action {
    17  	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
    18  		remotes, err := git.Remotes()
    19  		if err != nil {
    20  			return carapace.ActionMessage(err.Error())
    21  		}
    22  		return carapace.ActionValues(remotes...)
    23  	})
    24  }
    25  
    26  // RemoteBranches returns a carapace.Action containing all possible remote
    27  // branches values
    28  func RemoteBranches(argIndex int) carapace.Action {
    29  	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
    30  		remote := ""
    31  		if argIndex >= 0 {
    32  			remote = c.Args[argIndex]
    33  		}
    34  		branches, err := git.RemoteBranches(remote)
    35  		if err != nil {
    36  			return carapace.ActionMessage(err.Error())
    37  		}
    38  		return carapace.ActionValues(branches...)
    39  	})
    40  }
    41  
    42  // Snippets retuns a carapace.Action containing all available snippets
    43  func Snippets(snippetList func(args []string) ([]*gitlab.Snippet, error)) carapace.Action {
    44  	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
    45  		snips, err := snippetList(c.Args[:0])
    46  		if err != nil {
    47  			return carapace.ActionMessage(err.Error())
    48  		}
    49  
    50  		values := make([]string, len(snips)*2)
    51  		for index, snip := range snips {
    52  			values[index*2] = strconv.Itoa(snip.ID)
    53  			values[index*2+1] = snip.Title
    54  		}
    55  		return carapace.ActionValuesDescribed(values...)
    56  	})
    57  }
    58  
    59  // Issues retuns a carapace.Action containing all available issues
    60  func Issues(issueList func(args []string) ([]*gitlab.Issue, error)) carapace.Action {
    61  	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
    62  		issues, err := issueList(c.Args[:0])
    63  		if err != nil {
    64  			return carapace.ActionMessage(err.Error())
    65  		}
    66  
    67  		values := make([]string, len(issues)*2)
    68  		for index, issue := range issues {
    69  			values[index*2] = strconv.Itoa(issue.IID)
    70  			values[index*2+1] = issue.Title
    71  		}
    72  		return carapace.ActionValuesDescribed(values...)
    73  	})
    74  }
    75  
    76  // MergeRequests retuns a carapace.Action containing all available merge
    77  // requests
    78  func MergeRequests(mrList func(args []string) ([]*gitlab.MergeRequest, error)) carapace.Action {
    79  	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
    80  		mergeRequests, err := mrList(c.Args[:0])
    81  		if err != nil {
    82  			return carapace.ActionMessage(err.Error())
    83  		}
    84  
    85  		values := make([]string, len(mergeRequests)*2)
    86  		for index, mergeRequest := range mergeRequests {
    87  			values[index*2] = strconv.Itoa(mergeRequest.IID)
    88  			values[index*2+1] = mergeRequest.Title
    89  		}
    90  		return carapace.ActionValuesDescribed(values...)
    91  	})
    92  }
    93  
    94  // Labels returns a carapace.Action containing all possible labels
    95  func Labels(project string) carapace.Action {
    96  	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
    97  		labels, err := lab.LabelList(project)
    98  		if err != nil {
    99  			return carapace.ActionMessage(err.Error())
   100  		}
   101  
   102  		values := make([]string, len(labels)*2)
   103  		for index, label := range labels {
   104  			values[index*2] = label.Name
   105  			values[index*2+1] = label.Description
   106  		}
   107  		return carapace.ActionValuesDescribed(values...)
   108  	}).Cache(5*time.Minute, cache.String(project))
   109  }
   110  
   111  // MilestoneOpts store filtering information for the milestones to be
   112  // completed by Milestones().
   113  type MilestoneOpts struct {
   114  	Active bool
   115  }
   116  
   117  func (o MilestoneOpts) format() string {
   118  	if o.Active {
   119  		return "active"
   120  	}
   121  	return "closed"
   122  }
   123  
   124  // Milestones returns a carapace.Action containing all possible milestones with
   125  // their description
   126  func Milestones(project string, opts MilestoneOpts) carapace.Action {
   127  	return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
   128  		state := opts.format()
   129  		milestones, err := lab.MilestoneList(project, &gitlab.ListMilestonesOptions{State: &state})
   130  		if err != nil {
   131  			return carapace.ActionMessage(err.Error())
   132  		}
   133  
   134  		values := make([]string, len(milestones)*2)
   135  		for index, milestone := range milestones {
   136  			values[index*2] = milestone.Title
   137  			values[index*2+1] = strings.SplitN(milestone.Description, "\n", 2)[0]
   138  		}
   139  		return carapace.ActionValuesDescribed(values...)
   140  	}).Cache(5*time.Minute, cache.String(project, opts.format()))
   141  }