-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathbatch_remote.go
More file actions
180 lines (151 loc) · 4.2 KB
/
batch_remote.go
File metadata and controls
180 lines (151 loc) · 4.2 KB
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
package main
import (
"context"
"flag"
"fmt"
cliLog "log"
"time"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"
"github.com/sourcegraph/src-cli/internal/batches/service"
"github.com/sourcegraph/src-cli/internal/batches/ui"
)
func init() {
usage := `'src batch remote' runs a batch spec on the Sourcegraph instance.
Usage:
src batch remote [-f FILE]
src batch remote FILE
Examples:
$ src batch remote -f batch.spec.yaml
`
flagSet := flag.NewFlagSet("remote", flag.ExitOnError)
flags := newBatchExecutionFlags(flagSet)
var (
fileFlag = flagSet.String("f", "", "The name of the batch spec file to run.")
)
handler := func(args []string) error {
// Various bits of Batch Changes boilerplate.
ctx := context.Background()
if err := flagSet.Parse(args); err != nil {
return err
}
file, err := getBatchSpecFile(flagSet, fileFlag)
if err != nil {
return err
}
svc := service.New(&service.Opts{
Client: cfg.apiClient(flags.api, flagSet.Output()),
})
_, ffs, err := svc.DetermineLicenseAndFeatureFlags(ctx, flags.skipErrors)
if err != nil {
return err
}
if err := validateSourcegraphVersionConstraint(ffs); err != nil {
if !flags.skipErrors {
return err
} else {
cliLog.Printf("WARNING: %s", err)
}
}
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})
ui := &ui.TUI{Out: out}
// OK, now for the real stuff. We have to load in the batch spec, and we
// may as well validate it at the same time so we don't even have to go to
// the backend if it's invalid.
ui.ParsingBatchSpec()
spec, batchSpecDir, raw, err := parseBatchSpec(ctx, file, svc)
if err != nil {
ui.ParsingBatchSpecFailure(err)
return err
}
ui.ParsingBatchSpecSuccess()
// We're going to need the namespace ID, so let's figure that out.
ui.ResolvingNamespace()
namespace, err := svc.ResolveNamespace(ctx, flags.namespace)
if err != nil {
return err
}
ui.ResolvingNamespaceSuccess(namespace.ID)
ui.SendingBatchChange()
batchChangeID, batchChangeName, err := svc.UpsertBatchChange(ctx, spec.Name, namespace.ID)
if err != nil {
return err
}
ui.SendingBatchChangeSuccess()
ui.SendingBatchSpec()
batchSpecID, err := svc.CreateBatchSpecFromRaw(
ctx,
raw,
namespace.ID,
flags.allowIgnored,
flags.allowUnsupported,
flags.clearCache,
batchChangeID,
)
if err != nil {
return err
}
ui.SendingBatchSpecSuccess()
hasWorkspaceFiles := false
for _, step := range spec.Steps {
if len(step.Mount) > 0 {
hasWorkspaceFiles = true
break
}
}
if hasWorkspaceFiles {
ui.UploadingWorkspaceFiles()
if err = svc.UploadBatchSpecWorkspaceFiles(ctx, batchSpecDir, batchSpecID, spec.Steps); err != nil {
return err
}
ui.UploadingWorkspaceFilesSuccess()
}
// Wait for the workspaces to be resolved.
ui.ResolvingWorkspaces()
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
var res *service.BatchSpecWorkspaceResolution
for range ticker.C {
res, err = svc.GetBatchSpecWorkspaceResolution(ctx, batchSpecID)
if err != nil {
return err
}
if res.State == "FAILED" {
return errors.Newf("workspace resolution failed: %s", res.FailureMessage)
} else if res.State == "COMPLETED" {
break
}
}
ui.ResolvingWorkspacesSuccess(res.Workspaces.TotalCount)
// We have to enqueue this for execution with a separate operation.
//
// TODO: when the execute flag is wired up in the upsert mutation, just set
// it there and remove this.
ui.ExecutingBatchSpec()
batchSpecID, err = svc.ExecuteBatchSpec(ctx, batchSpecID, flags.clearCache)
if err != nil {
return err
}
ui.ExecutingBatchSpecSuccess()
executionURL := cfg.endpointURL.JoinPath(
fmt.Sprintf(
"%s/batch-changes/%s/executions/%s",
namespace.URL,
batchChangeName,
batchSpecID,
),
).String()
ui.RemoteSuccess(executionURL)
return nil
}
batchCommands = append(batchCommands, &command{
flagSet: flagSet,
aliases: []string{},
handler: handler,
usageFunc: func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src batch %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
},
})
}