/
integration_link.go
405 lines (363 loc) · 14.9 KB
/
integration_link.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package cmd
import (
"errors"
"strconv"
"github.com/AlecAivazis/survey"
"github.com/urfave/cli"
"github.com/Scalingo/cli/appdetect"
"github.com/Scalingo/cli/cmd/autocomplete"
"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/integrationlink"
"github.com/Scalingo/cli/scmintegrations"
"github.com/Scalingo/go-scalingo"
)
var (
integrationLinkShowCommand = cli.Command{
Name: "integration-link",
Category: "Integration Link",
Usage: "Show integration link of your app",
Flags: []cli.Flag{appFlag},
Description: ` Show integration link of your app:
$ scalingo --app my-app integration-link
# See also 'integration-link-create', 'integration-link-update', 'integration-link-delete', 'integration-link-manual-deploy', 'integration-link-manual-review-app'`,
Action: func(c *cli.Context) {
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "integration-link")
return
}
currentApp := appdetect.CurrentApp(c)
err := integrationlink.Show(currentApp)
if err != nil {
errorQuit(err)
}
},
BashComplete: func(c *cli.Context) {
_ = autocomplete.CmdFlagsAutoComplete(c, "integration-link")
},
}
integrationLinkCreateCommand = cli.Command{
Name: "integration-link-create",
Category: "Integration Link",
Flags: []cli.Flag{
appFlag,
cli.StringFlag{Name: "branch", Usage: "Branch used in auto deploy"},
cli.BoolFlag{Name: "auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
cli.BoolFlag{Name: "deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
cli.BoolFlag{Name: "destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
cli.BoolFlag{Name: "no-auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
cli.BoolFlag{Name: "no-deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
cli.BoolFlag{Name: "no-destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
cli.UintFlag{Name: "hours-before-destroy-on-close", Usage: "Time delay before auto destroying a review app when pull request is closed"},
cli.BoolFlag{Name: "destroy-on-stale", Usage: "Auto destroy review apps when no deploy/commits has happened"},
cli.BoolFlag{Name: "no-destroy-on-stale", Usage: "Auto destroy review apps when no deploy/commits has happened"},
cli.UintFlag{Name: "hours-before-destroy-on-stale", Usage: "Time delay before auto destroying a review app when no deploy/commits has happened"},
},
Usage: "Link your Scalingo application to an integration",
Description: ` Link your Scalingo application to an integration:
$ scalingo --app my-app integration-link-create <repository URL> [options]
OR
$ scalingo --app my-app integration-link-create <repository URL> [options]
List of available integrations:
- github => GitHub.com
- github-enterprise => GitHub Enterprise (private instance)
- gitlab => GitLab.com
- gitlab-self-hosted => GitLab Self-hosted (private instance)
Examples:
$ scalingo --app my-app integration-link-create https://gitlab.com/gitlab-org/gitlab-ce
$ scalingo --app my-app integration-link-create https://ghe.example.org/test/frontend-app --branch master --auto-deploy
# See also 'integration-link', 'integration-link-update', 'integration-link-delete', 'integration-link-manual-deploy' and 'integration-link-manual-review-app'`,
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
cli.ShowCommandHelp(c, "integration-link-create")
return
}
currentApp := appdetect.CurrentApp(c)
integrationURL := c.Args()[0]
integrationType, err := scmintegrations.GetTypeFromURL(integrationURL)
if err != nil {
errorQuit(err)
}
var params scalingo.SCMRepoLinkCreateParams
if c.NumFlags() == 0 {
params, err = interactiveCreate()
if err != nil {
errorQuit(err)
}
} else {
branch := c.String("branch")
autoDeploy := c.Bool("auto-deploy")
noAutoDeploy := c.Bool("no-auto-deploy")
if autoDeploy && noAutoDeploy {
errorQuit(errors.New("cannot define both auto-deploy and no-auto-deploy"))
}
deployReviewApps := c.Bool("deploy-review-apps")
noDeployReviewApps := c.Bool("no-deploy-review-apps")
if deployReviewApps && noDeployReviewApps {
errorQuit(errors.New("cannot define both deploy-review-apps and no-deploy-review-apps"))
}
destroyOnClose := c.Bool("destroy-on-close")
noDestroyOnClose := c.Bool("no-destroy-on-close")
if destroyOnClose && noDestroyOnClose {
errorQuit(errors.New("cannot define both destroy-on-close and no-destroy-on-close"))
}
hoursBeforeDestroyOnClose := c.Uint("hours-before-destroy-on-close")
destroyOnStale := c.Bool("destroy-on-stale")
noDestroyOnStale := c.Bool("no-destroy-on-stale")
if destroyOnStale && noDestroyOnStale {
errorQuit(errors.New("cannot define both destroy-on-stale and no-destroy-on-stale"))
}
hoursBeforeDestroyOnStale := c.Uint("hours-before-destroy-on-stale")
params = scalingo.SCMRepoLinkCreateParams{
Branch: &branch,
AutoDeployEnabled: &autoDeploy,
DeployReviewAppsEnabled: &deployReviewApps,
DestroyOnCloseEnabled: &destroyOnClose,
HoursBeforeDeleteOnClose: &hoursBeforeDestroyOnClose,
DestroyStaleEnabled: &destroyOnStale,
HoursBeforeDeleteStale: &hoursBeforeDestroyOnStale,
}
}
err = integrationlink.Create(currentApp, integrationType, integrationURL, params)
if err != nil {
errorQuit(err)
}
},
BashComplete: func(c *cli.Context) {
_ = autocomplete.CmdFlagsAutoComplete(c, "integration-link-create")
},
}
integrationLinkUpdateCommand = cli.Command{
Name: "integration-link-update",
Category: "Integration Link",
Flags: []cli.Flag{
appFlag,
cli.StringFlag{Name: "branch", Usage: "Branch used in auto deploy"},
cli.BoolFlag{Name: "auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
cli.BoolFlag{Name: "no-auto-deploy", Usage: "Enable auto deploy of application after each branch change"},
cli.BoolFlag{Name: "deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
cli.BoolFlag{Name: "no-deploy-review-apps", Usage: "Enable auto deploy of review app when new pull request is opened"},
cli.BoolFlag{Name: "destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
cli.BoolFlag{Name: "no-destroy-on-close", Usage: "Auto destroy review apps when pull request is closed"},
cli.UintFlag{Name: "hours-before-destroy-on-close", Usage: "Time delay before auto destroying a review app when pull request is closed"},
cli.BoolFlag{Name: "destroy-on-stale", Usage: "Auto destroy review apps when no deploy/commits has happened"},
cli.BoolFlag{Name: "no-destroy-on-stale", Usage: "Auto destroy review apps when no deploy/commits has happened"},
cli.StringFlag{Name: "hours-before-destroy-on-stale", Usage: "Time delay before auto destroying a review app when no deploy/commits has happened"},
},
Usage: "Update the integration link parameters",
Description: ` Update the integration link parameters:
$ scalingo --app my-app integration-link-update [options]
Examples:
$ scalingo --app my-app integration-link-update --branch master
$ scalingo --app my-app integration-link-update --auto-deploy --branch test --deploy-review-apps
$ scalingo --app my-app integration-link-update --destroy-on-close --hours-before-destroy-on-close 1
$ scalingo --app my-app integration-link-update --destroy-on-stale --hours-before-destroy-on-stale 2
# See also 'integration-link', 'integration-link-create', 'integration-link-delete', 'integration-link-manual-deploy' and 'integration-link-manual-review-app'`,
Action: func(c *cli.Context) {
if c.NumFlags() == 0 || len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "integration-link-update")
return
}
autoDeploy := c.Bool("auto-deploy")
noAutoDeploy := c.Bool("no-auto-deploy")
if autoDeploy && noAutoDeploy {
errorQuit(errors.New("cannot define both auto-deploy and no-auto-deploy"))
}
deployReviewApps := c.Bool("deploy-review-apps")
noDeployReviewApps := c.Bool("no-deploy-review-apps")
if deployReviewApps && noDeployReviewApps {
errorQuit(errors.New("cannot define both deploy-review-apps and no-deploy-review-apps"))
}
destroyOnClose := c.Bool("destroy-on-close")
noDestroyOnClose := c.Bool("no-destroy-on-close")
if destroyOnClose && noDestroyOnClose {
errorQuit(errors.New("cannot define both destroy-on-close and no-destroy-on-close"))
}
destroyOnStale := c.Bool("destroy-on-stale")
noDestroyOnStale := c.Bool("no-destroy-on-stale")
if destroyOnStale && noDestroyOnStale {
errorQuit(errors.New("cannot define both destroy-on-stale and no-destroy-on-stale"))
}
currentApp := appdetect.CurrentApp(c)
params, err := integrationlink.CheckAndFillParams(c, currentApp)
if err != nil {
errorQuit(err)
}
err = integrationlink.Update(currentApp, *params)
if err != nil {
errorQuit(err)
}
},
BashComplete: func(c *cli.Context) {
_ = autocomplete.CmdFlagsAutoComplete(c, "integration-link-update")
},
}
integrationLinkDeleteCommand = cli.Command{
Name: "integration-link-delete",
Category: "Integration Link",
Flags: []cli.Flag{appFlag},
Usage: "Delete the link between your Scalingo application and the integration",
Description: `Delete the link between your Scalingo application and the integration:
$ scalingo --app my-app integration-link-delete
# See also 'integration-link', 'integration-link-create', 'integration-link-update', 'integration-link-manual-deploy' and 'integration-link-manual-review-app'`,
Action: func(c *cli.Context) {
if len(c.Args()) != 0 {
cli.ShowCommandHelp(c, "integration-link-delete")
return
}
currentApp := appdetect.CurrentApp(c)
err := integrationlink.Delete(currentApp)
if err != nil {
errorQuit(err)
}
},
BashComplete: func(c *cli.Context) {
_ = autocomplete.CmdFlagsAutoComplete(c, "integration-link-delete")
},
}
integrationLinkManualDeployCommand = cli.Command{
Name: "integration-link-manual-deploy",
Category: "Integration Link",
Flags: []cli.Flag{appFlag},
Usage: "Trigger a manual deployment of the specified branch",
Description: `Trigger a manual deployment of the specified branch:
$ scalingo --app my-app integration-link-manual-deploy mybranch
# See also 'integration-link', 'integration-link-create', 'integration-link-update', 'integration-link-delete' and 'integration-link-manual-review-app'`,
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
cli.ShowCommandHelp(c, "integration-link-manual-deploy")
return
}
currentApp := appdetect.CurrentApp(c)
branchName := c.Args()[0]
err := integrationlink.ManualDeploy(currentApp, branchName)
if err != nil {
errorQuit(err)
}
},
BashComplete: func(c *cli.Context) {
_ = autocomplete.CmdFlagsAutoComplete(c, "integration-link-manual-deploy")
},
}
integrationLinkManualReviewAppCommand = cli.Command{
Name: "integration-link-manual-review-app",
Category: "Integration Link",
Flags: []cli.Flag{appFlag},
Usage: "Trigger a review app creation of the pull/merge request ID specified",
Description: `Trigger a review app creation of the pull/merge request ID specified:
$ scalingo --app my-app integration-link-manual-review-app pull-request-id (for GitHub and GitHub Enterprise)
$ scalingo --app my-app integration-link-manual-review-app merge-request-id (for GitLab and GitLab self-hosted)
Example:
$ scalingo --app my-app integration-link-manual-review-app 42
# See also 'integration-link', 'integration-link-create', 'integration-link-update', 'integration-link-delete' and 'integration-link-manual-deploy'`,
Action: func(c *cli.Context) {
if len(c.Args()) != 1 {
cli.ShowCommandHelp(c, "integration-link-manual-review-app")
return
}
currentApp := appdetect.CurrentApp(c)
pullRequestID := c.Args()[0]
err := integrationlink.ManualReviewApp(currentApp, pullRequestID)
if err != nil {
errorQuit(err)
}
},
BashComplete: func(c *cli.Context) {
_ = autocomplete.CmdFlagsAutoComplete(c, "integration-link-manual-review-app")
},
}
)
func interactiveCreate() (scalingo.SCMRepoLinkCreateParams, error) {
var params scalingo.SCMRepoLinkCreateParams
if config.C.DisableInteractive {
return params, errors.New("need at least one integration link parameter")
}
qs := []*survey.Question{
{
Name: "branch",
Prompt: &survey.Input{Message: "Branch to auto-deploy (empty to disable):"},
},
{
Name: "auto-review-apps",
Prompt: &survey.Confirm{
Message: "Automatically deploy review apps:",
Default: false,
},
},
}
answers := struct {
Branch string
AutoReviewApps bool `survey:"auto-review-apps"`
}{}
err := survey.Ask(qs, &answers)
if err != nil {
return params, err
}
t := true
if answers.Branch != "" {
params.Branch = &answers.Branch
params.AutoDeployEnabled = &t
}
if !answers.AutoReviewApps {
return params, nil
}
params.DeployReviewAppsEnabled = &t
destroyOnClose := true
err = survey.AskOne(&survey.Confirm{
Message: "Automatically destroy review apps when the pull/merge request is closed:",
Default: destroyOnClose,
}, &destroyOnClose, nil)
if err != nil {
return params, err
}
params.DestroyOnCloseEnabled = &destroyOnClose
if destroyOnClose {
answerHoursBeforeDestroyOnClose := "0"
err = survey.AskOne(&survey.Input{
Message: "Hours before automatically destroying the review apps:",
Default: "0",
}, &answerHoursBeforeDestroyOnClose, validateHoursBeforeDelete)
if err != nil {
return params, err
}
hoursBeforeDestroyOnClose64, _ := strconv.ParseUint(answerHoursBeforeDestroyOnClose, 10, 32)
hoursBeforeDestroyOnClose := uint(hoursBeforeDestroyOnClose64)
params.HoursBeforeDeleteOnClose = &hoursBeforeDestroyOnClose
}
destroyOnStale := false
err = survey.AskOne(&survey.Confirm{
Message: "Automatically destroy review apps after some time without deploy/commits:",
Default: destroyOnStale,
}, &destroyOnStale, nil)
if err != nil {
return params, err
}
params.DestroyStaleEnabled = &destroyOnStale
if destroyOnStale {
answerHoursBeforeDestroyOnStale := "0"
err = survey.AskOne(&survey.Input{
Message: "Hours before automatically destroying the review apps:",
Default: "0",
}, &answerHoursBeforeDestroyOnStale, validateHoursBeforeDelete)
if err != nil {
return params, err
}
hoursBeforeDestroyOnStale64, _ := strconv.ParseUint(answerHoursBeforeDestroyOnStale, 10, 32)
hoursBeforeDestroyOnStale := uint(hoursBeforeDestroyOnStale64)
params.HoursBeforeDeleteStale = &hoursBeforeDestroyOnStale
}
return params, nil
}
func validateHoursBeforeDelete(ans interface{}) error {
str, ok := ans.(string)
if !ok {
return errors.New("must be a string")
}
i, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
}
if i < 0 {
return errors.New("must be positive")
}
return nil
}