Skip to content

Commit da39862

Browse files
authored
Cleanup and complete json logs a bit (#623)
Required for new SSBC step resolvers to have all data needed.
1 parent c1aab13 commit da39862

File tree

11 files changed

+248
-164
lines changed

11 files changed

+248
-164
lines changed

cmd/src/batch_common.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,17 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
312312
if err != nil && !opts.flags.skipErrors {
313313
return err
314314
}
315-
taskExecUI.Success()
316-
if err != nil && opts.flags.skipErrors {
317-
opts.ui.ExecutingTasksSkippingErrors(err)
315+
if err == nil || opts.flags.skipErrors {
316+
if err == nil {
317+
taskExecUI.Success()
318+
} else {
319+
opts.ui.ExecutingTasksSkippingErrors(err)
320+
}
321+
} else {
322+
if err != nil {
323+
taskExecUI.Failed(err)
324+
return err
325+
}
318326
}
319327

320328
if len(logFiles) > 0 && opts.flags.keepLogs {
@@ -349,22 +357,23 @@ func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) (err error
349357

350358
opts.ui.CreatingBatchSpec()
351359
id, url, err := svc.CreateBatchSpec(ctx, namespace, rawSpec, ids)
352-
opts.ui.CreatingBatchSpecSuccess()
353360
if err != nil {
354361
return opts.ui.CreatingBatchSpecError(err)
355362
}
363+
previewURL := cfg.Endpoint + url
364+
opts.ui.CreatingBatchSpecSuccess(previewURL)
356365

357-
if opts.applyBatchSpec {
358-
opts.ui.ApplyingBatchSpec()
359-
batch, err := svc.ApplyBatchChange(ctx, id)
360-
if err != nil {
361-
return err
362-
}
363-
opts.ui.ApplyingBatchSpecSuccess(cfg.Endpoint + batch.URL)
366+
if !opts.applyBatchSpec {
367+
opts.ui.PreviewBatchSpec(previewURL)
368+
return
369+
}
364370

365-
} else {
366-
opts.ui.PreviewBatchSpec(cfg.Endpoint + url)
371+
opts.ui.ApplyingBatchSpec()
372+
batch, err := svc.ApplyBatchChange(ctx, id)
373+
if err != nil {
374+
return err
367375
}
376+
opts.ui.ApplyingBatchSpecSuccess(cfg.Endpoint + batch.URL)
368377

369378
return nil
370379
}

cmd/src/batch_exec.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/cockroachdb/errors"
1111
"github.com/hashicorp/go-multierror"
12+
1213
"github.com/sourcegraph/src-cli/internal/batches/executor"
1314
"github.com/sourcegraph/src-cli/internal/batches/graphql"
1415
"github.com/sourcegraph/src-cli/internal/batches/service"
@@ -164,12 +165,17 @@ func executeBatchSpecInWorkspaces(ctx context.Context, opts executeBatchSpecOpts
164165

165166
taskExecUI := opts.ui.ExecutingTasks(*verbose, opts.flags.parallelism)
166167
freshSpecs, _, err := coord.Execute(ctx, uncachedTasks, batchSpec, taskExecUI)
167-
if err != nil && !opts.flags.skipErrors {
168-
return err
169-
}
170-
taskExecUI.Success()
171-
if err != nil && opts.flags.skipErrors {
172-
opts.ui.ExecutingTasksSkippingErrors(err)
168+
if err == nil || opts.flags.skipErrors {
169+
if err == nil {
170+
taskExecUI.Success()
171+
} else {
172+
opts.ui.ExecutingTasksSkippingErrors(err)
173+
}
174+
} else {
175+
if err != nil {
176+
taskExecUI.Failed(err)
177+
return err
178+
}
173179
}
174180

175181
specs := append(cachedSpecs, freshSpecs...)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
2222
github.com/sourcegraph/go-diff v0.6.1
2323
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf
24-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210920114500-3eabeb8208ef
24+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210929121718-d5efc9330247
2525
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
2626
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
2727
golang.org/x/net v0.0.0-20210614182718-04defd469f4e

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0H
259259
github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs=
260260
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf h1:oAdWFqhStsWiiMP/vkkHiMXqFXzl1XfUNOdxKJbd6bI=
261261
github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf/go.mod h1:ppFaPm6kpcHnZGqQTFhUIAQRIEhdQDWP1PCv4/ON354=
262-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210920114500-3eabeb8208ef h1:NVbGTt8WfgPflixd3d1AFKZt+UgOevTE3rUz+8HEM6A=
263-
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210920114500-3eabeb8208ef/go.mod h1:9vLg5fQL9Q3iktfqu96OrswbOq7yqMesMJER9DCy8vk=
262+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210929121718-d5efc9330247 h1:U1qXWB+6i/YeQ73kirisIEsxCBgNXw4td4QYdYlBeKM=
263+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20210929121718-d5efc9330247/go.mod h1:9vLg5fQL9Q3iktfqu96OrswbOq7yqMesMJER9DCy8vk=
264264
github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152 h1:z/MpntplPaW6QW95pzcAR/72Z5TWDyDnSo0EOcyij9o=
265265
github.com/sourcegraph/yaml v1.0.1-0.20200714132230-56936252f152/go.mod h1:GIjDIg/heH5DOkXY3YJ/wNhfHsQHoXGjl8G8amsYQ1I=
266266
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=

internal/batches/executor/coordinator_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,9 @@ type dummyTaskExecutionUI struct {
519519
specs map[*Task][]*batcheslib.ChangesetSpec
520520
}
521521

522-
func (d *dummyTaskExecutionUI) Start([]*Task) {}
523-
func (d *dummyTaskExecutionUI) Success() {}
522+
func (d *dummyTaskExecutionUI) Start([]*Task) {}
523+
func (d *dummyTaskExecutionUI) Success() {}
524+
func (d *dummyTaskExecutionUI) Failed(err error) {}
524525
func (d *dummyTaskExecutionUI) TaskStarted(t *Task) {
525526
d.mu.Lock()
526527
defer d.mu.Unlock()

internal/batches/executor/run_steps.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ type executionOpts struct {
7373
func runSteps(ctx context.Context, opts *executionOpts) (result executionResult, stepResults []stepExecutionResult, err error) {
7474
opts.ui.ArchiveDownloadStarted()
7575
err = opts.task.Archive.Ensure(ctx)
76+
opts.ui.ArchiveDownloadFinished(err)
7677
if err != nil {
7778
return executionResult{}, nil, errors.Wrap(err, "fetching repo")
7879
}
7980
defer opts.task.Archive.Close()
80-
opts.ui.ArchiveDownloadFinished()
8181

8282
opts.ui.WorkspaceInitializationStarted()
8383
workspace, err := opts.wc.Create(ctx, opts.task.Repository, opts.task.Steps, opts.task.Archive)
@@ -167,6 +167,16 @@ func runSteps(ctx context.Context, opts *executionOpts) (result executionResult,
167167
return execResult, nil, err
168168
}
169169
stdoutBuffer, stderrBuffer, err := executeSingleStep(ctx, opts, workspace, i, step, digest, &stepContext)
170+
defer func() {
171+
if err != nil {
172+
exitCode := -1
173+
sfe := &stepFailedErr{}
174+
if errors.As(err, sfe) {
175+
exitCode = sfe.ExitCode
176+
}
177+
opts.ui.StepFailed(i+1, err, exitCode)
178+
}
179+
}()
170180
if err != nil {
171181
return execResult, nil, err
172182
}
@@ -233,44 +243,55 @@ func executeSingleStep(
233243
// ----------
234244
// PREPARATION
235245
// ----------
236-
opts.ui.StepPreparing(i + 1)
246+
opts.ui.StepPreparingStart(i + 1)
237247

238248
cidFile, cleanup, err := createCidFile(ctx, opts.tempDir, util.SlugForRepo(opts.task.Repository.Name, opts.task.Repository.Rev()))
239249
if err != nil {
250+
opts.ui.StepPreparingFailed(i+1, err)
240251
return bytes.Buffer{}, bytes.Buffer{}, err
241252
}
242253
defer cleanup()
243254

244255
// For now, we only support shell scripts provided via the Run field.
245256
shell, containerTemp, err := probeImageForShell(ctx, imageDigest)
246257
if err != nil {
247-
return bytes.Buffer{}, bytes.Buffer{}, errors.Wrapf(err, "probing image %q for shell", step.Container)
258+
err = errors.Wrapf(err, "probing image %q for shell", step.Container)
259+
opts.ui.StepPreparingFailed(i+1, err)
260+
return bytes.Buffer{}, bytes.Buffer{}, err
248261
}
249262

250263
runScriptFile, runScript, cleanup, err := createRunScriptFile(ctx, opts.tempDir, step.Run, stepContext)
251264
if err != nil {
265+
opts.ui.StepPreparingFailed(i+1, err)
252266
return bytes.Buffer{}, bytes.Buffer{}, err
253267
}
254268
defer cleanup()
255269

256270
// Parse and render the step.Files.
257271
filesToMount, cleanup, err := createFilesToMount(opts.tempDir, step, stepContext)
258272
if err != nil {
273+
opts.ui.StepPreparingFailed(i+1, err)
259274
return bytes.Buffer{}, bytes.Buffer{}, err
260275
}
261276
defer cleanup()
262277

263278
// Resolve step.Env given the current environment.
264279
stepEnv, err := step.Env.Resolve(os.Environ())
265280
if err != nil {
266-
return bytes.Buffer{}, bytes.Buffer{}, errors.Wrap(err, "resolving step environment")
281+
err = errors.Wrap(err, "resolving step environment")
282+
opts.ui.StepPreparingFailed(i+1, err)
283+
return bytes.Buffer{}, bytes.Buffer{}, err
267284
}
268285
// Render the step.Env variables as templates.
269286
env, err := template.RenderStepMap(stepEnv, stepContext)
270287
if err != nil {
271-
return bytes.Buffer{}, bytes.Buffer{}, errors.Wrap(err, "parsing step environment")
288+
err = errors.Wrap(err, "parsing step environment")
289+
opts.ui.StepPreparingFailed(i+1, err)
290+
return bytes.Buffer{}, bytes.Buffer{}, err
272291
}
273292

293+
opts.ui.StepPreparingSuccess(i + 1)
294+
274295
// ----------
275296
// EXECUTION
276297
// ----------
@@ -330,8 +351,14 @@ func executeSingleStep(
330351
}
331352

332353
newStepFailedErr := func(wrappedErr error) stepFailedErr {
354+
exitCode := -1
355+
exitErr := &exec.ExitError{}
356+
if errors.As(wrappedErr, &exitErr) {
357+
exitCode = exitErr.ExitCode()
358+
}
333359
return stepFailedErr{
334360
Err: wrappedErr,
361+
ExitCode: exitCode,
335362
Args: cmd.Args,
336363
Run: runScript,
337364
Container: step.Container,
@@ -570,7 +597,9 @@ type stepFailedErr struct {
570597
Stdout string
571598
Stderr string
572599

573-
Err error
600+
// ExitCode of the command, or -1 if a non-command error occured.
601+
ExitCode int
602+
Err error
574603
}
575604

576605
func (e stepFailedErr) Cause() error { return e.Err }
@@ -607,8 +636,8 @@ func (e stepFailedErr) Error() string {
607636
printOutput(e.Stderr)
608637
}
609638

610-
if exitErr, ok := e.Err.(*exec.ExitError); ok {
611-
out.WriteString(fmt.Sprintf("\nCommand failed with exit code %d.", exitErr.ExitCode()))
639+
if e.ExitCode != -1 {
640+
out.WriteString(fmt.Sprintf("\nCommand failed with exit code %d.", e.ExitCode))
612641
} else {
613642
out.WriteString(fmt.Sprintf("\nCommand failed: %s", e.Err))
614643
}

internal/batches/executor/ui.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type TaskExecutionUI interface {
1212
Start([]*Task)
1313
Success()
14+
Failed(err error)
1415

1516
TaskStarted(*Task)
1617
TaskFinished(*Task, error)
@@ -28,7 +29,7 @@ type StepOutputWriter interface {
2829

2930
type StepsExecutionUI interface {
3031
ArchiveDownloadStarted()
31-
ArchiveDownloadFinished()
32+
ArchiveDownloadFinished(error)
3233

3334
WorkspaceInitializationStarted()
3435
WorkspaceInitializationFinished()
@@ -37,7 +38,9 @@ type StepsExecutionUI interface {
3738

3839
StepSkipped(int)
3940

40-
StepPreparing(int)
41+
StepPreparingStart(int)
42+
StepPreparingSuccess(int)
43+
StepPreparingFailed(int, error)
4144
StepStarted(stepIdx int, runScript string, env map[string]string)
4245

4346
StepOutputWriter(context.Context, *Task, int) StepOutputWriter
@@ -46,18 +49,21 @@ type StepsExecutionUI interface {
4649
CalculatingDiffFinished()
4750

4851
StepFinished(idx int, diff []byte, changes *git.Changes, outputs map[string]interface{})
52+
StepFailed(idx int, err error, exitCode int)
4953
}
5054

5155
// NoopStepsExecUI is an implementation of StepsExecutionUI that does nothing.
5256
type NoopStepsExecUI struct{}
5357

5458
func (noop NoopStepsExecUI) ArchiveDownloadStarted() {}
55-
func (noop NoopStepsExecUI) ArchiveDownloadFinished() {}
59+
func (noop NoopStepsExecUI) ArchiveDownloadFinished(error) {}
5660
func (noop NoopStepsExecUI) WorkspaceInitializationStarted() {}
5761
func (noop NoopStepsExecUI) WorkspaceInitializationFinished() {}
5862
func (noop NoopStepsExecUI) SkippingStepsUpto(startStep int) {}
5963
func (noop NoopStepsExecUI) StepSkipped(step int) {}
60-
func (noop NoopStepsExecUI) StepPreparing(step int) {}
64+
func (noop NoopStepsExecUI) StepPreparingStart(step int) {}
65+
func (noop NoopStepsExecUI) StepPreparingSuccess(step int) {}
66+
func (noop NoopStepsExecUI) StepPreparingFailed(step int, err error) {}
6167
func (noop NoopStepsExecUI) StepStarted(step int, runScript string, env map[string]string) {}
6268
func (noop NoopStepsExecUI) StepOutputWriter(ctx context.Context, task *Task, step int) StepOutputWriter {
6369
return NoopStepOutputWriter{}
@@ -66,6 +72,8 @@ func (noop NoopStepsExecUI) CalculatingDiffStarted() {}
6672
func (noop NoopStepsExecUI) CalculatingDiffFinished() {}
6773
func (noop NoopStepsExecUI) StepFinished(idx int, diff []byte, changes *git.Changes, outputs map[string]interface{}) {
6874
}
75+
func (noop NoopStepsExecUI) StepFailed(idx int, err error, exitCode int) {
76+
}
6977

7078
type NoopStepOutputWriter struct{}
7179

internal/batches/ui/exec_ui.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type ExecUI interface {
4242
UploadingChangesetSpecsSuccess(ids []graphql.ChangesetSpecID)
4343

4444
CreatingBatchSpec()
45-
CreatingBatchSpecSuccess()
45+
CreatingBatchSpecSuccess(previewURL string)
4646
CreatingBatchSpecError(err error) error
4747

4848
PreviewBatchSpec(previewURL string)

0 commit comments

Comments
 (0)