( workspace: Workspace, permissions: ActionPermissions, )
| 49 | }; |
| 50 | |
| 51 | export const abilitiesByWorkspaceStatus = ( |
| 52 | workspace: Workspace, |
| 53 | permissions: ActionPermissions, |
| 54 | ): WorkspaceAbilities => { |
| 55 | const hasPermissionToCancel = |
| 56 | workspace.template_allow_user_cancel_workspace_jobs || permissions.isOwner; |
| 57 | |
| 58 | if (workspace.dormant_at) { |
| 59 | return { |
| 60 | actions: ["activate"], |
| 61 | canCancel: false, |
| 62 | canAcceptJobs: true, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | if (workspace.latest_build.has_external_agent) { |
| 67 | return { |
| 68 | actions: [], |
| 69 | canCancel: false, |
| 70 | canAcceptJobs: true, |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | const status = workspace.latest_build.status; |
| 75 | |
| 76 | switch (status) { |
| 77 | case "starting": { |
| 78 | return { |
| 79 | actions: ["starting"], |
| 80 | canCancel: hasPermissionToCancel, |
| 81 | canAcceptJobs: false, |
| 82 | }; |
| 83 | } |
| 84 | case "running": { |
| 85 | const actions: ActionType[] = ["stop"]; |
| 86 | |
| 87 | if (workspace.template_require_active_version && workspace.outdated) { |
| 88 | actions.push("updateAndRestartRequireActiveVersion"); |
| 89 | } else { |
| 90 | if (workspace.outdated) { |
| 91 | actions.unshift("updateAndRestart"); |
| 92 | } |
| 93 | actions.push("restart"); |
| 94 | } |
| 95 | |
| 96 | return { |
| 97 | actions, |
| 98 | canCancel: false, |
| 99 | canAcceptJobs: true, |
| 100 | }; |
| 101 | } |
| 102 | case "stopping": { |
| 103 | return { |
| 104 | actions: ["stopping"], |
| 105 | canCancel: hasPermissionToCancel, |
| 106 | canAcceptJobs: false, |
| 107 | }; |
| 108 | } |
no test coverage detected