@Summary Create a new AI task @ID create-a-new-ai-task @Security CoderSessionToken @Accept json @Produce json @Tags Tasks @Param user path string true "Username, user ID, or 'me' for the authenticated user" @Param request body codersdk.CreateTaskRequest true "Create task request" @Success 201 {objec
(rw http.ResponseWriter, r *http.Request)
| 45 | // @Success 201 {object} codersdk.Task |
| 46 | // @Router /api/v2/tasks/{user} [post] |
| 47 | func (api *API) tasksCreate(rw http.ResponseWriter, r *http.Request) { |
| 48 | var ( |
| 49 | ctx = r.Context() |
| 50 | apiKey = httpmw.APIKey(r) |
| 51 | auditor = api.Auditor.Load() |
| 52 | mems = httpmw.OrganizationMembersParam(r) |
| 53 | taskResourceInfo = audit.AdditionalFields{} |
| 54 | ) |
| 55 | |
| 56 | if mems.User != nil { |
| 57 | taskResourceInfo.WorkspaceOwner = mems.User.Username |
| 58 | } |
| 59 | |
| 60 | aReq, commitAudit := audit.InitRequest[database.TaskTable](rw, &audit.RequestParams{ |
| 61 | Audit: *auditor, |
| 62 | Log: api.Logger, |
| 63 | Request: r, |
| 64 | Action: database.AuditActionCreate, |
| 65 | AdditionalFields: taskResourceInfo, |
| 66 | }) |
| 67 | |
| 68 | defer commitAudit() |
| 69 | |
| 70 | var req codersdk.CreateTaskRequest |
| 71 | if !httpapi.Read(ctx, rw, r, &req) { |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | // Fetch the template version to verify access and whether or not it has an |
| 76 | // AI task. |
| 77 | templateVersion, err := api.Database.GetTemplateVersionByID(ctx, req.TemplateVersionID) |
| 78 | if err != nil { |
| 79 | if httpapi.Is404Error(err) { |
| 80 | // Avoid using httpapi.ResourceNotFound() here because this is an |
| 81 | // input error and 404 would be confusing. |
| 82 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 83 | Message: "Template version not found or you do not have access to this resource", |
| 84 | }) |
| 85 | return |
| 86 | } |
| 87 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 88 | Message: "Internal error fetching template version.", |
| 89 | Detail: err.Error(), |
| 90 | }) |
| 91 | return |
| 92 | } |
| 93 | |
| 94 | aReq.UpdateOrganizationID(templateVersion.OrganizationID) |
| 95 | |
| 96 | if !templateVersion.HasAITask.Valid || !templateVersion.HasAITask.Bool { |
| 97 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 98 | Message: `Template does not have a valid "coder_ai_task" resource.`, |
| 99 | }) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | taskName := req.Name |
| 104 | if taskName != "" { |
nothing calls this directly
no test coverage detected