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

Add validation for Git settings in bundles #578

Merged
merged 18 commits into from
Jul 30, 2023
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
3 changes: 3 additions & 0 deletions bundle/config/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type Bundle struct {
// Lock configures locking behavior on deployment.
Lock Lock `json:"lock" bundle:"readonly"`

// Force-override Git branch validation.
Force bool `json:"force" bundle:"readonly"`

// Contains Git information like current commit, current branch and
// origin url. Automatically loaded by reading .git directory if not specified
Git Git `json:"git,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions bundle/config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Environment struct {
// Does not permit defining new variables or redefining existing ones
// in the scope of an environment
Variables map[string]string `json:"variables,omitempty"`

Git Git `json:"git,omitempty"`
}

const (
Expand Down
6 changes: 6 additions & 0 deletions bundle/config/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ type Git struct {
Branch string `json:"branch,omitempty"`
OriginURL string `json:"origin_url,omitempty"`
Commit string `json:"commit,omitempty" bundle:"readonly"`

// Inferred is set to true if the Git details were inferred and weren't set explicitly
Inferred bool `json:"-" bundle:"readonly"`

// The actual branch according to Git (may be different from the configured branch)
ActualBranch string `json:"-" bundle:"readonly"`
}
2 changes: 2 additions & 0 deletions bundle/config/mutator/load_git_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) error {
log.Warnf(ctx, "failed to load current branch: %s", err)
} else {
b.Config.Bundle.Git.Branch = branch
b.Config.Bundle.Git.ActualBranch = branch
b.Config.Bundle.Git.Inferred = true
}
}
// load commit hash if undefined
Expand Down
5 changes: 5 additions & 0 deletions bundle/config/mutator/process_environment_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ func findIncorrectPath(b *bundle.Bundle, mode config.Mode) string {
}

func validateProductionMode(ctx context.Context, b *bundle.Bundle, isPrincipalUsed bool) error {
if b.Config.Bundle.Git.Inferred {
env := b.Config.Bundle.Environment
return fmt.Errorf("environment with 'mode: production' must specify an explicit 'environments.%s.git' configuration", env)
}

r := b.Config.Resources
for i := range r.Pipelines {
if r.Pipelines[i].Development {
Expand Down
15 changes: 15 additions & 0 deletions bundle/config/mutator/process_environment_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func mockBundle(mode config.Mode) *bundle.Bundle {
Config: config.Root{
Bundle: config.Bundle{
Mode: mode,
Git: config.Git{
Copy link
Contributor Author

@lennartkats-db lennartkats-db Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main thing to decide on for this PR is if we want customers to nest git under bundle... That's a lot of nesting.

This PR follows what's in main Git configuration for bundle:

bundle:
  git:
    branch: main

But it also adds support for setting the branch at the environment level. One way to do so could be the following:

environment:
  production:
    bundle:
      git:
        branch: main

But the nesting is pretty problematic there 😓 Instead, this PR supports:

environment:
  production:
    git:
      branch: main

I'd still be interested in feedback on this!

OriginURL: "http://origin",
Branch: "main",
},
},
Workspace: config.Workspace{
CurrentUser: &config.User{
Expand Down Expand Up @@ -114,6 +118,17 @@ func TestProcessEnvironmentModeProduction(t *testing.T) {
assert.False(t, bundle.Config.Resources.Pipelines["pipeline1"].PipelineSpec.Development)
}

func TestProcessEnvironmentModeProductionGit(t *testing.T) {
bundle := mockBundle(config.Production)

// Pretend the user didn't set Git configuration explicitly
bundle.Config.Bundle.Git.Inferred = true

err := validateProductionMode(context.Background(), bundle, false)
require.ErrorContains(t, err, "git")
bundle.Config.Bundle.Git.Inferred = false
}

func TestProcessEnvironmentModeProductionOkForPrincipal(t *testing.T) {
bundle := mockBundle(config.Production)

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

import (
"context"
"fmt"

"github.com/databricks/cli/bundle"
)

type validateGitDetails struct{}

func ValidateGitDetails() *validateGitDetails {
return &validateGitDetails{}
}

func (m *validateGitDetails) Name() string {
return "ValidateGitDetails"
}

func (m *validateGitDetails) Apply(ctx context.Context, b *bundle.Bundle) error {
if b.Config.Bundle.Git.Branch == "" || b.Config.Bundle.Git.ActualBranch == "" {
return nil
}

if b.Config.Bundle.Git.Branch != b.Config.Bundle.Git.ActualBranch && !b.Config.Bundle.Force {
return fmt.Errorf("not on the right Git branch:\n expected according to configuration: %s\n actual: %s\nuse --force to override", b.Config.Bundle.Git.Branch, b.Config.Bundle.Git.ActualBranch)
}
return nil
}
65 changes: 65 additions & 0 deletions bundle/config/mutator/validate_git_details_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package mutator

import (
"context"
"testing"

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

func TestValidateGitDetailsMatchingBranches(t *testing.T) {
bundle := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Git: config.Git{
Branch: "main",
ActualBranch: "main",
},
},
},
}

m := ValidateGitDetails()
err := m.Apply(context.Background(), bundle)

assert.NoError(t, err)
}

func TestValidateGitDetailsNonMatchingBranches(t *testing.T) {
bundle := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Git: config.Git{
Branch: "main",
ActualBranch: "feature",
},
},
},
}

m := ValidateGitDetails()
err := m.Apply(context.Background(), bundle)

expectedError := "not on the right Git branch:\n expected according to configuration: main\n actual: feature\nuse --force to override"
assert.EqualError(t, err, expectedError)
}

func TestValidateGitDetailsNotUsingGit(t *testing.T) {
bundle := &bundle.Bundle{
Config: config.Root{
Bundle: config.Bundle{
Git: config.Git{
Branch: "main",
ActualBranch: "",
},
},
},
}

m := ValidateGitDetails()
err := m.Apply(context.Background(), bundle)

assert.NoError(t, err)
}
12 changes: 12 additions & 0 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,17 @@ func (r *Root) MergeEnvironment(env *Environment) error {
r.Bundle.ComputeID = env.ComputeID
}

git := &r.Bundle.Git
if env.Git.Branch != "" {
git.Branch = env.Git.Branch
git.Inferred = false
}
if env.Git.Commit != "" {
git.Commit = env.Git.Commit
}
if env.Git.OriginURL != "" {
git.OriginURL = env.Git.OriginURL
}

return nil
}
2 changes: 2 additions & 0 deletions bundle/phases/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package phases
import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/artifacts"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/deploy/files"
"github.com/databricks/cli/bundle/deploy/lock"
"github.com/databricks/cli/bundle/deploy/terraform"
Expand All @@ -15,6 +16,7 @@ func Deploy() bundle.Mutator {
lock.Acquire(),
bundle.Defer(
bundle.Seq(
mutator.ValidateGitDetails(),
files.Upload(),
libraries.MatchWithArtifacts(),
artifacts.CleanUp(),
Expand Down
11 changes: 9 additions & 2 deletions bundle/tests/autoload_git/databricks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
bundle:
name: autoload git config test
git:
branch: foo

environments:
development:
default: true

production:
# production can only be deployed from the 'main' branch
git:
branch: main
15 changes: 10 additions & 5 deletions bundle/tests/autoload_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGitConfig(t *testing.T) {
func TestAutoLoad(t *testing.T) {
b := load(t, "./autoload_git")
assert.Equal(t, "foo", b.Config.Bundle.Git.Branch)
sshUrl := "git@github.com:databricks/cli.git"
httpsUrl := "https://github.com/databricks/cli"
assert.Contains(t, []string{sshUrl, httpsUrl}, b.Config.Bundle.Git.OriginURL)
assert.True(t, b.Config.Bundle.Git.Inferred)
assert.Contains(t, b.Config.Bundle.Git.OriginURL, "/cli")
}

func TestManuallySetBranch(t *testing.T) {
b := loadEnvironment(t, "./autoload_git", "production")
assert.False(t, b.Config.Bundle.Git.Inferred)
assert.Equal(t, "main", b.Config.Bundle.Git.Branch)
assert.Contains(t, b.Config.Bundle.Git.OriginURL, "/cli")
}
10 changes: 6 additions & 4 deletions cmd/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ func newDeployCommand() *cobra.Command {
PreRunE: ConfigureBundleWithVariables,
}

var forceDeploy bool
var force bool
var forceLock bool
var computeID string
cmd.Flags().BoolVar(&forceDeploy, "force", false, "Force acquisition of deployment lock.")
cmd.Flags().BoolVar(&force, "force", false, "Force-override Git branch validation.")
cmd.Flags().BoolVar(&forceLock, "force-deploy", false, "Force acquisition of deployment lock.")
cmd.Flags().StringVarP(&computeID, "compute-id", "c", "", "Override compute in the deployment with the given compute ID.")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
b := bundle.Get(cmd.Context())

// If `--force` is specified, force acquisition of the deployment lock.
b.Config.Bundle.Lock.Force = forceDeploy
b.Config.Bundle.Force = force
b.Config.Bundle.Lock.Force = forceLock
b.Config.Bundle.ComputeID = computeID

return bundle.Apply(cmd.Context(), b, bundle.Seq(
Expand Down
4 changes: 2 additions & 2 deletions cmd/bundle/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func newDestroyCommand() *cobra.Command {
var autoApprove bool
var forceDestroy bool
cmd.Flags().BoolVar(&autoApprove, "auto-approve", false, "Skip interactive approvals for deleting resources and files")
cmd.Flags().BoolVar(&forceDestroy, "force", false, "Force acquisition of deployment lock.")
cmd.Flags().BoolVar(&forceDestroy, "force-lock", false, "Force acquisition of deployment lock.")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
b := bundle.Get(ctx)

// If `--force` is specified, force acquisition of the deployment lock.
// If `--force-lock` is specified, force acquisition of the deployment lock.
b.Config.Bundle.Lock.Force = forceDestroy

// If `--auto-approve`` is specified, we skip confirmation checks
Expand Down