Skip to content
Closed
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
13 changes: 13 additions & 0 deletions acceptance/bundle/paths/alerts_file_path/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
bundle:
name: alerts_file_path

resources:
alerts:
my_alert:
display_name: "My Alert"
file_path: ./src/my_alert.dbalert.json
warehouse_id: cafef00d

targets:
development:
default: true
5 changes: 5 additions & 0 deletions acceptance/bundle/paths/alerts_file_path/out.test.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"/Workspace/Users/[USERNAME]/.bundle/alerts_file_path/development/files/src/my_alert.dbalert.json"
9 changes: 9 additions & 0 deletions acceptance/bundle/paths/alerts_file_path/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

>>> [CLI] bundle validate -t development -o json
Warning: required field "schedule" is not set
at resources.alerts.my_alert
in databricks.yml:7:7

Warning: required field "source" is not set
at resources.alerts.my_alert.evaluation

6 changes: 6 additions & 0 deletions acceptance/bundle/paths/alerts_file_path/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
errcode trace $CLI bundle validate -t development -o json > tmp.json

# Capture the file_path for the alert after translation
jq '.resources.alerts.my_alert.file_path' tmp.json > output.alert.txt

rm -f tmp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"query_text": "SELECT 1",
"evaluation": {
"empty_result_state": "UNKNOWN",
"comparison_operator": "GREATER_THAN",
"threshold": {
"value": {
"double_value": 0
}
}
}
}
2 changes: 1 addition & 1 deletion bundle/config/mutator/paths/alert_paths_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func VisitAlertPaths(value dyn.Value, fn VisitFunc) (dyn.Value, error) {
)

return dyn.MapByPattern(value, pattern, func(path dyn.Path, value dyn.Value) (dyn.Value, error) {
return fn(path, TranslateModeLocalRelative, value)
return fn(path, TranslateModeFile, value)
})
}
29 changes: 29 additions & 0 deletions bundle/config/mutator/paths/alert_paths_visitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package paths

import (
"testing"

"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/libs/dyn"
"github.com/stretchr/testify/assert"
)

func TestVisitAlertPaths(t *testing.T) {
root := config.Root{
Resources: config.Resources{
Alerts: map[string]*resources.Alert{
"alert0": {
FilePath: "foo.dbalert.json",
},
},
},
}

actual := collectVisitedPaths(t, root, VisitAlertPaths)
expected := []dyn.Path{
dyn.MustPathFromString("resources.alerts.alert0.file_path"),
}

assert.ElementsMatch(t, expected, actual)
}
1 change: 1 addition & 0 deletions bundle/config/mutator/translate_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
t.applyPipelineTranslations(paths.VisitPipelineLibrariesPaths, true),
t.applyArtifactTranslations,
t.applyAppsTranslations,
t.applyAlertTranslations,
})
}

Expand Down
22 changes: 22 additions & 0 deletions bundle/config/mutator/translate_paths_alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mutator

import (
"context"

"github.com/databricks/cli/bundle/config/mutator/paths"

"github.com/databricks/cli/libs/dyn"
)

func (t *translateContext) applyAlertTranslations(ctx context.Context, v dyn.Value) (dyn.Value, error) {
// Convert the `file_path` field to a remote absolute path.
// We use this path to point to the alert definition file in the workspace.

return paths.VisitAlertPaths(v, func(p dyn.Path, mode paths.TranslateMode, v dyn.Value) (dyn.Value, error) {
opts := translateOptions{
Mode: mode,
}

return t.rewriteValue(ctx, p, v, t.b.BundleRootPath, opts)
})
}
52 changes: 52 additions & 0 deletions bundle/config/mutator/translate_paths_alerts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mutator_test

import (
"path/filepath"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/bundle/internal/bundletest"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/vfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTranslatePathsAlerts_FilePathRelativeSubDirectory(t *testing.T) {
dir := t.TempDir()
touchEmptyFile(t, filepath.Join(dir, "src", "my_alert.dbalert.json"))

b := &bundle.Bundle{
SyncRootPath: dir,
BundleRootPath: dir,
SyncRoot: vfs.MustNew(dir),
Config: config.Root{
Workspace: config.Workspace{
FilePath: "/bundle",
},
Resources: config.Resources{
Alerts: map[string]*resources.Alert{
"alert": {
FilePath: "../src/my_alert.dbalert.json",
},
},
},
},
}

bundletest.SetLocation(b, "resources.alerts", []dyn.Location{{
File: filepath.Join(dir, "resources/alert.yml"),
}})

diags := bundle.ApplySeq(t.Context(), b, mutator.NormalizePaths(), mutator.TranslatePaths())
require.NoError(t, diags.Error())

assert.Equal(
t,
"/bundle/src/my_alert.dbalert.json",
b.Config.Resources.Alerts["alert"].FilePath,
)
}
Loading