From 6618378e8d2991d20d163dfbf135e4e3a2bda0d0 Mon Sep 17 00:00:00 2001 From: J Blackman Date: Sun, 19 Feb 2023 06:20:18 +1100 Subject: [PATCH] Simplified configurator version checking (#3346) --- src/js/BuildApi.js | 5 ++ src/js/release_checker.js | 60 --------------------- src/js/utils/checkForConfiguratorUpdates.js | 44 +++++---------- 3 files changed, 19 insertions(+), 90 deletions(-) delete mode 100644 src/js/release_checker.js diff --git a/src/js/BuildApi.js b/src/js/BuildApi.js index 577c9fde..b4ab14df 100644 --- a/src/js/BuildApi.js +++ b/src/js/BuildApi.js @@ -160,4 +160,9 @@ export default class BuildApi { const url = `${this._url}/api/releases/${release}/commits`; this.load(url, onSuccess, onFailure); } + + loadConfiguratorRelease(type, onSuccess, onFailure) { + const url = `${this._url}/api/configurator/releases/${type}`; + this.load(url, onSuccess, onFailure); + } } diff --git a/src/js/release_checker.js b/src/js/release_checker.js deleted file mode 100644 index 14529dfd..00000000 --- a/src/js/release_checker.js +++ /dev/null @@ -1,60 +0,0 @@ -import { gui_log } from "./gui_log"; -import { i18n } from "./localization"; -import { get as getStorage, set as setStorage } from "./SessionStorage"; - -const ReleaseChecker = function (releaseName, releaseUrl) { - const self = this; - - self._releaseName = releaseName; - self._releaseDataTag = `${self._releaseName}ReleaseData`; - self._releaseLastUpdateTag = `${self._releaseName}ReleaseLastUpdate`; - self._releaseUrl = releaseUrl; -}; - -ReleaseChecker.prototype.loadReleaseData = function (processFunction) { - const self = this; - const result = getStorage([self._releaseLastUpdateTag, self._releaseDataTag]); - const releaseDataTimestamp = $.now(); - const cacheReleaseData = result[self._releaseDataTag]; - const cachedReleaseLastUpdate = result[self._releaseLastUpdateTag]; - - if (!cacheReleaseData || !cachedReleaseLastUpdate || releaseDataTimestamp - cachedReleaseLastUpdate > 3600 * 1000) { - $.get(self._releaseUrl, function (releaseData) { - gui_log(i18n.getMessage('releaseCheckLoaded',[self._releaseName])); - - const data = {}; - data[self._releaseDataTag] = releaseData; - data[self._releaseLastUpdateTag] = releaseDataTimestamp; - setStorage(data); - - self._processReleaseData(releaseData, processFunction); - }).fail(function (data) { - let message = ''; - if (data['responseJSON']) { - message = data['responseJSON'].message; - } - gui_log(i18n.getMessage('releaseCheckFailed',[self._releaseName,message])); - - self._processReleaseData(cacheReleaseData, processFunction); - }); - } else { - if (cacheReleaseData) { - gui_log(i18n.getMessage('releaseCheckCached',[self._releaseName])); - } - - self._processReleaseData(cacheReleaseData, processFunction); - } -}; - - -ReleaseChecker.prototype._processReleaseData = function (releaseData, processFunction) { - if (releaseData) { - processFunction(releaseData); - } else { - gui_log(i18n.getMessage('releaseCheckNoInfo',[self._releaseName])); - - processFunction(); - } -}; - -export default ReleaseChecker; diff --git a/src/js/utils/checkForConfiguratorUpdates.js b/src/js/utils/checkForConfiguratorUpdates.js index 8bfc1f65..92bac73d 100644 --- a/src/js/utils/checkForConfiguratorUpdates.js +++ b/src/js/utils/checkForConfiguratorUpdates.js @@ -1,43 +1,21 @@ -import semver from "semver"; -import ReleaseChecker from "../release_checker"; +import BuildApi from "../BuildApi"; import { get as getConfig } from "../ConfigStorage"; import CONFIGURATOR from "../data_storage"; import { i18n } from "../localization"; import { gui_log } from "../gui_log"; -function notifyOutdatedVersion(releaseData) { - const result = getConfig('checkForConfiguratorUnstableVersions'); - let showUnstableReleases = false; - if (result.checkForConfiguratorUnstableVersions) { - showUnstableReleases = true; - } +function notifyOutdatedVersion(data) { - if (releaseData === undefined) { + if (data === undefined) { console.log('No releaseData'); return false; } - const versions = releaseData.filter(function (version) { - const semVerVersion = semver.parse(version.tag_name); - if (semVerVersion && (showUnstableReleases || semVerVersion.prerelease.length === 0)) { - return version; - } else { - return null; - } - }).sort(function (v1, v2) { - try { - return semver.compare(v2.tag_name, v1.tag_name); - } catch (e) { - return false; - } - }); + if (data.isCurrent === false && data.updatedVersion !== undefined) { - if (versions.length > 0) { - CONFIGURATOR.latestVersion = versions[0].tag_name; - CONFIGURATOR.latestVersionReleaseUrl = versions[0].html_url; - } + CONFIGURATOR.latestVersion = data.UpdatedVersion.Version; + CONFIGURATOR.latestVersionReleaseUrl = data.UpdatedVersion.Url; - if (semver.lt(CONFIGURATOR.version, CONFIGURATOR.latestVersion)) { const message = i18n.getMessage('configuratorUpdateNotice', [CONFIGURATOR.latestVersion, CONFIGURATOR.latestVersionReleaseUrl]); gui_log(message); @@ -60,7 +38,13 @@ function notifyOutdatedVersion(releaseData) { } export function checkForConfiguratorUpdates() { - const releaseChecker = new ReleaseChecker('configurator', 'https://api.github.com/repos/betaflight/betaflight-configurator/releases'); - releaseChecker.loadReleaseData(notifyOutdatedVersion); + const result = getConfig('checkForConfiguratorUnstableVersions'); + let type = "Stable"; + if (result.checkForConfiguratorUnstableVersions) { + type = "Unstable"; + } + + const buildApi = new BuildApi(); + buildApi.loadConfiguratorRelease(type, notifyOutdatedVersion); }