Skip to content

Commit 9096cae

Browse files
authored
refactor: Pass *Task directly to runSteps (#546)
This is a tiny follow-up to #540 and removes the TODO that Adam confirmed in his review.
1 parent 9f2a040 commit 9096cae

File tree

2 files changed

+25
-43
lines changed

2 files changed

+25
-43
lines changed

internal/batches/executor/executor.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,15 @@ func (x *executor) do(ctx context.Context, task *Task, status taskStatusHandler)
176176

177177
// Actually execute the steps.
178178
opts := &executionOpts{
179-
archive: task.Archive,
180-
batchChangeAttributes: task.BatchChangeAttributes,
181-
repo: task.Repository,
182-
path: task.Path,
183-
steps: task.Steps,
184-
wc: x.opts.Creator,
185-
logger: log,
186-
tempDir: x.opts.TempDir,
179+
task: task,
180+
logger: log,
181+
wc: x.opts.Creator,
182+
tempDir: x.opts.TempDir,
187183
reportProgress: func(currentlyExecuting string) {
188184
status.Update(task, func(status *TaskStatus) {
189185
status.CurrentlyExecuting = currentlyExecuting
190186
})
191187
},
192-
// TODO: Why don't we pass the task to this?
193-
cachedResultFound: task.CachedResultFound,
194-
cachedResult: task.CachedResult,
195188
}
196189

197190
result, stepResults, err := runSteps(runCtx, opts)

internal/batches/executor/run_steps.go

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/pkg/errors"
1717
"github.com/sourcegraph/src-cli/internal/batches"
1818
"github.com/sourcegraph/src-cli/internal/batches/git"
19-
"github.com/sourcegraph/src-cli/internal/batches/graphql"
2019
"github.com/sourcegraph/src-cli/internal/batches/log"
2120
"github.com/sourcegraph/src-cli/internal/batches/workspace"
2221

@@ -54,53 +53,43 @@ type executionResult struct {
5453
}
5554

5655
type executionOpts struct {
57-
archive batches.RepoZip
56+
wc workspace.Creator
5857

59-
wc workspace.Creator
60-
path string
61-
62-
batchChangeAttributes *BatchChangeAttributes
63-
repo *graphql.Repository
64-
steps []batches.Step
58+
task *Task
6559

6660
tempDir string
6761

6862
logger log.TaskLogger
6963
reportProgress func(string)
70-
71-
// cachedResultFound determines whether all steps are executed or only
72-
// steps >= cachedResult.Step.
73-
cachedResultFound bool
74-
cachedResult stepExecutionResult
7564
}
7665

7766
func runSteps(ctx context.Context, opts *executionOpts) (result executionResult, stepResults []stepExecutionResult, err error) {
7867
opts.reportProgress("Downloading archive")
79-
err = opts.archive.Fetch(ctx)
68+
err = opts.task.Archive.Fetch(ctx)
8069
if err != nil {
8170
return executionResult{}, nil, errors.Wrap(err, "fetching repo")
8271
}
83-
defer opts.archive.Close()
72+
defer opts.task.Archive.Close()
8473

8574
opts.reportProgress("Initializing workspace")
86-
workspace, err := opts.wc.Create(ctx, opts.repo, opts.steps, opts.archive)
75+
workspace, err := opts.wc.Create(ctx, opts.task.Repository, opts.task.Steps, opts.task.Archive)
8776
if err != nil {
8877
return executionResult{}, nil, errors.Wrap(err, "creating workspace")
8978
}
9079
defer workspace.Close(ctx)
9180

9281
execResult := executionResult{
9382
Outputs: make(map[string]interface{}),
94-
Path: opts.path,
83+
Path: opts.task.Path,
9584
}
96-
results := make([]StepResult, len(opts.steps))
85+
results := make([]StepResult, len(opts.task.Steps))
9786

9887
var startStep int
99-
if opts.cachedResultFound {
88+
if opts.task.CachedResultFound {
10089
// Set the Outputs to the cached outputs
101-
execResult.Outputs = opts.cachedResult.Outputs
90+
execResult.Outputs = opts.task.CachedResult.Outputs
10291

103-
startStep = opts.cachedResult.StepIndex + 1
92+
startStep = opts.task.CachedResult.StepIndex + 1
10493

10594
switch startStep {
10695
case 1:
@@ -112,25 +101,25 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
112101
startStep = 0
113102
}
114103

115-
for i := startStep; i < len(opts.steps); i++ {
116-
step := opts.steps[i]
104+
for i := startStep; i < len(opts.task.Steps); i++ {
105+
step := opts.task.Steps[i]
117106

118107
stepContext := StepContext{
119-
BatchChange: *opts.batchChangeAttributes,
120-
Repository: *opts.repo,
108+
BatchChange: *opts.task.BatchChangeAttributes,
109+
Repository: *opts.task.Repository,
121110
Outputs: execResult.Outputs,
122111
Steps: StepsContext{
123112
Path: execResult.Path,
124113
},
125114
}
126115

127-
if opts.cachedResultFound && i == startStep {
128-
if err := workspace.ApplyDiff(ctx, []byte(opts.cachedResult.Diff)); err != nil {
116+
if opts.task.CachedResultFound && i == startStep {
117+
if err := workspace.ApplyDiff(ctx, []byte(opts.task.CachedResult.Diff)); err != nil {
129118
return execResult, nil, errors.Wrap(err, "getting changed files in step")
130119
}
131120

132-
results[i-1] = opts.cachedResult.PreviousStepResult
133-
stepContext.Outputs = opts.cachedResult.Outputs
121+
results[i-1] = opts.task.CachedResult.PreviousStepResult
122+
stepContext.Outputs = opts.task.CachedResult.Outputs
134123
}
135124

136125
if i > 0 {
@@ -152,7 +141,7 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
152141
// Find a location that we can use for a cidfile, which will contain the
153142
// container ID that is used below. We can then use this to remove the
154143
// container on a successful run, rather than leaving it dangling.
155-
cidFile, err := ioutil.TempFile(opts.tempDir, opts.repo.Slug()+"-container-id")
144+
cidFile, err := ioutil.TempFile(opts.tempDir, opts.task.Repository.Slug()+"-container-id")
156145
if err != nil {
157146
return execResult, nil, errors.Wrap(err, "Creating a CID file failed")
158147
}
@@ -270,8 +259,8 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
270259

271260
// Where should we execute the steps.run script?
272261
scriptWorkDir := workDir
273-
if opts.path != "" {
274-
scriptWorkDir = workDir + "/" + opts.path
262+
if opts.task.Path != "" {
263+
scriptWorkDir = workDir + "/" + opts.task.Path
275264
}
276265

277266
args := append([]string{

0 commit comments

Comments
 (0)