/
hook.go
208 lines (170 loc) · 5.72 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package pipeline
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/ovh/cds/sdk"
)
func init() {
pipelineHookCmd.AddCommand(pipelineAddHookCmd())
pipelineHookCmd.AddCommand(pipelineDeleteHookCmd())
pipelineHookCmd.AddCommand(pipelineListHookCmd())
}
var pipelineHookCmd = &cobra.Command{
Use: "hook",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
func pipelineAddHookCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "cds pipeline hook add <projectKey> <applicationName> <pipelineName> [<host>/<project>/<slug>]",
Long: ``,
Run: addPipelineHook,
}
return cmd
}
func pipelineDeleteHookCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "cds pipeline hook delete <projectKey> <applicationName> <pipelineName> [<host>/<project>/<slug> if application non connected to a repository] [<idHook> if application connected to a repository]",
Long: ``,
Run: deletePipelineHook,
}
return cmd
}
var showURLOnly bool
func pipelineListHookCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "cds pipeline hook list <projectKey> <applicationName> <pipelineName>",
Long: ``,
Run: listPipelineHook,
}
cmd.Flags().BoolVarP(&showURLOnly, "show-url-only", "", false, "Shows only URL")
return cmd
}
func addPipelineHook(cmd *cobra.Command, args []string) {
if len(args) < 3 {
sdk.Exit("Wrong usage: See %s\n", cmd.Short)
}
pipelineProject := args[0]
appName := args[1]
pipelineName := args[2]
p, err := sdk.GetPipeline(pipelineProject, pipelineName)
if err != nil {
sdk.Exit("✘ Error: Cannot retrieve pipeline %s-%s (%s)\n", pipelineProject, pipelineName, err)
}
a, err := sdk.GetApplication(pipelineProject, appName)
if err != nil {
sdk.Exit("✘ Error: Cannot retrieve application %s-%s (%s)\n", pipelineProject, appName, err)
}
//If the application is attached to a repositories manager, parameter <host>/<project>/<slug> aren't taken in account
if a.RepositoriesManager != nil {
err = sdk.AddHookOnRepositoriesManager(pipelineProject, appName, a.RepositoriesManager.Name, a.RepositoryFullname, pipelineName)
if err != nil {
sdk.Exit("✘ Error: Cannot add hook to pipeline %s-%s-%s (%s)\n", pipelineProject, appName, pipelineName, err)
}
fmt.Println("✔ Success")
} else {
if len(args) != 4 {
sdk.Exit("✘ Error: Your application has to be attached to a repositories manager. Try : cds application reposmanager attach")
}
t := strings.Split(args[3], "/")
if len(t) != 3 {
sdk.Exit("✘ Error: Expected repository like <host>/<project>/<slug>. Got %d elements\n", len(t))
}
h, err := sdk.AddHook(a, p, t[0], t[1], t[2])
if err != nil {
sdk.Exit("✘ Error: Cannot add hook to pipeline %s-%s-%s (%s)\n", pipelineProject, appName, pipelineName, err)
}
if strings.Contains(t[0], "stash") {
fmt.Printf(`Hook created on CDS.
You now need to configure hook on stash. Use "Http Request Post Receive Hook" to create:
POST https://<url-to-cds>/hook?&uid=%s&project=%s&name=%s&branch=${refChange.name}&hash=${refChange.toHash}&message=${refChange.type}&author=${user.name}
`, h.UID, t[1], t[2])
}
}
}
func deletePipelineHook(cmd *cobra.Command, args []string) {
if len(args) < 3 {
sdk.Exit("Wrong usage: See %s\n", cmd.Short)
}
pipelineProject := args[0]
appName := args[1]
pipelineName := args[2]
a, err := sdk.GetApplication(pipelineProject, appName)
if err != nil {
sdk.Exit("Cannot retrieve application %s-%s (%s)\n", pipelineProject, appName, err)
}
//If the application is attached to a repositories manager, parameter <host>/<project>/<slug> aren't taken in account
if a.RepositoriesManager != nil {
if len(args) != 4 {
sdk.Exit("Wrong usage: See %s\n", cmd.Short)
}
hookIDString := args[3]
hookID, err := strconv.ParseInt(hookIDString, 10, 64)
if err != nil {
sdk.Exit("Hook id must be a number (%s)\n", err)
}
err = sdk.DeleteHookOnRepositoriesManager(pipelineProject, appName, a.RepositoriesManager.Name, hookID)
if err != nil {
sdk.Exit("Cannot delete on pipeline %s-%s-%s (%s)\n", pipelineProject, appName, pipelineName, err)
}
fmt.Println("✔ Success")
} else {
t := strings.Split(args[3], "/")
if len(t) != 3 {
sdk.Exit("Expected repository like <host>/<project>/<slug>. Got %d elements\n", len(t))
}
hooks, err := sdk.GetHooks(pipelineProject, appName, pipelineName)
if err != nil {
sdk.Exit("Cannot retrieve hooks from %s/%s/%s (%s)\n", pipelineProject, appName, pipelineName, err)
}
for _, h := range hooks {
if h.Host == t[0] && h.Project == t[1] && h.Repository == t[2] {
err = sdk.DeleteHook(pipelineProject, appName, pipelineName, h.ID)
if err != nil {
sdk.Exit("Cannot delete hook from %s/%s/%s (%s)", pipelineProject, appName, pipelineName, err)
}
return
}
}
}
}
func listPipelineHook(cmd *cobra.Command, args []string) {
if len(args) != 3 {
sdk.Exit("Wrong usage: See %s\n", cmd.Short)
}
pipelineProject := args[0]
appName := args[1]
pipelineName := args[2]
hooks, err := sdk.GetHooks(pipelineProject, appName, pipelineName)
if err != nil {
sdk.Exit("Cannot retrieve hooks from %s/%s/%s (%s)\n", pipelineProject, appName, pipelineName, err)
}
if showURLOnly {
for _, h := range hooks {
fmt.Printf("%s\n", h.Link)
}
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Repository", "URL"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
for _, h := range hooks {
table.Append([]string{
fmt.Sprintf("%d", h.ID),
fmt.Sprintf("%s/%s/%s", h.Host, h.Project, h.Repository),
h.Link,
})
}
table.Render()
}