ListRuns returns GET /patching/runs.
(w http.ResponseWriter, r *http.Request)
| 457 | |
| 458 | // ListRuns returns GET /patching/runs. |
| 459 | func (h *PatchingHandler) ListRuns(w http.ResponseWriter, r *http.Request) { |
| 460 | limit := parseIntQuery(r, "limit", 50) |
| 461 | if limit < 1 { |
| 462 | limit = 1 |
| 463 | } |
| 464 | if limit > 200 { |
| 465 | limit = 200 |
| 466 | } |
| 467 | offset := parseIntQuery(r, "offset", 0) |
| 468 | if offset < 0 { |
| 469 | offset = 0 |
| 470 | } |
| 471 | hostID := r.URL.Query().Get("host_id") |
| 472 | status := r.URL.Query().Get("status") |
| 473 | patchType := r.URL.Query().Get("patch_type") |
| 474 | sortBy := r.URL.Query().Get("sort_by") |
| 475 | sortDir := r.URL.Query().Get("sort_dir") |
| 476 | if hostID != "" && !isValidPatchUUID(hostID) { |
| 477 | hostID = "" |
| 478 | } |
| 479 | if patchType != "" && patchType != "patch_all" && patchType != "patch_package" { |
| 480 | patchType = "" |
| 481 | } |
| 482 | // Validate sort params |
| 483 | validSortBy := map[string]bool{"created_at": true, "started_at": true, "completed_at": true, "status": true} |
| 484 | if !validSortBy[sortBy] { |
| 485 | sortBy = "created_at" |
| 486 | } |
| 487 | if sortDir != "asc" && sortDir != "desc" { |
| 488 | sortDir = "desc" |
| 489 | } |
| 490 | |
| 491 | runs, total, err := h.patchRuns.List(r.Context(), hostID, status, patchType, sortBy, sortDir, limit, offset) |
| 492 | if err != nil { |
| 493 | h.log.Error("patching: runs list error", "error", err) |
| 494 | JSON(w, http.StatusInternalServerError, map[string]string{"error": "Failed to load patch runs"}) |
| 495 | return |
| 496 | } |
| 497 | pages := int(total) / limit |
| 498 | if int(total)%limit > 0 { |
| 499 | pages++ |
| 500 | } |
| 501 | JSON(w, http.StatusOK, map[string]interface{}{ |
| 502 | "runs": patchRunsListToResponse(runs), |
| 503 | "pagination": map[string]interface{}{ |
| 504 | "total": total, |
| 505 | "limit": limit, |
| 506 | "offset": offset, |
| 507 | "pages": pages, |
| 508 | }, |
| 509 | }) |
| 510 | } |
| 511 | |
| 512 | // GetRun returns GET /patching/runs/:id. |
| 513 | func (h *PatchingHandler) GetRun(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected