Some minor changes
- Now creating a static binary Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
4b9da303f5
commit
37c3cb53e4
|
@ -1,6 +1,12 @@
|
|||
cmake_minimum_required(VERSION 3.27)
|
||||
project(gh-wh-handler)
|
||||
project(gh_wh_handler)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
add_executable(gh-wh-handler main.cpp)
|
||||
add_executable(gh_wh_handler main.cpp)
|
||||
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
|
||||
|
||||
if (CMAKE_COMPILER_IS_GNUCXX)
|
||||
target_compile_options(gh_wh_handler PRIVATE -Wall -Wextra -pedantic)
|
||||
endif()
|
||||
|
|
|
@ -19,7 +19,7 @@ make
|
|||
|
||||
Run the application using your configuration file:
|
||||
```console
|
||||
./gh-wh-handler /path/to/config.json
|
||||
./gh_wh_handler /path/to/config.json
|
||||
```
|
||||
|
||||
## Config File
|
||||
|
|
30
main.cpp
30
main.cpp
|
@ -6,18 +6,20 @@
|
|||
#include <nlohmann/json.hpp>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
// Check for config file argument, exit if it's not there
|
||||
if (argc != 2) {
|
||||
std::cerr << "Usage: " << 0[argv] << " <config_file>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Open config file, exit if it fails
|
||||
std::ifstream config_file(1[argv]);
|
||||
if (!config_file.is_open()) {
|
||||
std::cerr << "Failed to open config.json" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Parse config file, exit if it fails
|
||||
nlohmann::json config;
|
||||
try {
|
||||
config_file >> config;
|
||||
|
@ -26,17 +28,24 @@ int main(int argc, char **argv) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
// Set up variables from config file
|
||||
int port = config["port"];
|
||||
nlohmann::json &repos = config["repos"];
|
||||
nlohmann::json &tokens = config["tokens"];
|
||||
|
||||
// Close config file
|
||||
config_file.close();
|
||||
|
||||
// Set up web server
|
||||
crow::SimpleApp app;
|
||||
|
||||
// Set up route for updating files
|
||||
CROW_ROUTE(app, "/update-files")
|
||||
.methods("POST"_method)
|
||||
([&repos, &tokens](const crow::request &req) {
|
||||
nlohmann::json payload;
|
||||
try {
|
||||
// Parse JSON payload from the request body
|
||||
// Parse JSON payload from the request body, exit if it fails
|
||||
payload = nlohmann::json::parse(req.body);
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Error processing webhook: " << e.what() << std::endl;
|
||||
|
@ -48,13 +57,14 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
|
||||
try {
|
||||
// Parse the payload
|
||||
std::string ref = payload["ref"];
|
||||
size_t last_slash = ref.find_last_of('/');
|
||||
if (last_slash != std::string::npos && last_slash + 1 < ref.length())
|
||||
ref = ref.substr(last_slash + 1);
|
||||
std::string repo = payload["repository"]["full_name"];
|
||||
bool is_private = payload["repository"]["private"];
|
||||
std::string token;
|
||||
std::string token = std::string();
|
||||
if (is_private) {
|
||||
if (tokens.find(repo) == tokens.end()) {
|
||||
printf("No token configured for private repo %s\n", repo.c_str());
|
||||
|
@ -69,6 +79,7 @@ int main(int argc, char **argv) {
|
|||
|
||||
printf("Received push to %s:%s (private: %s)\n", repo.c_str(), ref.c_str(), is_private ? "true" : "false");
|
||||
|
||||
// Check if the repo is configured
|
||||
if (repos.find(repo) == repos.end()) {
|
||||
printf("No webhook configured for %s\n", repo.c_str());
|
||||
nlohmann::json response = {
|
||||
|
@ -78,6 +89,7 @@ int main(int argc, char **argv) {
|
|||
return crow::response(404, response.dump());
|
||||
}
|
||||
|
||||
// Check if the branch is configured
|
||||
nlohmann::json repo_data;
|
||||
bool is_valid_branch = false;
|
||||
for (auto c_repo = repos.begin(); c_repo != repos.end(); ++c_repo) {
|
||||
|
@ -97,6 +109,7 @@ int main(int argc, char **argv) {
|
|||
return crow::response(404, response.dump());
|
||||
}
|
||||
|
||||
// Check if any files are configured
|
||||
if (repo_data["files"].empty()) {
|
||||
printf("No files configured for %s:%s\n", repo.c_str(), ref.c_str());
|
||||
nlohmann::json response = {
|
||||
|
@ -106,6 +119,7 @@ int main(int argc, char **argv) {
|
|||
return crow::response(404, response.dump());
|
||||
}
|
||||
|
||||
// Download files
|
||||
nlohmann::json response = {
|
||||
{"status", 200},
|
||||
{"file_count", 0},
|
||||
|
@ -115,12 +129,18 @@ int main(int argc, char **argv) {
|
|||
for (auto &file : commit["modified"]) {
|
||||
std::string file_path = file;
|
||||
if (repos[repo]["files"].find(file_path) == repos[repo]["files"].end()) continue;
|
||||
|
||||
std::string path = repos[repo]["files"][file_path];
|
||||
std::string create_dir = "mkdir -p $(dirname " + path + ")";
|
||||
try {
|
||||
std::filesystem::create_directories(path.substr(0, path.find_last_of('/')));
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Failed to create directories for " << path << ": " << e.what() << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
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(command.c_str());
|
||||
printf("Updated %s\n", path.c_str());
|
||||
response["file_count"] = response["file_count"].get<int>() + 1;
|
||||
|
|
Loading…
Reference in New Issue