@Summary Delete AI task @ID delete-ai-task @Security CoderSessionToken @Tags Tasks @Param user path string true "Username, user ID, or 'me' for the authenticated user" @Param task path string true "Task ID, or task name" @Success 202 @Router /api/v2/tasks/{user}/{task} [delete]
(rw http.ResponseWriter, r *http.Request)
| 586 | // @Success 202 |
| 587 | // @Router /api/v2/tasks/{user}/{task} [delete] |
| 588 | func (api *API) taskDelete(rw http.ResponseWriter, r *http.Request) { |
| 589 | ctx := r.Context() |
| 590 | apiKey := httpmw.APIKey(r) |
| 591 | task := httpmw.TaskParam(r) |
| 592 | |
| 593 | now := api.Clock.Now() |
| 594 | |
| 595 | if task.WorkspaceID.Valid { |
| 596 | workspace, err := api.Database.GetWorkspaceByID(ctx, task.WorkspaceID.UUID) |
| 597 | if err != nil { |
| 598 | if httpapi.Is404Error(err) { |
| 599 | httpapi.ResourceNotFound(rw) |
| 600 | return |
| 601 | } |
| 602 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 603 | Message: "Internal error fetching task workspace before deleting task.", |
| 604 | Detail: err.Error(), |
| 605 | }) |
| 606 | return |
| 607 | } |
| 608 | |
| 609 | // Construct a request to the workspace build creation handler to |
| 610 | // initiate deletion. |
| 611 | buildReq := codersdk.CreateWorkspaceBuildRequest{ |
| 612 | Transition: codersdk.WorkspaceTransitionDelete, |
| 613 | Reason: "Deleted via tasks API", |
| 614 | } |
| 615 | |
| 616 | _, err = api.postWorkspaceBuildsInternal( |
| 617 | ctx, |
| 618 | apiKey, |
| 619 | workspace, |
| 620 | buildReq, |
| 621 | func(action policy.Action, object rbac.Objecter) bool { |
| 622 | return api.Authorize(r, action, object) |
| 623 | }, |
| 624 | audit.WorkspaceBuildBaggageFromRequest(r), |
| 625 | ) |
| 626 | if err != nil { |
| 627 | httperror.WriteWorkspaceBuildError(ctx, rw, err) |
| 628 | return |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // As an implementation detail of the workspace build transition, we also delete |
| 633 | // the associated task. This means that we have a race between provisionerdserver |
| 634 | // and here with deleting the task. In a real world scenario we'll never lose the |
| 635 | // race but we should still handle it anyways. |
| 636 | _, err := api.Database.DeleteTask(ctx, database.DeleteTaskParams{ |
| 637 | ID: task.ID, |
| 638 | DeletedAt: dbtime.Time(now), |
| 639 | }) |
| 640 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 641 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 642 | Message: "Failed to delete task", |
| 643 | Detail: err.Error(), |
| 644 | }) |
| 645 | return |
nothing calls this directly
no test coverage detected