/
workflow_transform_as_code.go
115 lines (101 loc) · 3.06 KB
/
workflow_transform_as_code.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
package main
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
)
var workflowTransformAsCodeCmd = cli.Command{
Name: "ascode",
Short: "Transform an existing workflow to an as code workflow",
Ctx: []cli.Arg{
{Name: _ProjectKey},
{Name: _WorkflowName},
},
Flags: []cli.Flag{
{Name: "silent", Type: cli.FlagBool},
{Name: "branch", Type: cli.FlagString},
{Name: "message", Type: cli.FlagString},
},
}
func workflowTransformAsCodeRun(v cli.Values) (interface{}, error) {
projectKey := v.GetString(_ProjectKey)
w, err := client.WorkflowGet(projectKey, v.GetString(_WorkflowName))
if err != nil {
return nil, err
}
if w.FromRepository != "" {
return nil, sdk.WithStack(sdk.ErrWorkflowAlreadyAsCode)
}
noInteractive := v.GetBool("no-interactive")
branch := v.GetString("branch")
message := v.GetString("message")
if !noInteractive && branch == "" {
branch = cli.AskValue("Give a branch name")
}
if !noInteractive && message == "" {
message = cli.AskValue("Give a commit message")
}
ctx := context.Background()
chanMessageReceived := make(chan sdk.WebsocketEvent)
chanMessageToSend := make(chan []sdk.WebsocketFilter)
chanErrorReceived := make(chan error)
sdk.NewGoRoutines(ctx).Run(ctx, "WebsocketEventsListenCmd", func(ctx context.Context) {
client.WebsocketEventsListen(ctx, sdk.NewGoRoutines(ctx), chanMessageToSend, chanMessageReceived, chanErrorReceived)
})
ope, err := client.WorkflowTransformAsCode(projectKey, v.GetString(_WorkflowName), branch, message)
if err != nil {
return nil, err
}
chanMessageToSend <- []sdk.WebsocketFilter{{
Type: sdk.WebsocketFilterTypeOperation,
ProjectKey: projectKey,
OperationUUID: ope.UUID,
}}
if !v.GetBool("silent") {
fmt.Println("CDS is pushing files on your repository. A pull request will be created, please wait...")
}
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
forLoop:
for {
select {
case <-ctx.Done():
return nil, cli.NewError("timeout waiting operation to complete")
case err := <-chanErrorReceived:
fmt.Printf("Error: %v\n", err)
case evt := <-chanMessageReceived:
if evt.Event.EventType == fmt.Sprintf("%T", sdk.EventOperation{}) {
if err := sdk.JSONUnmarshal(evt.Event.Payload, &ope); err != nil {
return nil, cli.WrapError(err, "cannot parse operation from received event")
}
if ope.Status > sdk.OperationStatusProcessing {
break forLoop
}
}
}
}
if ope.Status == sdk.OperationStatusError {
sdk.Exit("An error occurred when migrate: %v", ope.Error)
}
urlSplit := strings.Split(ope.Setup.Push.PRLink, "/")
id, err := strconv.Atoi(urlSplit[len(urlSplit)-1])
if err != nil {
return nil, cli.WrapError(err, "cannot read id from pull request URL %s", ope.Setup.Push.PRLink)
}
response := struct {
URL string `cli:"url" json:"url"`
ID int `cli:"id" json:"id"`
}{
URL: ope.Setup.Push.PRLink,
ID: id,
}
switch ope.Status {
case sdk.OperationStatusError:
return nil, cli.WrapError(err, "cannot perform operation")
}
return response, nil
}