refactor!: simplifying publish logic
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-12-27 19:56:27 +01:00
parent eb1b6be4c5
commit 0aa37fcee8
10 changed files with 137 additions and 168 deletions

View File

@ -47,20 +47,16 @@ Abra does its best to read the "a.b.c" version scheme and communicate what
action needs to be taken when performing different operations such as an update
or a rollback of an app.
You may invoke this command in "wizard" mode and be prompted for input:
abra recipe release gitea
Publish your new release git.coopcloud.tech with "-p/--publish". This requires
that you have permission to git push to these repositories and have your SSH
keys configured on your account.
`,
Flags: []cli.Flag{
internal.DryFlag,
internal.MajorFlag,
internal.MinorFlag,
internal.PatchFlag,
internal.PushFlag,
internal.CommitFlag,
internal.CommitMessageFlag,
internal.TagMessageFlag,
internal.PublishFlag,
},
BashComplete: autocomplete.RecipeNameComplete,
Action: func(c *cli.Context) error {
@ -71,7 +67,11 @@ You may invoke this command in "wizard" mode and be prompted for input:
logrus.Fatal(err)
}
mainApp := internal.GetMainApp(recipe)
mainApp, err := internal.GetMainAppImage(recipe)
if err != nil {
logrus.Fatal(err)
}
mainAppVersion := imagesTmp[mainApp]
if mainAppVersion == "" {
logrus.Fatalf("main app service version for %s is empty?", recipe.Name)
@ -201,14 +201,14 @@ func createReleaseFromTag(recipe recipe.Recipe, tagString, mainAppVersion string
tag.MissingPatch = false
}
if err := commitRelease(recipe); err != nil {
logrus.Fatal(err)
}
if tagString == "" {
tagString = fmt.Sprintf("%s+%s", tag.String(), mainAppVersion)
}
if err := commitRelease(recipe, tagString); err != nil {
logrus.Fatal(err)
}
if err := tagRelease(tagString, repo); err != nil {
logrus.Fatal(err)
}
@ -230,50 +230,30 @@ func btoi(b bool) int {
}
// getTagCreateOptions constructs git tag create options
func getTagCreateOptions() (git.CreateTagOptions, error) {
if internal.TagMessage == "" && !internal.NoInput {
prompt := &survey.Input{
Message: "git tag message?",
Default: "chore: publish new release",
}
if err := survey.AskOne(prompt, &internal.TagMessage); err != nil {
return git.CreateTagOptions{}, err
}
}
return git.CreateTagOptions{Message: internal.TagMessage}, nil
func getTagCreateOptions(tag string) (git.CreateTagOptions, error) {
msg := fmt.Sprintf("chore: publish %s release", tag)
return git.CreateTagOptions{Message: msg}, nil
}
func commitRelease(recipe recipe.Recipe) error {
func commitRelease(recipe recipe.Recipe, tag string) error {
if internal.Dry {
logrus.Info("dry run: no changed committed")
logrus.Debugf("dry run: no changes committed")
return nil
}
if !internal.Commit && !internal.NoInput {
prompt := &survey.Confirm{
Message: "git commit changes?",
}
if err := survey.AskOne(prompt, &internal.Commit); err != nil {
return err
}
isClean, err := gitPkg.IsClean(recipe.Dir())
if err != nil {
return err
}
if internal.CommitMessage == "" && !internal.NoInput && internal.Commit {
prompt := &survey.Input{
Message: "git commit message?",
Default: "chore: publish new version",
}
if err := survey.AskOne(prompt, &internal.CommitMessage); err != nil {
return err
}
if isClean {
return fmt.Errorf("no changes discovered in %s, nothing to publish?", recipe.Dir())
}
if internal.Commit {
if internal.Publish {
msg := fmt.Sprintf("chore: publish %s release", tag)
repoPath := path.Join(config.RECIPES_DIR, recipe.Name)
if err := gitPkg.Commit(repoPath, "compose.**yml", internal.CommitMessage, internal.Dry); err != nil {
if err := gitPkg.Commit(repoPath, "compose.**yml", msg, internal.Dry); err != nil {
return err
}
}
@ -283,7 +263,7 @@ func commitRelease(recipe recipe.Recipe) error {
func tagRelease(tagString string, repo *git.Repository) error {
if internal.Dry {
logrus.Infof("dry run: no git tag created (%s)", tagString)
logrus.Debugf("dry run: no git tag created (%s)", tagString)
return nil
}
@ -292,7 +272,7 @@ func tagRelease(tagString string, repo *git.Repository) error {
return err
}
createTagOptions, err := getTagCreateOptions()
createTagOptions, err := getTagCreateOptions(tagString)
if err != nil {
return err
}
@ -303,33 +283,36 @@ func tagRelease(tagString string, repo *git.Repository) error {
}
hash := abraFormatter.SmallSHA(head.Hash().String())
logrus.Info(fmt.Sprintf("created tag %s at %s", tagString, hash))
logrus.Debugf(fmt.Sprintf("created tag %s at %s", tagString, hash))
return nil
}
func pushRelease(recipe recipe.Recipe) error {
if internal.Dry {
logrus.Info("dry run: no changes pushed")
logrus.Info("dry run: no changes published")
return nil
}
if !internal.Push && !internal.NoInput {
if !internal.Publish && !internal.NoInput {
prompt := &survey.Confirm{
Message: "git push changes?",
Message: "publish new release?",
}
if err := survey.AskOne(prompt, &internal.Push); err != nil {
if err := survey.AskOne(prompt, &internal.Publish); err != nil {
return err
}
}
if internal.Push {
if internal.Publish {
if err := recipe.Push(internal.Dry); err != nil {
return err
}
}
url := fmt.Sprintf("%s/%s/tags", config.REPOS_BASE_URL, recipe.Name)
logrus.Infof("new release published: %s", url)
return nil
}
@ -392,13 +375,13 @@ func createReleaseFromPreviousTag(tagString, mainAppVersion string, recipe recip
newTag.Major = strconv.Itoa(now + 1)
}
if err := commitRelease(recipe); err != nil {
logrus.Fatal(err)
}
newTag.Metadata = mainAppVersion
newTagString := newTag.String()
if err := commitRelease(recipe, newTagString); err != nil {
logrus.Fatal(err)
}
if err := tagRelease(newTagString, repo); err != nil {
logrus.Fatal(err)
}
@ -422,7 +405,7 @@ func cleanUpTag(tag, recipeName string) error {
return err
}
logrus.Warnf("removed freshly created tag %s", tag)
logrus.Debugf("removed freshly created tag %s", tag)
return nil
}