Skip to content

Commit aba8283

Browse files
John Frederickbzon
authored andcommitted
Add new tags command
1 parent cfdc3ac commit aba8283

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
@@ -211,7 +211,7 @@ Contributors are welcomed with love! Please read [CONTRIBUTING.md](./CONTRIBUTIN
211211

212212
* [x] `get tags [project id or project path] [flags]`
213213
* [x] `describe tag [tag name] [--project] [flags]`
214-
* [ ] `new tag [tag name] [--project] [flags]`
214+
* [x] `new tag [tag name] [--project] [flags]`
215215
* [ ] `edit tag [tag name] [--project] [flags]`
216216
* [ ] `delete tag [tag name] [--project]`
217217

cmd/new_tag.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright © 2018 github.com/devopsctl authors
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
4+
// in the Software without restriction, including without limitation the rights
5+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
6+
// 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+
"github.com/spf13/cobra"
24+
gitlab "github.com/xanzy/go-gitlab"
25+
)
26+
27+
var newTagCmd = &cobra.Command{
28+
Use: "tag",
29+
Aliases: []string{"t"},
30+
Short: "Create a new tag for a specified project",
31+
Example: `# create tag from master branch for project groupx/myapp
32+
gitlabctl new tag v2.0 --project=groupx/myapp --ref=master`,
33+
SilenceErrors: true,
34+
SilenceUsage: true,
35+
DisableAutoGenTag: true,
36+
Args: cobra.ExactArgs(1),
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
return runNewTag(cmd, args[0])
39+
},
40+
}
41+
42+
func init() {
43+
newCmd.AddCommand(newTagCmd)
44+
addProjectFlag(newTagCmd)
45+
verifyMarkFlagRequired(newTagCmd, "project")
46+
newTagCmd.Flags().StringP("ref", "r", "",
47+
"The branch name or commit SHA to create branch from")
48+
verifyMarkFlagRequired(newTagCmd, "ref")
49+
}
50+
51+
func runNewTag(cmd *cobra.Command, tag string) error {
52+
opts := new(gitlab.CreateTagOptions)
53+
opts.Ref = gitlab.String(getFlagString(cmd, "ref"))
54+
opts.TagName = gitlab.String(tag)
55+
createdTag, err := newTag(getFlagString(cmd, "project"), opts)
56+
if err != nil {
57+
return err
58+
}
59+
printTagsOut(getFlagString(cmd, "out"), createdTag)
60+
return nil
61+
}
62+
63+
func newTag(project string, opts *gitlab.CreateTagOptions ) (*gitlab.Tag, error) {
64+
git, err := newGitlabClient()
65+
if err != nil {
66+
return nil, err
67+
}
68+
tag, _, err := git.Tags.CreateTag(project, opts)
69+
if err != nil {
70+
return nil, err
71+
}
72+
return tag, nil
73+
}

cmd/new_tag_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
"testing"
26+
)
27+
28+
func TestNewTag(t *testing.T) {
29+
tt := []struct {
30+
name string
31+
flagsMap map[string]string
32+
args []string
33+
expect testResult
34+
}{
35+
{
36+
name: "create a new tag",
37+
flagsMap: map[string]string{
38+
"project": "Group1/project1",
39+
"ref": "master",
40+
},
41+
args: []string{"v2.0"},
42+
expect: pass,
43+
},
44+
{
45+
name: "flags must be required",
46+
args: []string{"v3.0"},
47+
expect: fail,
48+
},
49+
}
50+
51+
for _, tc := range tt {
52+
t.Run(tc.name, func(t *testing.T) {
53+
execT := execTestCmdFlags{
54+
t: t,
55+
cmd: newTagCmd,
56+
flagsMap: tc.flagsMap,
57+
args: tc.args,
58+
}
59+
stdout, execResult := execT.executeCommand()
60+
fmt.Println(stdout)
61+
assertEqualResult(t, execResult, tc.expect, printFlagsTable(tc.flagsMap, stdout))
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)