@Summary Update active template version by template ID @ID update-active-template-version-by-template-id @Security CoderSessionToken @Accept json @Produce json @Tags Templates @Param request body codersdk.UpdateActiveTemplateVersion true "Modified template version" @Param template path string true "
(rw http.ResponseWriter, r *http.Request)
| 1358 | // @Success 200 {object} codersdk.Response |
| 1359 | // @Router /api/v2/templates/{template}/versions [patch] |
| 1360 | func (api *API) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) { |
| 1361 | var ( |
| 1362 | ctx = r.Context() |
| 1363 | template = httpmw.TemplateParam(r) |
| 1364 | auditor = *api.Auditor.Load() |
| 1365 | aReq, commitAudit = audit.InitRequest[database.Template](rw, &audit.RequestParams{ |
| 1366 | Audit: auditor, |
| 1367 | Log: api.Logger, |
| 1368 | Request: r, |
| 1369 | Action: database.AuditActionWrite, |
| 1370 | OrganizationID: template.OrganizationID, |
| 1371 | }) |
| 1372 | ) |
| 1373 | defer commitAudit() |
| 1374 | aReq.Old = template |
| 1375 | |
| 1376 | var req codersdk.UpdateActiveTemplateVersion |
| 1377 | if !httpapi.Read(ctx, rw, r, &req) { |
| 1378 | return |
| 1379 | } |
| 1380 | version, err := api.Database.GetTemplateVersionByID(ctx, req.ID) |
| 1381 | if httpapi.Is404Error(err) { |
| 1382 | httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{ |
| 1383 | Message: "Template version not found.", |
| 1384 | }) |
| 1385 | return |
| 1386 | } |
| 1387 | if err != nil { |
| 1388 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 1389 | Message: "Internal error fetching template version.", |
| 1390 | Detail: err.Error(), |
| 1391 | }) |
| 1392 | return |
| 1393 | } |
| 1394 | if version.TemplateID.UUID.String() != template.ID.String() { |
| 1395 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1396 | Message: "The provided template version doesn't belong to the specified template.", |
| 1397 | }) |
| 1398 | return |
| 1399 | } |
| 1400 | job, err := api.Database.GetProvisionerJobByID(ctx, version.JobID) |
| 1401 | if err != nil { |
| 1402 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 1403 | Message: "Internal error fetching template version job status.", |
| 1404 | Detail: err.Error(), |
| 1405 | }) |
| 1406 | return |
| 1407 | } |
| 1408 | if job.JobStatus != database.ProvisionerJobStatusSucceeded { |
| 1409 | httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ |
| 1410 | Message: "Only versions that have been built successfully can be promoted.", |
| 1411 | Detail: fmt.Sprintf("Attempted to promote a version with a %s build", job.JobStatus), |
| 1412 | }) |
| 1413 | return |
| 1414 | } |
| 1415 | if version.Archived { |
| 1416 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1417 | Message: "The provided template version is archived.", |
nothing calls this directly
no test coverage detected