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
+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
{