From 316fdd3643d76031da3aff902b7d8b53d5abd45b Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 20 Jun 2024 08:36:09 +0200 Subject: [PATCH] fix: abra app new checks out latest version See https://git.coopcloud.tech/coop-cloud/organising/issues/618 --- cli/app/new.go | 25 ++++++++++++++++++++++--- pkg/recipe/recipe.go | 1 + tests/integration/app_new.bats | 13 +++++++++++-- tests/integration/helpers/git.bash | 8 ++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/cli/app/new.go b/cli/app/new.go index cdc713ab..e31e970d 100644 --- a/cli/app/new.go +++ b/cli/app/new.go @@ -10,7 +10,6 @@ import ( "coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/formatter" "coopcloud.tech/abra/pkg/jsontable" - "coopcloud.tech/abra/pkg/recipe" recipePkg "coopcloud.tech/abra/pkg/recipe" "coopcloud.tech/abra/pkg/secret" "github.com/AlecAivazis/survey/v2" @@ -78,9 +77,29 @@ var appNewCommand = cli.Command{ } } if c.Args().Get(1) == "" { - if err := recipePkg.EnsureLatest(recipe.Name); err != nil { + var version string + + recipeVersions, err := recipePkg.GetRecipeVersions(recipe.Name, internal.Offline) + if err != nil { logrus.Fatal(err) } + + // NOTE(d1): determine whether recipe versions exist or not and check + // out the latest version or current HEAD + if len(recipeVersions) > 0 { + latest := recipeVersions[len(recipeVersions)-1] + for tag := range latest { + version = tag + } + + if err := recipePkg.EnsureVersion(recipe.Name, version); err != nil { + logrus.Fatal(err) + } + } else { + if err := recipePkg.EnsureLatest(recipe.Name); err != nil { + logrus.Fatal(err) + } + } } else { if err := recipePkg.EnsureVersion(recipe.Name, c.Args().Get(1)); err != nil { logrus.Fatal(err) @@ -212,7 +231,7 @@ func createSecrets(cl *dockerClient.Client, secretsConfig map[string]secret.Secr } // ensureDomainFlag checks if the domain flag was used. if not, asks the user for it/ -func ensureDomainFlag(recipe recipe.Recipe, server string) error { +func ensureDomainFlag(recipe recipePkg.Recipe, server string) error { if internal.Domain == "" && !internal.NoInput { prompt := &survey.Input{ Message: "Specify app domain", diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index 4184e2db..c9df9feb 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -945,6 +945,7 @@ func GetRecipeVersions(recipeName string, offline bool) (RecipeVersions, error) if err != nil { return versions, err } + sortRecipeVersions(versions) logrus.Debugf("collected %s for %s", versions, recipeName) diff --git a/tests/integration/app_new.bats b/tests/integration/app_new.bats index 457ee152..0bba9234 100644 --- a/tests/integration/app_new.bats +++ b/tests/integration/app_new.bats @@ -37,6 +37,15 @@ teardown(){ } @test "create new app" { + _fetch_recipe + _latest_tag + + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" checkout $LATEST_TAG + assert_success + + _latest_hash + _reset_recipe + run $ABRA app new "$TEST_RECIPE" \ --no-input \ --server "$TEST_SERVER" \ @@ -44,8 +53,8 @@ teardown(){ assert_success assert_exists "$ABRA_DIR/servers/$TEST_SERVER/$TEST_APP_DOMAIN.env" - run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" status - assert_output --partial "up to date" + run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" log -1 + assert_output --partial "$LATEST_HASH" } @test "create new app with version" { diff --git a/tests/integration/helpers/git.bash b/tests/integration/helpers/git.bash index e11b76de..2bde0df1 100644 --- a/tests/integration/helpers/git.bash +++ b/tests/integration/helpers/git.bash @@ -35,3 +35,11 @@ _set_git_author() { run git -C "$ABRA_DIR/recipes/$TEST_RECIPE" config --local user.name test assert_success } + +_latest_tag() { + export LATEST_TAG=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" describe --abbrev=0) +} + +_latest_hash() { + export LATEST_HASH=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" log -1 --pretty=format:%h) +} -- 2.47.2