Skip to content

Commit b78b5a1

Browse files
authored
Merge pull request #139 from azubieta/implement_updates_using_github_releases
Implement updates using GitHub releases
2 parents f7048be + 9eeb6d1 commit b78b5a1

8 files changed

Lines changed: 153 additions & 45 deletions

File tree

src/AppGui.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "MacUtils.h"
2525
#endif
2626

27+
#define GITHUB_UPDATE_URL "https://api.github.com/repos/mooltipass/moolticute/releases"
28+
2729
#if defined(Q_OS_WIN)
2830
#define MC_UPDATE_URL "https://calaos.fr/mooltipass/windows/updater.json"
2931
#elif defined(Q_OS_MAC)
@@ -521,12 +523,12 @@ void AppGui::checkUpdate(bool displayMessage)
521523
return;
522524

523525
auto u = QSimpleUpdater::getInstance();
524-
u->setModuleVersion(MC_UPDATE_URL, APP_VERSION);
525-
u->setNotifyOnUpdate(MC_UPDATE_URL, true);
526-
u->setDownloaderEnabled(MC_UPDATE_URL, true);
527-
u->setNotifyOnFinish(MC_UPDATE_URL, displayMessage);
526+
u->setModuleVersion(GITHUB_UPDATE_URL, APP_VERSION);
527+
u->setNotifyOnUpdate(GITHUB_UPDATE_URL, true);
528+
u->setDownloaderEnabled(GITHUB_UPDATE_URL, true);
529+
u->setNotifyOnFinish(GITHUB_UPDATE_URL, displayMessage);
528530

529-
u->checkForUpdates(MC_UPDATE_URL);
531+
u->checkForUpdates(GITHUB_UPDATE_URL);
530532

531533
//Recheck in at least 30minutes plus some random time
532534
if (!displayMessage)

src/QSimpleUpdater/src/Updater.cpp

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <QDebug>
3131
#include <QJsonValue>
32+
#include <QJsonArray>
3233
#include <QJsonObject>
3334
#include <QMessageBox>
3435
#include <QApplication>
@@ -361,18 +362,58 @@ void Updater::onReply (QNetworkReply* reply)
361362
return;
362363
}
363364

364-
/* Get the platform information */
365-
QJsonObject updates = document.object().value ("updates").toObject();
366-
QJsonObject platform = updates.value (platformKey()).toObject();
365+
/* Get latest GitHub Release */
366+
QJsonArray githubReleases = document.array();
367+
QJsonObject latestGithubRelese = githubReleases.at(0).toObject();
368+
QString tagName = latestGithubRelese.value("tag_name").toString();
369+
QString htmlUrl = latestGithubRelese.value("html_url").toString();
370+
QString body = latestGithubRelese.value("body").toString();
367371

368-
/* Get update information */
369-
m_openUrl = platform.value ("open-url").toString();
370-
m_changelog = platform.value ("changelog").toString();
371-
m_downloadUrl = platform.value ("download-url").toString();
372-
m_latestVersion = platform.value ("latest-version").toString();
372+
QString releaseFileSuffix;
373+
if (platformKey().compare("osx") == 0)
374+
releaseFileSuffix = ".dmg";
373375

374-
/* Compare latest and current version */
375-
setUpdateAvailable (latestVersion() != moduleVersion());
376+
if (platformKey().compare("linux") == 0)
377+
releaseFileSuffix = ".deb";
378+
379+
if (platformKey().compare("windows") == 0)
380+
releaseFileSuffix = ".exe";
381+
382+
if (releaseFileSuffix.isEmpty())
383+
{
384+
qWarning() << "Automatic updates are not suppurted in this platform " << platformKey();
385+
setUpdateAvailable(false);
386+
emit checkingFinished (url());
387+
return;
388+
}
389+
390+
QJsonArray githubReleaseAssets = latestGithubRelese.value("assets").toArray();
391+
bool releaseFound = false;
392+
QString releaseName;
393+
QString downloadUrl;
394+
for (QJsonValue jsonValue : githubReleaseAssets)
395+
{
396+
QJsonObject releaseAsset = jsonValue.toObject();
397+
releaseName = releaseAsset.value("name").toString();
398+
399+
if (releaseName.endsWith(releaseFileSuffix))
400+
{
401+
releaseFound = true;
402+
downloadUrl = releaseAsset.value("browser_download_url").toString();
403+
break;
404+
}
405+
}
406+
if (releaseFound)
407+
{
408+
m_openUrl = htmlUrl;
409+
m_changelog = body;
410+
m_downloadUrl = downloadUrl;
411+
m_latestVersion = tagName;
412+
qDebug() << m_downloadUrl;
413+
414+
/* Compare latest and current version */
415+
setUpdateAvailable (latestVersion() != moduleVersion());
416+
}
376417
emit checkingFinished (url());
377418
}
378419

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

403444
if (box.exec() == QMessageBox::Yes) {
404-
if (!openUrl().isEmpty())
445+
if (downloadUrl().isEmpty())
405446
QDesktopServices::openUrl (QUrl (openUrl()));
406447

407-
else if (downloaderEnabled()) {
448+
if (downloaderEnabled()) {
408449
m_downloader->setUrlId (url());
409450
m_downloader->setFileName (downloadUrl().split ("/").last());
410451
m_downloader->startDownload (QUrl (downloadUrl()));
411452
}
412-
413453
else
414454
QDesktopServices::openUrl (QUrl (downloadUrl()));
415455
}
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
#include <QString>
2-
#include <QtTest>
1+
#include "FilesCacheTests.h"
32

4-
#include <QStringList>
5-
#include "../src/FilesCache.h"
6-
7-
class TestsFilesCache : public QObject
8-
{
9-
Q_OBJECT
10-
11-
public:
12-
TestsFilesCache();
13-
14-
private Q_SLOTS:
15-
void testSaveAndLoadFileNames();
16-
};
17-
18-
TestsFilesCache::TestsFilesCache()
3+
FilesCacheTests::FilesCacheTests()
194
{
205
}
216

22-
void TestsFilesCache::testSaveAndLoadFileNames()
7+
void FilesCacheTests::testSaveAndLoadFileNames()
238
{
249
QList<QVariantMap> testFiles;
2510
for (int i = 0; i< 3; i++)
@@ -46,8 +31,3 @@ void TestsFilesCache::testSaveAndLoadFileNames()
4631

4732
QVERIFY(cache.erase());
4833
}
49-
50-
51-
QTEST_APPLESS_MAIN(TestsFilesCache)
52-
53-
#include "tst_filescache.moc"

tests/FilesCacheTests.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <QString>
2+
#include <QtTest>
3+
4+
#include <QStringList>
5+
#include "../src/FilesCache.h"
6+
7+
class FilesCacheTests : public QObject
8+
{
9+
Q_OBJECT
10+
11+
public:
12+
FilesCacheTests();
13+
14+
private Q_SLOTS:
15+
void testSaveAndLoadFileNames();
16+
};
17+
18+
19+
20+

tests/UpdaterTests.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "UpdaterTests.h"
2+
3+
#include <QSignalSpy>
4+
5+
#include "../src/QSimpleUpdater/include/QSimpleUpdater.h"
6+
7+
void UpdaterTests::testCheckForUpdates()
8+
{
9+
QString githubApiReleasesUrl ="https://api.github.com/repos/mooltipass/moolticute/releases";
10+
QSimpleUpdater *updater = QSimpleUpdater::getInstance();
11+
12+
updater->setModuleVersion(githubApiReleasesUrl, "0.0");
13+
updater->setNotifyOnUpdate(githubApiReleasesUrl, true);
14+
updater->setDownloaderEnabled(githubApiReleasesUrl, true);
15+
updater->setNotifyOnFinish(githubApiReleasesUrl, "All done");
16+
17+
updater->checkForUpdates(githubApiReleasesUrl);
18+
19+
QSignalSpy spyChecking(updater, &QSimpleUpdater::checkingFinished);
20+
spyChecking.wait(5000);
21+
22+
QSignalSpy spyDownload(updater, &QSimpleUpdater::downloadFinished);
23+
spyDownload.wait(30000);
24+
}

tests/UpdaterTests.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <QString>
2+
#include <QtTest>
3+
4+
#include <QStringList>
5+
6+
class UpdaterTests : public QObject
7+
{
8+
Q_OBJECT
9+
private Q_SLOTS:
10+
void testCheckForUpdates();
11+
};

tests/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <QtTest>
2+
3+
#include "FilesCacheTests.h"
4+
#include "UpdaterTests.h"
5+
6+
#define TEST_CLASS(TestClass, status) \
7+
{ \
8+
TestClass tc; \
9+
status |= QTest::qExec(&tc, argc, argv);\
10+
}
11+
12+
13+
// Note: This is equivalent to QTEST_APPLESS_MAIN for multiple test classes.
14+
int main(int argc, char** argv)
15+
{
16+
QApplication app(argc, argv);
17+
app.setAttribute(Qt::AA_Use96Dpi, true);
18+
19+
int status = 0;
20+
TEST_CLASS(FilesCache, status);
21+
TEST_CLASS(UpdaterTests, status);
22+
23+
return status;
24+
}
25+
26+

tests/tests.pro

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ QT -= gui
1010

1111
QMAKE_CXXFLAGS += -std=c++0x
1212

13-
TARGET = tst_testsfilescache
13+
TARGET = tests
1414
CONFIG += console
1515
CONFIG -= app_bundle
1616

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

30+
include (../src/QSimpleUpdater/QSimpleUpdater.pri)
3031

3132
SOURCES += \
32-
tst_filescache.cpp \
3333
../src/SimpleCrypt/SimpleCrypt.cpp \
34-
../src/FilesCache.cpp
34+
../src/FilesCache.cpp \
35+
main.cpp \
36+
FilesCacheTests.cpp \
37+
UpdaterTests.cpp
3538

3639
HEADERS += \
3740
../src/SimpleCrypt/SimpleCrypt.h \
38-
../src/FilesCache.h
41+
../src/FilesCache.h \
42+
UpdaterTests.h \
43+
FilesCacheTests.h
3944

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

0 commit comments

Comments
 (0)