@Summary List AI tasks @ID list-ai-tasks @Security CoderSessionToken @Produce json @Tags Tasks @Param q query string false "Search query for filtering tasks. Supports: owner: , organization: , status: " @Success 200 {object} codersdk.TasksListResponse @Router /a
(rw http.ResponseWriter, r *http.Request)
| 402 | // @Success 200 {object} codersdk.TasksListResponse |
| 403 | // @Router /api/v2/tasks [get] |
| 404 | func (api *API) tasksList(rw http.ResponseWriter, r *http.Request) { |
| 405 | ctx := r.Context() |
| 406 | apiKey := httpmw.APIKey(r) |
| 407 | |
| 408 | // Parse query parameters for filtering tasks. |
| 409 | queryStr := r.URL.Query().Get("q") |
| 410 | filter, errs := searchquery.Tasks(ctx, api.Database, queryStr, apiKey.UserID) |
| 411 | if len(errs) > 0 { |
| 412 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 413 | Message: "Invalid task search query.", |
| 414 | Validations: errs, |
| 415 | }) |
| 416 | return |
| 417 | } |
| 418 | |
| 419 | // Fetch all tasks matching the filters from the database. |
| 420 | dbTasks, err := api.Database.ListTasks(ctx, filter) |
| 421 | if err != nil { |
| 422 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 423 | Message: "Internal error fetching tasks.", |
| 424 | Detail: err.Error(), |
| 425 | }) |
| 426 | return |
| 427 | } |
| 428 | |
| 429 | tasks, err := api.convertTasks(ctx, apiKey.UserID, dbTasks) |
| 430 | if err != nil { |
| 431 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 432 | Message: "Internal error converting tasks.", |
| 433 | Detail: err.Error(), |
| 434 | }) |
| 435 | return |
| 436 | } |
| 437 | |
| 438 | httpapi.Write(ctx, rw, http.StatusOK, codersdk.TasksListResponse{ |
| 439 | Tasks: tasks, |
| 440 | Count: len(tasks), |
| 441 | }) |
| 442 | } |
| 443 | |
| 444 | // convertTasks converts database tasks to API tasks, enriching them with |
| 445 | // workspace information. |