You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.2 KiB

1 month ago
// Copyright (C) 2024 Simon Quigley <tsimonq2@ubuntu.com>
//
// 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 <https://www.gnu.org/licenses/>.
#include "build.h"
#include "utils.h"
#include "launchpad.h"
#include <iostream>
#include <nlohmann/json.hpp>
#include <regex>
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> 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<std::string, std::function<void(const nlohmann::json&)>> json_map = {
{"arch_tag", [this](const nlohmann::json& val) { arch_tag = val.get<std::string>(); }},
{"buildstate", [this](const nlohmann::json& val) { buildstate = val.get<std::string>(); }},
{"can_be_cancelled", [this](const nlohmann::json& val) { can_be_cancelled = val.get<bool>(); }},
{"can_be_retried", [this](const nlohmann::json& val) { can_be_retried = val.get<bool>(); }},
{"title", [this](const nlohmann::json& val) { title = val.get<std::string>(); }},
{"self_link", [this](const nlohmann::json& val) {
self_link = val.get<std::string>();
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<std::string>(); }},
{"upload_log_url", [this](const nlohmann::json& val) { upload_log_url = val.get<std::string>(); }},
{"duration", [this](const nlohmann::json& val) { duration = val.get<std::string>(); }},
{"changesfile_url", [this](const nlohmann::json& val) { changesfile_url = val.get<std::string>(); }},
{"buildinfo_url", [this](const nlohmann::json& val) { buildinfo_url = val.get<std::string>(); }},
{"external_dependencies", [this](const nlohmann::json& val) { external_dependencies = val.get<std::string>(); }},
{"pocket", [this](const nlohmann::json& val) { pocket = val.get<std::string>(); }}
};
// 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;
1 month ago
}
}
} catch (...) {
std::cerr << "Error parsing build JSON data." << std::endl;
}
}
bool build::cancel() {
if (self_link.empty()) return false;
std::map<std::string, std::string> 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<std::string, std::string> 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<std::string, std::string> params;
params["ws.op"] = "rescore";
params["score"] = score;
auto response = lp->api_post(self_link, params);
return response.has_value();
}
1 month ago
void build::set_lp(launchpad* lp_ptr) {
lp = lp_ptr;
}