Skip to content

feat(site): Add change version for template admins #6988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get the flow
  • Loading branch information
BrunoQuaresma committed Apr 3, 2023
commit c12558f24bd59bad03fb64ef5ebd1300be3e689d
39 changes: 37 additions & 2 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosRequestHeaders } from "axios"
import axios from "axios"
import dayjs from "dayjs"
import * as Types from "./types"
import { DeploymentConfig } from "./types"
Expand Down Expand Up @@ -64,7 +64,7 @@ if (token !== null && token.getAttribute("content") !== null) {
}
}

const CONTENT_TYPE_JSON: AxiosRequestHeaders = {
const CONTENT_TYPE_JSON = {
"Content-Type": "application/json",
}

Expand Down Expand Up @@ -974,6 +974,41 @@ export class MissingBuildParameters extends Error {
}
}

/** Steps to change the workspace version
* - Get the latest template to access the latest active version
* - Get the current build parameters
* - Get the template parameters
* - Update the build parameters and check if there are missed parameters for the new version
* - If there are missing parameters raise an error
* - Create a build with the version and updated build parameters
*/
export const changeWorkspaceVersion = async (
workspace: TypesGen.Workspace,
templateVersionId: string,
newBuildParameters: TypesGen.WorkspaceBuildParameter[] = [],
): Promise<TypesGen.WorkspaceBuild> => {
const [currentBuildParameters, templateParameters] = await Promise.all([
getWorkspaceBuildParameters(workspace.latest_build.id),
getTemplateVersionRichParameters(templateVersionId),
])

const missingParameters = getMissingParameters(
currentBuildParameters,
newBuildParameters,
templateParameters,
)

if (missingParameters.length > 0) {
throw new MissingBuildParameters(missingParameters)
}

return postWorkspaceBuild(workspace.id, {
transition: "start",
template_version_id: templateVersionId,
rich_parameter_values: newBuildParameters,
})
}

/** Steps to update the workspace
* - Get the latest template to access the latest active version
* - Get the current build parameters
Expand Down
7 changes: 5 additions & 2 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,12 @@ export const WorkspaceReadyPage = ({
onClose={() => {
setChangeVersionDialogOpen(false)
}}
onConfirm={() => {
onConfirm={(templateVersion) => {
setChangeVersionDialogOpen(false)
workspaceSend({ type: "CHANGE_VERSION" })
workspaceSend({
type: "CHANGE_VERSION",
templateVersionId: templateVersion.id,
})
}}
/>
</>
Expand Down
70 changes: 69 additions & 1 deletion site/src/xServices/workspace/workspaceXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export interface WorkspaceContext {
createBuildLogLevel?: TypesGen.CreateWorkspaceBuildRequest["log_level"]
// SSH Config
sshPrefix?: string
// Change version
templateVersionIdToChange?: TypesGen.TemplateVersion["id"]
}

export type WorkspaceEvent =
Expand All @@ -90,6 +92,11 @@ export type WorkspaceEvent =
| { type: "DELETE" }
| { type: "CANCEL_DELETE" }
| { type: "UPDATE"; buildParameters?: TypesGen.WorkspaceBuildParameter[] }
| {
type: "CHANGE_VERSION"
templateVersionId: TypesGen.TemplateVersion["id"]
buildParameters?: TypesGen.WorkspaceBuildParameter[]
}
| { type: "CANCEL" }
| {
type: "REFRESH_TIMELINE"
Expand Down Expand Up @@ -157,6 +164,9 @@ export const workspaceMachine = createMachine(
updateWorkspace: {
data: TypesGen.WorkspaceBuild
}
changeWorkspaceVersion: {
data: TypesGen.WorkspaceBuild
}
startWorkspace: {
data: TypesGen.WorkspaceBuild
}
Expand Down Expand Up @@ -290,6 +300,10 @@ export const workspaceMachine = createMachine(
STOP: "requestingStop",
ASK_DELETE: "askingDelete",
UPDATE: "requestingUpdate",
CHANGE_VERSION: {
target: "requestingChangeVersion",
actions: ["assignTemplateVersionIdToChange"],
},
CANCEL: "requestingCancel",
RETRY_BUILD: [
{
Expand Down Expand Up @@ -341,10 +355,37 @@ export const workspaceMachine = createMachine(
],
},
},
requestingChangeVersion: {
entry: ["clearBuildError"],
invoke: {
src: "changeWorkspaceVersion",
onDone: {
target: "idle",
actions: ["assignBuild", "clearTemplateVersionIdToChange"],
},
onError: [
{
target: "askingForMissedBuildParameters",
cond: "isMissingBuildParameterError",
actions: ["assignMissedParameters"],
},
{
target: "idle",
actions: ["assignBuildError"],
},
],
},
},
askingForMissedBuildParameters: {
on: {
CANCEL: "idle",
UPDATE: "requestingUpdate",
UPDATE: [
{
target: "requestingChangeVersion",
cond: "isChangingVersion",
},
{ target: "requestingUpdate" },
],
},
},
requestingStart: {
Expand Down Expand Up @@ -669,6 +710,14 @@ export const workspaceMachine = createMachine(
// Debug mode when build fails
enableDebugMode: assign({ createBuildLogLevel: (_) => "debug" as const }),
disableDebugMode: assign({ createBuildLogLevel: (_) => undefined }),
// Change version
assignTemplateVersionIdToChange: assign({
templateVersionIdToChange: (_, { templateVersionId }) =>
templateVersionId,
}),
clearTemplateVersionIdToChange: assign({
templateVersionIdToChange: (_) => undefined,
}),
},
guards: {
moreBuildsAvailable,
Expand All @@ -684,6 +733,8 @@ export const workspaceMachine = createMachine(
lastBuildWasDeleting: ({ workspace }) => {
return workspace?.latest_build.transition === "delete"
},
isChangingVersion: ({ templateVersionIdToChange }) =>
Boolean(templateVersionIdToChange),
},
services: {
getWorkspace: async ({ username, workspaceName }) => {
Expand All @@ -708,6 +759,23 @@ export const workspaceMachine = createMachine(
send({ type: "REFRESH_TIMELINE" })
return build
},
changeWorkspaceVersion:
({ workspace, templateVersionIdToChange }, { buildParameters }) =>
async (send) => {
if (!workspace) {
throw new Error("Workspace is not set")
}
if (!templateVersionIdToChange) {
throw new Error("Template version id to change is not set")
}
const build = await API.changeWorkspaceVersion(
workspace,
templateVersionIdToChange,
buildParameters,
)
send({ type: "REFRESH_TIMELINE" })
return build
},
startWorkspace: (context) => async (send) => {
if (context.workspace) {
const startWorkspacePromise = await API.startWorkspace(
Expand Down