10000 feat: Add support for update checks and notifications by mafredri · Pull Request #4810 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: Add support for update checks and notifications #4810

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 14 commits into from
Dec 1, 2022
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
wip(site): Add support for informing of updates
  • Loading branch information
mafredri committed Nov 29, 2022
commit c163af8def61e991eb987273217115454d74e595
6 changes: 6 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ export const getBuildInfo = async (): Promise<TypesGen.BuildInfoResponse> => {
return response.data
}

export const getUpdateCheck =
async (): Promise<TypesGen.UpdateCheckResponse> => {
const response = await axios.get("/api/v2/updatecheck")
return response.data
}

export const putWorkspaceAutostart = async (
workspaceID: string,
autostart: TypesGen.UpdateWorkspaceAutostartRequest,
Expand Down
6 changes: 6 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export const MockBuildInfo: TypesGen.BuildInfoResponse = {
version: "v99.999.9999+c9cdf14",
}

export const MockUpdateCheck: TypesGen.UpdateCheckResponse = {
current: true,
url: "file:///mock-url",
version: "v99.999.9999+c9cdf14",
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice


export const MockOwnerRole: TypesGen.Role = {
name: "owner",
display_name: "Owner",
Expand Down
5 changes: 5 additions & 0 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export const handlers = [
return res(ctx.status(200), ctx.json(M.MockBuildInfo))
}),

// update check
rest.get("/api/v2/updatecheck", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockUpdateCheck))
}),

// organizations
rest.get("/api/v2/organizations/:organizationId", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockOrganization))
Expand Down
3 changes: 3 additions & 0 deletions site/src/xServices/StateContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createContext, FC, ReactNode } from "react"
import { ActorRefFrom } from "xstate"
import { authMachine } from "./auth/authXService"
import { buildInfoMachine } from "./buildInfo/buildInfoXService"
import { updateCheckMachine } from "./updateCheck/updateCheckXService"
import { deploymentConfigMachine } from "./deploymentConfig/deploymentConfigMachine"
import { entitlementsMachine } from "./entitlements/entitlementsXService"
import { siteRolesMachine } from "./roles/siteRolesXService"
Expand All @@ -14,6 +15,7 @@ interface XServiceContextType {
siteRolesXService: ActorRefFrom<typeof siteRolesMachine>
// Since the info here is used by multiple deployment settings page and we don't want to refetch them every time
deploymentConfigXService: ActorRefFrom<typeof deploymentConfigMachine>
updateCheckXService: ActorRefFrom<typeof updateCheckMachine>
}

/**
Expand All @@ -35,6 +37,7 @@ export const XServiceProvider: FC<{ children: ReactNode }> = ({ children }) => {
entitlementsXService: useInterpret(entitlementsMachine),
siteRolesXService: useInterpret(siteRolesMachine),
deploymentConfigXService: useInterpret(deploymentConfigMachine),
updateCheckXService: useInterpret(updateCheckMachine),
}}
>
{children}
Expand Down
103 changes: 103 additions & 0 deletions site/src/xServices/updateCheck/updateCheckXService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { assign, createMachine } from "xstate"
import * as API from "../../api/api"
import * as TypesGen from "../../api/typesGenerated"
import { displayMsg } from "../../components/GlobalSnackbar/utils"

export const Language = {
updateAvailable: "New version available",
updateAvailableMessage: (
version: string,
url: string,
upgrade_instructions_url: string,
): string =>
`Coder ${version} is now available at ${url}. See ${upgrade_instructions_url} for information on how to upgrade.`,
}

export interface UpdateCheckContext {
getUpdateCheckError?: Error | unknown
updateCheck?: TypesGen.UpdateCheckResponse
}

export const updateCheckMachine = createMachine(
{
id: "updateCheckState",
predictableActionArguments: true,
tsTypes: {} as import("./updateCheckXService.typegen").Typegen0,
schema: {
context: {} as UpdateCheckContext,
services: {} as {
getUpdateCheck: {
data: TypesGen.UpdateCheckResponse
}
},
},
context: {
updateCheck: undefined,
},
initial: "gettingUpdateCheck",
states: {
gettingUpdateCheck: {
invoke: {
src: "getUpdateCheck",
id: "getUpdateCheck",
onDone: [
{
actions: [
"assignUpdateCheck",
"clearGetUpdateCheckError",
"notifyUpdateAvailable",
],
target: "#updateCheckState.success",
},
],
onError: [
{
actions: ["assignGetUpdateCheckError", "clearUpdateCheck"],
target: "#updateCheckState.failure",
},
],
},
},
success: {
type: "final",
},
failure: {
type: "final",
},
},
},
{
services: {
getUpdateCheck: API.getUpdateCheck,
},
actions: {
assignUpdateCheck: assign({
updateCheck: (_, event) => event.data,
}),
clearUpdateCheck: assign((context: UpdateCheckContext) => ({
...context,
updateCheck: undefined,
})),
assignGetUpdateCheckError: assign({
getUpdateCheckError: (_, event) => event.data,
}),
clearGetUpdateCheckError: assign((context: UpdateCheckContext) => ({
...context,
getUpdateCheckError: undefined,
})),
notifyUpdateAvailable: (context: UpdateCheckContext) => {
if (context.updateCheck && !context.updateCheck.current) {
// TODO(mafredri): HTML formatting, persistance?
displayMsg(
Language.updateAvailable,
Language.updateAvailableMessage(
context.updateCheck.version,
context.updateCheck.url,
"https://coder.com/docs/coder-oss/latest/admin/upgrade",
),
)
}
},
},
},
)
0