Fix version checking (#4329)

https://github.com/Neargye/semver

* Use semver library for version downgrade checking

* Add test validating our current version is valid semver
This commit is contained in:
pajlada
2023-01-26 19:22:48 +01:00
committed by GitHub
parent ff9f63c5e0
commit bf5a5b839c
11 changed files with 981 additions and 33 deletions
+3
View File
@@ -768,6 +768,9 @@ endif ()
target_include_directories(${LIBRARY_PROJECT} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_BINARY_DIR}/autogen/)
# semver dependency https://github.com/Neargye/semver
target_include_directories(${LIBRARY_PROJECT} PUBLIC ${CMAKE_SOURCE_DIR}/lib/semver/include)
if (WinToast_FOUND)
target_link_libraries(${LIBRARY_PROJECT}
PUBLIC
+21
View File
@@ -3,6 +3,27 @@
#include <QString>
#include <QtGlobal>
/**
* Valid version formats, in order of latest to oldest
*
* Stable:
* - 2.4.0
*
* Release candidate:
* - 2.4.0-rc.3
* - 2.4.0-rc.2
* - 2.4.0-rc
*
* Beta:
* - 2.4.0-beta.3
* - 2.4.0-beta.2
* - 2.4.0-beta
*
* Alpha:
* - 2.4.0-alpha.3
* - 2.4.0-alpha.2
* - 2.4.0-alpha
**/
#define CHATTERINO_VERSION "2.4.0"
#if defined(Q_OS_WIN)
+25 -33
View File
@@ -15,6 +15,7 @@
#include <QMessageBox>
#include <QProcess>
#include <QRegularExpression>
#include <semver/semver.hpp>
namespace chatterino {
namespace {
@@ -23,37 +24,6 @@ namespace {
return getSettings()->betaUpdates ? "beta" : "stable";
}
/// Checks if the online version is newer or older than the current version.
bool isDowngradeOf(const QString &online, const QString &current)
{
static auto matchVersion =
QRegularExpression(R"((\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?)");
// Versions are just strings, they don't need to follow a specific
// format so we can only assume if one version is newer than another
// one.
// We match x.x.x.x with each version level being optional.
auto onlineMatch = matchVersion.match(online);
auto currentMatch = matchVersion.match(current);
for (int i = 1; i <= 4; i++)
{
if (onlineMatch.captured(i).toInt() <
currentMatch.captured(i).toInt())
{
return true;
}
if (onlineMatch.captured(i).toInt() >
currentMatch.captured(i).toInt())
{
break;
}
}
return false;
}
} // namespace
Updates::Updates()
@@ -71,6 +41,28 @@ Updates &Updates::instance()
return instance;
}
/// Checks if the online version is newer or older than the current version.
bool Updates::isDowngradeOf(const QString &online, const QString &current)
{
semver::version onlineVersion;
if (!onlineVersion.from_string_noexcept(online.toStdString()))
{
qCWarning(chatterinoUpdate) << "Unable to parse online version"
<< online << "into a proper semver string";
return false;
}
semver::version currentVersion;
if (!currentVersion.from_string_noexcept(current.toStdString()))
{
qCWarning(chatterinoUpdate) << "Unable to parse current version"
<< current << "into a proper semver string";
return false;
}
return onlineVersion < currentVersion;
}
const QString &Updates::getCurrentVersion() const
{
return currentVersion_;
@@ -313,8 +305,8 @@ void Updates::checkForUpdates()
if (this->currentVersion_ != this->onlineVersion_)
{
this->setStatus_(UpdateAvailable);
this->isDowngrade_ =
isDowngradeOf(this->onlineVersion_, this->currentVersion_);
this->isDowngrade_ = Updates::isDowngradeOf(
this->onlineVersion_, this->currentVersion_);
}
else
{
+2
View File
@@ -24,6 +24,8 @@ public:
// fourtf: don't add this class to the application class
static Updates &instance();
static bool isDowngradeOf(const QString &online, const QString &current);
void checkForUpdates();
const QString &getCurrentVersion() const;
const QString &getOnlineVersion() const;
+3
View File
@@ -107,6 +107,9 @@ AboutPage::AboutPage()
addLicense(form.getElement(), "magic_enum",
"https://github.com/Neargye/magic_enum",
":/licenses/magic_enum.txt");
addLicense(form.getElement(), "semver",
"https://github.com/Neargye/semver",
":/licenses/semver.txt");
}
// Attributions