Skip to content

Commit e50ad40

Browse files
authored
Build changeset specs server side (#661)
This PR changes src exec's behavior to not upload changeset specs anymore. They aren't required anymore. See main PR for more details: https://github.com/sourcegraph/sourcegraph/pull/28158
1 parent fb087af commit e50ad40

12 files changed

Lines changed: 36 additions & 69 deletions

File tree

cmd/src/batch_common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ func executeBatchSpec(ctx context.Context, ui ui.ExecUI, opts executeBatchSpecOp
250250

251251
var workspaceCreator workspace.Creator
252252

253-
if svc.HasDockerImages(batchSpec) {
253+
if len(batchSpec.Steps) > 0 {
254254
ui.PreparingContainerImages()
255-
images, err := svc.EnsureDockerImages(ctx, batchSpec, ui.PreparingContainerImagesProgress)
255+
images, err := svc.EnsureDockerImages(ctx, batchSpec.Steps, ui.PreparingContainerImagesProgress)
256256
if err != nil {
257257
return err
258258
}

cmd/src/batch_exec.go

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99

1010
"github.com/cockroachdb/errors"
11-
"github.com/hashicorp/go-multierror"
1211

1312
"github.com/sourcegraph/src-cli/internal/batches/executor"
1413
"github.com/sourcegraph/src-cli/internal/batches/graphql"
@@ -109,27 +108,11 @@ func executeBatchSpecInWorkspaces(ctx context.Context, ui *ui.JSONLines, opts ex
109108
// we can convert it to a RepoWorkspace and build a task only for that one.
110109
repoWorkspace := convertWorkspace(input.Workspace)
111110

112-
// Parse the raw batch spec contained in the input
113-
ui.ParsingBatchSpec()
114-
batchSpec, err := svc.ParseBatchSpec([]byte(input.RawSpec))
115-
if err != nil {
116-
var multiErr *multierror.Error
117-
if errors.As(err, &multiErr) {
118-
ui.ParsingBatchSpecFailure(multiErr)
119-
return cmderrors.ExitCode(2, nil)
120-
} else {
121-
// This shouldn't happen; let's just punt and let the normal
122-
// rendering occur.
123-
return err
124-
}
125-
}
126-
ui.ParsingBatchSpecSuccess()
127-
128111
var workspaceCreator workspace.Creator
129112

130-
if svc.HasDockerImages(batchSpec) {
113+
if len(input.Workspace.Steps) > 0 {
131114
ui.PreparingContainerImages()
132-
images, err := svc.EnsureDockerImages(ctx, batchSpec, ui.PreparingContainerImagesProgress)
115+
images, err := svc.EnsureDockerImages(ctx, input.Workspace.Steps, ui.PreparingContainerImagesProgress)
133116
if err != nil {
134117
return err
135118
}
@@ -162,13 +145,13 @@ func executeBatchSpecInWorkspaces(ctx context.Context, ui *ui.JSONLines, opts ex
162145
// `src batch exec` uses server-side caching for changeset specs, so we
163146
// only need to call `CheckStepResultsCache` to make sure that per-step cache entries
164147
// are loaded and set on the tasks.
165-
tasks := svc.BuildTasks(ctx, batchSpec, []service.RepoWorkspace{repoWorkspace})
148+
tasks := svc.BuildTasks(ctx, input.Spec, []service.RepoWorkspace{repoWorkspace})
166149
if err := coord.CheckStepResultsCache(ctx, tasks); err != nil {
167150
return err
168151
}
169152

170153
taskExecUI := ui.ExecutingTasks(*verbose, opts.flags.parallelism)
171-
specs, _, err := coord.Execute(ctx, tasks, batchSpec, taskExecUI)
154+
_, _, err = coord.Execute(ctx, tasks, input.Spec, taskExecUI)
172155
if err == nil || opts.flags.skipErrors {
173156
if err == nil {
174157
taskExecUI.Success()
@@ -181,18 +164,6 @@ func executeBatchSpecInWorkspaces(ctx context.Context, ui *ui.JSONLines, opts ex
181164
return err
182165
}
183166
}
184-
ids := make([]graphql.ChangesetSpecID, len(specs))
185-
186-
ui.UploadingChangesetSpecs(len(specs))
187-
for i, spec := range specs {
188-
id, err := svc.CreateChangesetSpec(ctx, spec)
189-
if err != nil {
190-
return err
191-
}
192-
ids[i] = id
193-
ui.UploadingChangesetSpecsProgress(i+1, len(specs))
194-
}
195-
ui.UploadingChangesetSpecsSuccess(ids)
196167

197168
return nil
198169
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
2121
github.com/sourcegraph/go-diff v0.6.1
2222
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf
23-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20211126224101-2ada4911722e
23+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20211129161320-80f1543dd742
2424
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
2525
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
2626
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
@@ -30,7 +30,7 @@ require (
3030

3131
require (
3232
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
33-
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
33+
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect
3434
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
3535
github.com/ghodss/yaml v1.0.0 // indirect
3636
github.com/gogo/protobuf v1.3.2 // indirect

go.sum

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ github.com/cockroachdb/datadriven v1.0.0/go.mod h1:5Ib8Meh+jk1RlHIXej6Pzevx/NLlN
2525
github.com/cockroachdb/errors v1.6.1/go.mod h1:tm6FTP5G81vwJ5lC0SizQo374JNCOPrHyXGitRJoDqM=
2626
github.com/cockroachdb/errors v1.8.6 h1:Am9evxl/po3RzpokemQvq7S7Cd0mxv24xy0B/trlQF4=
2727
github.com/cockroachdb/errors v1.8.6/go.mod h1:hOm5fabihW+xEyY1kuypGwqT+Vt7rafg04ytBtIpeIQ=
28-
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f h1:o/kfcElHqOiXqcou5a3rIlMc7oJbMQkeLk0VQJ7zgqY=
2928
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
29+
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f h1:6jduT9Hfc0njg5jJ1DdKCFPdMBrp/mdZfCpa5h+WM74=
30+
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
3031
github.com/cockroachdb/redact v1.1.1/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
3132
github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ=
3233
github.com/cockroachdb/redact v1.1.3/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
@@ -258,8 +259,8 @@ github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0H
258259
github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs=
259260
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf h1:oAdWFqhStsWiiMP/vkkHiMXqFXzl1XfUNOdxKJbd6bI=
260261
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf/go.mod h1:ppFaPm6kpcHnZGqQTFhUIAQRIEhdQDWP1PCv4/ON354=
261-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20211126224101-2ada4911722e h1:BZpmejyF9KR5h7U9U/lfg4PfcPQZc29fuNGn0c1kT+c=
262-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20211126224101-2ada4911722e/go.mod h1:4WdX5odo9YL0GD0Wg9OZJJtIJIn1jIsh+UWrQ4MEcaw=
262+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20211129161320-80f1543dd742 h1:8IegMj6lV20zSCt4f+fjOKRD7ef3/hONH9MmZ9OMYp4=
263+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20211129161320-80f1543dd742/go.mod h1:4WdX5odo9YL0GD0Wg9OZJJtIJIn1jIsh+UWrQ4MEcaw=
263264
github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152 h1:z/MpntplPaW6QW95pzcAR/72Z5TWDyDnSo0EOcyij9o=
264265
github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I=
265266
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=

internal/batches/executor/coordinator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ func (c *Coordinator) checkCacheForTask(ctx context.Context, task *Task) (specs
174174

175175
func (c Coordinator) buildChangesetSpecs(task *Task, result execution.Result) ([]*batcheslib.ChangesetSpec, error) {
176176
input := &batcheslib.ChangesetSpecInput{
177-
BaseRepositoryID: task.Repository.ID,
178-
HeadRepositoryID: task.Repository.ID,
179-
Repository: batcheslib.ChangesetSpecRepository{
177+
Repository: batcheslib.Repository{
178+
ID: task.Repository.ID,
180179
Name: task.Repository.Name,
181180
FileMatches: task.Repository.SortedFileMatches(),
182181
BaseRef: task.Repository.BaseRef(),

internal/batches/executor/coordinator_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,9 @@ func TestCoordinator_Execute_StepCaching(t *testing.T) {
376376
Path: "",
377377
},
378378
stepResults: []execution.AfterStepResult{
379-
{StepIndex: 0, Diff: []byte(`step-0-diff`)},
380-
{StepIndex: 1, Diff: []byte(`step-1-diff`)},
381-
{StepIndex: 2, Diff: []byte(`step-2-diff`)},
379+
{StepIndex: 0, Diff: `step-0-diff`},
380+
{StepIndex: 1, Diff: `step-1-diff`},
381+
{StepIndex: 2, Diff: `step-2-diff`},
382382
},
383383
}}
384384

internal/batches/executor/executor_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,14 @@ func TestExecutor_CachedStepResults(t *testing.T) {
480480
},
481481
}
482482

483-
cachedDiff := []byte(`diff --git README.md README.md
483+
cachedDiff := `diff --git README.md README.md
484484
index 02a19af..c9644dd 100644
485485
--- README.md
486486
+++ README.md
487487
@@ -1 +1,2 @@
488488
# Welcome to the README
489489
+foobar
490-
`)
490+
`
491491

492492
task := &Task{
493493
BatchChangeAttributes: &template.BatchChangeAttributes{},
@@ -516,7 +516,7 @@ index 02a19af..c9644dd 100644
516516
// We want the diff to be the same as the cached one, since we only had to
517517
// execute a single step
518518
executionResult := results[0].result
519-
if diff := cmp.Diff(executionResult.Diff, string(cachedDiff)); diff != "" {
519+
if diff := cmp.Diff(executionResult.Diff, cachedDiff); diff != "" {
520520
t.Fatalf("wrong diff: %s", diff)
521521
}
522522

@@ -543,7 +543,7 @@ This repository is used to test opening and closing pull request with Automation
543543
},
544544
}
545545

546-
cachedDiff := []byte(`diff --git README.md README.md
546+
cachedDiff := `diff --git README.md README.md
547547
index 1914491..cd2ccbf 100644
548548
--- README.md
549549
+++ README.md
@@ -562,7 +562,7 @@ index 0000000..888e1ec
562562
+++ README.txt
563563
@@ -0,0 +1 @@
564564
+this is step 1
565-
`)
565+
`
566566

567567
wantFinalDiff := `diff --git README.md README.md
568568
index 1914491..d6782d3 100644

internal/batches/executor/run_steps.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ func runSteps(ctx context.Context, opts *executionOpts) (result execution.Result
7878
// If we have cached results and don't need to execute any more steps,
7979
// we can quit
8080
if lastStep == len(opts.task.Steps)-1 {
81-
changes, err := git.ChangesInDiff(opts.task.CachedResult.Diff)
81+
changes, err := git.ChangesInDiff([]byte(opts.task.CachedResult.Diff))
8282
if err != nil {
8383
return execResult, nil, errors.Wrap(err, "parsing cached step diff")
8484
}
8585

86-
execResult.Diff = string(opts.task.CachedResult.Diff)
86+
execResult.Diff = opts.task.CachedResult.Diff
8787
execResult.ChangedFiles = &changes
8888
stepResults = append(stepResults, opts.task.CachedResult)
8989

@@ -119,7 +119,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result execution.Result
119119
stepContext.Steps.Changes = previousStepResult.Files
120120
stepContext.Outputs = opts.task.CachedResult.Outputs
121121

122-
if err := workspace.ApplyDiff(ctx, opts.task.CachedResult.Diff); err != nil {
122+
if err := workspace.ApplyDiff(ctx, []byte(opts.task.CachedResult.Diff)); err != nil {
123123
return execResult, nil, errors.Wrap(err, "getting changed files in step")
124124
}
125125
}
@@ -178,7 +178,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result execution.Result
178178
}
179179
stepResult := execution.AfterStepResult{
180180
StepIndex: i,
181-
Diff: stepDiff,
181+
Diff: string(stepDiff),
182182
Outputs: make(map[string]interface{}),
183183
PreviousStepResult: stepContext.PreviousStep,
184184
}

internal/batches/executor/ui.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type StepsExecutionUI interface {
4848
CalculatingDiffStarted()
4949
CalculatingDiffFinished()
5050

51-
StepFinished(idx int, diff []byte, changes *git.Changes, outputs map[string]interface{})
51+
StepFinished(idx int, diff string, changes *git.Changes, outputs map[string]interface{})
5252
StepFailed(idx int, err error, exitCode int)
5353
}
5454

@@ -70,7 +70,7 @@ func (noop NoopStepsExecUI) StepOutputWriter(ctx context.Context, task *Task, st
7070
}
7171
func (noop NoopStepsExecUI) CalculatingDiffStarted() {}
7272
func (noop NoopStepsExecUI) CalculatingDiffFinished() {}
73-
func (noop NoopStepsExecUI) StepFinished(idx int, diff []byte, changes *git.Changes, outputs map[string]interface{}) {
73+
func (noop NoopStepsExecUI) StepFinished(idx int, diff string, changes *git.Changes, outputs map[string]interface{}) {
7474
}
7575
func (noop NoopStepsExecUI) StepFailed(idx int, err error, exitCode int) {
7676
}

internal/batches/service/service.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,30 +158,26 @@ func (svc *Service) CreateChangesetSpec(ctx context.Context, spec *batcheslib.Ch
158158
//
159159
// Progress information is reported back to the given progress function: perc
160160
// will be a value between 0.0 and 1.0, inclusive.
161-
func (svc *Service) EnsureDockerImages(ctx context.Context, spec *batcheslib.BatchSpec, progress func(done, total int)) (map[string]docker.Image, error) {
162-
total := len(spec.Steps)
161+
func (svc *Service) EnsureDockerImages(ctx context.Context, steps []batcheslib.Step, progress func(done, total int)) (map[string]docker.Image, error) {
162+
total := len(steps)
163163
progress(0, total)
164164

165165
// TODO: this _really_ should be parallelised, since the image cache takes
166166
// care to only pull the same image once.
167167
images := make(map[string]docker.Image)
168-
for i := range spec.Steps {
169-
img, err := svc.EnsureImage(ctx, spec.Steps[i].Container)
168+
for i := range steps {
169+
img, err := svc.EnsureImage(ctx, steps[i].Container)
170170
if err != nil {
171171
return nil, err
172172
}
173-
images[spec.Steps[i].Container] = img
173+
images[steps[i].Container] = img
174174

175175
progress(i+1, total)
176176
}
177177

178178
return images, nil
179179
}
180180

181-
func (svc *Service) HasDockerImages(spec *batcheslib.BatchSpec) bool {
182-
return len(spec.Steps) > 0
183-
}
184-
185181
func (svc *Service) EnsureImage(ctx context.Context, name string) (docker.Image, error) {
186182
img := svc.imageCache.Get(name)
187183

0 commit comments

Comments
 (0)