From a6ac59de826d5f65d0d0053516608c537c521e35 Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Wed, 21 Feb 2024 12:09:59 +0000 Subject: [PATCH] Add support for actions - Repository refactor Signed-off-by: TiagoRG --- .gitignore | 8 ++------ CMakeLists.txt | 12 ------------ Makefile | 7 +++++++ README.md | 9 ++++++++- main.cpp => src/main.cpp | 22 +++++++++++++++++++++- 5 files changed, 38 insertions(+), 20 deletions(-) delete mode 100644 CMakeLists.txt create mode 100644 Makefile rename main.cpp => src/main.cpp (86%) diff --git a/.gitignore b/.gitignore index 794b0b1..2632c7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,2 @@ -.cmake/ -CMakeFiles/ -CMakeCache.txt -Makefile -cmake_install.cmake -bin/ \ No newline at end of file +bin/ +config.json diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 2af714c..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -cmake_minimum_required(VERSION 3.27) -project(gh_wh_handler) - -set(CMAKE_CXX_STANDARD 23) - -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() diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..034f017 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: static + +static: + @mkdir -p bin || exit + @echo "Building static binary..." + @g++ -Wall -Werror -static -static-libgcc -static-libstdc++ -std=c++23 src/*.cpp -o bin/gh_wh_handler || exit + diff --git a/README.md b/README.md index 7f10ae3..9876211 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ Install the dependencies: Compile the application: ```console -cmake . make ``` @@ -22,6 +21,8 @@ Run the application using your configuration file: ./gh_wh_handler /path/to/config.json ``` +Note: default config file path is `/etc/gh_wh_handler/config.json` + ## Config File The configuration file should be a JSON file with the following format: @@ -39,6 +40,12 @@ The configuration file should be a JSON file with the following format: }, "tokens": { "repo_full_name": "github_token" + }, + "actions": { + "repo_full_name": [ + "command1", + "command2" + ] } } ``` diff --git a/main.cpp b/src/main.cpp similarity index 86% rename from main.cpp rename to src/main.cpp index 6501c08..ffa6c29 100644 --- a/main.cpp +++ b/src/main.cpp @@ -40,6 +40,7 @@ int main(int argc, char **argv) { int port = config["port"]; nlohmann::json &repos = config["repos"]; nlohmann::json &tokens = config["tokens"]; + nlohmann::json &actions = config["actions"]; // Close config file config_file.close(); @@ -50,7 +51,7 @@ int main(int argc, char **argv) { // Set up route for updating files CROW_ROUTE(app, "/update-files") .methods("POST"_method) - ([&repos, &tokens](const crow::request &req) { + ([&repos, &tokens, &actions](const crow::request &req) { nlohmann::json payload; try { // Parse JSON payload from the request body, exit if it fails @@ -180,6 +181,25 @@ int main(int argc, char **argv) { response["updated"].push_back(file_path); } + // Run actions + for (auto c_action_list = actions.begin(); c_action_list != actions.end(); ++c_action_list) { + if (const std::string &c_action_repo = c_action_list.key(); c_action_repo != repo) continue; + for (auto &action : c_action_list.value()) { + std::cout << "Executing action: " << action << std::endl; + std::string command = action; + int return_code = std::system(command.c_str()); + std::ofstream log_file("/var/log/gh_wh_handler.log", std::ios_base::app); + time_t now = time(0); + if (return_code == 0) { + printf("Successfully executed action: %s\n", command.c_str()); + log_file << now << " > Successfully executed action: " << command << std::endl; + } else { + printf("Failed to execute action: %s\n", command.c_str()); + log_file << now << " > Failed to execute action: " << command << std::endl; + } + } + } + return crow::response(200, response.dump()); } catch (const std::exception &e) { std::cerr << "Error processing webhook: " << e.what() << std::endl;