feat: support chaos commits on deploy

See coop-cloud/organising#517
This commit is contained in:
2024-07-09 11:31:52 +02:00
parent 0ff8e49cfd
commit 2fb5493ab5
10 changed files with 115 additions and 35 deletions

View File

@ -57,21 +57,23 @@ func (r Recipe) EnsureExists() error {
}
// EnsureVersion checks whether a specific version exists for a recipe.
func (r Recipe) EnsureVersion(version string) error {
func (r Recipe) EnsureVersion(version string) (bool, error) {
isChaosCommit := false
recipeDir := path.Join(config.RECIPES_DIR, r.Name)
if err := gitPkg.EnsureGitRepo(recipeDir); err != nil {
return err
return isChaosCommit, err
}
repo, err := git.PlainOpen(recipeDir)
if err != nil {
return err
return isChaosCommit, err
}
tags, err := repo.Tags()
if err != nil {
return nil
return isChaosCommit, err
}
var parsedTags []string
@ -83,7 +85,7 @@ func (r Recipe) EnsureVersion(version string) error {
}
return nil
}); err != nil {
return err
return isChaosCommit, err
}
joinedTags := strings.Join(parsedTags, ", ")
@ -91,27 +93,33 @@ func (r Recipe) EnsureVersion(version string) error {
log.Debugf("read %s as tags for recipe %s", joinedTags, r.Name)
}
var opts *git.CheckoutOptions
if tagRef.String() == "" {
return fmt.Errorf("the local copy of %s doesn't seem to have version %s available?", r.Name, version)
log.Debugf("attempting to checkout '%s' as chaos commit", version)
hash, err := repo.ResolveRevision(plumbing.Revision(version))
if err != nil {
log.Fatalf("unable to resolve '%s': %s", version, err)
}
opts = &git.CheckoutOptions{Hash: *hash, Create: false, Force: true}
isChaosCommit = true
} else {
opts = &git.CheckoutOptions{Branch: tagRef, Create: false, Force: true}
}
worktree, err := repo.Worktree()
if err != nil {
return err
return isChaosCommit, nil
}
opts := &git.CheckoutOptions{
Branch: tagRef,
Create: false,
Force: true,
}
if err := worktree.Checkout(opts); err != nil {
return err
return isChaosCommit, nil
}
log.Debugf("successfully checked %s out to %s in %s", r.Name, tagRef.Short(), recipeDir)
return nil
return isChaosCommit, nil
}
// EnsureIsClean makes sure that the recipe repository has no unstaged changes.