forgejo-hook: impl Hook::try_from_event
Utilize X-Forgejo-Event http header to facilitate conversion. Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
parent
834268f07f
commit
222ed138e4
3 changed files with 88 additions and 1 deletions
|
|
@ -7,7 +7,7 @@ license = "AGPL-3.0+"
|
|||
[dependencies]
|
||||
chrono = { version = "0.4", default-features = false, features = ["serde"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
serde_json = "1.0"
|
||||
serde_path_to_error = "0.1.16"
|
||||
|
|
|
|||
48
forgejo-hooks/src/error.rs
Normal file
48
forgejo-hooks/src/error.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright (C) 2025-2099 The crate authors.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Affero General Public License as published by the
|
||||
// Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
||||
// for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::error::Error as StdError;
|
||||
use std::io::Error as IoError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
UnknownHookType,
|
||||
Io(IoError),
|
||||
SerdeJson(serde_json::Error),
|
||||
}
|
||||
|
||||
impl StdError for Error {}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::UnknownHookType => write!(fmt, "Unknown hook type"),
|
||||
Error::Io(e) => write!(fmt, "Io error: {}", e),
|
||||
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IoError> for Error {
|
||||
fn from(err: IoError) -> Error {
|
||||
Error::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(err: serde_json::Error) -> Error {
|
||||
Error::SerdeJson(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -13,9 +13,12 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
mod error;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub use error::Error;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
|
||||
|
|
@ -295,6 +298,18 @@ impl From<Push> for Hook {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<AddedBranch> for Hook {
|
||||
fn from(branch: AddedBranch) -> Self {
|
||||
Self::AddedBranch(branch)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RemovedBranch> for Hook {
|
||||
fn from(branch: RemovedBranch) -> Self {
|
||||
Self::RemovedBranch(branch)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Issue> for Hook {
|
||||
fn from(issue: Issue) -> Self {
|
||||
Self::Issue(issue)
|
||||
|
|
@ -309,3 +324,27 @@ pub enum Hook {
|
|||
RemovedBranch(RemovedBranch),
|
||||
Issue(Issue),
|
||||
}
|
||||
|
||||
/// Deserializes the payload into a struct
|
||||
fn parse_payload<Output>(payload: &[u8]) -> Result<Output, serde_json::Error>
|
||||
where
|
||||
for<'a> Output: Deserialize<'a>,
|
||||
{
|
||||
serde_json::from_slice(payload)
|
||||
}
|
||||
|
||||
impl Hook {
|
||||
/// Generate the proper struct based on the event type provided by Forgejo. Generally found in
|
||||
/// the "X-Forgejo-Event" http header, which is not the same as the "X-Forgejo-Event-Type"
|
||||
/// header.
|
||||
pub fn try_from_event(event: &str, payload: &[u8]) -> Result<Hook, Error> {
|
||||
Ok(match event {
|
||||
"create" => parse_payload::<AddedBranch>(payload).map(Hook::from),
|
||||
"delete" => parse_payload::<RemovedBranch>(payload).map(Hook::from),
|
||||
"push" => parse_payload::<Push>(payload).map(Hook::from),
|
||||
"issues" => parse_payload::<Issue>(payload).map(Hook::from),
|
||||
"issue_comment" => parse_payload::<Issue>(payload).map(Hook::from),
|
||||
_ => return Err(Error::UnknownHookType),
|
||||
}?)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue