1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-11-21 11:06:32 +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 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())
}

View file

@ -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<Mailer>,
}
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<u8>;
let stderr: Vec<u8>;
let status_code: Option<i32>;
let stdout: Vec<u8>;
let stderr: Vec<u8>;
let status_code: Option<i32>;
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:?}"),
};
}
}