diff --git a/internal/handlers/app.go b/internal/handlers/app.go index 49ff60d..492fa9a 100644 --- a/internal/handlers/app.go +++ b/internal/handlers/app.go @@ -354,6 +354,32 @@ func (h *Handlers) HandleAppDeploy() http.HandlerFunc { } } +// HandleCancelDeploy cancels an in-progress deployment for an app. +func (h *Handlers) HandleCancelDeploy() http.HandlerFunc { + return func(writer http.ResponseWriter, request *http.Request) { + appID := chi.URLParam(request, "id") + + application, findErr := models.FindApp(request.Context(), h.db, appID) + if findErr != nil || application == nil { + http.NotFound(writer, request) + + return + } + + cancelled := h.deploy.CancelDeploy(application.ID) + if cancelled { + h.log.Info("deployment cancelled by user", "app", application.Name) + } + + http.Redirect( + writer, + request, + "/apps/"+application.ID, + http.StatusSeeOther, + ) + } +} + // HandleAppDeployments returns the deployments history handler. func (h *Handlers) HandleAppDeployments() http.HandlerFunc { tmpl := templates.GetParsed() diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go index efb996f..df6964d 100644 --- a/internal/handlers/handlers_test.go +++ b/internal/handlers/handlers_test.go @@ -684,6 +684,47 @@ func TestDeletePortOwnershipVerification(t *testing.T) { assert.NotNil(t, found, "port should still exist after IDOR attempt") } +func TestHandleCancelDeployRedirects(t *testing.T) { + t.Parallel() + + testCtx := setupTestHandlers(t) + + createdApp := createTestApp(t, testCtx, "cancel-deploy-app") + + request := httptest.NewRequest( + http.MethodPost, + "/apps/"+createdApp.ID+"/deployments/cancel", + nil, + ) + request = addChiURLParams(request, map[string]string{"id": createdApp.ID}) + recorder := httptest.NewRecorder() + + handler := testCtx.handlers.HandleCancelDeploy() + handler.ServeHTTP(recorder, request) + + assert.Equal(t, http.StatusSeeOther, recorder.Code) + assert.Equal(t, "/apps/"+createdApp.ID, recorder.Header().Get("Location")) +} + +func TestHandleCancelDeployReturns404ForUnknownApp(t *testing.T) { + t.Parallel() + + testCtx := setupTestHandlers(t) + + request := httptest.NewRequest( + http.MethodPost, + "/apps/nonexistent/deployments/cancel", + nil, + ) + request = addChiURLParams(request, map[string]string{"id": "nonexistent"}) + recorder := httptest.NewRecorder() + + handler := testCtx.handlers.HandleCancelDeploy() + handler.ServeHTTP(recorder, request) + + assert.Equal(t, http.StatusNotFound, recorder.Code) +} + func TestHandleWebhookReturns404ForUnknownSecret(t *testing.T) { t.Parallel() diff --git a/internal/server/routes.go b/internal/server/routes.go index 2fb632f..3acadea 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -68,6 +68,7 @@ func (s *Server) SetupRoutes() { r.Post("/apps/{id}", s.handlers.HandleAppUpdate()) r.Post("/apps/{id}/delete", s.handlers.HandleAppDelete()) r.Post("/apps/{id}/deploy", s.handlers.HandleAppDeploy()) + r.Post("/apps/{id}/deployments/cancel", s.handlers.HandleCancelDeploy()) r.Get("/apps/{id}/deployments", s.handlers.HandleAppDeployments()) r.Get("/apps/{id}/deployments/{deploymentID}/logs", s.handlers.HandleDeploymentLogsAPI()) r.Get("/apps/{id}/deployments/{deploymentID}/download", s.handlers.HandleDeploymentLogDownload()) diff --git a/internal/service/deploy/deploy.go b/internal/service/deploy/deploy.go index dc260e4..9c629fc 100644 --- a/internal/service/deploy/deploy.go +++ b/internal/service/deploy/deploy.go @@ -283,6 +283,26 @@ func (svc *Service) GetLogFilePath(app *models.App, deployment *models.Deploymen return filepath.Join(svc.config.DataDir, "logs", hostname, app.Name, filename) } +// HasActiveDeploy returns true if there is an active deployment for the given app. +func (svc *Service) HasActiveDeploy(appID string) bool { + _, ok := svc.activeDeploys.Load(appID) + + return ok +} + +// CancelDeploy cancels any in-progress deployment for the given app +// and waits for it to finish before returning. Returns true if a deployment +// was cancelled, false if there was nothing to cancel. +func (svc *Service) CancelDeploy(appID string) bool { + if !svc.HasActiveDeploy(appID) { + return false + } + + svc.cancelActiveDeploy(appID) + + return true +} + // Deploy deploys an app. If cancelExisting is true (e.g. webhook-triggered), // any in-progress deploy for the same app will be cancelled before starting. // If cancelExisting is false and a deploy is in progress, ErrDeploymentInProgress is returned. diff --git a/templates/app_detail.html b/templates/app_detail.html index fa520d5..14fb91e 100644 --- a/templates/app_detail.html +++ b/templates/app_detail.html @@ -40,6 +40,10 @@ +