From d42d5fa17f6cb2f04435a75b96ea9bf18a0fe743 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 27 Sep 2022 15:57:41 -0700 Subject: [PATCH] blueprints: Fix commit message when version is empty With an empty or missing version number the commit message would not include the version (which is set to 0.0.0 by calling Initialize). This adds a call to Initialize() in the API code before constructing the commit message. It also moves the check for non-empty blueprint name into the Initialize call where it belongs. --- internal/blueprint/blueprint.go | 4 ++++ internal/store/store.go | 10 +++------- internal/weldr/api.go | 11 +++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/internal/blueprint/blueprint.go b/internal/blueprint/blueprint.go index 7efc9dfed..1c96edf0a 100644 --- a/internal/blueprint/blueprint.go +++ b/internal/blueprint/blueprint.go @@ -67,6 +67,10 @@ func (b *Blueprint) DeepCopy() Blueprint { // Initialize ensures that the blueprint has sane defaults for any missing fields func (b *Blueprint) Initialize() error { + if len(b.Name) == 0 { + return fmt.Errorf("empty blueprint name not allowed") + } + if b.Packages == nil { b.Packages = []Package{} } diff --git a/internal/store/store.go b/internal/store/store.go index 400ddfd25..bcc37dfee 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -209,17 +209,13 @@ func (s *Store) GetBlueprintChanges(name string) []blueprint.Change { func (s *Store) PushBlueprint(bp blueprint.Blueprint, commitMsg string) error { return s.change(func() error { - if len(bp.Name) == 0 { - return fmt.Errorf("empty blueprint name not allowed") - } - - commit, err := randomSHA1String() + // Make sure the blueprint has default values and that the version is valid + err := bp.Initialize() if err != nil { return err } - // Make sure the blueprint has default values and that the version is valid - err = bp.Initialize() + commit, err := randomSHA1String() if err != nil { return err } diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 73c7dd5f5..aff0364aa 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -1945,6 +1945,17 @@ func (api *API) blueprintsNewHandler(writer http.ResponseWriter, request *http.R } } + // Make sure the blueprint has default values and that the version is valid + err = blueprint.Initialize() + if err != nil { + errors := responseError{ + ID: "BlueprintsError", + Msg: err.Error(), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } + commitMsg := "Recipe " + blueprint.Name + ", version " + blueprint.Version + " saved." err = api.store.PushBlueprint(blueprint, commitMsg) if err != nil {