/** * Copyright (C) 2021-2024 Saturneric * * This file is part of GpgFrontend. * * GpgFrontend 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. * * GpgFrontend 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 GpgFrontend. If not, see . * * The initial version of the source code is inherited from * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by * Saturneric starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * */ #include "GitHubVersionCheckTask.h" #include #include #include #include #include "GFModule.h" #include "SoftwareVersion.h" #include "Utils.h" GitHubVersionCheckTask::GitHubVersionCheckTask() : network_manager_(new QNetworkAccessManager(this)), current_version_(GFAppVersion(GFModuleSdkContext())) { qRegisterMetaType("SoftwareVersion"); meta_.api = "GitHub"; meta_.current_version = current_version_; meta_.local_commit_hash = GFAppGitCommitHash(GFModuleSdkContext()); connect(this, &GitHubVersionCheckTask::SignalUpgradeVersion, this, [](const SoftwareVersion& sv) { gf::sdk::SetCacheText( GFModuleSdkContext(), GF_STORE_DURABLE, "update_checking_cache", (QJsonDocument(sv.ToJson()).toJson()).constData()); }); } auto GitHubVersionCheckTask::Run() -> int { QString base_url = "https://api.github.com/repos/saturneric/gpgfrontend"; QList urls = { // fetch the release list (not /releases/latest): with parallel stable and // mainline tracks we must pick the newest release within the running // build's own series, which a single global "latest" cannot express. {base_url + "/releases?per_page=100"}, {base_url + "/releases/tags/" + current_version_}, {base_url + "/commits/" + meta_.local_commit_hash}, }; connect(network_manager_, &QNetworkAccessManager::finished, this, &GitHubVersionCheckTask::slot_parse_reply); int index = 0; for (const QUrl& url : urls) { QNetworkRequest request(url); request.setHeader(QNetworkRequest::UserAgentHeader, GFAppUserAgent(GFModuleSdkContext())); QNetworkReply* reply = network_manager_->get(request); reply->setProperty("GFCheckIndex", index++); replies_.append(reply); } return 0; } void GitHubVersionCheckTask::slot_parse_reply(QNetworkReply* reply) { if (reply->error() == QNetworkReply::NoError) { auto index = reply->property("GFCheckIndex").toInt(); FLOG_DEBUG("get reply from url: %1, index: %2", reply->url().toString(), index); switch (index) { case 0: slot_parse_latest_version_info(reply); break; case 1: slot_parse_current_version_info(reply); break; case 2: slot_parse_current_commit_info(reply); break; default: break; } } else { FLOG_DEBUG("get reply from url: %1, error: %2 %3", reply->url().toString(), reply->errorString(), reply->readAll()); } replies_.removeAll(reply); reply->deleteLater(); if (replies_.isEmpty()) { meta_.timestamp = QDateTime::currentDateTime(); FillGrtWithVersionInfo(meta_); emit SignalUpgradeVersion(meta_); } } void GitHubVersionCheckTask::slot_parse_latest_version_info( QNetworkReply* reply) { if (reply == nullptr || reply->error() != QNetworkReply::NoError) { return; } auto reply_bytes = reply->readAll(); auto latest_reply_json = QJsonDocument::fromJson(reply_bytes); if (!latest_reply_json.isArray()) { FLOG_WARN("cannot parse data from github: %1", reply_bytes); return; } // pick the newest release that belongs to the same series as the running // build, so a stable user is never offered a mainline release and vice versa. for (const auto& value : latest_reply_json.array()) { if (!value.isObject()) continue; auto release = value.toObject(); if (release["draft"].toBool()) continue; auto raw_tag = release["tag_name"].toString(); auto version = ExtractVersionFromRawTag(raw_tag); if (version.isEmpty()) { FLOG_WARN("raw tag name from github: %1 cannot match regex rules", raw_tag); continue; } if (!SoftwareVersion::SameSeries(version, current_version_)) continue; if (!meta_.latest_version.isEmpty() && GFCompareSoftwareVersion(version.toUtf8().constData(), meta_.latest_version.toUtf8().constData()) <= 0) { continue; } meta_.latest_version = version; meta_.publish_date = release["published_at"].toString(); meta_.release_note = release["body"].toString(); } if (meta_.latest_version.isEmpty()) { FLOG_WARN("no github release found in series of current version: %1", current_version_); } } void GitHubVersionCheckTask::slot_parse_current_version_info( QNetworkReply* reply) { if (reply == nullptr || reply->error() != QNetworkReply::NoError) return; auto reply_bytes = reply->readAll(); auto current_reply_json = QJsonDocument::fromJson(reply_bytes); if (!current_reply_json.isObject()) { FLOG_WARN("cannot parse data from github: %1", reply_bytes); return; } meta_.current_version_publish_in_remote = true; } void GitHubVersionCheckTask::slot_parse_current_commit_info( QNetworkReply* reply) { if (reply == nullptr || reply->error() != QNetworkReply::NoError) { return; } auto reply_bytes = reply->readAll(); auto current_reply_json = QJsonDocument::fromJson(reply_bytes); if (!current_reply_json.isObject()) { FLOG_WARN("cannot parse data from github: %1", reply_bytes); return; } auto sha = current_reply_json["sha"].toString(); meta_.current_commit_hash_publish_in_remote = sha == meta_.local_commit_hash; }