Skip to content

Commit 37c6d74

Browse files
John Frederickbzon
authored andcommitted
Add new release command
1 parent b14c90e commit 37c6d74

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ 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
* [x] `delete tag [tag name] [--project]`
216+
* [x] `new release [description] [--project] [flags]`
217+
* [ ] `edit release [description] [--project] [flags]`
216218

217219
### Project Hooks
218220

cmd/helpers.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,27 @@ func printTagsOut(format string, tags ...*gitlab.Tag) {
369369
}
370370
printTable(header, rows)
371371
}
372+
}
373+
374+
func printReleasesOut(format string, releases ...*gitlab.Release) {
375+
switch format {
376+
case YAML:
377+
printYAML(releases)
378+
case JSON:
379+
printJSON(releases)
380+
default:
381+
if len(releases) == 0 {
382+
fmt.Println(noResultMsg)
383+
return
384+
}
385+
header := []string{"TAG NAME","DESCRIPTION"}
386+
var rows [][]string
387+
for _, v := range releases {
388+
rows = append(rows, []string{
389+
v.TagName,
390+
v.Description,
391+
})
392+
}
393+
printTable(header, rows)
394+
}
372395
}

cmd/new_release.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 newReleaseCmd = &cobra.Command{
28+
Use: "release",
29+
Aliases: []string{"r"},
30+
Short: "Create a new release for a specified project",
31+
Example: `# create release from v1.0 tag of project groupx/myapp
32+
gitlabctl new release sample --project=groupx/myapp --tag=v1.0`,
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 runNewRelease(cmd, args[0])
39+
},
40+
}
41+
42+
func init() {
43+
newCmd.AddCommand(newReleaseCmd)
44+
addProjectFlag(newReleaseCmd)
45+
verifyMarkFlagRequired(newReleaseCmd, "project")
46+
newReleaseCmd.Flags().StringP("tag", "t", "",
47+
"The name of a tag")
48+
verifyMarkFlagRequired(newReleaseCmd, "tag")
49+
}
50+
51+
func runNewRelease(cmd *cobra.Command, description string) error {
52+
opts := new(gitlab.CreateReleaseOptions)
53+
opts.Description = gitlab.String(description)
54+
createdRelease, err := newRelease(getFlagString(cmd, "project"), getFlagString(cmd, "tag"), opts)
55+
if err != nil {
56+
return err
57+
}
58+
printReleasesOut(getFlagString(cmd, "out"), createdRelease)
59+
return nil
60+
}
61+
62+
func newRelease(project string, tag string, opts *gitlab.CreateReleaseOptions) (*gitlab.Release, error) {
63+
git, err := newGitlabClient()
64+
if err != nil {
65+
return nil, err
66+
}
67+
release, _, err := git.Tags.CreateRelease(project, tag, opts)
68+
if err != nil {
69+
return nil, err
70+
}
71+
return release, nil
72+
}

cmd/new_release_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 TestNewRelease(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 release",
37+
flagsMap: map[string]string{
38+
"project": "Group1/project1",
39+
"tag": "sample_1.0",
40+
},
41+
args: []string{"Amazing"},
42+
expect: pass,
43+
},
44+
{
45+
name: "arg must be required",
46+
flagsMap: map[string]string{
47+
"project": "Group1/project1",
48+
"tag": "sample_1.0",
49+
},
50+
expect: fail,
51+
},
52+
{
53+
name: "flags must be required",
54+
args: []string{"Amazing"},
55+
expect: fail,
56+
},
57+
}
58+
59+
for _, tc := range tt {
60+
t.Run(tc.name, func(t *testing.T) {
61+
execT := execTestCmdFlags{
62+
t: t,
63+
cmd: newReleaseCmd,
64+
flagsMap: tc.flagsMap,
65+
args: tc.args,
66+
}
67+
stdout, execResult := execT.executeCommand()
68+
fmt.Println(stdout)
69+
assertEqualResult(t, execResult, tc.expect, printFlagsTable(tc.flagsMap, stdout))
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)