1
0
Fork 0
mirror of https://codeberg.org/Mo8it/git-webhook-client synced 2024-10-18 07:22:39 +00:00

Replace all unwraps with expect

This commit is contained in:
Mo 2022-10-13 22:23:15 +02:00
parent 6c3157f049
commit 18e6a837f3
4 changed files with 48 additions and 17 deletions

View file

@ -21,9 +21,15 @@ pub struct Config {
impl Config { impl Config {
pub fn new() -> Self { pub fn new() -> Self {
let config_path = Path::new("config.json"); let config_path = Path::new("config.json");
let config_file = File::open(config_path).unwrap(); let config_file = File::open(config_path).expect(&format!(
"Can not open the config file at the path {}!",
config_path
.to_str()
.expect("Can not convert the config file path into a string")
));
let config_reader = BufReader::new(config_file); let config_reader = BufReader::new(config_file);
let config: Self = serde_json::from_reader(config_reader).unwrap(); let config: Self =
serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!");
config config
} }

View file

@ -1,6 +1,6 @@
use chrono::Local; use chrono::Local;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool}; use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
use std::env; use std::env;
use std::process::Output; use std::process::Output;
@ -20,8 +20,13 @@ pub fn establish_connection_pool() -> DBPool {
.expect("Could not build database connection pool!") .expect("Could not build database connection pool!")
} }
fn get_conn(pool: &DBPool) -> PooledConnection<ConnectionManager<SqliteConnection>> {
pool.get()
.expect("Can not get a connection from the database pool!")
}
pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> i32 { pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> i32 {
let conn = &mut pool.get().unwrap(); let conn = &mut get_conn(pool);
let command_with_args = hook.command.to_owned() + " " + &hook.args.join(" "); let command_with_args = hook.command.to_owned() + " " + &hook.args.join(" ");
@ -30,8 +35,8 @@ pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> i32 {
repo_url: &hook.repo_url, repo_url: &hook.repo_url,
command_with_args: &command_with_args, command_with_args: &command_with_args,
current_dir: &hook.current_dir, current_dir: &hook.current_dir,
stdout: std::str::from_utf8(&output.stdout).unwrap(), stdout: std::str::from_utf8(&output.stdout).expect("Can not convert stdout to str!"),
stderr: std::str::from_utf8(&output.stderr).unwrap(), stderr: std::str::from_utf8(&output.stderr).expect("Can not convert stderr to str!"),
status_code: output.status.code(), status_code: output.status.code(),
}; };
@ -44,15 +49,18 @@ pub fn add_hook_log(pool: &DBPool, hook: &Hook, output: &Output) -> i32 {
} }
pub fn get_hook_log(pool: &DBPool, id: i32) -> HookLog { pub fn get_hook_log(pool: &DBPool, id: i32) -> HookLog {
let conn = &mut pool.get().unwrap(); let conn = &mut get_conn(pool);
if id >= 0 { if id >= 0 {
hooklog::dsl::hooklog.find(id).first(conn).unwrap() hooklog::dsl::hooklog
.find(id)
.first(conn)
.expect("No hook log exists for this id!")
} else { } else {
hooklog::dsl::hooklog hooklog::dsl::hooklog
.order(hooklog::dsl::id.desc()) .order(hooklog::dsl::id.desc())
.offset((-id - 1).into()) .offset((-id - 1).into())
.first(conn) .first(conn)
.unwrap() .expect("No hook log exists for this negative id!")
} }
} }

View file

@ -35,7 +35,9 @@ impl<'r> FromData<'r> for Repo<'r> {
let mut received_signatures = req.headers().get("X-GITEA-SIGNATURE"); let mut received_signatures = req.headers().get("X-GITEA-SIGNATURE");
let received_signature = match received_signatures.next() { let received_signature = match received_signatures.next() {
Some(signature) => hex::decode(signature).unwrap(), Some(signature) => {
hex::decode(signature).expect("Can not hex decode the received signature!")
}
None => return Outcome::Failure((Status::BadRequest, Self::Error::MissingSignature)), None => return Outcome::Failure((Status::BadRequest, Self::Error::MissingSignature)),
}; };
@ -43,15 +45,26 @@ impl<'r> FromData<'r> for Repo<'r> {
return Outcome::Failure((Status::BadRequest, Self::Error::MoreThatOneSignature)); return Outcome::Failure((Status::BadRequest, Self::Error::MoreThatOneSignature));
} }
let config_state = req.rocket().state::<states::Config>().unwrap(); let config_state = req
.rocket()
.state::<states::Config>()
.expect("Can not get the config state!");
if !is_valid_signature(&config_state.secret, &received_signature, &payload) { if !is_valid_signature(&config_state.secret, &received_signature, &payload) {
return Outcome::Failure((Status::BadRequest, Self::Error::InvalidSignature)); return Outcome::Failure((Status::BadRequest, Self::Error::InvalidSignature));
} }
let json: Value = serde_json::from_slice(&payload).unwrap(); let json: Value =
let repo = json.get("repository").unwrap(); serde_json::from_slice(&payload).expect("Can not parse payload into JSON!");
let clone_url = repo.get("clone_url").unwrap().as_str().unwrap().to_string(); let repo = json
.get("repository")
.expect("Can not get the repository value from the payload!");
let clone_url = repo
.get("clone_url")
.expect("Can not get value clone_url from repository in the payload!")
.as_str()
.expect("The value of clone_url from repository in the payload is not a string!")
.to_string();
let clone_url = request::local_cache!(req, clone_url); let clone_url = request::local_cache!(req, clone_url);
@ -60,7 +73,8 @@ impl<'r> FromData<'r> for Repo<'r> {
} }
fn is_valid_signature(secret: &[u8], received_signature: &[u8], payload: &[u8]) -> bool { fn is_valid_signature(secret: &[u8], received_signature: &[u8], payload: &[u8]) -> bool {
let mut mac = Hmac::<Sha256>::new_from_slice(secret).unwrap(); let mut mac =
Hmac::<Sha256>::new_from_slice(secret).expect("Can not generate a mac from the secret!");
mac.update(payload); mac.update(payload);
let expected_signature = mac.finalize().into_bytes(); let expected_signature = mac.finalize().into_bytes();

View file

@ -59,13 +59,16 @@ pub fn trigger(
config_state: &State<states::Config>, config_state: &State<states::Config>,
repo: guards::Repo, repo: guards::Repo,
) -> String { ) -> String {
let hook = config_state.get_hook(repo.clone_url).unwrap(); let hook = config_state.get_hook(repo.clone_url).expect(&format!(
"No matching repository with url {} in the configuration file.",
repo.clone_url
));
let output = Command::new(&hook.command) let output = Command::new(&hook.command)
.args(&hook.args) .args(&hook.args)
.current_dir(&hook.current_dir) .current_dir(&hook.current_dir)
.output() .output()
.unwrap(); .expect("Can not run the hook command!");
let new_hook_log_id = db::add_hook_log(&db_state.pool, hook, &output); let new_hook_log_id = db::add_hook_log(&db_state.pool, hook, &output);