Skip to content

Commit 39806c9

Browse files
committed
Improve git config section validation
1 parent a623cfd commit 39806c9

File tree

4 files changed

+43
-82
lines changed

4 files changed

+43
-82
lines changed

bundle/config/mutator/load_git_details.go

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mutator
22

33
import (
44
"context"
5+
"fmt"
56
"path/filepath"
67

78
"github.com/databricks/cli/bundle"
@@ -27,29 +28,55 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
2728
}
2829

2930
b.WorktreeRoot = info.GuessedWorktreeRoot
31+
config := &b.Config.Bundle.Git
3032

31-
b.Config.Bundle.Git.ActualBranch = info.CurrentBranch
32-
if b.Config.Bundle.Git.Branch == "" {
33-
// Only load branch if there's no user defined value
34-
b.Config.Bundle.Git.Inferred = true
35-
b.Config.Bundle.Git.Branch = info.CurrentBranch
33+
config.ActualBranch = info.CurrentBranch
34+
if config.Branch == "" && info.CurrentBranch != "" {
35+
config.Inferred = true
3636
}
3737

38-
// load commit hash if undefined
39-
if b.Config.Bundle.Git.Commit == "" {
40-
b.Config.Bundle.Git.Commit = info.LatestCommit
41-
}
42-
43-
// load origin url if undefined
44-
if b.Config.Bundle.Git.OriginURL == "" {
45-
b.Config.Bundle.Git.OriginURL = info.OriginURL
46-
}
38+
diags = checkMatch(diags, "Git branch", info.CurrentBranch, &config.Branch, b.Config.Bundle.Force)
39+
diags = checkMatch(diags, "Git commit", info.LatestCommit, &config.Commit, b.Config.Bundle.Force)
40+
diags = checkMatch(diags, "Git originURL", info.OriginURL, &config.OriginURL, true)
4741

4842
relBundlePath, err := filepath.Rel(b.WorktreeRoot.Native(), b.BundleRoot.Native())
4943
if err != nil {
5044
diags = append(diags, diag.FromErr(err)...)
5145
}
5246

53-
b.Config.Bundle.Git.BundleRootPath = filepath.ToSlash(relBundlePath)
47+
config.BundleRootPath = filepath.ToSlash(relBundlePath)
48+
return diags
49+
}
50+
51+
func checkMatch(diags []diag.Diagnostic, field, fetchedValue string, configValue *string, allowedToMismatch bool) []diag.Diagnostic {
52+
if fetchedValue == "" {
53+
return diags
54+
}
55+
56+
// The value from config takes precedence; however, we always warn if configValue and fetchedValue disagree.
57+
// In case of branch and commit and absence of --force we raise severity to Error
58+
59+
if *configValue == "" {
60+
*configValue = fetchedValue
61+
return diags
62+
}
63+
64+
if *configValue != fetchedValue {
65+
tmpl := "not on the right %s:\n expected according to configuration: %s\n actual: %s%s"
66+
extra := ""
67+
var severity diag.Severity
68+
if allowedToMismatch {
69+
severity = diag.Warning
70+
} else {
71+
severity = diag.Error
72+
extra = "\nuse --force to override"
73+
}
74+
75+
return append(diags, diag.Diagnostic{
76+
Severity: severity,
77+
Summary: fmt.Sprintf(tmpl, field, *configValue, fetchedValue, extra),
78+
})
79+
}
80+
5481
return diags
5582
}

bundle/config/mutator/validate_git_details_test.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

bundle/phases/deploy.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/databricks/cli/bundle"
88
"github.com/databricks/cli/bundle/artifacts"
99
"github.com/databricks/cli/bundle/config"
10-
"github.com/databricks/cli/bundle/config/mutator"
1110
"github.com/databricks/cli/bundle/deploy"
1211
"github.com/databricks/cli/bundle/deploy/files"
1312
"github.com/databricks/cli/bundle/deploy/lock"
@@ -149,7 +148,6 @@ func Deploy(outputHandler sync.OutputHandler) bundle.Mutator {
149148
terraform.StatePull(),
150149
terraform.CheckDashboardsModifiedRemotely(),
151150
deploy.StatePull(),
152-
mutator.ValidateGitDetails(),
153151
artifacts.CleanUp(),
154152
libraries.ExpandGlobReferences(),
155153
libraries.Upload(),

bundle/tests/git_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ func TestGitBundleBranchValidation(t *testing.T) {
3636
})
3737

3838
b := load(t, "./git_branch_validation")
39-
bundle.Apply(context.Background(), b, mutator.LoadGitDetails())
39+
diags := bundle.Apply(context.Background(), b, mutator.LoadGitDetails())
4040
assert.False(t, b.Config.Bundle.Git.Inferred)
4141
assert.Equal(t, "feature-a", b.Config.Bundle.Git.Branch)
4242
assert.Equal(t, "feature-b", b.Config.Bundle.Git.ActualBranch)
4343

44-
diags := bundle.Apply(context.Background(), b, mutator.ValidateGitDetails())
4544
assert.ErrorContains(t, diags.Error(), "not on the right Git branch:")
4645
}

0 commit comments

Comments
 (0)