Skip to content

Commit

Permalink
label: Add delete subcommand
Browse files Browse the repository at this point in the history
After supporting label creation, it just seems logical to support
the reverse action as well.
  • Loading branch information
fmuellner committed Jan 5, 2021
1 parent f5b306c commit 2c097ca
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cmd/label_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"log"

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

var labelDeleteCmd = &cobra.Command{
Use: "delete [remote] <name>",
Aliases: []string{"remove"},
Short: "Deletes an existing label",
Long: ``,
PersistentPreRun: LabPersistentPreRun,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rn, name, err := parseArgsRemoteAndProject(args)
if err != nil {
log.Fatal(err)
}

labels, err := MapLabels(rn, []string{name})
if err != nil {
log.Fatal(err)
}

err = lab.LabelDelete(rn, labels[0])
if err != nil {
log.Fatal(err)
}
},
}

func init() {
labelCmd.AddCommand(labelDeleteCmd)
carapace.Gen(labelCmd).PositionalCompletion(
action.Remotes(),
)
}
60 changes: 60 additions & 0 deletions cmd/label_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test_labelCmd(t *testing.T) {
t.Parallel()
repo := copyTestRepo(t)
t.Run("prepare", func(t *testing.T) {
cmd := exec.Command("sh", "-c", labBinaryPath+` label list lab-testing | grep -q 'test-label' && `+labBinaryPath+` label delete test-label`)
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
//t.Fatal(err)
}
})
t.Run("create", func(t *testing.T) {
cmd := exec.Command(labBinaryPath, "label", "create", "lab-testing", "test-label",
"--color", "crimson",
"--description", "Reddish test label",
)
cmd.Dir = repo

_ = cmd.Run()

cmd = exec.Command(labBinaryPath, "label", "list", "lab-testing")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
out := string(b)
if err != nil {
t.Log(out)
//t.Fatal(err)
}
require.Contains(t, out, "test-label")
})
t.Run("delete", func(t *testing.T) {
cmd := exec.Command(labBinaryPath, "label", "delete", "lab-testing", "test-label")
cmd.Dir = repo

_ = cmd.Run()

cmd = exec.Command(labBinaryPath, "label", "list", "lab-testing")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
out := string(b)
if err != nil {
t.Log(out)
//t.Fatal(err)
}
require.NotContains(t, out, "test-label")
})
}
13 changes: 13 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,19 @@ func LabelCreate(project string, opts *gitlab.CreateLabelOptions) error {
return err
}

// LabelDelete removes a project label
func LabelDelete(project, name string) error {
p, err := FindProject(project)
if err != nil {
return err
}

_, err = lab.Labels.DeleteLabel(p.ID, &gitlab.DeleteLabelOptions{
Name: &name,
})
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 2c097ca

Please sign in to comment.