// Copyright (C) 2024 Simon Quigley // // This program 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. // // This program 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 this program. If not, see . #include "build.h" #include "utils.h" #include "launchpad.h" #include #include #include build::build() : arch_tag(""), buildstate(""), can_be_cancelled(false), can_be_retried(false), title(""), self_link(""), id(-1), lp(nullptr) {} build::~build() {} std::optional build::parse(const std::string& json_data) { build b; b.parse_json(json_data); return b; } void build::parse_json(const std::string& json_data) { try { auto data = nlohmann::json::parse(json_data); // Map JSON keys to lambda functions std::map> json_map = { {"arch_tag", [this](const nlohmann::json& val) { arch_tag = val.get(); }}, {"buildstate", [this](const nlohmann::json& val) { buildstate = val.get(); }}, {"can_be_cancelled", [this](const nlohmann::json& val) { can_be_cancelled = val.get(); }}, {"can_be_retried", [this](const nlohmann::json& val) { can_be_retried = val.get(); }}, {"title", [this](const nlohmann::json& val) { title = val.get(); }}, {"self_link", [this](const nlohmann::json& val) { self_link = val.get(); std::regex rgx("/builds/(\\d+)"); std::smatch match; if (std::regex_search(self_link, match, rgx)) { id = std::stol(match[1]); } }}, {"build_log_url", [this](const nlohmann::json& val) { build_log_url = val.get(); }}, {"upload_log_url", [this](const nlohmann::json& val) { upload_log_url = val.get(); }}, {"duration", [this](const nlohmann::json& val) { duration = val.get(); }}, {"changesfile_url", [this](const nlohmann::json& val) { changesfile_url = val.get(); }}, {"buildinfo_url", [this](const nlohmann::json& val) { buildinfo_url = val.get(); }}, {"external_dependencies", [this](const nlohmann::json& val) { external_dependencies = val.get(); }}, {"pocket", [this](const nlohmann::json& val) { pocket = val.get(); }} }; // Process JSON keys for (auto& [key, value] : data.items()) { try { if (json_map.find(key) != json_map.end()) { json_map[key](value); json_map.erase(key); } } catch (...) { continue; } } } catch (...) { std::cerr << "Error parsing build JSON data." << std::endl; } } bool build::cancel() { if (self_link.empty()) return false; std::map params; params["ws.op"] = "cancel"; auto response = lp->api_post(self_link, params); return response.has_value(); } bool build::retry() { if (self_link.empty()) return false; std::map params; params["ws.op"] = "retry"; auto response = lp->api_post(self_link, params); return response.has_value(); } bool build::rescore(int score) { if (self_link.empty()) return false; std::map params; params["ws.op"] = "rescore"; params["score"] = score; auto response = lp->api_post(self_link, params); return response.has_value(); } void build::set_lp(launchpad* lp_ptr) { lp = lp_ptr; }