Skip to content

Commit

Permalink
label: Add new create subcommand
Browse files Browse the repository at this point in the history
Now that we only allow existing labels with issues/merge requests,
it is no longer possible to create a new label on-the-fly.

Instead, cover that use case with a new dedicated command.
  • Loading branch information
fmuellner committed Jan 5, 2021
1 parent 23e8f40 commit f5b306c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
58 changes: 58 additions & 0 deletions cmd/label_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"log"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var labelCreateCmd = &cobra.Command{
Use: "create [remote] <name>",
Aliases: []string{"add"},
Short: "Create a new label",
Long: ``,
Example: `lab label create my-label
lab label create --color cornflowerblue --description "Blue as a cornflower" blue
lab label create --color #6495ed --description "Also blue as a cornflower" blue2`,
PersistentPreRun: LabPersistentPreRun,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rn, name, err := parseArgsRemoteAndProject(args)
if err != nil {
log.Fatal(err)
}

color, err := cmd.Flags().GetString("color")
if err != nil {
log.Fatal(err)
}

desc, err := cmd.Flags().GetString("description")
if err != nil {
log.Fatal(err)
}

err = lab.LabelCreate(rn, &gitlab.CreateLabelOptions{
Name: &name,
Description: &desc,
Color: &color,
})

if err != nil {
log.Fatal(err)
}
},
}

func init() {
labelCreateCmd.Flags().String("color", "#428BCA", "color of the new label in HTML hex notation or CSS color name")
labelCreateCmd.Flags().String("description", "", "description of the new label")
labelCmd.AddCommand(labelCreateCmd)
carapace.Gen(labelCmd).PositionalCompletion(
action.Remotes(),
)
}
11 changes: 11 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,17 @@ func LabelList(project string) ([]*gitlab.Label, error) {
return labels, nil
}

// LabelCreate creates a new project label
func LabelCreate(project string, opts *gitlab.CreateLabelOptions) error {
p, err := FindProject(project)
if err != nil {
return err
}

_, _, err = lab.Labels.CreateLabel(p.ID, opts)
return err
}

// ProjectSnippetCreate creates a snippet in a project
func ProjectSnippetCreate(pid interface{}, opts *gitlab.CreateProjectSnippetOptions) (*gitlab.Snippet, error) {
snip, _, err := lab.ProjectSnippets.CreateSnippet(pid, opts)
Expand Down

0 comments on commit f5b306c

Please sign in to comment.