From 386295f94df54575429744780a4b361111978f90 Mon Sep 17 00:00:00 2001 From: Eoin McAfee Date: Tue, 26 Oct 2021 15:17:41 +0100 Subject: [PATCH] move schema update on step to runnner go logic --- internal/merge.go | 1 + pipeline/runtime/execer.go | 5 ++--- pipeline/runtime/type.go | 2 +- pipeline/uploader.go | 4 ++-- pipeline/uploader/upload.go | 17 +++++++++++++++-- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/internal/merge.go b/internal/merge.go index 98a6722..08a12f9 100644 --- a/internal/merge.go +++ b/internal/merge.go @@ -25,4 +25,5 @@ func MergeStep(src, dst *drone.Step) { dst.Started = src.Started dst.Stopped = src.Stopped dst.Version = src.Version + dst.Schema = src.Schema } diff --git a/pipeline/runtime/execer.go b/pipeline/runtime/execer.go index 5fdc861..4b3d28d 100644 --- a/pipeline/runtime/execer.go +++ b/pipeline/runtime/execer.go @@ -254,10 +254,9 @@ func (e *Execer) exec(ctx context.Context, state *pipeline.State, spec Spec, ste } // stream card data to server if exists - file, _ := e.engine.StreamFile(ctx, copy, "/tmp/card.json") + file, _ := e.engine.StreamFile(ctx, spec, copy, "/tmp/card.json") if file != nil { - s := state.Find(step.GetName()) - err = e.uploader.UploadCard(ctx, file, s.ID) + err = e.uploader.UploadCard(ctx, file, state, step.GetName()) if err != nil { return nil } diff --git a/pipeline/runtime/type.go b/pipeline/runtime/type.go index 3aa66d0..8ded0fa 100644 --- a/pipeline/runtime/type.go +++ b/pipeline/runtime/type.go @@ -86,7 +86,7 @@ type ( Run(context.Context, Spec, Step, io.Writer) (*State, error) // StreamFile copies a file to the server - StreamFile(context.Context, Step, string) (io.ReadCloser, error) + StreamFile(context.Context, Spec, Step, string) (io.ReadCloser, error) } // Spec is an interface that must be implemented by all diff --git a/pipeline/uploader.go b/pipeline/uploader.go index 543928a..d2a5df4 100644 --- a/pipeline/uploader.go +++ b/pipeline/uploader.go @@ -6,7 +6,7 @@ import ( ) type Uploader interface { - UploadCard(ctx context.Context, r io.ReadCloser, step int64) error + UploadCard(context.Context, io.ReadCloser, *State, string) error } func NopUploader() Uploader { @@ -15,4 +15,4 @@ func NopUploader() Uploader { type nopUploader struct{} -func (*nopUploader) UploadCard(ctx context.Context, r io.ReadCloser, step int64) error { return nil } +func (*nopUploader) UploadCard(context.Context, io.ReadCloser, *State, string) error { return nil } diff --git a/pipeline/uploader/upload.go b/pipeline/uploader/upload.go index 2feb3b8..3d1628e 100644 --- a/pipeline/uploader/upload.go +++ b/pipeline/uploader/upload.go @@ -7,6 +7,7 @@ import ( "github.com/drone/drone-go/drone" "github.com/drone/runner-go/client" + "github.com/drone/runner-go/internal" "github.com/drone/runner-go/pipeline" ) @@ -22,7 +23,8 @@ func New(client client.Client) *Upload { } } -func (s *Upload) UploadCard(ctx context.Context, r io.ReadCloser, step int64) error { +func (s *Upload) UploadCard(ctx context.Context, r io.ReadCloser, state *pipeline.State, stepName string) error { + src := state.Find(stepName) bytes, err := io.ReadAll(r) if err != nil { return err @@ -32,9 +34,20 @@ func (s *Upload) UploadCard(ctx context.Context, r io.ReadCloser, step int64) er if err != nil { return err } - err = s.client.UploadCard(ctx, step, &card) + err = s.client.UploadCard(ctx, src.ID, &card) if err != nil { return err } + // update step schema + state.Lock() + src.Schema = card.Schema + cpy := internal.CloneStep(src) + state.Unlock() + err = s.client.UpdateStep(ctx, cpy) + if err == nil { + state.Lock() + internal.MergeStep(cpy, src) + state.Unlock() + } return nil }