From 932fad6436cbd89fa1b88e687cb445735d8628fe Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 23 Oct 2023 17:34:30 -0300 Subject: [PATCH] chore: allow supported versions throughout envvar (#30559) --- .../supportedVersionsToken.ts | 19 +++- .../functions/buildVersionUpdateMessage.ts | 59 ++++++++++++ .../server/functions/checkVersionUpdate.ts | 93 +++++++------------ .../server/functions/getNewUpdates.ts | 2 + .../src/definitions/index.ts | 2 + 5 files changed, 110 insertions(+), 65 deletions(-) create mode 100644 apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts diff --git a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts index 577abd4383d08..f4c662362f5c8 100644 --- a/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts +++ b/apps/meteor/app/cloud/server/functions/supportedVersionsToken/supportedVersionsToken.ts @@ -7,6 +7,7 @@ import { serverFetch as fetch } from '@rocket.chat/server-fetch'; import { SystemLogger } from '../../../../../server/lib/logger/system'; import { settings } from '../../../../settings/server'; +import { buildVersionUpdateMessage } from '../../../../version-check/server/functions/buildVersionUpdateMessage'; import { generateWorkspaceBearerHttpHeader } from '../getWorkspaceAccessToken'; import { supportedVersionsChooseLatest } from './supportedVersionsChooseLatest'; @@ -83,7 +84,9 @@ const cacheValueInSettings = ( ); }; -/** CODE */ +const releaseEndpoint = process.env.OVERWRITE_INTERNAL_RELEASE_URL?.trim() + ? process.env.OVERWRITE_INTERNAL_RELEASE_URL.trim() + : 'https://releases.rocket.chat/v2/server/supportedVersions'; const getSupportedVersionsFromCloud = async () => { if (process.env.CLOUD_SUPPORTED_VERSIONS_TOKEN) { @@ -97,7 +100,7 @@ const getSupportedVersionsFromCloud = async () => { const headers = await generateWorkspaceBearerHttpHeader(); const response = await handleResponse( - fetch('https://releases.rocket.chat/v2/server/supportedVersions', { + fetch(releaseEndpoint, { headers, }), ); @@ -105,7 +108,7 @@ const getSupportedVersionsFromCloud = async () => { if (!response.success) { SystemLogger.error({ msg: 'Failed to communicate with Rocket.Chat Cloud', - url: 'https://releases.rocket.chat/v2/server/supportedVersions', + url: releaseEndpoint, err: response.error, }); } @@ -123,8 +126,14 @@ const getSupportedVersionsToken = async () => { const [versionsFromLicense, response] = await Promise.all([License.getLicense(), getSupportedVersionsFromCloud()]); - return (await supportedVersionsChooseLatest(versionsFromLicense?.supportedVersions, (response.success && response.result) || undefined)) - ?.signed; + const supportedVersions = await supportedVersionsChooseLatest( + versionsFromLicense?.supportedVersions, + (response.success && response.result) || undefined, + ); + + await buildVersionUpdateMessage(supportedVersions?.versions); + + return supportedVersions?.signed; }; export const getCachedSupportedVersionsToken = cacheValueInSettings('Cloud_Workspace_Supported_Versions_Token', getSupportedVersionsToken); diff --git a/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts new file mode 100644 index 0000000000000..8d159213d4e34 --- /dev/null +++ b/apps/meteor/app/version-check/server/functions/buildVersionUpdateMessage.ts @@ -0,0 +1,59 @@ +import { Settings } from '@rocket.chat/models'; +import semver from 'semver'; + +import { i18n } from '../../../../server/lib/i18n'; +import { sendMessagesToAdmins } from '../../../../server/lib/sendMessagesToAdmins'; +import { settings } from '../../../settings/server'; +import { Info } from '../../../utils/rocketchat.info'; + +export const buildVersionUpdateMessage = async ( + versions: { + version: string; + security: boolean; + infoUrl: string; + }[] = [], +) => { + const lastCheckedVersion = settings.get('Update_LatestAvailableVersion'); + + if (!lastCheckedVersion) { + return; + } + + for await (const version of versions) { + if (semver.lte(version.version, lastCheckedVersion)) { + continue; + } + + if (semver.lte(version.version, Info.version)) { + continue; + } + + await Settings.updateValueById('Update_LatestAvailableVersion', version.version); + + await sendMessagesToAdmins({ + msgs: async ({ adminUser }) => [ + { + msg: `*${i18n.t('Update_your_RocketChat', { ...(adminUser.language && { lng: adminUser.language }) })}*\n${i18n.t( + 'New_version_available_(s)', + { + postProcess: 'sprintf', + sprintf: [version.version], + }, + )}\n${version.infoUrl}`, + }, + ], + banners: [ + { + id: `versionUpdate-${version.version}`.replace(/\./g, '_'), + priority: 10, + title: 'Update_your_RocketChat', + text: 'New_version_available_(s)', + textArguments: [version.version], + link: version.infoUrl, + modifiers: [], + }, + ], + }); + break; + } +}; diff --git a/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts b/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts index 16a3034c0bd33..ca616950a55b4 100644 --- a/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts +++ b/apps/meteor/app/version-check/server/functions/checkVersionUpdate.ts @@ -1,14 +1,11 @@ import type { IUser } from '@rocket.chat/core-typings'; -import { Settings, Users } from '@rocket.chat/models'; -import semver from 'semver'; +import { Users } from '@rocket.chat/models'; import { i18n } from '../../../../server/lib/i18n'; import { sendMessagesToAdmins } from '../../../../server/lib/sendMessagesToAdmins'; -import { settings } from '../../../settings/server'; -import { Info } from '../../../utils/rocketchat.info'; import logger from '../logger'; +import { buildVersionUpdateMessage } from './buildVersionUpdateMessage'; import { getNewUpdates } from './getNewUpdates'; -// import getNewUpdates from '../sampleUpdateData'; const getMessagesToSendToAdmins = async ( alerts: { @@ -42,67 +39,43 @@ const getMessagesToSendToAdmins = async ( } return msgs; }; - +/** + * @deprecated + */ export const checkVersionUpdate = async () => { logger.info('Checking for version updates'); const { versions, alerts } = await getNewUpdates(); - const lastCheckedVersion = settings.get('Update_LatestAvailableVersion'); - - for await (const version of versions) { - if (!lastCheckedVersion) { - break; - } - if (semver.lte(version.version, lastCheckedVersion)) { - continue; - } + await buildVersionUpdateMessage(versions); - if (semver.lte(version.version, Info.version)) { - continue; - } - - await Settings.updateValueById('Update_LatestAvailableVersion', version.version); - - await sendMessagesToAdmins({ - msgs: async ({ adminUser }) => [ - { - msg: `*${i18n.t('Update_your_RocketChat', { ...(adminUser.language && { lng: adminUser.language }) })}*\n${i18n.t( - 'New_version_available_(s)', - { - postProcess: 'sprintf', - sprintf: [version.version], - }, - )}\n${version.infoUrl}`, - }, - ], - banners: [ - { - id: `versionUpdate-${version.version}`.replace(/\./g, '_'), - priority: 10, - title: 'Update_your_RocketChat', - text: 'New_version_available_(s)', - textArguments: [version.version], - link: version.infoUrl, - modifiers: [], - }, - ], - }); - break; - } + await showAlertsFromCloud(alerts); +}; - if (alerts?.length) { - await sendMessagesToAdmins({ - msgs: async ({ adminUser }) => getMessagesToSendToAdmins(alerts, adminUser), - banners: alerts.map((alert) => ({ - id: `alert-${alert.id}`.replace(/\./g, '_'), - priority: 10, - title: alert.title, - text: alert.text, - textArguments: alert.textArguments, - modifiers: alert.modifiers, - link: alert.infoUrl, - })), - }); +const showAlertsFromCloud = async ( + alerts?: { + id: string; + priority: number; + title: string; + text: string; + textArguments?: string[]; + modifiers: string[]; + infoUrl: string; + }[], +) => { + if (!alerts?.length) { + return; } + return sendMessagesToAdmins({ + msgs: async ({ adminUser }) => getMessagesToSendToAdmins(alerts, adminUser), + banners: alerts.map((alert) => ({ + id: `alert-${alert.id}`.replace(/\./g, '_'), + priority: 10, + title: alert.title, + text: alert.text, + textArguments: alert.textArguments, + modifiers: alert.modifiers, + link: alert.infoUrl, + })), + }); }; diff --git a/apps/meteor/app/version-check/server/functions/getNewUpdates.ts b/apps/meteor/app/version-check/server/functions/getNewUpdates.ts index d17191a09be70..926926253a6c9 100644 --- a/apps/meteor/app/version-check/server/functions/getNewUpdates.ts +++ b/apps/meteor/app/version-check/server/functions/getNewUpdates.ts @@ -7,6 +7,8 @@ import { check, Match } from 'meteor/check'; import { getWorkspaceAccessToken } from '../../../cloud/server'; import { Info } from '../../../utils/rocketchat.info'; +/** @deprecated */ + export const getNewUpdates = async () => { try { const uniqueID = await Settings.findOne('uniqueID'); diff --git a/packages/server-cloud-communication/src/definitions/index.ts b/packages/server-cloud-communication/src/definitions/index.ts index d554aa5380594..4ac455ccefd14 100644 --- a/packages/server-cloud-communication/src/definitions/index.ts +++ b/packages/server-cloud-communication/src/definitions/index.ts @@ -17,6 +17,8 @@ type Message = { type Version = { version: string; expiration: Date; + security: boolean; + infoUrl: string; messages?: Message[]; };