Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform artifact files source patterns in build not upload stage #1359

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bundle/artifacts/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/libs/diag"
)

Expand Down Expand Up @@ -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
Expand Down
31 changes: 0 additions & 31 deletions bundle/artifacts/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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))
}

Expand Down
14 changes: 12 additions & 2 deletions bundle/artifacts/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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")
}
Loading