1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-11-24 11:21:36 +00:00

Use implementation instead of function

This commit is contained in:
Mo 2022-12-27 19:30:04 +01:00
parent 3baa503fbf
commit d53793e77b
2 changed files with 44 additions and 42 deletions

View file

@ -65,7 +65,7 @@ pub async fn trigger(
let hook_log_link = format!("{}/?id={}", state_config.base_url, hook_log_id); 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: hook.clone(),
hook_log_id, hook_log_id,
hook_log_link: hook_log_link.clone(), 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. // 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 // 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. // 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()) Ok(hook_log_link.into_response())
} }

View file

@ -3,7 +3,7 @@ use tracing::{error, info};
use crate::{config, db, mailer::Mailer, states}; use crate::{config, db, mailer::Mailer, states};
pub struct TaskContext { pub struct Task {
pub hook: config::Hook, pub hook: config::Hook,
pub hook_log_id: i32, pub hook_log_id: i32,
pub hook_log_link: String, pub hook_log_link: String,
@ -11,16 +11,17 @@ pub struct TaskContext {
pub mailer: Arc<Mailer>, pub mailer: Arc<Mailer>,
} }
pub async fn run(context: TaskContext) { impl Task {
info!("Running webhook for repo: {}", context.hook.clone_url); pub async fn run(self) {
info!("Running webhook for repo: {}", self.hook.clone_url);
let stdout: Vec<u8>; let stdout: Vec<u8>;
let stderr: Vec<u8>; let stderr: Vec<u8>;
let status_code: Option<i32>; let status_code: Option<i32>;
match Command::new(&context.hook.command) match Command::new(&self.hook.command)
.args(&context.hook.args) .args(&self.hook.args)
.current_dir(&context.hook.current_dir) .current_dir(&self.hook.current_dir)
.output() .output()
{ {
Ok(output) => { Ok(output) => {
@ -40,19 +41,20 @@ pub async fn run(context: TaskContext) {
let status = if status_code == Some(0) { "Ok" } else { "Err" }; let status = if status_code == Some(0) { "Ok" } else { "Err" };
db::fill_hook_log( db::fill_hook_log(
&context.db.pool, &self.db.pool,
context.hook_log_id, self.hook_log_id,
&stdout, &stdout,
&stderr, &stderr,
status_code, status_code,
); );
match context match self
.mailer .mailer
.send(&context.hook.name, &context.hook_log_link, status) .send(&self.hook.name, &self.hook_log_link, status)
.await .await
{ {
Ok(_) => info!("Sent email with hook name {}", context.hook.name), Ok(_) => info!("Sent email with hook name {}", self.hook.name),
Err(e) => error!("{e:?}"), Err(e) => error!("{e:?}"),
}; };
} }
}