Skip to content

Commit

Permalink
Merge pull request #139 from azubieta/implement_updates_using_github_…
Browse files Browse the repository at this point in the history
…releases

Implement updates using GitHub releases
  • Loading branch information
raoulh authored Oct 30, 2017
2 parents f7048be + 9eeb6d1 commit b78b5a1
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 45 deletions.
12 changes: 7 additions & 5 deletions src/AppGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "MacUtils.h"
#endif

#define GITHUB_UPDATE_URL "https://api.github.com/repos/mooltipass/moolticute/releases"

#if defined(Q_OS_WIN)
#define MC_UPDATE_URL "https://calaos.fr/mooltipass/windows/updater.json"
#elif defined(Q_OS_MAC)
Expand Down Expand Up @@ -521,12 +523,12 @@ void AppGui::checkUpdate(bool displayMessage)
return;

auto u = QSimpleUpdater::getInstance();
u->setModuleVersion(MC_UPDATE_URL, APP_VERSION);
u->setNotifyOnUpdate(MC_UPDATE_URL, true);
u->setDownloaderEnabled(MC_UPDATE_URL, true);
u->setNotifyOnFinish(MC_UPDATE_URL, displayMessage);
u->setModuleVersion(GITHUB_UPDATE_URL, APP_VERSION);
u->setNotifyOnUpdate(GITHUB_UPDATE_URL, true);
u->setDownloaderEnabled(GITHUB_UPDATE_URL, true);
u->setNotifyOnFinish(GITHUB_UPDATE_URL, displayMessage);

u->checkForUpdates(MC_UPDATE_URL);
u->checkForUpdates(GITHUB_UPDATE_URL);

//Recheck in at least 30minutes plus some random time
if (!displayMessage)
Expand Down
66 changes: 53 additions & 13 deletions src/QSimpleUpdater/src/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <QDebug>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QMessageBox>
#include <QApplication>
Expand Down Expand Up @@ -361,18 +362,58 @@ void Updater::onReply (QNetworkReply* reply)
return;
}

/* Get the platform information */
QJsonObject updates = document.object().value ("updates").toObject();
QJsonObject platform = updates.value (platformKey()).toObject();
/* Get latest GitHub Release */
QJsonArray githubReleases = document.array();
QJsonObject latestGithubRelese = githubReleases.at(0).toObject();
QString tagName = latestGithubRelese.value("tag_name").toString();
QString htmlUrl = latestGithubRelese.value("html_url").toString();
QString body = latestGithubRelese.value("body").toString();

/* Get update information */
m_openUrl = platform.value ("open-url").toString();
m_changelog = platform.value ("changelog").toString();
m_downloadUrl = platform.value ("download-url").toString();
m_latestVersion = platform.value ("latest-version").toString();
QString releaseFileSuffix;
if (platformKey().compare("osx") == 0)
releaseFileSuffix = ".dmg";

/* Compare latest and current version */
setUpdateAvailable (latestVersion() != moduleVersion());
if (platformKey().compare("linux") == 0)
releaseFileSuffix = ".deb";

if (platformKey().compare("windows") == 0)
releaseFileSuffix = ".exe";

if (releaseFileSuffix.isEmpty())
{
qWarning() << "Automatic updates are not suppurted in this platform " << platformKey();
setUpdateAvailable(false);
emit checkingFinished (url());
return;
}

QJsonArray githubReleaseAssets = latestGithubRelese.value("assets").toArray();
bool releaseFound = false;
QString releaseName;
QString downloadUrl;
for (QJsonValue jsonValue : githubReleaseAssets)
{
QJsonObject releaseAsset = jsonValue.toObject();
releaseName = releaseAsset.value("name").toString();

if (releaseName.endsWith(releaseFileSuffix))
{
releaseFound = true;
downloadUrl = releaseAsset.value("browser_download_url").toString();
break;
}
}
if (releaseFound)
{
m_openUrl = htmlUrl;
m_changelog = body;
m_downloadUrl = downloadUrl;
m_latestVersion = tagName;
qDebug() << m_downloadUrl;

/* Compare latest and current version */
setUpdateAvailable (latestVersion() != moduleVersion());
}
emit checkingFinished (url());
}

Expand Down Expand Up @@ -401,15 +442,14 @@ void Updater::setUpdateAvailable (const bool available)
box.setDefaultButton (QMessageBox::Yes);

if (box.exec() == QMessageBox::Yes) {
if (!openUrl().isEmpty())
if (downloadUrl().isEmpty())
QDesktopServices::openUrl (QUrl (openUrl()));

else if (downloaderEnabled()) {
if (downloaderEnabled()) {
m_downloader->setUrlId (url());
m_downloader->setFileName (downloadUrl().split ("/").last());
m_downloader->startDownload (QUrl (downloadUrl()));
}

else
QDesktopServices::openUrl (QUrl (downloadUrl()));
}
Expand Down
26 changes: 3 additions & 23 deletions tests/tst_filescache.cpp → tests/FilesCacheTests.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
#include <QString>
#include <QtTest>
#include "FilesCacheTests.h"

#include <QStringList>
#include "../src/FilesCache.h"

class TestsFilesCache : public QObject
{
Q_OBJECT

public:
TestsFilesCache();

private Q_SLOTS:
void testSaveAndLoadFileNames();
};

TestsFilesCache::TestsFilesCache()
FilesCacheTests::FilesCacheTests()
{
}

void TestsFilesCache::testSaveAndLoadFileNames()
void FilesCacheTests::testSaveAndLoadFileNames()
{
QList<QVariantMap> testFiles;
for (int i = 0; i< 3; i++)
Expand All @@ -46,8 +31,3 @@ void TestsFilesCache::testSaveAndLoadFileNames()

QVERIFY(cache.erase());
}


QTEST_APPLESS_MAIN(TestsFilesCache)

#include "tst_filescache.moc"
20 changes: 20 additions & 0 deletions tests/FilesCacheTests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <QString>
#include <QtTest>

#include <QStringList>
#include "../src/FilesCache.h"

class FilesCacheTests : public QObject
{
Q_OBJECT

public:
FilesCacheTests();

private Q_SLOTS:
void testSaveAndLoadFileNames();
};




24 changes: 24 additions & 0 deletions tests/UpdaterTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "UpdaterTests.h"

#include <QSignalSpy>

#include "../src/QSimpleUpdater/include/QSimpleUpdater.h"

void UpdaterTests::testCheckForUpdates()
{
QString githubApiReleasesUrl ="https://api.github.com/repos/mooltipass/moolticute/releases";
QSimpleUpdater *updater = QSimpleUpdater::getInstance();

updater->setModuleVersion(githubApiReleasesUrl, "0.0");
updater->setNotifyOnUpdate(githubApiReleasesUrl, true);
updater->setDownloaderEnabled(githubApiReleasesUrl, true);
updater->setNotifyOnFinish(githubApiReleasesUrl, "All done");

updater->checkForUpdates(githubApiReleasesUrl);

QSignalSpy spyChecking(updater, &QSimpleUpdater::checkingFinished);
spyChecking.wait(5000);

QSignalSpy spyDownload(updater, &QSimpleUpdater::downloadFinished);
spyDownload.wait(30000);
}
11 changes: 11 additions & 0 deletions tests/UpdaterTests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <QString>
#include <QtTest>

#include <QStringList>

class UpdaterTests : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testCheckForUpdates();
};
26 changes: 26 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <QtTest>

#include "FilesCacheTests.h"
#include "UpdaterTests.h"

#define TEST_CLASS(TestClass, status) \
{ \
TestClass tc; \
status |= QTest::qExec(&tc, argc, argv);\
}


// Note: This is equivalent to QTEST_APPLESS_MAIN for multiple test classes.
int main(int argc, char** argv)
{
QApplication app(argc, argv);
app.setAttribute(Qt::AA_Use96Dpi, true);

int status = 0;
TEST_CLASS(FilesCache, status);
TEST_CLASS(UpdaterTests, status);

return status;
}


13 changes: 9 additions & 4 deletions tests/tests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ QT -= gui

QMAKE_CXXFLAGS += -std=c++0x

TARGET = tst_testsfilescache
TARGET = tests
CONFIG += console
CONFIG -= app_bundle

Expand All @@ -27,14 +27,19 @@ DEFINES += QT_DEPRECATED_WARNINGS
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

include (../src/QSimpleUpdater/QSimpleUpdater.pri)

SOURCES += \
tst_filescache.cpp \
../src/SimpleCrypt/SimpleCrypt.cpp \
../src/FilesCache.cpp
../src/FilesCache.cpp \
main.cpp \
FilesCacheTests.cpp \
UpdaterTests.cpp

HEADERS += \
../src/SimpleCrypt/SimpleCrypt.h \
../src/FilesCache.h
../src/FilesCache.h \
UpdaterTests.h \
FilesCacheTests.h

DEFINES += SRCDIR=\\\"$$PWD/\\\"

0 comments on commit b78b5a1

Please sign in to comment.