Use ForgejoHook::try_from_event

Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
pep 2025-05-04 00:25:16 +02:00
commit b97b5373ba
No known key found for this signature in database
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 19 additions and 3 deletions

View file

@ -18,6 +18,7 @@ use std::error::Error as StdError;
use std::io::Error as IoError; use std::io::Error as IoError;
use std::str::Utf8Error; use std::str::Utf8Error;
use forgejo_hooks::Error as FjError;
use hex::FromHexError; use hex::FromHexError;
use hmac::digest::InvalidLength as HmacInvalidLength; use hmac::digest::InvalidLength as HmacInvalidLength;
use hyper::StatusCode; use hyper::StatusCode;
@ -31,6 +32,7 @@ pub enum Error {
InvalidSignature, InvalidSignature,
InvalidRequest, InvalidRequest,
UnsupportedHookConversion, UnsupportedHookConversion,
ForgejoHooks(FjError),
Hex(FromHexError), Hex(FromHexError),
Hmac(HmacInvalidLength), Hmac(HmacInvalidLength),
Hyper(hyper::Error), Hyper(hyper::Error),
@ -68,6 +70,7 @@ impl std::fmt::Display for Error {
Error::InvalidSignature => write!(fmt, "the signature is invalid"), Error::InvalidSignature => write!(fmt, "the signature is invalid"),
Error::InvalidRequest => write!(fmt, "the request is invalid"), Error::InvalidRequest => write!(fmt, "the request is invalid"),
Error::UnsupportedHookConversion => write!(fmt, "Unable to convert hook"), Error::UnsupportedHookConversion => write!(fmt, "Unable to convert hook"),
Error::ForgejoHooks(e) => write!(fmt, "forgejo-hooks error: {}", e),
Error::Hex(e) => write!(fmt, "hex error: {}", e), Error::Hex(e) => write!(fmt, "hex error: {}", e),
Error::Hmac(e) => write!(fmt, "hmac error: {}", e), Error::Hmac(e) => write!(fmt, "hmac error: {}", e),
Error::Hyper(e) => write!(fmt, "hyper error: {}", e), Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
@ -81,6 +84,12 @@ impl std::fmt::Display for Error {
} }
} }
impl From<FjError> for Error {
fn from(err: FjError) -> Error {
Error::ForgejoHooks(err)
}
}
impl From<FromHexError> for Error { impl From<FromHexError> for Error {
fn from(err: FromHexError) -> Error { fn from(err: FromHexError) -> Error {
Error::Hex(err) Error::Hex(err)

View file

@ -89,9 +89,16 @@ async fn hooks_inner(req: Request<Incoming>, secret: &str) -> Result<Hook, Error
} }
trace!("Payload: {:?}", std::str::from_utf8(&payload)); trace!("Payload: {:?}", std::str::from_utf8(&payload));
let hook: ForgejoHook = serde_json::from_slice(&payload[..])?; trace!("X-Forgejo-Event: {:?}", headers.get("X-Forgejo-Event"));
debug!("Found Forgejo payload."); if let Some(event) = headers.get("X-Forgejo-Event") {
return Ok(Hook::try_from(hook)?); let hook: ForgejoHook =
ForgejoHook::try_from_event(event.to_str().unwrap(), &payload[..])?;
debug!("Found Forgejo payload.");
return Ok(Hook::try_from(hook)?);
} else {
debug!("Missing X-Forgejo-Event");
return Err(Error::InvalidRequest);
}
} }
// No match found for the payload // No match found for the payload