2024-07-14 22:33:24 +00:00
|
|
|
use axum::async_trait;
|
|
|
|
use axum::extract::FromRequest;
|
|
|
|
use axum::extract::Request;
|
2024-07-14 20:38:07 +00:00
|
|
|
use axum::http::HeaderMap;
|
2024-07-14 20:13:06 +00:00
|
|
|
use axum::http::StatusCode;
|
2024-07-14 22:33:24 +00:00
|
|
|
use axum::response::IntoResponse;
|
|
|
|
use axum::response::Response;
|
2024-07-14 20:13:06 +00:00
|
|
|
use axum::Json;
|
2024-07-14 22:33:24 +00:00
|
|
|
use axum::RequestExt;
|
2024-07-14 20:13:06 +00:00
|
|
|
use serde::Serialize;
|
2024-07-14 22:33:24 +00:00
|
|
|
use tracing::debug;
|
|
|
|
|
|
|
|
use crate::hook_push::HookPush;
|
2024-07-14 20:13:06 +00:00
|
|
|
|
2024-07-14 20:38:07 +00:00
|
|
|
pub(crate) async fn hook(
|
2024-07-14 22:33:24 +00:00
|
|
|
_headers: HeaderMap,
|
|
|
|
payload: HookRequest,
|
2024-07-14 20:38:07 +00:00
|
|
|
) -> (StatusCode, Json<HookResponse>) {
|
2024-07-14 22:33:24 +00:00
|
|
|
debug!("REQ: {:?}", payload);
|
|
|
|
match payload {
|
|
|
|
HookRequest::Push(_payload) => (
|
|
|
|
StatusCode::OK,
|
|
|
|
Json(HookResponse {
|
|
|
|
ok: true,
|
|
|
|
message: None,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
HookRequest::Unrecognized(payload) => (
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
Json(HookResponse {
|
|
|
|
ok: false,
|
|
|
|
message: Some(format!("unrecognized event type: {payload}")),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
}
|
2024-07-14 21:04:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-14 22:33:24 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum HookRequest {
|
|
|
|
Push(HookPush),
|
|
|
|
Unrecognized(String),
|
2024-07-14 21:04:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-14 22:33:24 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl<S> FromRequest<S> for HookRequest
|
|
|
|
where
|
|
|
|
S: Send + Sync,
|
|
|
|
{
|
|
|
|
type Rejection = Response;
|
2024-07-14 21:04:08 +00:00
|
|
|
|
2024-07-14 22:33:24 +00:00
|
|
|
async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
|
|
|
|
let event_type = req
|
|
|
|
.headers()
|
|
|
|
.get("X-Gitea-Event-Type")
|
|
|
|
.ok_or(StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response())?;
|
|
|
|
let event_type = event_type
|
|
|
|
.to_str()
|
|
|
|
.map_err(|_| StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response())?;
|
|
|
|
match event_type {
|
|
|
|
"push" => {
|
|
|
|
let Json(payload): Json<HookPush> =
|
|
|
|
req.extract().await.map_err(IntoResponse::into_response)?;
|
|
|
|
Ok(HookRequest::Push(payload))
|
|
|
|
}
|
|
|
|
_ => Ok(HookRequest::Unrecognized(event_type.to_owned())),
|
|
|
|
}
|
|
|
|
}
|
2024-07-14 20:38:07 +00:00
|
|
|
}
|
|
|
|
|
2024-07-14 22:33:24 +00:00
|
|
|
#[derive(Debug, Serialize)]
|
2024-07-14 20:13:06 +00:00
|
|
|
pub(crate) struct HookResponse {
|
|
|
|
ok: bool,
|
2024-07-14 22:33:24 +00:00
|
|
|
message: Option<String>,
|
2024-07-14 20:13:06 +00:00
|
|
|
}
|