From 1f3fc335496631207536d022e6245b66ddf5d537 Mon Sep 17 00:00:00 2001 From: idelafuente-oc Date: Mon, 16 Nov 2026 15:10:01 -0300 Subject: [PATCH] Add opt-in bundle Git branch freshness check Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .nextchanges/bundles/git-branch-freshness.md | 1 + .../deploy/git-freshness/databricks.yml | 16 ++ .../bundle/deploy/git-freshness/out.test.toml | 2 + .../bundle/deploy/git-freshness/output.txt | 24 +++ acceptance/bundle/deploy/git-freshness/script | 19 ++ .../bundle/deploy/git-freshness/test.toml | 1 + bundle/config/experimental.go | 3 + bundle/internal/schema/annotations.yml | 8 + bundle/phases/git_freshness.go | 120 ++++++++++++ bundle/phases/git_freshness_test.go | 174 ++++++++++++++++++ bundle/schema/jsonschema.json | 4 + cmd/bundle/utils/process.go | 6 + 12 files changed, 378 insertions(+) create mode 100644 .nextchanges/bundles/git-branch-freshness.md create mode 100644 acceptance/bundle/deploy/git-freshness/databricks.yml create mode 100644 acceptance/bundle/deploy/git-freshness/out.test.toml create mode 100644 acceptance/bundle/deploy/git-freshness/output.txt create mode 100644 acceptance/bundle/deploy/git-freshness/script create mode 100644 acceptance/bundle/deploy/git-freshness/test.toml create mode 100644 bundle/phases/git_freshness.go create mode 100644 bundle/phases/git_freshness_test.go diff --git a/.nextchanges/bundles/git-branch-freshness.md b/.nextchanges/bundles/git-branch-freshness.md new file mode 100644 index 00000000000..e3f6aad8ed1 --- /dev/null +++ b/.nextchanges/bundles/git-branch-freshness.md @@ -0,0 +1 @@ +* Add an opt-in Git branch freshness check before bundle deployment. diff --git a/acceptance/bundle/deploy/git-freshness/databricks.yml b/acceptance/bundle/deploy/git-freshness/databricks.yml new file mode 100644 index 00000000000..e642f9ec335 --- /dev/null +++ b/acceptance/bundle/deploy/git-freshness/databricks.yml @@ -0,0 +1,16 @@ +bundle: + name: git-freshness + +experimental: + check_git_branch_freshness: true + +sync: + exclude: + - remote.git/** + - writer/** + - tmp.plan.json + +resources: + jobs: + job: + name: test-job diff --git a/acceptance/bundle/deploy/git-freshness/out.test.toml b/acceptance/bundle/deploy/git-freshness/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/deploy/git-freshness/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/deploy/git-freshness/output.txt b/acceptance/bundle/deploy/git-freshness/output.txt new file mode 100644 index 00000000000..2d8611f6b21 --- /dev/null +++ b/acceptance/bundle/deploy/git-freshness/output.txt @@ -0,0 +1,24 @@ + +>>> [CLI] bundle plan -o json + +=== A stale branch blocks non-interactive deployments + +>>> [CLI] bundle deploy +Warning: Git branch "main" is 1 commit(s) behind its upstream "origin/main" + +Error: Git branch "main" is 1 commit(s) behind its upstream "origin/main"; the current console does not support prompting. To proceed, use --auto-approve (which also approves other deployment actions) + +>>> [CLI] bundle deploy --plan tmp.plan.json +Warning: Git branch "main" is 1 commit(s) behind its upstream "origin/main" + +Error: Git branch "main" is 1 commit(s) behind its upstream "origin/main"; the current console does not support prompting. To proceed, use --auto-approve (which also approves other deployment actions) + +=== Auto-approve allows a stale branch + +>>> [CLI] bundle deploy --plan tmp.plan.json --auto-approve +Warning: Git branch "main" is 1 commit(s) behind its upstream "origin/main" + +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/git-freshness/default/files... +Created jobs.job +Files: 2 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged diff --git a/acceptance/bundle/deploy/git-freshness/script b/acceptance/bundle/deploy/git-freshness/script new file mode 100644 index 00000000000..6cb888cb3c6 --- /dev/null +++ b/acceptance/bundle/deploy/git-freshness/script @@ -0,0 +1,19 @@ +git-repo-init +git clone --bare . remote.git &> LOG.git +git remote add origin remote.git +git fetch origin >> LOG.git 2>&1 +git branch --set-upstream-to=origin/main main >> LOG.git 2>&1 +git clone remote.git writer >> LOG.git 2>&1 +git -C writer config user.name "Tester" +git -C writer config user.email "tester@databricks.com" + +trace $CLI bundle plan -o json > tmp.plan.json +git -C writer commit --allow-empty -m "Update upstream" >> LOG.git 2>&1 +git -C writer push >> LOG.git 2>&1 + +title "A stale branch blocks non-interactive deployments" +musterr trace $CLI bundle deploy +musterr trace $CLI bundle deploy --plan tmp.plan.json + +title "Auto-approve allows a stale branch" +trace $CLI bundle deploy --plan tmp.plan.json --auto-approve diff --git a/acceptance/bundle/deploy/git-freshness/test.toml b/acceptance/bundle/deploy/git-freshness/test.toml new file mode 100644 index 00000000000..843ac68eb06 --- /dev/null +++ b/acceptance/bundle/deploy/git-freshness/test.toml @@ -0,0 +1 @@ +Ignore = [".git", "remote.git", "writer", "tmp.plan.json"] diff --git a/bundle/config/experimental.go b/bundle/config/experimental.go index bfbd02f5adb..5274040e9c6 100644 --- a/bundle/config/experimental.go +++ b/bundle/config/experimental.go @@ -3,6 +3,9 @@ package config type Experimental struct { Scripts map[ScriptHook]Command `json:"scripts,omitempty"` + // CheckGitBranchFreshness checks the current branch against its upstream before deployment. + CheckGitBranchFreshness *bool `json:"check_git_branch_freshness,omitempty"` + // ImmutableFolder specifies that bundle files and artifacts are uploaded as a // single immutable snapshot rather than being synced individually. When true, // the deployment calls /api/2.0/repos/snapshots with a zip of all files and sets diff --git a/bundle/internal/schema/annotations.yml b/bundle/internal/schema/annotations.yml index 58abf721658..0e893dec238 100644 --- a/bundle/internal/schema/annotations.yml +++ b/bundle/internal/schema/annotations.yml @@ -159,6 +159,14 @@ experimental: "description": |- Defines attributes for experimental features. "$fields": + "check_git_branch_freshness": + "description": |- + Before deployment, fetch the current Git branch's upstream and ask for confirmation if the branch is behind. This also applies when deploying a saved plan. If the check cannot run, a warning is emitted and deployment continues. Use --auto-approve to proceed without prompting, including in CI; this also approves other deployment actions. + "markdown_examples": |- + ```yaml + experimental: + check_git_branch_freshness: true + ``` "deployment_history": "description": |- Whether to record deployment history using the deployment metadata service (DMS), which tracks what changed across deployments. diff --git a/bundle/phases/git_freshness.go b/bundle/phases/git_freshness.go new file mode 100644 index 00000000000..b5beaf132e5 --- /dev/null +++ b/bundle/phases/git_freshness.go @@ -0,0 +1,120 @@ +package phases + +import ( + "context" + "errors" + "fmt" + "os" + "os/exec" + "slices" + "strconv" + "strings" + "time" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/diag" + "github.com/databricks/cli/libs/logdiag" +) + +const gitFreshnessTimeout = 30 * time.Second + +// CheckGitBranchFreshness asks for approval when the current branch is behind its upstream. +func CheckGitBranchFreshness(ctx context.Context, b *bundle.Bundle) error { + if b.Config.Experimental == nil || !config.IsExplicitlyEnabled(b.Config.Experimental.CheckGitBranchFreshness) { + return nil + } + + checkCtx, cancel := context.WithTimeout(ctx, gitFreshnessTimeout) + defer cancel() + + branch, upstream, behind, err := gitBranchFreshness(checkCtx, b.BundleRoot.Native()) + if err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + if checkCtx.Err() != nil { + err = checkCtx.Err() + } + logdiag.LogDiag(ctx, diag.Warningf("could not check Git branch freshness: %s; run git fetch and verify the branch's upstream", err)[0]) + return nil + } + if behind == 0 { + return nil + } + + message := fmt.Sprintf("Git branch %q is %d commit(s) behind its upstream %q", branch, behind, upstream) + logdiag.LogDiag(ctx, diag.Warningf("%s", message)[0]) + if b.AutoApprove { + return nil + } + if !cmdio.IsPromptSupported(ctx) { + return fmt.Errorf("%s; the current console does not support prompting. To proceed, use --auto-approve (which also approves other deployment actions)", message) + } + approved, err := cmdio.AskYesOrNo(ctx, "Deploy from this branch anyway?") + if err != nil { + return fmt.Errorf("confirming deployment from a stale Git branch: %w", err) + } + if !approved { + return errors.New("deployment cancelled") + } + return nil +} + +func gitBranchFreshness(ctx context.Context, dir string) (string, string, int, error) { + branch, err := gitFreshnessCommand(ctx, dir, "symbolic-ref", "--quiet", "--short", "HEAD") + if err != nil { + return "", "", 0, fmt.Errorf("cannot determine the current branch: %w", err) + } + // https://git-scm.com/docs/git-config#Documentation/git-config.txt-branchltnamegtmerge + remote, err := gitFreshnessCommand(ctx, dir, "config", "--get", "branch."+branch+".remote") + if err != nil { + return "", "", 0, fmt.Errorf("cannot determine the upstream of branch %q: %w", branch, err) + } + refs, err := gitFreshnessCommand(ctx, dir, "config", "--get-all", "branch."+branch+".merge") + if err != nil { + return "", "", 0, fmt.Errorf("cannot determine the upstream of branch %q: %w", branch, err) + } + ref, _, _ := strings.Cut(refs, "\n") + if remote == "" || ref == "" { + return "", "", 0, fmt.Errorf("cannot determine the upstream of branch %q", branch) + } + upstream := strings.TrimPrefix(ref, "refs/heads/") + if remote != "." { + remotes, err := gitFreshnessCommand(ctx, dir, "remote") + if err != nil { + return "", "", 0, fmt.Errorf("cannot list Git remotes: %w", err) + } + // branch..remote can be a URL containing credentials, not just a remote name. + if slices.Contains(strings.Split(remotes, "\n"), remote) { + upstream = remote + "/" + upstream + } + } + if _, err := gitFreshnessCommand(ctx, dir, "fetch", "--quiet", "--", remote, ref); err != nil { + return "", "", 0, fmt.Errorf("git fetch failed: %w", err) + } + // The tracking ref may not be updated if the remote's fetch refspec excludes it. + count, err := gitFreshnessCommand(ctx, dir, "rev-list", "--count", "HEAD..FETCH_HEAD") + if err != nil { + return "", "", 0, fmt.Errorf("cannot compare branch %q with its upstream: %w", branch, err) + } + behind, err := strconv.Atoi(count) + if err != nil || behind < 0 { + return "", "", 0, fmt.Errorf("invalid Git commit count %q", count) + } + return branch, upstream, behind, nil +} + +func gitFreshnessCommand(ctx context.Context, dir string, args ...string) (string, error) { + cmd := exec.CommandContext(ctx, "git", args...) + cmd.Dir = dir + cmd.WaitDelay = time.Second + // https://git-scm.com/docs/git#Documentation/git.txt-codeGITTERMINALPROMPTcode + cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") + out, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(out)), nil +} diff --git a/bundle/phases/git_freshness_test.go b/bundle/phases/git_freshness_test.go new file mode 100644 index 00000000000..6986fb90a8e --- /dev/null +++ b/bundle/phases/git_freshness_test.go @@ -0,0 +1,174 @@ +package phases + +import ( + "bytes" + "context" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/flags" + "github.com/databricks/cli/libs/logdiag" + "github.com/databricks/cli/libs/vfs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func runGit(t *testing.T, dir string, args ...string) { + t.Helper() + cmd := exec.CommandContext(t.Context(), "git", args...) + cmd.Dir = dir + output, err := cmd.CombinedOutput() + require.NoError(t, err, "git %v: %s", args, output) +} + +func freshnessRepositories(t *testing.T) (string, string) { + t.Helper() + dir := t.TempDir() + remote := filepath.Join(dir, "remote.git") + local := filepath.Join(dir, "local") + writer := filepath.Join(dir, "writer") + runGit(t, dir, "init", "--bare", "--initial-branch=main", remote) + runGit(t, dir, "clone", remote, writer) + runGit(t, writer, "config", "user.name", "Test") + runGit(t, writer, "config", "user.email", "test@example.test") + runGit(t, writer, "commit", "--allow-empty", "-m", "initial") + runGit(t, writer, "push", "-u", "origin", "main") + runGit(t, dir, "clone", remote, local) + return local, writer +} + +func freshnessContext(ctx context.Context, input string, interactive bool) (context.Context, *bytes.Buffer) { + var stderr bytes.Buffer + if interactive { + ctx = cmdio.InContext(ctx, cmdio.NewTestIO(strings.NewReader(input), &bytes.Buffer{}, &stderr)) + } else { + ctx = cmdio.InContext(ctx, cmdio.NewIO(ctx, flags.OutputText, strings.NewReader(input), &bytes.Buffer{}, &stderr, "", "")) + } + return logdiag.InitContext(ctx), &stderr +} + +func TestCheckGitBranchFreshness(t *testing.T) { + local, writer := freshnessRepositories(t) + b := &bundle.Bundle{ + BundleRoot: vfs.MustNew(local), + Config: config.Root{ + Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)}, + }, + } + + ctx, output := freshnessContext(t.Context(), "", false) + require.NoError(t, CheckGitBranchFreshness(ctx, b)) + assert.Empty(t, output.String()) + + runGit(t, local, "config", "--replace-all", "remote.origin.fetch", "+refs/heads/other:refs/remotes/origin/other") + runGit(t, writer, "commit", "--allow-empty", "-m", "one") + runGit(t, writer, "commit", "--allow-empty", "-m", "two") + runGit(t, writer, "push") + + tests := []struct { + name string + input string + interactive bool + autoApprove bool + wantErr string + }{ + {name: "noninteractive", wantErr: "--auto-approve"}, + {name: "approved", input: "yes\n", interactive: true}, + {name: "declined", input: "no\n", interactive: true, wantErr: "deployment cancelled"}, + {name: "auto approved", autoApprove: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx, output := freshnessContext(t.Context(), tt.input, tt.interactive) + b.AutoApprove = tt.autoApprove + err := CheckGitBranchFreshness(ctx, b) + if tt.wantErr != "" { + require.ErrorContains(t, err, tt.wantErr) + } else { + require.NoError(t, err) + } + assert.Contains(t, output.String(), `Git branch "main" is 2 commit(s) behind its upstream "origin/main"`) + }) + } + + b.Config.Experimental.CheckGitBranchFreshness = new(false) + ctx, output = freshnessContext(t.Context(), "", false) + require.NoError(t, CheckGitBranchFreshness(ctx, b)) + assert.Empty(t, output.String()) +} + +func TestCheckGitBranchFreshnessUnavailable(t *testing.T) { + local, _ := freshnessRepositories(t) + b := &bundle.Bundle{ + BundleRoot: vfs.MustNew(local), + Config: config.Root{ + Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)}, + }, + } + + runGit(t, local, "branch", "--unset-upstream") + ctx, output := freshnessContext(t.Context(), "", false) + require.NoError(t, CheckGitBranchFreshness(ctx, b)) + assert.Contains(t, output.String(), `cannot determine the upstream of branch "main"`) + + runGit(t, local, "branch", "--set-upstream-to=origin/main") + runGit(t, local, "remote", "set-url", "origin", filepath.Join(t.TempDir(), "missing.git")) + ctx, output = freshnessContext(t.Context(), "", false) + require.NoError(t, CheckGitBranchFreshness(ctx, b)) + assert.Contains(t, output.String(), "git fetch failed") + + ctx, cancel := context.WithCancel(t.Context()) + cancel() + ctx, output = freshnessContext(ctx, "", false) + require.ErrorIs(t, CheckGitBranchFreshness(ctx, b), context.Canceled) + assert.Empty(t, output.String()) + + runGit(t, local, "checkout", "--detach") + ctx, output = freshnessContext(t.Context(), "", false) + require.NoError(t, CheckGitBranchFreshness(ctx, b)) + assert.Contains(t, output.String(), "cannot determine the current branch") +} + +func TestCheckGitBranchFreshnessLocalUpstream(t *testing.T) { + local, _ := freshnessRepositories(t) + runGit(t, local, "config", "user.name", "Test") + runGit(t, local, "config", "user.email", "test@example.test") + runGit(t, local, "checkout", "-b", "other") + runGit(t, local, "commit", "--allow-empty", "-m", "advance local branch") + runGit(t, local, "checkout", "main") + runGit(t, local, "branch", "--set-upstream-to=other") + + b := &bundle.Bundle{ + BundleRoot: vfs.MustNew(local), + Config: config.Root{ + Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)}, + }, + } + ctx, output := freshnessContext(t.Context(), "", false) + require.ErrorContains(t, CheckGitBranchFreshness(ctx, b), "--auto-approve") + assert.Contains(t, output.String(), `Git branch "main" is 1 commit(s) behind its upstream "other"`) +} + +func TestCheckGitBranchFreshnessRemoteURL(t *testing.T) { + local, writer := freshnessRepositories(t) + runGit(t, writer, "commit", "--allow-empty", "-m", "advance upstream") + runGit(t, writer, "push") + remote := filepath.Join(filepath.Dir(local), "remote.git") + runGit(t, local, "config", "branch.main.remote", remote) + + b := &bundle.Bundle{ + BundleRoot: vfs.MustNew(local), + Config: config.Root{ + Experimental: &config.Experimental{CheckGitBranchFreshness: new(true)}, + }, + } + ctx, output := freshnessContext(t.Context(), "", false) + require.ErrorContains(t, CheckGitBranchFreshness(ctx, b), "--auto-approve") + assert.Contains(t, output.String(), `Git branch "main" is 1 commit(s) behind its upstream "main"`) + assert.NotContains(t, output.String(), remote) +} diff --git a/bundle/schema/jsonschema.json b/bundle/schema/jsonschema.json index 2d5a35b5a5e..6bc6ee8d1e8 100644 --- a/bundle/schema/jsonschema.json +++ b/bundle/schema/jsonschema.json @@ -4193,6 +4193,10 @@ { "type": "object", "properties": { + "check_git_branch_freshness": { + "description": "Before deployment, fetch the current Git branch's upstream and ask for confirmation if the branch is behind. This also applies when deploying a saved plan. If the check cannot run, a warning is emitted and deployment continues. Use --auto-approve to proceed without prompting, including in CI; this also approves other deployment actions.", + "$ref": "#/$defs/bool" + }, "deployment_history": { "description": "Whether to record deployment history using the deployment metadata service (DMS), which tracks what changed across deployments.", "$ref": "#/$defs/bool" diff --git a/cmd/bundle/utils/process.go b/cmd/bundle/utils/process.go index 68798a4bb01..d5f96b40ead 100644 --- a/cmd/bundle/utils/process.go +++ b/cmd/bundle/utils/process.go @@ -233,6 +233,12 @@ func ProcessBundleRet(cmd *cobra.Command, opts ProcessOptions) (b *bundle.Bundle return b, nil, err } + if opts.Deploy { + if err := phases.CheckGitBranchFreshness(ctx, b); err != nil { + return b, nil, err + } + } + // Record the engine up front so deploy telemetry reports it even when // the deploy fails before the state is pulled (e.g. PullResourcesState itself // errors). Refined to the state's engine below once it is known; the two differ