Skip to content
Snippets Groups Projects
Verified Commit a034e17c authored by Jay McCure's avatar Jay McCure
Browse files

feat(label list): flag to list group labels

parent effa7538
No related branches found
No related tags found
1 merge request!1764feat(label list): flag to list group labels
......@@ -2,10 +2,38 @@ package api
import "github.com/xanzy/go-gitlab"
var CreateLabel = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateLabelOptions) (*gitlab.Label, error) {
type ListLabelsOptions struct {
WithCounts *bool
PerPage int
Page int
}
func (opts *ListLabelsOptions) ListLabelsOptions() *gitlab.ListLabelsOptions {
projectOpts := &gitlab.ListLabelsOptions{}
projectOpts.WithCounts = opts.WithCounts
projectOpts.PerPage = opts.PerPage
projectOpts.Page = opts.Page
return projectOpts
}
func (opts *ListLabelsOptions) ListGroupLabelsOptions() *gitlab.ListGroupLabelsOptions {
groupOpts := &gitlab.ListGroupLabelsOptions{}
groupOpts.WithCounts = opts.WithCounts
groupOpts.PerPage = opts.PerPage
groupOpts.Page = opts.Page
return groupOpts
}
func getClient(client *gitlab.Client) *gitlab.Client {
if client == nil {
client = apiClient.Lab()
return apiClient.Lab()
}
return client
}
var CreateLabel = func(client *gitlab.Client, projectID interface{}, opts *gitlab.CreateLabelOptions) (*gitlab.Label, error) {
client = getClient(client)
label, _, err := client.Labels.CreateLabel(projectID, opts)
if err != nil {
return nil, err
......@@ -13,18 +41,30 @@ var CreateLabel = func(client *gitlab.Client, projectID interface{}, opts *gitla
return label, nil
}
var ListLabels = func(client *gitlab.Client, projectID interface{}, opts *gitlab.ListLabelsOptions) ([]*gitlab.Label, error) {
if client == nil {
client = apiClient.Lab()
}
var ListLabels = func(client *gitlab.Client, projectID interface{}, opts *ListLabelsOptions) ([]*gitlab.Label, error) {
client = getClient(client)
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
label, _, err := client.Labels.ListLabels(projectID, opts)
label, _, err := client.Labels.ListLabels(projectID, opts.ListLabelsOptions())
if err != nil {
return nil, err
}
return label, nil
}
var ListGroupLabels = func(client *gitlab.Client, groupID interface{}, opts *ListLabelsOptions) ([]*gitlab.GroupLabel, error) {
client = getClient(client)
if opts.PerPage == 0 {
opts.PerPage = DefaultListLimit
}
labels, _, err := client.GroupLabels.ListGroupLabels(groupID, opts.ListGroupLabelsOptions())
if err != nil {
return nil, err
}
return labels, nil
}
......@@ -146,9 +146,9 @@ func EditorPrompt(response *string, question, templateContent, editorCommand str
type GetTextUsingEditor func(editor, tmpFileName, content string) (string, error)
func LabelsPrompt(response *[]string, apiClient *gitlab.Client, repoRemote *glrepo.Remote) (err error) {
lOpts := &gitlab.ListLabelsOptions{}
lOpts.PerPage = 100
labels, err := api.ListLabels(apiClient, repoRemote.FullName(), lOpts)
labelOpts := &api.ListLabelsOptions{}
labelOpts.PerPage = 100
labels, err := api.ListLabels(apiClient, repoRemote.FullName(), labelOpts)
if err != nil {
return err
}
......
......@@ -1043,7 +1043,7 @@ func Test_LabelsPromptAPIFail(t *testing.T) {
Repo: repo,
}
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *gitlab.ListLabelsOptions) ([]*gitlab.Label, error) {
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *api.ListLabelsOptions) ([]*gitlab.Label, error) {
return nil, errors.New("API call failed")
}
......@@ -1067,7 +1067,7 @@ func Test_LabelsPromptPromptsFail(t *testing.T) {
t.Run("MultiSelect", func(t *testing.T) {
// Return a list with at least one value so we hit the MultiSelect path
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *gitlab.ListLabelsOptions) ([]*gitlab.Label, error) {
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *api.ListLabelsOptions) ([]*gitlab.Label, error) {
return []*gitlab.Label{
{
Name: "foo",
......@@ -1093,7 +1093,7 @@ func Test_LabelsPromptPromptsFail(t *testing.T) {
t.Run("AskQuestionWithInput", func(t *testing.T) {
// Return an empty list so we hit the AskQuestionWithInput prompt path
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *gitlab.ListLabelsOptions) ([]*gitlab.Label, error) {
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *api.ListLabelsOptions) ([]*gitlab.Label, error) {
return []*gitlab.Label{}, nil
}
......@@ -1126,7 +1126,7 @@ func Test_LabelsPromptMultiSelect(t *testing.T) {
Repo: repo,
}
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *gitlab.ListLabelsOptions) ([]*gitlab.Label, error) {
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *api.ListLabelsOptions) ([]*gitlab.Label, error) {
return []*gitlab.Label{
{
Name: "foo",
......@@ -1206,7 +1206,7 @@ func Test_LabelsPromptAskQuestionWithInput(t *testing.T) {
Repo: repo,
}
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *gitlab.ListLabelsOptions) ([]*gitlab.Label, error) {
api.ListLabels = func(_ *gitlab.Client, _ interface{}, _ *api.ListLabelsOptions) ([]*gitlab.Label, error) {
return []*gitlab.Label{}, nil
}
......
......@@ -3,10 +3,10 @@ package list
import (
"encoding/json"
"fmt"
"strings"
"github.com/MakeNowJust/heredoc/v2"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/pkg/utils"
......@@ -14,9 +14,16 @@ import (
"github.com/xanzy/go-gitlab"
)
var OutputFormat string
type LabelListOptions struct {
Group string
Page int
PerPage int
OutputFormat string
}
func NewCmdList(f *cmdutils.Factory) *cobra.Command {
opts := &LabelListOptions{}
labelListCmd := &cobra.Command{
Use: "list [flags]",
Short: `List labels in the repository.`,
......@@ -26,6 +33,7 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
glab label list
glab label ls
glab label list -R owner/repository
glab label list -g mygroup
`),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
......@@ -41,56 +49,64 @@ func NewCmdList(f *cmdutils.Factory) *cobra.Command {
return err
}
l := &gitlab.ListLabelsOptions{}
labelApiOpts := &api.ListLabelsOptions{}
labelApiOpts.WithCounts = gitlab.Ptr(true)
l.WithCounts = gitlab.Ptr(true)
if p, _ := cmd.Flags().GetInt("page"); p != 0 {
l.Page = p
if p := opts.Page; p != 0 {
labelApiOpts.Page = p
}
if p, _ := cmd.Flags().GetInt("per-page"); p != 0 {
l.PerPage = p
if pp := opts.PerPage; pp != 0 {
labelApiOpts.PerPage = pp
}
// List all labels
labels, err := api.ListLabels(apiClient, repo.FullName(), l)
if err != nil {
return err
}
if OutputFormat == "json" {
labelListJSON, _ := json.Marshal(labels)
fmt.Fprintln(f.IO.StdOut, string(labelListJSON))
var labelBuilder strings.Builder
if opts.Group != "" {
labels, err := api.ListGroupLabels(apiClient, opts.Group, labelApiOpts)
if err != nil {
return err
}
if opts.OutputFormat == "json" {
labelListJSON, _ := json.Marshal(labels)
fmt.Fprintln(f.IO.StdOut, string(labelListJSON))
} else {
fmt.Fprintf(f.IO.StdOut, "Showing label %d of %d for group %s.\n\n", len(labels), len(labels), opts.Group)
for _, label := range labels {
labelBuilder.WriteString(formatLabelInfo(label.Description, label.Name, label.Color))
}
}
} else {
fmt.Fprintf(f.IO.StdOut, "Showing label %d of %d on %s.\n\n", len(labels), len(labels), repo.FullName())
var labelPrintInfo string
for _, label := range labels {
var description string
if label.Description != "" {
description = fmt.Sprintf(" -> %s", label.Description)
labels, err := api.ListLabels(apiClient, repo.FullName(), labelApiOpts)
if err != nil {
return err
}
if opts.OutputFormat == "json" {
labelListJSON, _ := json.Marshal(labels)
fmt.Fprintln(f.IO.StdOut, string(labelListJSON))
} else {
fmt.Fprintf(f.IO.StdOut, "Showing label %d of %d on %s.\n\n", len(labels), len(labels), repo.FullName())
for _, label := range labels {
labelBuilder.WriteString(formatLabelInfo(label.Description, label.Name, label.Color))
}
labelPrintInfo += fmt.Sprintf("%s%s (%s)\n", label.Name, description, label.Color)
}
fmt.Fprintln(f.IO.StdOut, utils.Indent(labelPrintInfo, " "))
}
// Cache labels for host
//labelNames := make([]string, 0, len(labels))
//for _, label := range labels {
// labelNames = append(labelNames, label.Name)
//}
//labelsEntry := strings.Join(labelNames, ",")
//if err := cfg.Set(repo.RepoHost(), "project_labels", labelsEntry); err != nil {
// _ = cfg.Write()
//}
}
fmt.Fprintln(f.IO.StdOut, utils.Indent(labelBuilder.String(), " "))
return nil
},
}
labelListCmd.Flags().IntP("page", "p", 1, "Page number.")
labelListCmd.Flags().IntP("per-page", "P", 30, "Number of items to list per page.")
labelListCmd.Flags().StringVarP(&OutputFormat, "output", "F", "text", "Format output as: text, json.")
labelListCmd.Flags().IntVarP(&opts.Page, "page", "p", 1, "Page number.")
labelListCmd.Flags().IntVarP(&opts.PerPage, "per-page", "P", 30, "Number of items to list per page.")
labelListCmd.Flags().StringVarP(&opts.OutputFormat, "output", "F", "text", "Format output as: text, json.")
labelListCmd.Flags().StringVarP(&opts.Group, "group", "g", "", "List labels for a group.")
return labelListCmd
}
func formatLabelInfo(description string, name string, color string) string {
if description != "" {
description = fmt.Sprintf(" -> %s", description)
}
return fmt.Sprintf("%s%s (%s)\n", name, description, color)
}
......@@ -116,3 +116,49 @@ func TestLabelListJSON(t *testing.T) {
assert.JSONEq(t, expectedBody, output.String())
assert.Empty(t, output.Stderr())
}
func TestGroupLabelList(t *testing.T) {
fakeHTTP := &httpmock.Mocker{}
defer fakeHTTP.Verify(t)
fakeHTTP.RegisterResponder(http.MethodGet, "/api/v4/groups/foo/labels",
httpmock.NewStringResponse(http.StatusOK, `
[
{
"id":1,
"name":"groupbug",
"description":null,
"text_color":"#FFFFFF",
"color":"#6699cc",
"priority":null,
"is_project_label":false
},
{
"id":2,
"name":"groupux",
"description":"User Experience",
"text_color":"#FFFFFF",
"color":"#3cb371",
"priority":null,
"is_project_label":false
}
]
`))
flags := "--group foo"
output, err := runCommand(fakeHTTP, flags)
if err != nil {
t.Errorf("error running command `label list %s`: %v", flags, err)
}
out := output.String()
assert.Equal(t, heredoc.Doc(`
Showing label 2 of 2 for group foo.
groupbug (#6699cc)
groupux -> User Experience (#3cb371)
`), out)
assert.Empty(t, output.Stderr())
}
......@@ -29,12 +29,14 @@ ls
glab label list
glab label ls
glab label list -R owner/repository
glab label list -g mygroup
```
## Options
```plaintext
-g, --group string List labels for a group.
-F, --output string Format output as: text, json. (default "text")
-p, --page int Page number. (default 1)
-P, --per-page int Number of items to list per page. (default 30)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment