From cb1766b0e43d4dddebd66adb944f320c208ad62e Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Fri, 12 Apr 2024 11:36:33 +0200 Subject: [PATCH] Transform artifact files source patterns in build not upload stage --- bundle/artifacts/build.go | 31 +++++++++++++++++++++++++++++++ bundle/artifacts/upload.go | 31 ------------------------------- bundle/artifacts/upload_test.go | 14 ++++++++++++-- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/bundle/artifacts/build.go b/bundle/artifacts/build.go index 349b1ff898..722891ada1 100644 --- a/bundle/artifacts/build.go +++ b/bundle/artifacts/build.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/libs/diag" ) @@ -34,6 +35,36 @@ func (m *build) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { return diag.Errorf("artifact doesn't exist: %s", m.name) } + // Check if source paths are absolute, if not, make them absolute + for k := range artifact.Files { + f := &artifact.Files[k] + if !filepath.IsAbs(f.Source) { + dirPath := filepath.Dir(artifact.ConfigFilePath) + f.Source = filepath.Join(dirPath, f.Source) + } + } + + // Expand any glob reference in files source path + files := make([]config.ArtifactFile, 0, len(artifact.Files)) + for _, f := range artifact.Files { + matches, err := filepath.Glob(f.Source) + if err != nil { + return diag.Errorf("unable to find files for %s: %v", f.Source, err) + } + + if len(matches) == 0 { + return diag.Errorf("no files found for %s", f.Source) + } + + for _, match := range matches { + files = append(files, config.ArtifactFile{ + Source: match, + }) + } + } + + artifact.Files = files + // Skip building if build command is not specified or infered if artifact.BuildCommand == "" { // If no build command was specified or infered and there is no diff --git a/bundle/artifacts/upload.go b/bundle/artifacts/upload.go index e2c2fc1c94..5c12c94449 100644 --- a/bundle/artifacts/upload.go +++ b/bundle/artifacts/upload.go @@ -3,10 +3,8 @@ package artifacts import ( "context" "fmt" - "path/filepath" "github.com/databricks/cli/bundle" - "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/libs/diag" "github.com/databricks/databricks-sdk-go/service/workspace" ) @@ -44,35 +42,6 @@ func (m *upload) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { return diag.Errorf("artifact source is not configured: %s", m.name) } - // Check if source paths are absolute, if not, make them absolute - for k := range artifact.Files { - f := &artifact.Files[k] - if !filepath.IsAbs(f.Source) { - dirPath := filepath.Dir(artifact.ConfigFilePath) - f.Source = filepath.Join(dirPath, f.Source) - } - } - - // Expand any glob reference in files source path - files := make([]config.ArtifactFile, 0, len(artifact.Files)) - for _, f := range artifact.Files { - matches, err := filepath.Glob(f.Source) - if err != nil { - return diag.Errorf("unable to find files for %s: %v", f.Source, err) - } - - if len(matches) == 0 { - return diag.Errorf("no files found for %s", f.Source) - } - - for _, match := range matches { - files = append(files, config.ArtifactFile{ - Source: match, - }) - } - } - - artifact.Files = files return bundle.Apply(ctx, b, getUploadMutator(artifact.Type, m.name)) } diff --git a/bundle/artifacts/upload_test.go b/bundle/artifacts/upload_test.go index 687d73b4a8..cf08843a70 100644 --- a/bundle/artifacts/upload_test.go +++ b/bundle/artifacts/upload_test.go @@ -58,7 +58,12 @@ func TestExpandGlobFilesSource(t *testing.T) { return &noop{} } - diags := bundle.Apply(context.Background(), b, u) + bm := &build{"test"} + buildMutators[config.ArtifactType("custom")] = func(name string) bundle.Mutator { + return &noop{} + } + + diags := bundle.Apply(context.Background(), b, bundle.Seq(bm, u)) require.NoError(t, diags.Error()) require.Equal(t, 2, len(b.Config.Artifacts["test"].Files)) @@ -94,6 +99,11 @@ func TestExpandGlobFilesSourceWithNoMatches(t *testing.T) { return &noop{} } - diags := bundle.Apply(context.Background(), b, u) + bm := &build{"test"} + buildMutators[config.ArtifactType("custom")] = func(name string) bundle.Mutator { + return &noop{} + } + + diags := bundle.Apply(context.Background(), b, bundle.Seq(bm, u)) require.ErrorContains(t, diags.Error(), "no files found for") }