Skip to content

Commit 10d976b

Browse files
authored
Validate that either source_code_path or git_source is set for apps (#4632)
## Changes Validate that either source_code_path or git_source is set for apps ## Why Previously, we always required setting the `source_code_path` field. Now, with the introduction of the git_source field, this isn't required anymore, and one of the two fields can only be set ## Tests Added an acceptance test <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent c520ed9 commit 10d976b

File tree

10 files changed

+58
-25
lines changed

10 files changed

+58
-25
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### CLI
66

77
### Bundles
8+
* Validate that either source_code_path or git_source is set for apps ([#4632](https://github.com/databricks/cli/pull/4632))
89

910
### Dependency updates
1011

acceptance/bundle/apps/git_source/databricks.yml.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ resources:
1515
git_source:
1616
branch: main
1717
source_code_path: internal/testdata/simple-app
18-
source_code_path: ./app

acceptance/bundle/validate/empty_resources/empty_dict/output.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,10 @@ Warning: required field "name" is not set
146146
at resources.apps.rname
147147
in databricks.yml:6:12
148148

149-
Warning: required field "source_code_path" is not set
150-
at resources.apps.rname
151-
in databricks.yml:6:12
152-
153-
Error: Missing app source code path
149+
Error: Missing app source code path or git source
154150
in databricks.yml:6:12
155151

156-
app resource 'rname' is missing required source_code_path field
152+
app resource 'rname' should have either source_code_path or git_source field
157153

158154
{
159155
"apps": {

acceptance/bundle/validate/empty_resources/with_grants/output.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,10 @@ Warning: required field "name" is not set
183183
at resources.apps.rname
184184
in databricks.yml:7:7
185185

186-
Warning: required field "source_code_path" is not set
187-
at resources.apps.rname
188-
in databricks.yml:7:7
189-
190-
Error: Missing app source code path
186+
Error: Missing app source code path or git source
191187
in databricks.yml:7:7
192188

193-
app resource 'rname' is missing required source_code_path field
189+
app resource 'rname' should have either source_code_path or git_source field
194190

195191
{
196192
"apps": {

acceptance/bundle/validate/empty_resources/with_permissions/output.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,10 @@ Warning: required field "name" is not set
162162
at resources.apps.rname
163163
in databricks.yml:7:7
164164

165-
Warning: required field "source_code_path" is not set
166-
at resources.apps.rname
167-
in databricks.yml:7:7
168-
169-
Error: Missing app source code path
165+
Error: Missing app source code path or git source
170166
in databricks.yml:7:7
171167

172-
app resource 'rname' is missing required source_code_path field
168+
app resource 'rname' should have either source_code_path or git_source field
173169

174170
{
175171
"apps": {

bundle/apps/validate.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,21 @@ func (v *validate) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
1515
usedSourceCodePaths := make(map[string]string)
1616

1717
for key, app := range b.Config.Resources.Apps {
18-
if app.SourceCodePath == "" {
18+
if app.SourceCodePath == "" && app.GitSource == nil {
1919
diags = append(diags, diag.Diagnostic{
2020
Severity: diag.Error,
21-
Summary: "Missing app source code path",
22-
Detail: fmt.Sprintf("app resource '%s' is missing required source_code_path field", key),
21+
Summary: "Missing app source code path or git source",
22+
Detail: fmt.Sprintf("app resource '%s' should have either source_code_path or git_source field", key),
23+
Locations: b.Config.GetLocations("resources.apps." + key),
24+
})
25+
continue
26+
}
27+
28+
if app.SourceCodePath != "" && app.GitSource != nil {
29+
diags = append(diags, diag.Diagnostic{
30+
Severity: diag.Error,
31+
Summary: "Both source_code_path and git_source fields are set",
32+
Detail: fmt.Sprintf("app resource '%s' should have either source_code_path or git_source field, not both", key),
2333
Locations: b.Config.GetLocations("resources.apps." + key),
2434
})
2535
continue

bundle/apps/validate_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,39 @@ func TestAppsValidateSameSourcePath(t *testing.T) {
5555
require.Equal(t, "Duplicate app source code path", diags[0].Summary)
5656
require.Contains(t, diags[0].Detail, "has the same source code path as app resource")
5757
}
58+
59+
func TestAppsValidateBothSourceCodePathAndGitSource(t *testing.T) {
60+
tmpDir := t.TempDir()
61+
testutil.Touch(t, tmpDir, "app1", "app.py")
62+
63+
b := &bundle.Bundle{
64+
BundleRootPath: tmpDir,
65+
SyncRootPath: tmpDir,
66+
SyncRoot: vfs.MustNew(tmpDir),
67+
Config: config.Root{
68+
Workspace: config.Workspace{
69+
FilePath: "/foo/bar/",
70+
},
71+
Resources: config.Resources{
72+
Apps: map[string]*resources.App{
73+
"app1": {
74+
App: apps.App{
75+
Name: "app1",
76+
},
77+
SourceCodePath: "./app1",
78+
GitSource: &apps.GitSource{
79+
Branch: "main",
80+
},
81+
},
82+
},
83+
},
84+
},
85+
}
86+
87+
bundletest.SetLocation(b, ".", []dyn.Location{{File: filepath.Join(tmpDir, "databricks.yml")}})
88+
89+
diags := bundle.ApplySeq(context.Background(), b, mutator.TranslatePaths(), Validate())
90+
require.Len(t, diags, 1)
91+
require.Equal(t, "Both source_code_path and git_source fields are set", diags[0].Summary)
92+
require.Contains(t, diags[0].Detail, "should have either source_code_path or git_source field, not both")
93+
}

bundle/config/resources/apps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type App struct {
3939

4040
// SourceCodePath is a required field used by DABs to point to Databricks app source code
4141
// on local disk and to the corresponding workspace path during app deployment.
42-
SourceCodePath string `json:"source_code_path"`
42+
SourceCodePath string `json:"source_code_path,omitempty"`
4343

4444
// Config represents inline app.yaml configuration for the app.
4545
// When specified, this configuration is written to an app.yaml file in the source code path during deployment.

bundle/internal/validation/generated/required_fields.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/schema/jsonschema.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)