parse gitea pr webhook

This commit is contained in:
2026-05-28 21:35:41 +00:00
parent e33187dc80
commit c119bed142
7 changed files with 321 additions and 8 deletions
+44 -7
View File
@@ -1,10 +1,15 @@
use axum::Router;
use axum::routing::get;
use axum::response::{Response, IntoResponse};
use axum::{Json, Router};
use axum::extract::FromRequest;
use axum::routing::{post, get};
use reqwest::StatusCode;
use serde_json::Value;
use crate::env;
use crate::env::{EnvConfig};
use crate::gitea::WebhookType;
pub async fn start_api(config: env::EnvConfig) -> anyhow::Result<()> {
let app = Router::new().route("/webhook", get(webhook));
pub async fn start_api(config: EnvConfig) -> anyhow::Result<()> {
let app = Router::new().route("/", get(root)).route("/webhook", post(webhook));
let listerner = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", config.http_port)).await?;
axum::serve(listerner, app)
@@ -12,6 +17,38 @@ pub async fn start_api(config: env::EnvConfig) -> anyhow::Result<()> {
.map_err(|e| anyhow::anyhow!(e))
}
async fn webhook() -> &'static str {
"Hello, World!"
async fn root() -> &'static str {
"Hi, i'm Herald :)"
}
async fn webhook(WebhookExtract(wb): WebhookExtract) -> Response {
match wb {
WebhookType::Review(id, _) => format!("Received {} pr id", id),
_ => String::from("Nothing to see :/")
}.into_response()
}
pub struct WebhookExtract(pub WebhookType);
impl<S> FromRequest<S> for WebhookExtract
where
S: Send + Sync,
{
type Rejection = Response;
async fn from_request(
req: axum::extract::Request,
state: &S,
) -> Result<Self, Self::Rejection> {
let Json(value) = Json::<Value>::from_request(req, state)
.await
.map_err(|e| e.into_response())?;
let webhook = WebhookType::try_from(value)
.map_err(|e| {
(StatusCode::UNPROCESSABLE_ENTITY, e.to_string()).into_response()
})?;
Ok(WebhookExtract(webhook))
}
}