Repository navigation
feat: update and issue-report support for desktop tools #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+3,079
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,4 @@ ui_add_library(ui.backend.qt QT | |
| ) | ||
|
|
||
| add_subdirectory(test) | ||
| add_subdirectory(update) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| find_package(Qt6 6.2 REQUIRED COMPONENTS Core Network) | ||
|
|
||
| ui_add_library(ui.backend.qt.update QT | ||
| SOURCES | ||
| IssueLog.cpp | ||
| IssueLog.hpp | ||
| IssueReport.cpp | ||
| IssueReport.hpp | ||
| ReleaseFeed.cpp | ||
| ReleaseFeed.hpp | ||
| ReleaseVersion.cpp | ||
| ReleaseVersion.hpp | ||
| UpdateChecker.cpp | ||
| UpdateChecker.hpp | ||
| UpdateDownloader.cpp | ||
| UpdateDownloader.hpp | ||
| UpdateInstallation.cpp | ||
| UpdateInstallation.hpp | ||
| UpdateInstaller.cpp | ||
| UpdateInstaller.hpp | ||
| UpdateSession.cpp | ||
| UpdateSession.hpp | ||
| LINK | ||
| Qt6::Core | ||
| Qt6::Network | ||
| ) | ||
|
|
||
| add_subdirectory(test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| #include "ui/backend/qt/update/IssueLog.hpp" | ||
| #include <QDateTime> | ||
| #include <QDir> | ||
| #include <algorithm> | ||
| #include <atomic> | ||
|
|
||
| namespace ui::backend::qt::update | ||
| { | ||
| namespace | ||
| { | ||
| struct Capture | ||
| { | ||
| std::atomic<IssueLog*> log{ nullptr }; | ||
| QtMessageHandler previousHandler{ nullptr }; | ||
| }; | ||
|
|
||
| Capture& CaptureState() | ||
| { | ||
| constinit static Capture capture; | ||
| return capture; | ||
| } | ||
|
|
||
| bool& WritingOnThisThread() | ||
| { | ||
| constinit thread_local bool writing{ false }; | ||
| return writing; | ||
| } | ||
|
|
||
| void ForwardQtMessage(QtMsgType type, const QMessageLogContext& context, const QString& message) | ||
| { | ||
| const auto& capture = CaptureState(); | ||
| auto* log = capture.log.load(); | ||
| if (log != nullptr && !WritingOnThisThread() && type != QtDebugMsg && type != QtInfoMsg) | ||
| log->Write(type == QtWarningMsg ? IssueSeverity::warning : IssueSeverity::error, "Qt: " + message); | ||
|
|
||
| if (capture.previousHandler != nullptr) | ||
| capture.previousHandler(type, context, message); | ||
| } | ||
| } | ||
|
|
||
| QString ToString(IssueSeverity severity) | ||
| { | ||
| switch (severity) | ||
| { | ||
| case IssueSeverity::warning: | ||
| return "WARN "; | ||
| case IssueSeverity::error: | ||
| return "ERROR"; | ||
| default: | ||
| return "INFO "; | ||
| } | ||
| } | ||
|
|
||
| IssueLog::IssueLog(const Config& config) | ||
| : config(config) | ||
| { | ||
| this->config.keptFiles = std::max(this->config.keptFiles, 1); | ||
| QDir{}.mkpath(config.directory); | ||
| Open(); | ||
| } | ||
|
|
||
| IssueLog::~IssueLog() | ||
| { | ||
| if (capturing) | ||
| { | ||
| auto& capture = CaptureState(); | ||
| capture.log = nullptr; | ||
| qInstallMessageHandler(capture.previousHandler); | ||
| capture.previousHandler = nullptr; | ||
| } | ||
| } | ||
|
|
||
| void IssueLog::Write(IssueSeverity severity, const QString& message) | ||
| { | ||
| auto line = QString("%1 [%2] %3") | ||
| .arg(QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs), ToString(severity), message.simplified()); | ||
|
|
||
| std::scoped_lock lock{ mutex }; | ||
| WritingOnThisThread() = true; | ||
| Remember(line); | ||
|
|
||
| if (file.isOpen()) | ||
| RotateIfNeeded(); | ||
|
|
||
| if (file.isOpen()) | ||
| { | ||
| file.write(line.append('\n').toUtf8()); | ||
| file.flush(); | ||
| } | ||
|
|
||
| WritingOnThisThread() = false; | ||
| } | ||
|
|
||
| bool IssueLog::IsWritable() const | ||
| { | ||
| return file.isOpen(); | ||
| } | ||
|
|
||
| QString IssueLog::FilePath() const | ||
| { | ||
| return RotatedPath(0); | ||
| } | ||
|
|
||
| QString IssueLog::Directory() const | ||
| { | ||
| return config.directory; | ||
| } | ||
|
|
||
| QStringList IssueLog::RecentLines(std::size_t count) const | ||
| { | ||
| std::scoped_lock lock{ mutex }; | ||
| QStringList lines; | ||
| const auto skip = recent.size() > count ? recent.size() - count : 0; | ||
| for (auto it = recent.begin() + static_cast<std::ptrdiff_t>(skip); it != recent.end(); ++it) | ||
| lines.append(*it); | ||
| return lines; | ||
| } | ||
|
|
||
| void IssueLog::CaptureQtMessages() | ||
| { | ||
| auto& capture = CaptureState(); | ||
| if (capturing || capture.log.load() != nullptr) | ||
| return; | ||
|
|
||
| capturing = true; | ||
| capture.previousHandler = qInstallMessageHandler(ForwardQtMessage); | ||
| capture.log = this; | ||
| } | ||
|
|
||
| QString IssueLog::RotatedPath(int index) const | ||
| { | ||
| const auto name = index == 0 ? QString("%1.log").arg(config.baseName) : QString("%1.%2.log").arg(config.baseName).arg(index); | ||
| return QDir{ config.directory }.filePath(name); | ||
| } | ||
|
|
||
| void IssueLog::Open() | ||
| { | ||
| file.setFileName(FilePath()); | ||
| if (OpenForAppend()) | ||
| RotateIfNeeded(); | ||
| } | ||
|
|
||
| bool IssueLog::OpenForAppend() | ||
| { | ||
| return file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text); | ||
| } | ||
|
|
||
| void IssueLog::RotateIfNeeded() | ||
| { | ||
| if (file.size() < config.maxFileBytes) | ||
| return; | ||
|
|
||
| file.close(); | ||
| QFile::remove(RotatedPath(config.keptFiles - 1)); | ||
| for (int index = config.keptFiles - 2; index >= 0; --index) | ||
| QFile::rename(RotatedPath(index), RotatedPath(index + 1)); | ||
|
|
||
| OpenForAppend(); | ||
| } | ||
|
|
||
| void IssueLog::Remember(const QString& line) | ||
| { | ||
| recent.push_back(line); | ||
| while (recent.size() > config.recentCapacity) | ||
| recent.pop_front(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #pragma once | ||
|
|
||
| #include <QFile> | ||
| #include <QString> | ||
| #include <QStringList> | ||
| #include <QtGlobal> | ||
| #include <cstddef> | ||
| #include <deque> | ||
| #include <mutex> | ||
|
|
||
| namespace ui::backend::qt::update | ||
| { | ||
| enum class IssueSeverity | ||
| { | ||
| info, | ||
| warning, | ||
| error | ||
| }; | ||
|
|
||
| class IssueLog | ||
| { | ||
| public: | ||
| struct Config | ||
| { | ||
| QString directory; | ||
| QString baseName{ "application" }; | ||
| qint64 maxFileBytes{ 1024 * 1024 }; | ||
| int keptFiles{ 3 }; | ||
| std::size_t recentCapacity{ 200 }; | ||
| }; | ||
|
|
||
| explicit IssueLog(const Config& config); | ||
| ~IssueLog(); | ||
|
|
||
| IssueLog(const IssueLog&) = delete; | ||
| IssueLog& operator=(const IssueLog&) = delete; | ||
|
|
||
| void Write(IssueSeverity severity, const QString& message); | ||
|
|
||
| bool IsWritable() const; | ||
| QString FilePath() const; | ||
| QString Directory() const; | ||
| QStringList RecentLines(std::size_t count) const; | ||
|
|
||
| void CaptureQtMessages(); | ||
|
|
||
| private: | ||
| QString RotatedPath(int index) const; | ||
| void Open(); | ||
| bool OpenForAppend(); | ||
| void RotateIfNeeded(); | ||
| void Remember(const QString& line); | ||
|
|
||
| Config config; | ||
| QFile file; | ||
| std::deque<QString> recent; | ||
| mutable std::mutex mutex; | ||
| bool capturing{ false }; | ||
| }; | ||
|
|
||
| QString ToString(IssueSeverity severity); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #include "ui/backend/qt/update/IssueReport.hpp" | ||
| #include <QDir> | ||
| #include <QSysInfo> | ||
| #include <QUrlQuery> | ||
|
|
||
| namespace ui::backend::qt::update | ||
| { | ||
| namespace | ||
| { | ||
| QString IssueBody(const IssueEnvironment& environment, const QStringList& logLines) | ||
| { | ||
| auto body = QString("### What happened?\n\n" | ||
| "<!-- What did you do, what did you expect, and what happened instead? -->\n\n" | ||
| "### Environment\n\n" | ||
| "| | |\n|---|---|\n" | ||
| "| %1 | %2 |\n" | ||
| "| Operating system | %3 |\n" | ||
| "| Architecture | %4 |\n" | ||
| "| Qt | %5 |\n\n") | ||
| .arg(environment.product.displayName, environment.applicationVersion, environment.operatingSystem, environment.cpuArchitecture, environment.qtVersion); | ||
|
|
||
| body += "### Recent log\n\n```text\n"; | ||
| body += logLines.isEmpty() ? QString("(no entries)") : logLines.join('\n'); | ||
| body += "\n```\n"; | ||
|
|
||
| if (!environment.logFilePath.isEmpty()) | ||
| body += QString("\nFull log: `%1` — attach it if more context helps.\n").arg(environment.logFilePath); | ||
|
|
||
| return body; | ||
| } | ||
|
|
||
| QString Encoded(const QString& text) | ||
| { | ||
| return QString::fromLatin1(QUrl::toPercentEncoding(text)); | ||
| } | ||
|
|
||
| QUrl IssueUrl(const QUrl& newIssueUrl, const IssueEnvironment& environment, const QStringList& logLines) | ||
| { | ||
| QUrlQuery query; | ||
| query.addQueryItem("title", Encoded(QString("[%1] ").arg(environment.product.tag))); | ||
| query.addQueryItem("body", Encoded(IssueBody(environment, logLines))); | ||
|
|
||
| QUrl url{ newIssueUrl }; | ||
| url.setQuery(query); | ||
| return url; | ||
| } | ||
| } | ||
|
|
||
| IssueEnvironment CurrentIssueEnvironment(const IssueProduct& product, const QString& applicationVersion, const QString& logFilePath) | ||
| { | ||
| auto shownPath = QDir::toNativeSeparators(logFilePath); | ||
| const auto home = QDir::toNativeSeparators(QDir::homePath()); | ||
| if (!home.isEmpty() && shownPath.startsWith(home)) | ||
| shownPath.replace(0, home.size(), "~"); | ||
|
|
||
| return IssueEnvironment{ product, applicationVersion, QSysInfo::prettyProductName(), QSysInfo::currentCpuArchitecture(), QString::fromUtf8(qVersion()), shownPath }; | ||
| } | ||
|
|
||
| QUrl BuildIssueUrl(const QUrl& newIssueUrl, const IssueEnvironment& environment, const QStringList& recentLog, int maxUrlLength) | ||
| { | ||
| auto lines = recentLog; | ||
| auto url = IssueUrl(newIssueUrl, environment, lines); | ||
|
|
||
| while (!lines.isEmpty() && url.toEncoded().size() > maxUrlLength) | ||
| { | ||
| lines.removeFirst(); | ||
| url = IssueUrl(newIssueUrl, environment, lines); | ||
| } | ||
|
|
||
| return url; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MegaLinter] reported by reviewdog 🐶