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.

165 lines
6.7 KiB

// 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 "source_package_publishing_history.h"
#include "utils.h"
#include "launchpad.h"
#include <date/date.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>(); }},
{"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>(); }},
{"date_first_dispatched", [this](const nlohmann::json& val) {
std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(val.get<std::string>());
ss >> date::parse("%FT%T%z", parsed_time);
if (ss.fail()) {
throw std::runtime_error("Failed to parse date_first_dispatched: " + val.get<std::string>());
}
date_first_dispatched = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}},
{"date_started", [this](const nlohmann::json& val) {
std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(val.get<std::string>());
ss >> date::parse("%FT%T%z", parsed_time);
if (ss.fail()) {
throw std::runtime_error("Failed to parse date_started: " + val.get<std::string>());
}
date_started = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}},
{"datebuilt", [this](const nlohmann::json& val) {
std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(val.get<std::string>());
ss >> date::parse("%FT%T%z", parsed_time);
if (ss.fail()) {
throw std::runtime_error("Failed to parse datebuilt: " + val.get<std::string>());
}
datebuilt = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}},
{"datecreated", [this](const nlohmann::json& val) {
std::chrono::sys_time<std::chrono::microseconds> parsed_time;
std::istringstream ss(val.get<std::string>());
ss >> date::parse("%FT%T%z", parsed_time);
if (ss.fail()) {
throw std::runtime_error("Failed to parse datecreated: " + val.get<std::string>());
}
datecreated = std::chrono::time_point_cast<std::chrono::system_clock::duration>(parsed_time);
}}
};
// 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<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();
}
void build::set_lp(launchpad* lp_ptr) {
lp = lp_ptr;
}
const std::optional<source_package_publishing_history> build::getCurrentSourcePublication() {
if (current_source_publication_link.empty()) return std::nullopt;
auto response = lp->api_get(current_source_publication_link);
if (!response) return std::nullopt;
auto data = nlohmann::json::parse(response.value());
auto sp = source_package_publishing_history::parse(data.dump());
if (sp) {
sp->set_lp(lp);
return sp.value();
}
return std::nullopt;
}