Skip to content

Commit dcc6e8f

Browse files
John Frederickbzon
authored andcommitted
Add delete tag command
1 parent aba8283 commit dcc6e8f

3 files changed

Lines changed: 138 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Contributors are welcomed with love! Please read [CONTRIBUTING.md](./CONTRIBUTIN
213213
* [x] `describe tag [tag name] [--project] [flags]`
214214
* [x] `new tag [tag name] [--project] [flags]`
215215
* [ ] `edit tag [tag name] [--project] [flags]`
216-
* [ ] `delete tag [tag name] [--project]`
216+
* [x] `delete tag [tag name] [--project]`
217217

218218
### Project Hooks
219219

cmd/delete_tag.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright © 2018 github.com/devopsctl authors
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package cmd
22+
23+
import (
24+
"fmt"
25+
26+
"github.com/spf13/cobra"
27+
)
28+
29+
var deleteTagCmd = &cobra.Command{
30+
Use: "tag",
31+
Aliases: []string{"t"},
32+
Short: "Delete a project tag",
33+
Example: `# delete v1.0 tag from project groupx/myapp
34+
gitlabctl delete tag v1.0 --project=groupx/myapp`,
35+
SilenceErrors: true,
36+
SilenceUsage: true,
37+
DisableAutoGenTag: true,
38+
Args: cobra.ExactArgs(1),
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
return deleteTag(getFlagString(cmd, "project"), args[0])
41+
},
42+
}
43+
44+
func init() {
45+
deleteCmd.AddCommand(deleteTagCmd)
46+
addProjectFlag(deleteTagCmd)
47+
verifyMarkFlagRequired(deleteTagCmd, "project")
48+
}
49+
50+
func deleteTag(project, tag string) error {
51+
git, err := newGitlabClient()
52+
if err != nil {
53+
return err
54+
}
55+
_, err = git.Tags.DeleteTag(project, tag)
56+
if err != nil {
57+
return err
58+
}
59+
fmt.Printf("Tag (%s) from project (%s) has been deleted\n", tag, project)
60+
return nil
61+
}

cmd/delete_tag_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright © 2018 github.com/devopsctl authors
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7+
// furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in
10+
// all copies or substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18+
// THE SOFTWARE.
19+
20+
package cmd
21+
22+
import (
23+
"testing"
24+
25+
gitlab "github.com/xanzy/go-gitlab"
26+
)
27+
28+
func TestDeleteTagCmd(t *testing.T) {
29+
tt := []struct {
30+
name string
31+
flagsMap map[string]string
32+
args []string
33+
expect testResult
34+
}{
35+
{
36+
name: "delete an existent tag",
37+
flagsMap: map[string]string{
38+
"project": "Group1/project1",
39+
},
40+
args: []string{"sample_5.0"},
41+
expect: pass,
42+
},
43+
{
44+
name: "delete a non existent tag fails",
45+
flagsMap: map[string]string{
46+
"project": "Group1/project1",
47+
},
48+
args: []string{"nil-tag"},
49+
expect: fail,
50+
},
51+
}
52+
53+
for _, tc := range tt {
54+
// SETUP
55+
// Create tag before deleting
56+
if tc.expect == pass {
57+
if _, err := newTag(tc.flagsMap["project"],
58+
&gitlab.CreateTagOptions{
59+
TagName: gitlab.String(tc.args[0]),
60+
Ref: gitlab.String("master"),
61+
}); err != nil {
62+
tInfo(err)
63+
}
64+
}
65+
t.Run(tc.name, func(t *testing.T) {
66+
execT := execTestCmdFlags{
67+
t: t,
68+
cmd: deleteTagCmd,
69+
flagsMap: tc.flagsMap,
70+
args: tc.args,
71+
}
72+
stdout, execResult := execT.executeCommand()
73+
assertEqualResult(t, execResult, tc.expect, printFlagsTable(tc.flagsMap, stdout))
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)