From 8e8c47b477934bc983532f98414d068dc63fd2db Mon Sep 17 00:00:00 2001 From: monalisa Date: Mon, 7 Aug 2023 15:45:36 +0200 Subject: [PATCH] Include job source information in deployed jobs --- bundle/config/mutator/set_job_sources.go | 65 ++++++++++++++++++++++++ bundle/phases/initialize.go | 1 + libs/git/repository.go | 5 ++ 3 files changed, 71 insertions(+) create mode 100644 bundle/config/mutator/set_job_sources.go diff --git a/bundle/config/mutator/set_job_sources.go b/bundle/config/mutator/set_job_sources.go new file mode 100644 index 00000000..e813616e --- /dev/null +++ b/bundle/config/mutator/set_job_sources.go @@ -0,0 +1,65 @@ +package mutator + +import ( + "context" + "path/filepath" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/libs/git" + "github.com/databricks/databricks-sdk-go/service/jobs" +) + +type setJobSources struct{} + +// TranslatePaths converts paths to local notebook files into paths in the workspace file system. +func SetJobSources() bundle.Mutator { + return &setJobSources{} +} + +func (m *setJobSources) Name() string { + return "SetJobSources" +} + +func (m *setJobSources) Apply(ctx context.Context, b *bundle.Bundle) error { + repo, err := git.NewRepository(b.Config.Path) + if err != nil { + return err + } + if !repo.IsRealRepo() { + return nil + } + + for _, job := range b.Config.Resources.Jobs { + branch := "" + commit := "" + + if b.Config.Bundle.Git.Branch != "" { + // Set branch, If current checkout is a branch. + branch = b.Config.Bundle.Git.Branch + } else { + // Set the commit SHA if current checkout is not a branch. + commit = b.Config.Bundle.Git.Commit + } + + relPath, err := filepath.Rel(repo.Root(), job.ConfigFilePath) + if err != nil { + return err + } + relPath = filepath.ToSlash(relPath) + + job.GitSource = &jobs.GitSource{ + GitBranch: branch, + GitCommit: commit, + GitUrl: b.Config.Bundle.Git.OriginURL, + JobSource: &jobs.JobSource{ + ImportFromGitBranch: branch, + + // Set job source config path, i.e the path to yml file containing + // the job definition. + JobConfigPath: relPath, + }, + } + } + + return nil +} diff --git a/bundle/phases/initialize.go b/bundle/phases/initialize.go index fc5056f6..08e9c57c 100644 --- a/bundle/phases/initialize.go +++ b/bundle/phases/initialize.go @@ -28,6 +28,7 @@ func Initialize() bundle.Mutator { mutator.OverrideCompute(), mutator.ProcessEnvironmentMode(), mutator.TranslatePaths(), + mutator.SetJobSources(), terraform.Initialize(), }, ) diff --git a/libs/git/repository.go b/libs/git/repository.go index 3b93669a..17b25101 100644 --- a/libs/git/repository.go +++ b/libs/git/repository.go @@ -43,6 +43,11 @@ func (r *Repository) Root() string { return r.rootPath } +// Returns if the *Repository struct represents a real git repository +func (r *Repository) IsRealRepo() bool { + return r.real +} + func (r *Repository) CurrentBranch() (string, error) { // load .git/HEAD ref, err := LoadReferenceFile(filepath.Join(r.rootPath, ".git", "HEAD"))