Skip to content

Commit 137021e

Browse files
John Frederickbzon
authored andcommitted
Add delete project hook command
1 parent 5a84cd3 commit 137021e

5 files changed

Lines changed: 201 additions & 38 deletions

File tree

cmd/delete_project_hook.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
"strconv"
26+
27+
"github.com/spf13/cobra"
28+
)
29+
30+
var deleteProjectHookCmd = &cobra.Command{
31+
Use: "project-hook",
32+
Aliases: []string{"h"},
33+
SuggestFor: []string{"hook"},
34+
Short: "Delete a Gitlab project hook by specifying the project's full path or id",
35+
Example: `# delete a project hook by project's path
36+
gitlabctl delete project-hook 1 --project=GroupX/ProjectX
37+
38+
# delete a project hook by project id
39+
gitlabctl delete project-hook 2 --project=22`,
40+
Args: cobra.ExactArgs(1),
41+
SilenceErrors: true,
42+
SilenceUsage: true,
43+
RunE: func(cmd *cobra.Command, args []string) error {
44+
return deleteProjectHook(args[0], getFlagString(cmd, "project"))
45+
},
46+
}
47+
48+
func init() {
49+
deleteCmd.AddCommand(deleteProjectHookCmd)
50+
addProjectFlag(deleteProjectHookCmd)
51+
verifyMarkFlagRequired(deleteProjectHookCmd, "project")
52+
}
53+
54+
func deleteProjectHook(hook string, project string) error {
55+
git, err := newGitlabClient()
56+
if err != nil {
57+
return err
58+
}
59+
hid, err := strconv.Atoi(hook)
60+
if err != nil {
61+
return err
62+
}
63+
projectInfo, _, err := git.Projects.GetProject(project)
64+
if err != nil {
65+
return err
66+
}
67+
_, err = git.Projects.DeleteProjectHook(projectInfo.ID, hid)
68+
if err != nil {
69+
return err
70+
}
71+
fmt.Printf("project hook with id (%d) from (%s) has been deleted\n", hid, projectInfo.PathWithNamespace)
72+
return nil
73+
}

cmd/delete_project_hook_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
gitlab "github.com/xanzy/go-gitlab"
29+
)
30+
31+
func TestDeleteProjectHook(t *testing.T) {
32+
tt := []struct {
33+
name string
34+
args []string
35+
flagsMap map[string]string
36+
expect testResult
37+
}{
38+
{
39+
name: "successfully delete a project hook",
40+
args: []string{"6"},
41+
flagsMap: map[string]string{
42+
"project": "23",
43+
},
44+
expect: pass,
45+
},
46+
{
47+
name: "deleting a non existent project hook should fail",
48+
args: []string{"55"},
49+
flagsMap: map[string]string{
50+
"project": "23",
51+
},
52+
expect: fail,
53+
},
54+
}
55+
56+
for _, tc := range tt {
57+
t.Run(tc.name, func(t *testing.T) {
58+
if tc.expect == pass {
59+
sampleHookURL := "http://example.com/"
60+
_, err := newProjectHook("23", &gitlab.AddProjectHookOptions{
61+
URL: &sampleHookURL,
62+
})
63+
if err != nil {
64+
tInfo(fmt.Sprintf("failed to create project hook %s", tc.args[0]))
65+
}
66+
}
67+
68+
execT := execTestCmdFlags{
69+
t: t,
70+
cmd: deleteProjectHookCmd,
71+
args: tc.args,
72+
flagsMap: tc.flagsMap,
73+
}
74+
stdout, execResult := execT.executeCommand()
75+
require.Equal(t, tc.expect, execResult, stdout)
76+
77+
// NOTE: assert positive test
78+
// Validate the command output
79+
if tc.expect == pass {
80+
require.Contains(t, stdout, "has been deleted")
81+
}
82+
})
83+
}
84+
}

cmd/edit_project_hook.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package cmd
2222

2323
import (
24+
"strconv"
25+
2426
"github.com/spf13/cobra"
2527
gitlab "github.com/xanzy/go-gitlab"
2628
)
@@ -30,12 +32,12 @@ var editProjectHookCmd = &cobra.Command{
3032
Aliases: []string{"h"},
3133
SuggestFor: []string{"hook"},
3234
Short: "Edit a project hook by specifying the project id or path and using flags for fields to modify",
33-
Example: `# update a project hook by path
34-
gitlabctl edit project-hook ProjectX --hook-id=1 --url="http://www.sample123.com/"
35-
gitlabctl edit project-hook GroupX/ProjectX --hook-id=2 --tag-push-events=false
35+
Example: `# update a project hook by project path
36+
gitlabctl edit project-hook 1 --project=ProjectX --url="http://www.sample123.com/"
37+
gitlabctl edit project-hook 2 --project=GroupX/ProjectX --tag-push-events=false
3638
37-
# update a project hook with id
38-
gitlabctl edit project-hook 3 --hook-id=3 --url="http://www.sample321.com/" --issues-events`,
39+
# update a project hook by project id
40+
gitlabctl edit project-hook 3 --project=3 --url="http://www.sample321.com/" --issues-events`,
3941
Args: cobra.ExactArgs(1),
4042
SilenceErrors: true,
4143
SilenceUsage: true,
@@ -49,13 +51,17 @@ func init() {
4951
addEditProjectHookFlags(editProjectHookCmd)
5052
}
5153

52-
func runEditProjectHook(cmd *cobra.Command, project string) error {
54+
func runEditProjectHook(cmd *cobra.Command, hook string) error {
5355
opts, err := assignEditProjectHookOptions(cmd)
5456
if err != nil {
5557
return err
5658
}
57-
hook := getFlagInt(cmd, "hook-id")
58-
editedProjectHook, err := editProjectHook(project, hook, (*gitlab.EditProjectHookOptions)(opts))
59+
hid, err := strconv.Atoi(hook)
60+
if err != nil {
61+
return err
62+
}
63+
project := getFlagString(cmd, "project")
64+
editedProjectHook, err := editProjectHook(project, hid, (*gitlab.EditProjectHookOptions)(opts))
5965
if err != nil {
6066
return err
6167
}

cmd/edit_project_hook_test.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,41 @@ func TestEditProjectHook(t *testing.T) {
3535
{
3636
name: "edit an existing project hook by project path",
3737
flagsMap: map[string]string{
38-
"hook-id": "4",
39-
"url": "http://example1.com/",
40-
"push-events": "false",
41-
"issues-events": "false",
42-
"confidential-issues-events": "true",
43-
"merge-requests-events": "false",
44-
"tag-push-events": "false",
45-
"note-events": "true",
46-
"job-events": "true",
47-
"pipeline-events": "true",
48-
"wiki-page-events": "true",
49-
"enable-ssl-verification": "true",
50-
"token": "gAWLwCxxK5ss0z6PzgpS",
38+
"project": "frank.watson/project22",
39+
"url": "http://example1.com/",
40+
"push-events": "false",
41+
"issues-events": "false",
42+
"confidential-issues-events": "true",
43+
"merge-requests-events": "false",
44+
"tag-push-events": "false",
45+
"note-events": "true",
46+
"job-events": "true",
47+
"pipeline-events": "true",
48+
"wiki-page-events": "true",
49+
"enable-ssl-verification": "true",
50+
"token": "gAWLwCxxK5ss0z6PzgpS",
5151
},
52-
args: []string{"frank.watson/project22"},
52+
args: []string{"4"},
5353
expect: pass,
5454
},
5555
{
5656
name: "edit an existing project hook by project id",
5757
flagsMap: map[string]string{
58-
"hook-id": "5",
59-
"url": "http://example2.com/",
60-
"push-events": "false",
61-
"issues-events": "false",
62-
"confidential-issues-events": "true",
63-
"merge-requests-events": "false",
64-
"tag-push-events": "false",
65-
"note-events": "true",
66-
"job-events": "true",
67-
"pipeline-events": "true",
68-
"wiki-page-events": "true",
69-
"enable-ssl-verification": "true",
70-
"token": "EKdYF9AvxRWKlch0XBKO",
58+
"project": "23",
59+
"url": "http://example2.com/",
60+
"push-events": "false",
61+
"issues-events": "false",
62+
"confidential-issues-events": "true",
63+
"merge-requests-events": "false",
64+
"tag-push-events": "false",
65+
"note-events": "true",
66+
"job-events": "true",
67+
"pipeline-events": "true",
68+
"wiki-page-events": "true",
69+
"enable-ssl-verification": "true",
70+
"token": "EKdYF9AvxRWKlch0XBKO",
7171
},
72-
args: []string{"23"},
72+
args: []string{"5"},
7373
expect: pass,
7474
},
7575
}

cmd/flags_definers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ func addNewProjectHookFlags(cmd *cobra.Command) {
213213
// addEditProjectHookFlags add the required flags for editing a project hook
214214
// Flag usage reference: https://docs.gitlab.com/ce/api/projects.html#edit-project-hook
215215
func addEditProjectHookFlags(cmd *cobra.Command) {
216-
cmd.Flags().Int("hook-id", 0, "The hook ID")
216+
addProjectFlag(cmd)
217217
addNewProjectHookEditProjectHookFlags(cmd)
218218
verifyMarkFlagRequired(cmd, "url")
219-
verifyMarkFlagRequired(cmd, "hook-id")
219+
verifyMarkFlagRequired(cmd, "project")
220220
}
221221

222222
func addNewProjectHookEditProjectHookFlags(cmd *cobra.Command) {

0 commit comments

Comments
 (0)