Include job source information in deployed jobs

This commit is contained in:
monalisa 2023-08-07 15:45:36 +02:00
parent f7a76ff5d8
commit 8e8c47b477
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 71 additions and 0 deletions

View File

@ -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
}

View File

@ -28,6 +28,7 @@ func Initialize() bundle.Mutator {
mutator.OverrideCompute(),
mutator.ProcessEnvironmentMode(),
mutator.TranslatePaths(),
mutator.SetJobSources(),
terraform.Initialize(),
},
)

View File

@ -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"))