@Summary Update prebuilds settings @ID update-prebuilds-settings @Security CoderSessionToken @Accept json @Produce json @Tags Prebuilds @Param request body codersdk.PrebuildsSettings true "Prebuilds settings request" @Success 200 {object} codersdk.PrebuildsSettings @Success 304 @Router /api/v2/prebu
(rw http.ResponseWriter, r *http.Request)
| 57 | // @Success 304 |
| 58 | // @Router /api/v2/prebuilds/settings [put] |
| 59 | func (api *API) putPrebuildsSettings(rw http.ResponseWriter, r *http.Request) { |
| 60 | ctx := r.Context() |
| 61 | |
| 62 | var settings codersdk.PrebuildsSettings |
| 63 | if !httpapi.Read(ctx, rw, r, &settings) { |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | settingsJSON, err := json.Marshal(&settings) |
| 68 | if err != nil { |
| 69 | httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{ |
| 70 | Message: "Failed to marshal prebuilds settings.", |
| 71 | Detail: err.Error(), |
| 72 | }) |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | currentSettingsJSON, err := api.Database.GetPrebuildsSettings(ctx) |
| 77 | if err != nil { |
| 78 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 79 | Message: "Failed to fetch current prebuilds settings.", |
| 80 | Detail: err.Error(), |
| 81 | }) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | if bytes.Equal(settingsJSON, []byte(currentSettingsJSON)) { |
| 86 | // See: https://www.rfc-editor.org/rfc/rfc7232#section-4.1 |
| 87 | httpapi.Write(ctx, rw, http.StatusNotModified, nil) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | auditor := api.AGPL.Auditor.Load() |
| 92 | aReq, commitAudit := audit.InitRequest[database.PrebuildsSettings](rw, &audit.RequestParams{ |
| 93 | Audit: *auditor, |
| 94 | Log: api.Logger, |
| 95 | Request: r, |
| 96 | Action: database.AuditActionWrite, |
| 97 | }) |
| 98 | defer commitAudit() |
| 99 | |
| 100 | aReq.New = database.PrebuildsSettings{ |
| 101 | ID: uuid.New(), |
| 102 | ReconciliationPaused: settings.ReconciliationPaused, |
| 103 | } |
| 104 | |
| 105 | err = prebuilds.SetPrebuildsReconciliationPaused(ctx, api.Database, settings.ReconciliationPaused) |
| 106 | if err != nil { |
| 107 | if rbac.IsUnauthorizedError(err) { |
| 108 | httpapi.Forbidden(rw) |
| 109 | return |
| 110 | } |
| 111 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 112 | Message: "Failed to update prebuilds settings.", |
| 113 | Detail: err.Error(), |
| 114 | }) |
| 115 | |
| 116 | return |
nothing calls this directly
no test coverage detected