diff --git a/src/routes.rs b/src/routes.rs index e9fdf01..6381393 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -65,7 +65,7 @@ pub async fn trigger( let hook_log_link = format!("{}/?id={}", state_config.base_url, hook_log_id); - let webhook_task_context = webhook::TaskContext { + let webhook_task = webhook::Task { hook: hook.clone(), hook_log_id, hook_log_link: hook_log_link.clone(), @@ -76,7 +76,7 @@ pub async fn trigger( // Spawn and detach a task that runs the command and fills the output in the log. // This is useful to give a quick response to the git server in case that the command has a long // execution time. It prevents reaching the webhook timeout of the git server. - task::spawn(async move { webhook::run(webhook_task_context).await }); + task::spawn(async move { webhook_task.run().await }); Ok(hook_log_link.into_response()) } diff --git a/src/webhook.rs b/src/webhook.rs index df41314..d1c8a8f 100644 --- a/src/webhook.rs +++ b/src/webhook.rs @@ -3,7 +3,7 @@ use tracing::{error, info}; use crate::{config, db, mailer::Mailer, states}; -pub struct TaskContext { +pub struct Task { pub hook: config::Hook, pub hook_log_id: i32, pub hook_log_link: String, @@ -11,48 +11,50 @@ pub struct TaskContext { pub mailer: Arc, } -pub async fn run(context: TaskContext) { - info!("Running webhook for repo: {}", context.hook.clone_url); +impl Task { + pub async fn run(self) { + info!("Running webhook for repo: {}", self.hook.clone_url); - let stdout: Vec; - let stderr: Vec; - let status_code: Option; + let stdout: Vec; + let stderr: Vec; + let status_code: Option; - match Command::new(&context.hook.command) - .args(&context.hook.args) - .current_dir(&context.hook.current_dir) - .output() - { - Ok(output) => { - stdout = output.stdout; - stderr = output.stderr; - status_code = output.status.code(); - } - Err(e) => { - stdout = Vec::new(); - stderr = format!("Error while running the hook command: {e}") - .as_bytes() - .to_vec(); - status_code = Some(1); - } - }; + match Command::new(&self.hook.command) + .args(&self.hook.args) + .current_dir(&self.hook.current_dir) + .output() + { + Ok(output) => { + stdout = output.stdout; + stderr = output.stderr; + status_code = output.status.code(); + } + Err(e) => { + stdout = Vec::new(); + stderr = format!("Error while running the hook command: {e}") + .as_bytes() + .to_vec(); + status_code = Some(1); + } + }; - let status = if status_code == Some(0) { "Ok" } else { "Err" }; + let status = if status_code == Some(0) { "Ok" } else { "Err" }; - db::fill_hook_log( - &context.db.pool, - context.hook_log_id, - &stdout, - &stderr, - status_code, - ); + db::fill_hook_log( + &self.db.pool, + self.hook_log_id, + &stdout, + &stderr, + status_code, + ); - match context - .mailer - .send(&context.hook.name, &context.hook_log_link, status) - .await - { - Ok(_) => info!("Sent email with hook name {}", context.hook.name), - Err(e) => error!("{e:?}"), - }; + match self + .mailer + .send(&self.hook.name, &self.hook_log_link, status) + .await + { + Ok(_) => info!("Sent email with hook name {}", self.hook.name), + Err(e) => error!("{e:?}"), + }; + } }