diff --git a/api/http/handler/stacks/create_compose_stack.go b/api/http/handler/stacks/create_compose_stack.go index fc5bed1ff..4e1f4b904 100644 --- a/api/http/handler/stacks/create_compose_stack.go +++ b/api/http/handler/stacks/create_compose_stack.go @@ -269,7 +269,7 @@ func (handler *Handler) createComposeStackFromGitRepository(w http.ResponseWrite //make sure the webhook ID is unique if payload.AutoUpdate != nil && payload.AutoUpdate.Webhook != "" { - isUnique, err := handler.checkUniqueWebhookID(payload.AutoUpdate.Webhook) + isUnique, err := handler.checkUniqueWebhookID(handler.DataStore, payload.AutoUpdate.Webhook) if err != nil { return httperror.InternalServerError("Unable to check for webhook ID collision", err) } diff --git a/api/http/handler/stacks/create_kubernetes_stack.go b/api/http/handler/stacks/create_kubernetes_stack.go index 530d2eaee..a9d0845d4 100644 --- a/api/http/handler/stacks/create_kubernetes_stack.go +++ b/api/http/handler/stacks/create_kubernetes_stack.go @@ -214,7 +214,7 @@ func (handler *Handler) createKubernetesStackFromGitRepository(w http.ResponseWr // Make sure the webhook ID is unique if payload.AutoUpdate != nil && payload.AutoUpdate.Webhook != "" { - if isUnique, err := handler.checkUniqueWebhookID(payload.AutoUpdate.Webhook); err != nil { + if isUnique, err := handler.checkUniqueWebhookID(handler.DataStore, payload.AutoUpdate.Webhook); err != nil { return httperror.InternalServerError("Unable to check for webhook ID collision", err) } else if !isUnique { return httperror.Conflict(fmt.Sprintf("Webhook ID: %s already exists", payload.AutoUpdate.Webhook), stackutils.ErrWebhookIDAlreadyExists) diff --git a/api/http/handler/stacks/create_swarm_stack.go b/api/http/handler/stacks/create_swarm_stack.go index e10d23f2f..6a12d1670 100644 --- a/api/http/handler/stacks/create_swarm_stack.go +++ b/api/http/handler/stacks/create_swarm_stack.go @@ -192,28 +192,23 @@ func createStackPayloadFromSwarmGitPayload(name, swarmID, repoUrl, repoReference // @router /stacks/create/swarm/repository [post] func (handler *Handler) createSwarmStackFromGitRepository(w http.ResponseWriter, r *http.Request, endpoint *portainer.Endpoint, userID portainer.UserID) *httperror.HandlerError { var payload swarmStackFromGitRepositoryPayload - err := request.DecodeAndValidateJSONPayload(r, &payload) - if err != nil { + if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil { return httperror.BadRequest("Invalid request payload", err) } payload.Name = handler.SwarmStackManager.NormalizeStackName(payload.Name) - isUnique, err := handler.checkUniqueStackNameInDocker(endpoint, payload.Name, 0, true) - if err != nil { + if isUnique, err := handler.checkUniqueStackNameInDocker(endpoint, payload.Name, 0, true); err != nil { return httperror.InternalServerError("Unable to check for name collision", err) - } - if !isUnique { + } else if !isUnique { return stackExistsError(payload.Name) } //make sure the webhook ID is unique if payload.AutoUpdate != nil && payload.AutoUpdate.Webhook != "" { - isUnique, err := handler.checkUniqueWebhookID(payload.AutoUpdate.Webhook) - if err != nil { + if isUnique, err := handler.checkUniqueWebhookID(handler.DataStore, payload.AutoUpdate.Webhook); err != nil { return httperror.InternalServerError("Unable to check for webhook ID collision", err) - } - if !isUnique { + } else if !isUnique { return httperror.Conflict(fmt.Sprintf("Webhook ID: %s already exists", payload.AutoUpdate.Webhook), stackutils.ErrWebhookIDAlreadyExists) } } diff --git a/api/http/handler/stacks/handler.go b/api/http/handler/stacks/handler.go index b8ff2eba0..bb0486d72 100644 --- a/api/http/handler/stacks/handler.go +++ b/api/http/handler/stacks/handler.go @@ -206,9 +206,9 @@ func (handler *Handler) checkUniqueStackNameInDocker(endpoint *portainer.Endpoin return isUniqueStackName, nil } -func (handler *Handler) checkUniqueWebhookID(webhookID string) (bool, error) { - _, err := handler.DataStore.Stack().StackByWebhookID(webhookID) - if handler.DataStore.IsErrObjectNotFound(err) { +func (handler *Handler) checkUniqueWebhookID(tx dataservices.DataStoreTx, webhookID string) (bool, error) { + _, err := tx.Stack().StackByWebhookID(webhookID) + if tx.IsErrObjectNotFound(err) { return true, nil } return false, err diff --git a/api/http/handler/stacks/stack_update_git.go b/api/http/handler/stacks/stack_update_git.go index 07cd616da..1ba8e312f 100644 --- a/api/http/handler/stacks/stack_update_git.go +++ b/api/http/handler/stacks/stack_update_git.go @@ -76,7 +76,7 @@ func (handler *Handler) stackUpdateGit(w http.ResponseWriter, r *http.Request) * if payload.AutoUpdate != nil && payload.AutoUpdate.Webhook != "" && (stack.AutoUpdate == nil || (stack.AutoUpdate != nil && stack.AutoUpdate.Webhook != payload.AutoUpdate.Webhook)) { - if isUnique, err := handler.checkUniqueWebhookID(payload.AutoUpdate.Webhook); !isUnique || err != nil { + if isUnique, err := handler.checkUniqueWebhookID(handler.DataStore, payload.AutoUpdate.Webhook); !isUnique || err != nil { return httperror.Conflict("Webhook ID already exists", errors.New("webhook ID already exists")) } }