Skip to content

Commit 1dee09a

Browse files
John Frederickbzon
authored andcommitted
Add edit release command, reorganize new release command
1 parent cb80ab7 commit 1dee09a

File tree

7 files changed

+219
-22
lines changed

7 files changed

+219
-22
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ before_install:
2020
jq --version
2121
2222
ctr=0
23-
max_retries=30
23+
max_retries=50
2424
while [ $(curl -I -u "root:123qwe123" http://localhost:10080/users/sign_in | head -1 | grep 200 | wc -l | tr -d ' ') -ne 1 ]; do
2525
ctr=`expr $ctr + 1`
2626
echo waiting for gitlab to come up

cmd/edit_release.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
"github.com/spf13/cobra"
25+
gitlab "github.com/xanzy/go-gitlab"
26+
)
27+
28+
var editReleaseCmd = &cobra.Command{
29+
Use: "release",
30+
Aliases: []string{"r"},
31+
Short: "Update the release note of a project's release",
32+
Example: `gitlabctl edit release v1.0 --project=groupx/myapp --description="Updated Release Note"`,
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 runEditRelease(cmd, args[0])
39+
},
40+
}
41+
42+
func init() {
43+
editCmd.AddCommand(editReleaseCmd)
44+
addProjectFlag(editReleaseCmd)
45+
verifyMarkFlagRequired(editReleaseCmd, "project")
46+
editReleaseCmd.Flags().StringP("description", "d", "", "Release note")
47+
verifyMarkFlagRequired(editReleaseCmd, "description")
48+
}
49+
50+
func runEditRelease(cmd *cobra.Command, tag string) error {
51+
opts := new(gitlab.UpdateReleaseOptions)
52+
opts.Description = gitlab.String(getFlagString(cmd, "description"))
53+
updatedRelease, err := editRelease(getFlagString(cmd, "project"), tag, opts)
54+
if err != nil {
55+
return err
56+
}
57+
printReleasesOut(getFlagString(cmd, "out"), updatedRelease)
58+
return nil
59+
}
60+
61+
func editRelease(project string, tag string, opts *gitlab.UpdateReleaseOptions) (*gitlab.Release, error) {
62+
git, err := newGitlabClient()
63+
if err != nil {
64+
return nil, err
65+
}
66+
release, _, err := git.Tags.UpdateRelease(project, tag, opts)
67+
if err != nil {
68+
return nil, err
69+
}
70+
return release, nil
71+
}

cmd/edit_release_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
gitlab "github.com/xanzy/go-gitlab"
28+
)
29+
30+
func TestEditRelease(t *testing.T) {
31+
tt := []struct {
32+
name string
33+
flagsMap map[string]string
34+
args []string
35+
expect testResult
36+
}{
37+
{
38+
name: "no flags should fail",
39+
args: []string{"sample_1.0"},
40+
expect: fail,
41+
},
42+
{
43+
name: "edit a release successfully",
44+
flagsMap: map[string]string{
45+
"project": "Group1/project1",
46+
"description": "Updated",
47+
},
48+
args: []string{"sample_1.0"},
49+
expect: pass,
50+
},
51+
{
52+
name: "no arg should fail",
53+
flagsMap: map[string]string{
54+
"project": "Group1/project1",
55+
"description": "Updated",
56+
},
57+
expect: fail,
58+
},
59+
}
60+
61+
for _, tc := range tt {
62+
t.Run(tc.name, func(t *testing.T) {
63+
// SETUP
64+
// Create the release before editing
65+
if tc.expect == pass {
66+
_, err := newRelease(
67+
tc.flagsMap["project"],
68+
tc.args[0],
69+
&gitlab.CreateReleaseOptions{
70+
Description: gitlab.String("A release"),
71+
},
72+
)
73+
tInfo(err)
74+
}
75+
execT := execTestCmdFlags{
76+
t: t,
77+
cmd: editReleaseCmd,
78+
flagsMap: tc.flagsMap,
79+
args: tc.args,
80+
}
81+
stdout, execResult := execT.executeCommand()
82+
fmt.Println(stdout)
83+
assertEqualResult(t, execResult, tc.expect, printFlagsTable(tc.flagsMap, stdout))
84+
})
85+
}
86+
}

cmd/new_release.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ import (
2727
var newReleaseCmd = &cobra.Command{
2828
Use: "release",
2929
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`,
30+
Short: "Create a new release for the specified project's tag",
31+
Example: `# ensure to create the tag where the release will be created from
32+
gitlabctl new tag v1.0 --ref=master --project=groupx/myapp
33+
34+
# create the release
35+
gitlabctl new release v1.0 --project=groupx/myapp --description="Sample Release Note"`,
3336
SilenceErrors: true,
3437
SilenceUsage: true,
3538
DisableAutoGenTag: true,
@@ -43,15 +46,15 @@ func init() {
4346
newCmd.AddCommand(newReleaseCmd)
4447
addProjectFlag(newReleaseCmd)
4548
verifyMarkFlagRequired(newReleaseCmd, "project")
46-
newReleaseCmd.Flags().StringP("tag", "t", "",
47-
"The name of a tag")
48-
verifyMarkFlagRequired(newReleaseCmd, "tag")
49+
newReleaseCmd.Flags().StringP("description", "d", "",
50+
"The release note or description")
51+
verifyMarkFlagRequired(newReleaseCmd, "description")
4952
}
5053

51-
func runNewRelease(cmd *cobra.Command, description string) error {
54+
func runNewRelease(cmd *cobra.Command, tag string) error {
5255
opts := new(gitlab.CreateReleaseOptions)
53-
opts.Description = gitlab.String(description)
54-
createdRelease, err := newRelease(getFlagString(cmd, "project"), getFlagString(cmd, "tag"), opts)
56+
opts.Description = gitlab.String(getFlagString(cmd, "description"))
57+
createdRelease, err := newRelease(getFlagString(cmd, "project"), tag, opts)
5558
if err != nil {
5659
return err
5760
}

cmd/new_release_test.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ package cmd
2222

2323
import (
2424
"fmt"
25+
"strings"
2526
"testing"
27+
28+
gitlab "github.com/xanzy/go-gitlab"
2629
)
2730

2831
func TestNewRelease(t *testing.T) {
@@ -33,30 +36,47 @@ func TestNewRelease(t *testing.T) {
3336
expect testResult
3437
}{
3538
{
36-
name: "create a new release",
39+
name: "no flags should fail",
40+
args: []string{"sample_1.0"},
41+
expect: fail,
42+
},
43+
{
44+
name: "create a new release successfully",
3745
flagsMap: map[string]string{
38-
"project": "Group1/project1",
39-
"tag": "sample_1.0",
46+
"project": "Group1/project1",
47+
"description": "Sample",
4048
},
41-
args: []string{"Amazing"},
49+
args: []string{"v2.0-alpha"},
4250
expect: pass,
4351
},
4452
{
45-
name: "arg must be required",
53+
name: "no arg should fail",
4654
flagsMap: map[string]string{
47-
"project": "Group1/project1",
48-
"tag": "sample_1.0",
55+
"project": "Group1/project1",
56+
"description": "Sample",
4957
},
5058
expect: fail,
5159
},
52-
{
53-
name: "flags must be required",
54-
args: []string{"Amazing"},
55-
expect: fail,
56-
},
5760
}
5861

5962
for _, tc := range tt {
63+
// SETUP
64+
if tc.expect == pass {
65+
// Ensure to delete the tag/release
66+
err := deleteTag(tc.flagsMap["project"], tc.args[0])
67+
tInfo(err)
68+
// Ensure to create a tag for the release
69+
_, err = newTag(tc.flagsMap["project"],
70+
&gitlab.CreateTagOptions{
71+
TagName: gitlab.String(tc.args[0]),
72+
Ref: gitlab.String("master"),
73+
})
74+
if err != nil {
75+
if !strings.Contains(err.Error(), "message: Release already exists") {
76+
t.Fatalf("Can't create test data: %v\n", err)
77+
}
78+
}
79+
}
6080
t.Run(tc.name, func(t *testing.T) {
6181
execT := execTestCmdFlags{
6282
t: t,

cmd/new_tag.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ func init() {
4646
newTagCmd.Flags().StringP("ref", "r", "",
4747
"The branch name or commit SHA to create branch from")
4848
verifyMarkFlagRequired(newTagCmd, "ref")
49+
newTagCmd.Flags().StringP("message", "m", "",
50+
"Creates annotated tag")
51+
newTagCmd.Flags().StringP("description", "d", "",
52+
"Add release notes to the git tag")
4953
}
5054

5155
func runNewTag(cmd *cobra.Command, tag string) error {
5256
opts := new(gitlab.CreateTagOptions)
5357
opts.Ref = gitlab.String(getFlagString(cmd, "ref"))
5458
opts.TagName = gitlab.String(tag)
59+
opts.Message = gitlab.String(getFlagString(cmd, "message"))
60+
opts.ReleaseDescription = gitlab.String(getFlagString(cmd, "description"))
5561
createdTag, err := newTag(getFlagString(cmd, "project"), opts)
5662
if err != nil {
5763
return err

cmd/new_tag_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ func TestNewTag(t *testing.T) {
4141
args: []string{"v2.0"},
4242
expect: pass,
4343
},
44+
{
45+
name: "create a new tag with release note",
46+
flagsMap: map[string]string{
47+
"project": "Group1/project1",
48+
"ref": "master",
49+
"message": "v2.0",
50+
"description": "Sample Release Note",
51+
},
52+
args: []string{"v2.0"},
53+
expect: pass,
54+
},
4455
{
4556
name: "flags must be required",
4657
args: []string{"v3.0"},

0 commit comments

Comments
 (0)