Add support for actions
- Repository refactor Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
1f9e0d863e
commit
9dd35b2ec0
|
@ -1,6 +1,2 @@
|
||||||
.cmake/
|
|
||||||
CMakeFiles/
|
|
||||||
CMakeCache.txt
|
|
||||||
Makefile
|
|
||||||
cmake_install.cmake
|
|
||||||
bin/
|
bin/
|
||||||
|
config.json
|
||||||
|
|
|
@ -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()
|
|
|
@ -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
|
||||||
|
|
|
@ -13,7 +13,6 @@ Install the dependencies:
|
||||||
|
|
||||||
Compile the application:
|
Compile the application:
|
||||||
```console
|
```console
|
||||||
cmake .
|
|
||||||
make
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -22,6 +21,8 @@ Run the application using your configuration file:
|
||||||
./gh_wh_handler /path/to/config.json
|
./gh_wh_handler /path/to/config.json
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note: default config file path is `/etc/gh_wh_handler/config.json`
|
||||||
|
|
||||||
## Config File
|
## Config File
|
||||||
|
|
||||||
The configuration file should be a JSON file with the following format:
|
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": {
|
"tokens": {
|
||||||
"repo_full_name": "github_token"
|
"repo_full_name": "github_token"
|
||||||
|
},
|
||||||
|
"actions": {
|
||||||
|
"repo_full_name": [
|
||||||
|
"command1",
|
||||||
|
"command2"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -40,6 +40,7 @@ int main(int argc, char **argv) {
|
||||||
int port = config["port"];
|
int port = config["port"];
|
||||||
nlohmann::json &repos = config["repos"];
|
nlohmann::json &repos = config["repos"];
|
||||||
nlohmann::json &tokens = config["tokens"];
|
nlohmann::json &tokens = config["tokens"];
|
||||||
|
nlohmann::json &actions = config["actions"];
|
||||||
|
|
||||||
// Close config file
|
// Close config file
|
||||||
config_file.close();
|
config_file.close();
|
||||||
|
@ -50,7 +51,7 @@ int main(int argc, char **argv) {
|
||||||
// Set up route for updating files
|
// Set up route for updating files
|
||||||
CROW_ROUTE(app, "/update-files")
|
CROW_ROUTE(app, "/update-files")
|
||||||
.methods("POST"_method)
|
.methods("POST"_method)
|
||||||
([&repos, &tokens](const crow::request &req) {
|
([&repos, &tokens, &actions](const crow::request &req) {
|
||||||
nlohmann::json payload;
|
nlohmann::json payload;
|
||||||
try {
|
try {
|
||||||
// Parse JSON payload from the request body, exit if it fails
|
// 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);
|
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());
|
return crow::response(200, response.dump());
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
std::cerr << "Error processing webhook: " << e.what() << std::endl;
|
std::cerr << "Error processing webhook: " << e.what() << std::endl;
|
Loading…
Reference in New Issue