Skip to content

Commit 25220e8

Browse files
John Frederickbzon
authored andcommitted
Add get tags command
1 parent 4a04fea commit 25220e8

5 files changed

Lines changed: 202 additions & 13 deletions

File tree

cmd/get_tags.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 getTagsCmd = &cobra.Command{
29+
Use: "tags",
30+
Short: "List all tags of a repository",
31+
Aliases: []string{"t"},
32+
Example: "gitlabctl get tags devopsctl/gitlabctl",
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 runGetTags(cmd, args[0])
39+
},
40+
}
41+
42+
func init() {
43+
getCmd.AddCommand(getTagsCmd)
44+
}
45+
46+
func runGetTags(cmd *cobra.Command, project string) error {
47+
opts := new(gitlab.ListTagsOptions)
48+
tags, err := getTags(project, opts)
49+
if err != nil {
50+
return err
51+
}
52+
printTagsOut(getFlagString(cmd, "out"), tags...)
53+
return nil
54+
}
55+
56+
func getTags(project string, opts *gitlab.ListTagsOptions) ([]*gitlab.Tag, error) {
57+
git, err := newGitlabClient()
58+
if err != nil {
59+
return nil, err
60+
}
61+
tags, _, err := git.Tags.ListTags(project, opts)
62+
if err != nil {
63+
return nil, err
64+
}
65+
return tags, nil
66+
}

cmd/get_tags_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
"github.com/stretchr/testify/require"
28+
)
29+
30+
func TestGetTags(t *testing.T) {
31+
tt := []struct {
32+
name string
33+
flagsMap map[string]string
34+
args []string
35+
expect testResult
36+
}{
37+
{
38+
name: "list all tags by specifying project's id",
39+
args: []string{"1"},
40+
expect: pass,
41+
},
42+
{
43+
name: "list all tags by specifying project's path",
44+
args: []string{"Group1/project1"},
45+
expect: pass,
46+
},
47+
{
48+
name: "list all tags and print in json",
49+
flagsMap: map[string]string{
50+
"out": "json",
51+
},
52+
args: []string{"Group1/project2"},
53+
expect: pass,
54+
},
55+
{
56+
name: "list all tags and print in yaml",
57+
flagsMap: map[string]string{
58+
"out": "yaml",
59+
},
60+
args: []string{"Group1/project3"},
61+
expect: pass,
62+
},
63+
{
64+
name: "list non existing project tags",
65+
args: []string{"4"},
66+
expect: pass,
67+
},
68+
{
69+
name: "no project id or path as argument should fail",
70+
expect: fail,
71+
},
72+
}
73+
74+
for _, tc := range tt {
75+
t.Run(tc.name, func(t *testing.T) {
76+
execT := execTestCmdFlags{
77+
t: t,
78+
cmd: getTagsCmd,
79+
flagsMap: tc.flagsMap,
80+
args: tc.args,
81+
}
82+
stdout, execResult := execT.executeCommand()
83+
fmt.Println(stdout)
84+
require.Equal(t, tc.expect, execResult,
85+
printFlagsTable(tc.flagsMap, stdout))
86+
})
87+
}
88+
}

cmd/helpers.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,26 @@ func printBranchOut(format string, branches ...*gitlab.Branch) {
347347
printTable(header, rows)
348348
}
349349
}
350+
351+
func printTagsOut(format string, tags ...*gitlab.Tag) {
352+
switch format {
353+
case YAML:
354+
printYAML(tags)
355+
case JSON:
356+
printJSON(tags)
357+
default:
358+
if len(tags) == 0 {
359+
fmt.Println(noResultMsg)
360+
return
361+
}
362+
header := []string{"TAG NAME","COMMIT ID"}
363+
var rows [][]string
364+
for _, v := range tags {
365+
rows = append(rows, []string{
366+
v.Name,
367+
v.Commit.ID,
368+
})
369+
}
370+
printTable(header, rows)
371+
}
372+
}

cmd/helpers_for_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"os"
2929
"strings"
3030
"testing"
31-
31+
3232
"github.com/kyokomi/emoji"
3333
"github.com/olekukonko/tablewriter"
3434
"github.com/spf13/cobra"

testdata/seeder.sh

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ echo "=============================================="
88
access_token=$(curl -X POST "${GITLAB_HTTP_URL}/oauth/token?grant_type=password&username=${GITLAB_USERNAME}&password=${GITLAB_PASSWORD}" | jq '.access_token' | tr -d '"')
99

1010
# create user
11-
echo "Creating a user"
11+
echo "Creating users"
1212
user1_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/users?access_token=${access_token}&name=John+Doe&email=john.doe@gmail.com&username=john.doe&password=123qwe123&skip_confirmation=true" | jq '.id')
1313
user2_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/users?access_token=${access_token}&name=John+Smith&email=john.smith@gmail.com&username=john.smith&password=123qwe123&skip_confirmation=true" | jq '.id')
1414
user3_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/users?access_token=${access_token}&name=Matt+Hunter&email=matt.hunter@gmail.com&username=matt.hunter&password=123qwe123&skip_confirmation=true" | jq '.id')
@@ -22,12 +22,12 @@ user10_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/users?access_token=${access_
2222
user11_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/users?access_token=${access_token}&name=Paul+Lyman&email=paul.lyman@gmail.com&username=paul.lyman&password=123qwe123&skip_confirmation=true" | jq '.id')
2323

2424
# create group
25-
echo "Creating a group"
25+
echo "Creating groups"
2626
pgroup1_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups?access_token=${access_token}&name=Group1&path=Group1" | jq '.id')
2727
pgroup2_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups?access_token=${access_token}&name=Group2&path=Group2" | jq '.id')
2828

2929
# add user to group
30-
echo "Adding user to a group"
30+
echo "Adding users to group"
3131
curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups/${pgroup1_id}/members?access_token=${access_token}&user_id=${user1_id}&access_level=30"
3232
curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups/${pgroup1_id}/members?access_token=${access_token}&user_id=${user2_id}&access_level=40"
3333
curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups/${pgroup1_id}/members?access_token=${access_token}&user_id=${user3_id}&access_level=50"
@@ -36,7 +36,7 @@ curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups/${pgroup2_id}/members?access_toke
3636
curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups/${pgroup2_id}/members?access_token=${access_token}&user_id=${user6_id}&access_level=50"
3737

3838
# create subgroup
39-
echo "Creating a subgroup"
39+
echo "Creating subgroup"
4040
sgroup1_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups?access_token=${access_token}&name=SubGroup1&path=SubGroup1&parent_id=${pgroup1_id}" | jq '.id')
4141
sgroup2_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups?access_token=${access_token}&name=SubGroup2&path=SubGroup2&parent_id=${pgroup1_id}" | jq '.id')
4242
sgroup3_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/groups?access_token=${access_token}&name=SubGroup3&path=SubGroup3&parent_id=${pgroup2_id}" | jq '.id')
@@ -66,14 +66,6 @@ groupproject16_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects?access_toke
6666
groupproject17_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects?access_token=${access_token}&name=Project17&namespace_id=${sgroup4_id}" | jq '.id')
6767
groupproject18_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects?access_token=${access_token}&name=Project18&namespace_id=${sgroup4_id}" | jq '.id')
6868

69-
# create user project
70-
echo "Creating users project"
71-
project1_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user7_id}?access_token=${access_token}&name=Project19" | jq '.id')
72-
project2_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user8_id}?access_token=${access_token}&name=Project20" | jq '.id')
73-
project3_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user9_id}?access_token=${access_token}&name=Project21" | jq '.id')
74-
project4_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user10_id}?access_token=${access_token}&name=Project22" | jq '.id')
75-
project5_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user11_id}?access_token=${access_token}&name=Project23" | jq '.id')
76-
7769
# add user to project
7870
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject1_id}/members?access_token=${access_token}&user_id=${user1_id}&access_level=30"
7971
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject1_id}/members?access_token=${access_token}&user_id=${user2_id}&access_level=40"
@@ -84,10 +76,30 @@ curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject2_id}/members?acc
8476
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject2_id}/members?access_token=${access_token}&user_id=${user7_id}&access_level=40"
8577
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject2_id}/members?access_token=${access_token}&user_id=${user8_id}&access_level=40"
8678

79+
# create user project
80+
echo "Creating users project"
81+
project1_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user7_id}?access_token=${access_token}&name=Project19" | jq '.id')
82+
project2_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user8_id}?access_token=${access_token}&name=Project20" | jq '.id')
83+
project3_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user9_id}?access_token=${access_token}&name=Project21" | jq '.id')
84+
project4_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user10_id}?access_token=${access_token}&name=Project22" | jq '.id')
85+
project5_id=$(curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/user/${user11_id}?access_token=${access_token}&name=Project23" | jq '.id')
86+
8787
# create hooks for projects
8888
echo "Creating hooks for projects"
8989
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${project1_id}/hooks?access_token=${access_token}&url=http%3A%2F%2Fwww.sample1.com%2F"
9090
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${project2_id}/hooks?access_token=${access_token}&url=http%3A%2F%2Fwww.sample2.com%2F"
9191
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${project3_id}/hooks?access_token=${access_token}&url=http%3A%2F%2Fwww.sample3.com%2F"
9292
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${project4_id}/hooks?access_token=${access_token}&url=http%3A%2F%2Fwww.sample4.com%2F"
9393
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${project5_id}/hooks?access_token=${access_token}&url=http%3A%2F%2Fwww.sample5.com%2F"
94+
95+
# push some commits in preparation for creation of git tags
96+
echo "Push commits for projects"
97+
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject1_id}/repository/files/README.md?access_token=${access_token}&branch=master&content=Test&commit_message=First+commit"
98+
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject2_id}/repository/files/README.md?access_token=${access_token}&branch=master&content=Test&commit_message=First+commit"
99+
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject3_id}/repository/files/README.md?access_token=${access_token}&branch=master&content=Test&commit_message=First+commit"
100+
101+
# create tags
102+
echo "Creating tags for projects"
103+
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject1_id}/repository/tags?access_token=${access_token}&tag_name=sample_1.0&ref=master"
104+
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject2_id}/repository/tags?access_token=${access_token}&tag_name=sample_1.0&ref=master"
105+
curl -X POST "${GITLAB_HTTP_URL}/api/v4/projects/${groupproject3_id}/repository/tags?access_token=${access_token}&tag_name=sample_1.0&ref=master"

0 commit comments

Comments
 (0)