mirror of
https://codeberg.org/Mo8it/git-webhook-client
synced 2024-11-21 11:06:32 +00:00
Replace all unwraps with expect
This commit is contained in:
parent
6c3157f049
commit
18e6a837f3
4 changed files with 48 additions and 17 deletions
|
@ -21,9 +21,15 @@ pub struct Config {
|
|||
impl Config {
|
||||
pub fn new() -> Self {
|
||||
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: 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
|
||||
}
|
||||
|
|
22
src/db.rs
22
src/db.rs
|
@ -1,6 +1,6 @@
|
|||
use chrono::Local;
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||
use std::env;
|
||||
use std::process::Output;
|
||||
|
||||
|
@ -20,8 +20,13 @@ pub fn establish_connection_pool() -> DBPool {
|
|||
.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 {
|
||||
let conn = &mut pool.get().unwrap();
|
||||
let conn = &mut get_conn(pool);
|
||||
|
||||
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,
|
||||
command_with_args: &command_with_args,
|
||||
current_dir: &hook.current_dir,
|
||||
stdout: std::str::from_utf8(&output.stdout).unwrap(),
|
||||
stderr: std::str::from_utf8(&output.stderr).unwrap(),
|
||||
stdout: std::str::from_utf8(&output.stdout).expect("Can not convert stdout to str!"),
|
||||
stderr: std::str::from_utf8(&output.stderr).expect("Can not convert stderr to str!"),
|
||||
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 {
|
||||
let conn = &mut pool.get().unwrap();
|
||||
let conn = &mut get_conn(pool);
|
||||
|
||||
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 {
|
||||
hooklog::dsl::hooklog
|
||||
.order(hooklog::dsl::id.desc())
|
||||
.offset((-id - 1).into())
|
||||
.first(conn)
|
||||
.unwrap()
|
||||
.expect("No hook log exists for this negative id!")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,9 @@ impl<'r> FromData<'r> for Repo<'r> {
|
|||
|
||||
let mut received_signatures = req.headers().get("X-GITEA-SIGNATURE");
|
||||
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)),
|
||||
};
|
||||
|
||||
|
@ -43,15 +45,26 @@ impl<'r> FromData<'r> for Repo<'r> {
|
|||
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) {
|
||||
return Outcome::Failure((Status::BadRequest, Self::Error::InvalidSignature));
|
||||
}
|
||||
|
||||
let json: Value = serde_json::from_slice(&payload).unwrap();
|
||||
let repo = json.get("repository").unwrap();
|
||||
let clone_url = repo.get("clone_url").unwrap().as_str().unwrap().to_string();
|
||||
let json: Value =
|
||||
serde_json::from_slice(&payload).expect("Can not parse payload into JSON!");
|
||||
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);
|
||||
|
||||
|
@ -60,7 +73,8 @@ impl<'r> FromData<'r> for Repo<'r> {
|
|||
}
|
||||
|
||||
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);
|
||||
let expected_signature = mac.finalize().into_bytes();
|
||||
|
||||
|
|
|
@ -59,13 +59,16 @@ pub fn trigger(
|
|||
config_state: &State<states::Config>,
|
||||
repo: guards::Repo,
|
||||
) -> 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)
|
||||
.args(&hook.args)
|
||||
.current_dir(&hook.current_dir)
|
||||
.output()
|
||||
.unwrap();
|
||||
.expect("Can not run the hook command!");
|
||||
|
||||
let new_hook_log_id = db::add_hook_log(&db_state.pool, hook, &output);
|
||||
|
||||
|
|
Loading…
Reference in a new issue