Skip to content

Commit 75bfefa

Browse files
committed
Add delete branch command
1 parent 0aec63f commit 75bfefa

3 files changed

Lines changed: 176 additions & 0 deletions

File tree

cmd/delete_branch.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
26+
"github.com/spf13/cobra"
27+
)
28+
29+
var deleteBranchCmd = &cobra.Command{
30+
Use: "branch",
31+
Aliases: []string{"b"},
32+
Short: "Delete a project branch",
33+
Example: `# delete a develop branch from project groupx/myapp
34+
gitlabctl delete branch develop --project=groupx/myapp`,
35+
SilenceErrors: true,
36+
SilenceUsage: true,
37+
Args: cobra.ExactArgs(1),
38+
RunE: func(cmd *cobra.Command, args []string) error {
39+
return deleteBranch(getFlagString(cmd, "project"), args[0])
40+
},
41+
}
42+
43+
func init() {
44+
deleteCmd.AddCommand(deleteBranchCmd)
45+
addProjectFlag(deleteBranchCmd)
46+
verifyMarkFlagRequired(deleteBranchCmd, "project")
47+
}
48+
49+
func deleteBranch(project, branch string) error {
50+
git, err := newGitlabClient()
51+
if err != nil {
52+
return err
53+
}
54+
_, err = git.Branches.DeleteBranch(project, branch)
55+
if err != nil {
56+
return err
57+
}
58+
fmt.Printf("Branch (%s) from project (%s) has been deleted\n", branch, project)
59+
return nil
60+
}

cmd/delete_branch_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 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+
"testing"
24+
25+
"github.com/stretchr/testify/require"
26+
gitlab "github.com/xanzy/go-gitlab"
27+
)
28+
29+
func TestDeleteBranchCmd(t *testing.T) {
30+
tt := []struct {
31+
name string
32+
flagsMap map[string]string
33+
args []string
34+
expect testResult
35+
}{
36+
{
37+
name: "delete an existent branch",
38+
flagsMap: map[string]string{
39+
"project": "Group2/project12",
40+
},
41+
args: []string{"feature-x"},
42+
expect: pass,
43+
},
44+
{
45+
name: "delete a non existent branch fails",
46+
flagsMap: map[string]string{
47+
"project": "Group2/project12",
48+
},
49+
args: []string{"nil-branch"},
50+
expect: fail,
51+
},
52+
}
53+
54+
for _, tc := range tt {
55+
// SETUP
56+
// Create branch before deleting
57+
if tc.expect == pass {
58+
if _, err := newBranch(tc.flagsMap["project"],
59+
&gitlab.CreateBranchOptions{
60+
Branch: gitlab.String(tc.args[0]),
61+
Ref: gitlab.String("master"),
62+
}); err != nil {
63+
tInfo(err)
64+
}
65+
}
66+
t.Run(tc.name, func(t *testing.T) {
67+
execT := execTestCmdFlags{
68+
t: t,
69+
cmd: deleteBranchCmd,
70+
flagsMap: tc.flagsMap,
71+
args: tc.args,
72+
}
73+
stdout, execResult := execT.executeCommand()
74+
require.Equal(t, tc.expect, execResult,
75+
printFlagsTable(tc.flagsMap, stdout))
76+
})
77+
}
78+
79+
}

docs/gitlabctl_delete_branch.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## gitlabctl delete branch
2+
3+
Delete a project branch
4+
5+
### Synopsis
6+
7+
Delete a project branch
8+
9+
```
10+
gitlabctl delete branch [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
# delete a develop branch from project groupx/myapp
17+
gitlabctl delete branch develop --project=groupx/myapp
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help help for branch
24+
-p, --project string The name or ID of the project
25+
```
26+
27+
### Options inherited from parent commands
28+
29+
```
30+
--config string config file (default is $HOME/.gitlabctl.yaml)
31+
```
32+
33+
### SEE ALSO
34+
35+
* [gitlabctl delete](gitlabctl_delete.md) - Delete a Gitlab resource
36+
37+
###### Auto generated by spf13/cobra on 17-Jun-2018

0 commit comments

Comments
 (0)