From 18c993c0d5c712e807a56a9ff9126905c4757fd7 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Sat, 20 Aug 2016 12:14:22 +0300 Subject: [PATCH] add github issue reporing backend (#104) --- .../awesome-widget/plugin/awbugreporter.cpp | 87 +++++++++++++++++++ sources/awesome-widget/plugin/awbugreporter.h | 46 ++++++++++ .../plugin/awdataaggregator.cpp | 1 - .../awesome-widget/plugin/awesomewidget.cpp | 2 + sources/test/CMakeLists.txt | 16 ++-- sources/test/testawbugreporter.cpp | 53 +++++++++++ sources/test/testawbugreporter.h | 43 +++++++++ sources/version.h.in | 1 + 8 files changed, 242 insertions(+), 7 deletions(-) create mode 100644 sources/awesome-widget/plugin/awbugreporter.cpp create mode 100644 sources/awesome-widget/plugin/awbugreporter.h create mode 100644 sources/test/testawbugreporter.cpp create mode 100644 sources/test/testawbugreporter.h diff --git a/sources/awesome-widget/plugin/awbugreporter.cpp b/sources/awesome-widget/plugin/awbugreporter.cpp new file mode 100644 index 0000000..c8e6c6b --- /dev/null +++ b/sources/awesome-widget/plugin/awbugreporter.cpp @@ -0,0 +1,87 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + +#include "awbugreporter.h" + +#include +#include +#include +#include +#include + +#include "awdebug.h" + + +AWBugReporter::AWBugReporter(QObject *parent) + : QObject(parent) +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; +} + + +AWBugReporter::~AWBugReporter() +{ + qCDebug(LOG_AW) << __PRETTY_FUNCTION__; +} + + +void AWBugReporter::sendBugReport(const QString title, const QString body) +{ + QNetworkAccessManager *manager = new QNetworkAccessManager(nullptr); + connect(manager, SIGNAL(finished(QNetworkReply *)), this, + SLOT(issueReplyRecieved(QNetworkReply *))); + + QNetworkRequest request(QUrl(BUGTRACKER_API)); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + // generate payload + QVariantMap payload; + payload[QString("title")] = title; + payload[QString("body")] = body; + payload[QString("labels")] = QStringList() << QString("from application"); + // convert to QByteArray to send request + QByteArray data + = QJsonDocument::fromVariant(payload).toJson(QJsonDocument::Compact); + qCInfo(LOG_AW) << "Send request with body" << data.data() << "and size" + << data.size(); + + manager->post(request, data); +} + + +void AWBugReporter::issueReplyRecieved(QNetworkReply *reply) +{ + if (reply->error() != QNetworkReply::NoError) { + qCWarning(LOG_AW) << "An error occurs" << reply->error() + << "with message" << reply->errorString(); + return emit(replyReceived(false, QString())); + } + + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qCWarning(LOG_AW) << "Parse error" << error.errorString(); + return emit(replyReceived(false, QString())); + } + reply->deleteLater(); + + // convert to map + QVariantMap response = jsonDoc.toVariant().toMap(); + QString url = response[QString("html_url")].toString(); + + return emit(replyReceived(true, url)); +} diff --git a/sources/awesome-widget/plugin/awbugreporter.h b/sources/awesome-widget/plugin/awbugreporter.h new file mode 100644 index 0000000..4c460f2 --- /dev/null +++ b/sources/awesome-widget/plugin/awbugreporter.h @@ -0,0 +1,46 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#ifndef AWBUGREPORTER_H +#define AWBUGREPORTER_H + +#include + + +class QNetworkReply; + +class AWBugReporter : public QObject +{ + Q_OBJECT + +public: + explicit AWBugReporter(QObject *parent = nullptr); + virtual ~AWBugReporter(); + Q_INVOKABLE void sendBugReport(const QString title, const QString body); + +signals: + void replyReceived(bool status, QString url); + +private slots: + void issueReplyRecieved(QNetworkReply *reply); + +private: +}; + + +#endif /* AWBUGREPORTER_H */ diff --git a/sources/awesome-widget/plugin/awdataaggregator.cpp b/sources/awesome-widget/plugin/awdataaggregator.cpp index 805ccec..89f910c 100644 --- a/sources/awesome-widget/plugin/awdataaggregator.cpp +++ b/sources/awesome-widget/plugin/awdataaggregator.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include diff --git a/sources/awesome-widget/plugin/awesomewidget.cpp b/sources/awesome-widget/plugin/awesomewidget.cpp index e327c47..ec86494 100644 --- a/sources/awesome-widget/plugin/awesomewidget.cpp +++ b/sources/awesome-widget/plugin/awesomewidget.cpp @@ -20,6 +20,7 @@ #include #include "awactions.h" +#include "awbugreporter.h" #include "awconfighelper.h" #include "awformatterconfigfactory.h" #include "awkeys.h" @@ -30,6 +31,7 @@ void AWPlugin::registerTypes(const char *uri) Q_ASSERT(uri == QLatin1String("org.kde.plasma.private.awesomewidget")); qmlRegisterType(uri, 1, 0, "AWActions"); + qmlRegisterType(uri, 1, 0, "AWBugReporter"); qmlRegisterType(uri, 1, 0, "AWConfigHelper"); qmlRegisterType(uri, 1, 0, "AWFormatterConfigFactory"); diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index c2821a5..c0e5bbf 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -16,7 +16,7 @@ include_directories( ${Kf5_INCLUDE} ) -## library +# library set(AWTESTLIBRARY_HEADERS awtestlibrary.h) set(AWTESTLIBRARY_SOURCES awtestlibrary.cpp) add_library(${SUBPROJECT}-awtest STATIC ${AWTESTLIBRARY_SOURCES} ${AWTESTLIBRARY_HEADERS}) @@ -24,18 +24,20 @@ target_link_libraries(${SUBPROJECT}-awtest ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) set(LIBRARY_TEST_SET ${SUBPROJECT}-awtest ${PROJECT_LIBRARY} ${PROJECT_MONITORSOURCES} ${Qt_LIBRARIES} ${Kf5_LIBRARIES} ${Qt5Test_LIBRARIES}) -## modules +# modules set(TEST_MODULES abstractextitem extquotes extscript extupgrade extweather abstractformatter datetimeformatter floatformatter listformatter noformatter scriptformatter stringformatter extitemaggregator batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource - awconfighelper awkeycache awkeys awpatternfunctions awupdatehelper + awbugreporter awconfighelper awkeycache awkeys awpatternfunctions awupdatehelper dpplugin) foreach (TEST_MODULE ${TEST_MODULES}) set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h) set(${TEST_MODULE}_SOURCES test${TEST_MODULE}.cpp) - if (TEST_MODULE MATCHES "awconfighelper") + if (TEST_MODULE MATCHES "awbugreporter") + set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awbugreporter.cpp) + elseif (TEST_MODULE MATCHES "awconfighelper") set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awconfighelper.cpp) elseif (TEST_MODULE MATCHES "awkeycache") set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeycache.cpp) @@ -60,8 +62,10 @@ foreach (TEST_MODULE ${TEST_MODULES}) elseif (TEST_MODULE MATCHES "dpplugin") set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../desktop-panel/plugin/dpadds.cpp ${PROJECT_TRDPARTY_DIR}/fontdialog/fontdialog.cpp) - endif (TEST_MODULE MATCHES "awconfighelper") + endif (TEST_MODULE MATCHES "awbugreporter") add_executable(${SUBPROJECT}-${TEST_MODULE} ${${TEST_MODULE}_HEADERS} ${${TEST_MODULE}_SOURCES}) target_link_libraries(${SUBPROJECT}-${TEST_MODULE} ${LIBRARY_TEST_SET}) - add_test(NAME ${TEST_MODULE} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-${TEST_MODULE}) + if (NOT TEST_MODULE MATCHES "awbugreporter") + add_test(NAME ${TEST_MODULE} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-${TEST_MODULE}) + endif (NOT TEST_MODULE MATCHES "awbugreporter") endforeach (TEST_MODULE) diff --git a/sources/test/testawbugreporter.cpp b/sources/test/testawbugreporter.cpp new file mode 100644 index 0000000..e5c7455 --- /dev/null +++ b/sources/test/testawbugreporter.cpp @@ -0,0 +1,53 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#include "testawbugreporter.h" + +#include + +#include "awbugreporter.h" +#include "awtestlibrary.h" + + +void TestAWBugReporter::initTestCase() +{ + plugin = new AWBugReporter(this); +} + + +void TestAWBugReporter::cleanupTestCase() +{ + delete plugin; +} + + +void TestAWBugReporter::test_sendBugReport() +{ + QSignalSpy spy(plugin, SIGNAL(replyReceived(bool, QString))); + plugin->sendBugReport(AWTestLibrary::randomString(), + AWTestLibrary::randomString()); + + QVERIFY(spy.wait(5000)); + QVariantList arguments = spy.takeFirst(); + + QVERIFY(arguments.at(0).toBool()); + QVERIFY(!arguments.at(1).toString().isEmpty()); +} + + +QTEST_MAIN(TestAWBugReporter); diff --git a/sources/test/testawbugreporter.h b/sources/test/testawbugreporter.h new file mode 100644 index 0000000..7b85254 --- /dev/null +++ b/sources/test/testawbugreporter.h @@ -0,0 +1,43 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#ifndef TESTAWBUGREPORTER_H +#define TESTAWBUGREPORTER_H + +#include + + +class AWBugReporter; + +class TestAWBugReporter : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_sendBugReport(); + +private: + AWBugReporter *plugin = nullptr; +}; + + +#endif /* TESTAWBUGREPORTER_H */ diff --git a/sources/version.h.in b/sources/version.h.in index b52da4d..46eeac4 100644 --- a/sources/version.h.in +++ b/sources/version.h.in @@ -61,6 +61,7 @@ #define VERSION_API \ "https://api.github.com/repos/arcan1s/awesome-widgets/releases" #define BUGTRACKER "https://github.com/arcan1s/awesome-widgets/issues" +#define BUGTRACKER_API "http://arcanis.me/repos/arcan1s/awesome-widgets/issues" #define TRANSLATION "https://github.com/arcan1s/awesome-widgets/issues/14" #define AUR_PACKAGES \ "https://aur.archlinux.org/packages/plasma5-applet-awesome-widgets/"