chatStopWorkspace stops a workspace by creating a new build with the "stop" transition. It mirrors chatStartWorkspace, without start-only active-version behavior.
( ctx context.Context, ownerID uuid.UUID, workspaceID uuid.UUID, req codersdk.CreateWorkspaceBuildRequest, )
| 3931 | // "stop" transition. It mirrors chatStartWorkspace, without start-only |
| 3932 | // active-version behavior. |
| 3933 | func (api *API) chatStopWorkspace( |
| 3934 | ctx context.Context, |
| 3935 | ownerID uuid.UUID, |
| 3936 | workspaceID uuid.UUID, |
| 3937 | req codersdk.CreateWorkspaceBuildRequest, |
| 3938 | ) (codersdk.WorkspaceBuild, error) { |
| 3939 | actor, _, err := httpmw.UserRBACSubject(ctx, api.Database, ownerID, rbac.ScopeAll) |
| 3940 | if err != nil { |
| 3941 | return codersdk.WorkspaceBuild{}, xerrors.Errorf("load user authorization: %w", err) |
| 3942 | } |
| 3943 | ctx = dbauthz.As(ctx, actor) |
| 3944 | |
| 3945 | workspace, err := api.Database.GetWorkspaceByID(ctx, workspaceID) |
| 3946 | if err != nil { |
| 3947 | return codersdk.WorkspaceBuild{}, xerrors.Errorf("get workspace: %w", err) |
| 3948 | } |
| 3949 | |
| 3950 | req.Transition = codersdk.WorkspaceTransitionStop |
| 3951 | |
| 3952 | // Build a synthetic API key so postWorkspaceBuildsInternal can |
| 3953 | // record the correct initiator. |
| 3954 | syntheticKey := database.APIKey{ |
| 3955 | UserID: ownerID, |
| 3956 | } |
| 3957 | |
| 3958 | apiBuild, err := api.postWorkspaceBuildsInternal( |
| 3959 | ctx, |
| 3960 | syntheticKey, |
| 3961 | workspace, |
| 3962 | req, |
| 3963 | func(action policy.Action, object rbac.Objecter) bool { |
| 3964 | // Authorization is handled by dbauthz on the context. |
| 3965 | authErr := api.HTTPAuth.Authorizer.Authorize(ctx, actor, action, object.RBACObject()) |
| 3966 | return authErr == nil |
| 3967 | }, |
| 3968 | audit.WorkspaceBuildBaggage{}, |
| 3969 | ) |
| 3970 | if err != nil { |
| 3971 | return codersdk.WorkspaceBuild{}, xerrors.Errorf("create workspace build: %w", err) |
| 3972 | } |
| 3973 | |
| 3974 | return apiBuild, nil |
| 3975 | } |
| 3976 | |
| 3977 | func rewriteChatStartWorkspaceManualUpdateResponse(resp codersdk.Response, fallbackDetail string, retryInstructions string) codersdk.Response { |
| 3978 | originalMessage := resp.Message |
nothing calls this directly
no test coverage detected