Skip to content

Commit aff4c1e

Browse files
committed
Add describe branch command
1 parent e079cf0 commit aff4c1e

5 files changed

Lines changed: 174 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Contributors are welcomed with love! Please read [CONTRIBUTING.md](./CONTRIBUTIN
200200
### Project Repository Branch
201201

202202
* [ ] `get branch [flags]`
203-
* [ ] `describe branch [branch name] [flags]`
203+
* [x] `describe branch [branch name] [flags]`
204204
* [x] `new branch [branch name] [flags]`
205205
* [x] `delete branch [branch name] [flags]`
206206
* [ ] `edit branch [branch name] [--protect] [flags]`

cmd/desc_branch.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
)
26+
27+
var descBranchCmd = &cobra.Command{
28+
Use: "branch",
29+
Aliases: []string{"b"},
30+
Short: "Describe a branch of a specified project",
31+
Example: "gitlabctl describe master --project=devopsctl/gitlabctl",
32+
SilenceErrors: true,
33+
SilenceUsage: true,
34+
DisableAutoGenTag: true,
35+
Args: cobra.ExactArgs(1),
36+
PreRunE: func(cmd *cobra.Command, args []string) error {
37+
return nil
38+
},
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
return descBranch(
41+
getFlagString(cmd, "project"),
42+
args[0],
43+
getFlagString(cmd, "out"),
44+
)
45+
},
46+
}
47+
48+
func init() {
49+
descCmd.AddCommand(descBranchCmd)
50+
addProjectFlag(descBranchCmd)
51+
verifyMarkFlagRequired(descBranchCmd, "project")
52+
}
53+
54+
func descBranch(project, branch, out string) error {
55+
git, err := newGitlabClient()
56+
if err != nil {
57+
return err
58+
}
59+
branchInfo, _, err := git.Branches.GetBranch(project, branch)
60+
if err != nil {
61+
return err
62+
}
63+
printBranchOut(out, branchInfo)
64+
return nil
65+
}

cmd/desc_branch_test.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+
"testing"
25+
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func TestDescBranchCmd(t *testing.T) {
30+
tt := []struct {
31+
name string
32+
flagsMap map[string]string
33+
args []string
34+
expect testResult
35+
}{
36+
{
37+
name: "describe project with id 11 master branch",
38+
flagsMap: map[string]string{
39+
"project": "11",
40+
},
41+
args: []string{"master"},
42+
expect: pass,
43+
},
44+
{
45+
name: "describing a non existent branch fails",
46+
flagsMap: map[string]string{
47+
"project": "11",
48+
},
49+
args: []string{"release-99"},
50+
expect: fail,
51+
},
52+
}
53+
54+
for _, tc := range tt {
55+
t.Run(tc.name, func(t *testing.T) {
56+
execT := execTestCmdFlags{
57+
t: t,
58+
cmd: descBranchCmd,
59+
flagsMap: tc.flagsMap,
60+
args: tc.args,
61+
}
62+
stdout, execResult := execT.executeCommand()
63+
require.Equal(t, tc.expect, execResult,
64+
printFlagsTable(tc.flagsMap, stdout))
65+
if tc.expect == pass {
66+
require.Contains(t, stdout, tc.args[0], stdout)
67+
}
68+
})
69+
}
70+
71+
}

docs/gitlabctl_describe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All gitlab resources can be found here https://docs.gitlab.com/ce/api/#resources
2222
### SEE ALSO
2323

2424
* [gitlabctl](gitlabctl.md) - Gitlab command-line interface
25+
* [gitlabctl describe branch](gitlabctl_describe_branch.md) - Describe a branch of a specified project
2526
* [gitlabctl describe group](gitlabctl_describe_group.md) - Describe a group by specifying the id or group path
2627
* [gitlabctl describe member](gitlabctl_describe_member.md) - Describe a member by specifying the username and source
2728
* [gitlabctl describe project](gitlabctl_describe_project.md) - Describe a project by specifying the id or project path

docs/gitlabctl_describe_branch.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## gitlabctl describe branch
2+
3+
Describe a branch of a specified project
4+
5+
### Synopsis
6+
7+
Describe a branch of a specified project
8+
9+
```
10+
gitlabctl describe branch [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
gitlabctl describe master --project=devopsctl/gitlabctl
17+
```
18+
19+
### Options
20+
21+
```
22+
-h, --help help for branch
23+
-p, --project string The name or ID of the project
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
--config string config file (default is $HOME/.gitlabctl.yaml)
30+
-o, --out string Print the command output to the desired format. (json, yaml, simple) (default "simple")
31+
```
32+
33+
### SEE ALSO
34+
35+
* [gitlabctl describe](gitlabctl_describe.md) - Describe a gitlab resource
36+

0 commit comments

Comments
 (0)