Fix token handling bug

Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2023-12-30 21:10:58 +00:00
parent adf3e84355
commit 62801f1bd9
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
1 changed files with 46 additions and 20 deletions

View File

@ -8,11 +8,11 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc != 2) { if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <config_file>" << std::endl; std::cerr << "Usage: " << 0[argv] << " <config_file>" << std::endl;
return 1; return 1;
} }
std::ifstream config_file(argv[1]); std::ifstream config_file(1[argv]);
if (!config_file.is_open()) { if (!config_file.is_open()) {
std::cerr << "Failed to open config.json" << std::endl; std::cerr << "Failed to open config.json" << std::endl;
return 1; return 1;
@ -31,34 +31,50 @@ int main(int argc, char **argv) {
nlohmann::json &tokens = config["tokens"]; nlohmann::json &tokens = config["tokens"];
crow::SimpleApp app; crow::SimpleApp app;
CROW_ROUTE(app, "/github-webhook") CROW_ROUTE(app, "/update-files")
.methods("POST"_method) .methods("POST"_method)
([&repos, &tokens](const crow::request &req) { ([&repos, &tokens](const crow::request &req) {
nlohmann::json payload;
try { try {
// Parse JSON payload from the request body // Parse JSON payload from the request body
nlohmann::json payload = nlohmann::json::parse(req.body); payload = nlohmann::json::parse(req.body);
nlohmann::json response; } catch (const std::exception &e) {
std::cerr << "Error processing webhook: " << e.what() << std::endl;
nlohmann::json response = {
{"status", 400},
{"error", "Invalid JSON payload"}
};
return crow::response(400, response.dump());
}
try {
std::string ref = payload["ref"]; std::string ref = payload["ref"];
size_t last_slash = ref.find_last_of('/'); size_t last_slash = ref.find_last_of('/');
if (last_slash != std::string::npos && last_slash + 1 < ref.length()) if (last_slash != std::string::npos && last_slash + 1 < ref.length())
ref = ref.substr(last_slash + 1); ref = ref.substr(last_slash + 1);
std::string repo = payload["repository"]["full_name"]; std::string repo = payload["repository"]["full_name"];
bool is_private = payload["repository"]["private"]; bool is_private = payload["repository"]["private"];
if (is_private && tokens.find(repo) == tokens.end()) { std::string token;
if (is_private) {
if (tokens.find(repo) == tokens.end()) {
printf("No token configured for private repo %s\n", repo.c_str()); printf("No token configured for private repo %s\n", repo.c_str());
response["status"] = 403; nlohmann::json response = {
response["error"] = "No token configured for private repo"; {"status", 403},
{"error", "No token configured for private repo"}
};
return crow::response(403, response.dump()); return crow::response(403, response.dump());
} }
std::string token = tokens[repo]; token = tokens[repo];
}
printf("Received push to %s:%s (private: %s)\n", repo.c_str(), ref.c_str(), is_private ? "true" : "false"); printf("Received push to %s:%s (private: %s)\n", repo.c_str(), ref.c_str(), is_private ? "true" : "false");
if (repos.find(repo) == repos.end()) { if (repos.find(repo) == repos.end()) {
printf("No webhook configured for %s\n", repo.c_str()); printf("No webhook configured for %s\n", repo.c_str());
response["status"] = 404; nlohmann::json response = {
response["error"] = "No webhook configured for repo"; {"status", 404},
{"error", "No webhook configured for repo"}
};
return crow::response(404, response.dump()); return crow::response(404, response.dump());
} }
@ -74,33 +90,43 @@ int main(int argc, char **argv) {
if (!is_valid_branch) { if (!is_valid_branch) {
printf("No webhook configured for %s:%s\n", repo.c_str(), ref.c_str()); printf("No webhook configured for %s:%s\n", repo.c_str(), ref.c_str());
response["status"] = 404; nlohmann::json response = {
response["error"] = "No webhook configured for branch" + ref; {"status", 404},
{"error", "No webhook configured for branch" + ref}
};
return crow::response(404, response.dump()); return crow::response(404, response.dump());
} }
if (repo_data["files"].empty()) { if (repo_data["files"].empty()) {
printf("No files configured for %s:%s\n", repo.c_str(), ref.c_str()); printf("No files configured for %s:%s\n", repo.c_str(), ref.c_str());
response["status"] = 404; nlohmann::json response = {
response["error"] = "No files configured for branch " + ref; {"status", 404},
{"error", "No files configured for branch" + ref}
};
return crow::response(404, response.dump()); return crow::response(404, response.dump());
} }
response["updated"] = nlohmann::json::array(); nlohmann::json response = {
{"status", 200},
{"file_count", 0},
{"updated", nlohmann::json::array()}
};
for (auto &commit : payload["commits"]) { for (auto &commit : payload["commits"]) {
for (auto &file : commit["modified"]) { for (auto &file : commit["modified"]) {
std::string file_path = file; std::string file_path = file;
if (repos[repo]["files"].find(file_path) == repos[repo]["files"].end()) continue; if (repos[repo]["files"].find(file_path) == repos[repo]["files"].end()) continue;
std::string path = repos[repo]["files"][file_path]; std::string path = repos[repo]["files"][file_path];
std::string create_dir = "mkdir -p $(dirname " + path + ")"; std::string create_dir = "mkdir -p $(dirname " + path + ")";
std::string command = "curl -s https://raw.githubusercontent.com/" + repo + "/" + ref + "/" + file_path + " -H 'Authorization: token " + token + "' -o " + path; std::string command = "curl -s https://raw.githubusercontent.com/" + repo + "/" + ref + "/" + file_path + " -o " + path;
if (is_private)
command += " -H 'Authorization: token " + token + "'";
std::system(create_dir.c_str()); std::system(create_dir.c_str());
std::system(command.c_str()); std::system(command.c_str());
printf("Updated %s\n", path.c_str()); printf("Updated %s\n", path.c_str());
response["file_count"] = response["file_count"].get<int>() + 1;
response["updated"].push_back(file_path); response["updated"].push_back(file_path);
} }
} }
response["status"] = 200;
return crow::response(200, response.dump()); return crow::response(200, response.dump());
} catch (const std::exception &e) { } catch (const std::exception &e) {